全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 ページ
トランザクション処理

標準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>
using namespace bzs::db::protocol::tdap::client;
using namespace bzs::db::protocol::tdap;
/**
@brief update records in transaction example
This program updates some records of a "user" table.
Change group of user in group 1 to 3
Please execute "create database" , "change schema" and "insert records" example
before execute this example.
*/
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 char_td keynum_group = 1;
/** show database operation error
*/
void showError(const _TCHAR* caption, const _TCHAR* tableName, short statusCode)
{
_TCHAR tmp[1024] = { 0x00 };
nstable::tdapErr(0x00, statusCode, tableName, tmp);
_tprintf(_T("[ERROR] %s No.%ld %s\n"), caption, statusCode, tmp);
}
bool updateUsers(table* tb)
{
tb->clearBuffer();
tb->setKeyNum(keynum_group); // use group key
tb->setFV(fieldnum_group, 1); // set group = 1;
tb->seekGreater(true /*orEqual*/);
while (tb->stat() == 0)
{
// check group value.
if (tb->getFVint(fieldnum_group) != 1)
break;
// update group
tb->setFV(fieldnum_group, 3); // change group 1 to 3
tb->update(
nstable::changeCurrentNcc /*ncc=true*/); // Important ncc=true !
if (tb->stat() != 0)
showError(_T("update user"), tb->tableDef()->tableName(),
tb->stat());
else
tb->seekNext();
}
return ((tb->stat() == STATUS_EOF) || (tb->stat() == 0));
}
/** Open database
*/
bool openDatabase(database* db, const _TCHAR* uri)
{
/******************************************************
!!! Important !!!
When using a multi-threaded,
please request a new connection for each database.
*******************************************************/
// When using a multi-threaded, set to true.
bool newConnection = false;
if (!db->connect(uri, newConnection))
{
showError(_T("connect daatabase"), NULL, db->stat());
return false;
}
db->open(uri, TYPE_SCHEMA_BDF);
if (db->stat() != 0)
{
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");
if (openDatabase(db, uri))
{
table* tbu = db->openTable(_T("user"));
if (db->stat() != 0)
showError(_T("open user table"), NULL, db->stat());
else
{
db->beginTrn();
if (updateUsers(tbu))
{
db->endTrn();
_tprintf(_T("Update records success. \n"));
}
else
db->abortTrn();
tbu->release();
}
db->close();
}
return result;
}

コンビニエンスAPIを使ったサンプル

#include <bzs/db/protocol/tdap/client/trdboostapi.h>
#include <iostream>
using namespace bzs::db::protocol::tdap::client;
using namespace bzs::db::protocol::tdap;
/**
@brief update records in transaction example
This program updates some records of a "user" table.
Change group of user in group 1 to 3
Please execute "create database" , "change schema" and "insert records" example
before execute this example.
*/
static const short fieldnum_id = 0;
static const short fieldnum_group = 2;
static const char_td keynum_group = 1;
int isGroupOne(const fields& fds)
{
return (fds[fieldnum_group].i() == 1) ? filter_validate_value
: filter_invalidate_value;
}
void changeGroup1To3(const fields& fds)
{
fds[fieldnum_group] = 3;
}
void updateUsers(table_ptr tb)
{
// Execute seekGreaterOrEqual with key number and key value
indexIterator it = readIndex_v(tb, eSeekGreaterOrEqual, keynum_group, 1);
// Wrap filtered iterator
filterdIndexIterator itf(it, isGroupOne);
// loop each group=1 records and update
for_each(itf, changeGroup1To3);
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
try
{
connectParams param(_T("tdap"), _T("localhost"), _T("test"),
_T("test"));
/******************************************************
!!! Important !!!
When using a multi-threaded,
please request a new connection for each database.
*******************************************************/
// When using a multi-threaded, set to true.
bool newConnection = false;
connectOpen(db, param, newConnection);
table_ptr tb = openTable(db, _T("user"));
dbTransaction trn(db);
// start transaction
trn.begin();
updateUsers(tb);
// Commit transaction. If an error throwed then abort transaction
// automaticaly.
trn.end();
std::cout << "Update records success." << std::endl;
return 0;
}
catch (bzs::rtl::exception& e)
{
std::tcout << _T("[ERROR] ") << *bzs::rtl::getMsg(e) << std::endl;
}
return 1;
}

Transactd SDK 2018年07月31日(火) 19時40分26秒 doxygen