int main(int argc, char* argv[]) { SAConnection con; // connection object SACommand cmd; // command object try { // connect to database (Oracle in our example) con.Connect("test", "tester", "tester", SA_Oracle_Client); // associate a command with connection cmd.setConnection(&con); // Insert 2 rows cmd.setCommandText( "Insert into test_tbl(fid, fvarchar20) values(:1, :2)"); // use first method of binding - param assignment cmd.Param(1).setAsLong() = 2; cmd.Param(2).setAsString() = "Some string (2)"; // Insert first row cmd.Execute(); // use second method of binding - stream binding cmd << (long)3 << "Some string (3)"; // Insert second row cmd.Execute(); // commit changes on success con.Commit(); printf("Input parameters bound, rows inserted!\n"); } catch(SAException &x) { // SAConnection::Rollback() // can also throw an exception // (if a network error for example), // we will be ready try { // on error rollback changes con.Rollback(); } catch(SAException &) { } // print error message printf("%s\n", (const char*)x.ErrText()); } return 0; }
int main(int argc, char* argv[]) { SAConnection con; // connection object SACommand cmd; // create command object try { // connect to database (Oracle in our example) con.Connect("test", "tester", "tester", SA_Oracle_Client); // associate a command with connection // connection can also be specified in SACommand constructor cmd.setConnection(&con); // create table cmd.setCommandText( "Create table test_tbl(fid integer, fvarchar20 varchar(20), fblob blob)"); cmd.Execute(); // insert value cmd.setCommandText( "Insert into test_tbl(fid, fvarchar20) values (1, 'Some string (1)')"); cmd.Execute(); // commit changes on success con.Commit(); printf("Table created, row inserted!\n"); } catch(SAException &x) { // SAConnection::Rollback() // can also throw an exception // (if a network error for example), // we will be ready try { // on error rollback changes con.Rollback(); } catch(SAException &) { } // print error message printf("%s\n", (const char*)x.ErrText()); } return 0; }
long unsigned int CDbCommand::execute(const CCommandParameterMap & params) throw (CDbException) { mergeParametersWith(params); string parametersDump = _makeParametersDump(_params); #ifdef CWS_DEBUG Cws::trace("Executing SQL: " + _text + parametersDump, "system.db.CDbCommand"); #endif try { SAConnection * connection = _connection->getConnection(); _saCommand = new SACommand(connection); _saCommand->setCommandText(_text.c_str()); for (CCommandParameterMap::const_iterator iter = _params.begin(); iter != _params.end(); ++iter) { iter->second.get()->bind(_saCommand); } PROFILE_BEGIN("Executing SQL: " + _text + parametersDump); _saCommand->Execute(); connection->Commit(); PROFILE_END(); return _saCommand->RowsAffected(); } catch (const SAException & e) { string message = e.ErrText().GetMultiByteChars(); Cws::log( "CDbCommand::execute() failed: " + message + ". The SQL statement executed was: " + _text + "." ); #ifdef CWS_DEBUG message += ". The SQL statement executed was: " + _text + parametersDump; #endif throw CDbException( "CDbCommand failed to execute the SQL statement: " + message, (int)e.ErrNativeCode() ); } return 0; }
//CMD_TRSACTION //请求: [B0: 协议字] [B1: 0x0-Rollback, 0x1-StartTrsaction, 0x2-Commit] //响应: [B0: 协议字] [B1: 执行结果0|1] int OnTrsaction(QUEUE_LIST *pDataNode) { #ifndef _SUPPORT_CLITR //不支持客户端事物 setCmdError(pDataNode); return 1; #endif //客户端事务连接链表 CTrastionList *pTrList = pDataNode->pCliTr; SAConnection *psaConn = pTrList->getCliTrConn(pDataNode->SrvrPort, pDataNode->PeerSockId); //请求报文(MSG) CliTrsaction_t Req = (CliTrsaction_t)pDataNode->Buffer[1]; switch (Req) { case TR_ROLLBACK: if (NULL == psaConn) setCmdError(pDataNode); else { psaConn->Rollback(); //删除客户端事物连接 pTrList->delCliTrConn(pDataNode->SrvrPort, pDataNode->PeerSockId); setCmdSucceed(pDataNode); } break; case TR_START: if (NULL == psaConn) { //新建客户端事物连接 TRSACTION_NODE *pNode = (TRSACTION_NODE *) GetMem(sizeof(TRSACTION_NODE)); pNode->SrvrPort = pDataNode->SrvrPort; pNode->PeerSockId = pDataNode->PeerSockId; pNode->psaConn = new SAConnection(); if (isDBConnect(pNode->psaConn, pDataNode->thd_info)) { pTrList->PushHead((void *)pNode); //加入节点到链表 } else { delete pNode->psaConn; FreeMem(pNode); setCmdError(pDataNode); return 1; } } setCmdSucceed(pDataNode); break; case TR_COMMIT: if (NULL == psaConn) setCmdError(pDataNode); else { psaConn->Commit(); //删除客户端事物连接 pTrList->delCliTrConn(pDataNode->SrvrPort, pDataNode->PeerSockId); setCmdSucceed(pDataNode); } break; default: return 0; } return 1; }