標準APIを使ったサンプル
#include <stdio.h>
#include <bzs/db/protocol/tdap/client/database.h>
#include <bzs/db/protocol/tdap/client/table.h>
#include <bzs/db/protocol/tdap/client/dbDef.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace bzs::db::protocol::tdap::client;
using namespace bzs::db::protocol::tdap;
static const short fieldnum_id = 0;
static const short fieldnum_name = 1;
static const short fieldnum_group = 2;
static const short fieldnum_tel = 3;
static const short fieldnum_pic_type = 0;
static const short fieldnum_pic_id = 1;
static const short fieldnum_pic_pic = 2;
void showError(const _TCHAR* caption, const _TCHAR* tableName, short statusCode)
{
_TCHAR tmp[1024] = { 0x00 };
_tprintf(_T("[ERROR] %s No.%ld %s\n"), caption, statusCode, tmp);
}
bool insertUser(
table* tb,
int id,
const _TCHAR* name,
int groupid,
const _TCHAR* tel)
{
tb->
setFV(fieldnum_id,
id);
tb->
setFV(fieldnum_name, name);
tb->
setFV(fieldnum_group, groupid);
tb->
setFV(fieldnum_tel, tel);
return (tb->
stat() == 0);
}
bool insertUsers(
table* tb)
{
bool ret = insertUser(tb, 1, _T("akio"), 1, _T("81-3-2222-3569"));
if (ret == false)
return false;
ret = insertUser(tb, 2, _T("yoko"), 2, _T("81-263-80-5555"));
if (ret == false)
return false;
ret = insertUser(tb, 3, _T("satoshi"), 1, _T("81-3-1111-1234"));
if (ret == false)
return false;
ret = insertUser(tb, 4, _T("keiko"), 2, _T("81-26-222-3569"));
if (ret == false)
return false;
ret = insertUser(tb, 5, _T("john"), 3, _T("81-26-222-3565"));
if (ret == false)
return false;
return true;
}
bool insertGroup(
table* tb,
int id,
const _TCHAR* name)
{
tb->
setFV(fieldnum_id,
id);
tb->
setFV(fieldnum_name, name);
return (tb->
stat() == 0);
}
bool insertGroups(
table* tb)
{
bool ret = insertGroup(tb, 1, _T("develop"));
if (ret == false)
return false;
ret = insertGroup(tb, 2, _T("sales"));
if (ret == false)
return false;
ret = insertGroup(tb, 3, _T("finance"));
if (ret == false)
return false;
return true;
}
bool insertPicure(
table* tb,
short type,
int id,
const void* img,
size_t size)
{
tb->
setFV(fieldnum_pic_type, type);
tb->
setFV(fieldnum_pic_id,
id);
tb->
setFV(fieldnum_pic_pic, img, (uint_td)size);
return (tb->
stat() == 0);
}
void readImage(const _TCHAR* path, std::vector<char>& s)
{
std::ifstream ifs(path, std::ios::in | std::ios::binary);
ifs.seekg(0, std::ios::end);
s.resize((unsigned int)ifs.tellg());
ifs.seekg(0, std::ios::beg);
ifs.read(&s[0], s.size());
}
{
bool newConnection = false;
if (!db->
connect(uri, newConnection))
{
showError(_T(
"connect daatabase"), NULL, db->
stat());
return false;
}
db->
open(uri, TYPE_SCHEMA_BDF);
{
showError(_T(
"open daatabase"), NULL, db->
stat());
return false;
}
return true;
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
int result = 0;
static const _TCHAR* uri = _T("tdap://localhost/test?dbfile=test.bdf");
{
showError(_T(
"open group1 table"), NULL, db->
stat());
else
{
if (insertGroups(tbg))
{
showError(_T(
"open user table"), NULL, db->
stat());
else
{
if (insertUsers(tbu))
{
showError(_T(
"open user table"), NULL, db->
stat());
else
{
std::vector<char> s;
readImage(argv[0], s);
if (insertPicure(tbp, 1, 1, &s[0], s.size()))
{
_tprintf(_T("Insert records success. \n"));
result = 0;
}
}
}
}
}
}
}
return result;
}
コンビニエンスAPIを使ったサンプル
#include <bzs/db/protocol/tdap/client/trdboostapi.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace bzs::db::protocol::tdap::client;
using namespace bzs::db::protocol::tdap;
static const short fieldnum_id = 0;
static const short fieldnum_name = 1;
static const short fieldnum_group = 2;
static const short fieldnum_tel = 3;
static const short fieldnum_pic_type = 0;
static const short fieldnum_pic_id = 1;
static const short fieldnum_pic_pic = 2;
void insertUser(fields& fds, int id, const _TCHAR* name, int groupid,
const _TCHAR* tel)
{
fds[fieldnum_id] = id;
fds[fieldnum_name] = name;
fds[fieldnum_group] = groupid;
if ((
int)fds.
size() > fieldnum_tel)
fds[fieldnum_tel] = tel;
}
{
fields fds(tb);
insertUser(fds, 1, _T("akio"), 1, _T("81-3-2222-3569"));
insertUser(fds, 2, _T("yoko"), 2, _T("81-263-80-5555"));
insertUser(fds, 3, _T("satoshi"), 1, _T("81-3-1111-1234"));
insertUser(fds, 4, _T("keiko"), 2, _T("81-26-222-3569"));
insertUser(fds, 5, _T("john"), 3, _T("81-26-222-3565"));
}
void insertGroup(fields& fds, int id, const _TCHAR* name)
{
fds[fieldnum_id] = id;
fds[fieldnum_name] = name;
}
{
fields fds(tb);
insertGroup(fds, 1, _T("develop"));
insertGroup(fds, 2, _T("sales"));
insertGroup(fds, 3, _T("finance"));
}
void insertPicure(
table_ptr tb,
short type,
int id,
const void* img,
size_t size)
{
fields fds(tb);
fds[fieldnum_pic_type] = type;
fds[fieldnum_pic_id] = id;
fds[fieldnum_pic_pic].setBin(img, (int)size);
}
void readImage(const _TCHAR* path, std::vector<char>& s)
{
std::ifstream ifs(path, std::ios::in | std::ios::binary);
ifs.seekg(0, std::ios::end);
s.resize(ifs.tellg());
ifs.seekg(0, std::ios::beg);
ifs.read(&s[0], s.size());
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
try
{
_T("test"));
bool newConnection = false;
insertGroups(tb);
insertUsers(tb);
std::vector<char> s;
readImage(argv[0], s);
insertPicure(tb, 1, 1, &s[0], s.size());
std::cout << "Insert records success." << std::endl;
return 0;
}
catch (bzs::rtl::exception& e)
{
std::tcout << _T("[ERROR] ") << *bzs::rtl::getMsg(e) << std::endl;
}
return 1;
}