📄 context.cpp
字号:
m_HostName = host_name;}void CTLibContext::CTLIB_SetPacketSize(CS_INT packet_size){ m_PacketSize = packet_size;}void CTLibContext::CTLIB_SetLoginRetryCount(CS_INT n){ m_LoginRetryCount = n;}void CTLibContext::CTLIB_SetLoginLoopDelay(CS_INT nof_sec){ m_LoginLoopDelay = nof_sec;}CS_CONTEXT* CTLibContext::CTLIB_GetContext() const{ return m_Context;}bool CTLibContext::CTLIB_cserr_handler(CS_CONTEXT* context, CS_CLIENTMSG* msg){ CS_INT outlen; CPointerPot* p_pot = 0; CS_RETCODE r; r = cs_config(context, CS_GET, CS_USERDATA, (void*) &p_pot, (CS_INT) sizeof(p_pot), &outlen); if (r == CS_SUCCEED && p_pot != 0 && p_pot->NofItems() > 0) { CTLibContext* drv = (CTLibContext*) p_pot->Get(0); EDB_Severity sev = eDB_Error; if (msg->severity == CS_SV_INFORM) { sev = eDB_Info; } else if (msg->severity == CS_SV_FATAL) { sev = eDB_Fatal; } CDB_ClientEx ex(sev, msg->msgnumber, "cslib", msg->msgstring); drv->m_CntxHandlers.PostMsg(&ex); } else if (msg->severity != CS_SV_INFORM) { // nobody can be informed, so put it to stderr cerr << "CSLIB error handler detects the following error" << endl << msg->msgstring << endl; } return true;}bool CTLibContext::CTLIB_cterr_handler(CS_CONTEXT* context, CS_CONNECTION* con, CS_CLIENTMSG* msg){ CS_INT outlen; CPointerPot* p_pot = 0; CTL_Connection* link = 0; CDBHandlerStack* hs; if (con != 0 && ct_con_props(con, CS_GET, CS_USERDATA, (void*) &link, (CS_INT) sizeof(link), &outlen) == CS_SUCCEED && link != 0) { hs = &link->m_MsgHandlers; } else if (cs_config(context, CS_GET, CS_USERDATA, (void*) &p_pot, (CS_INT) sizeof(p_pot), &outlen) == CS_SUCCEED && p_pot != 0 && p_pot->NofItems() > 0) { CTLibContext* drv = (CTLibContext*) p_pot->Get(0); hs = &drv->m_CntxHandlers; } else { if (msg->severity != CS_SV_INFORM) { // nobody can be informed, let's put it in stderr cerr << "CTLIB error handler detects the following error" << endl << "Severity:" << msg->severity << " Msg # " << msg->msgnumber << endl << msg->msgstring << endl; if (msg->osstringlen > 1) { cerr << "OS # " << msg->osnumber << " OS msg " << msg->osstring << endl; } if (msg->sqlstatelen > 1 && (msg->sqlstate[0] != 'Z' || msg->sqlstate[1] != 'Z')) { cerr << "SQL: " << msg->sqlstate << endl; } } return true; } switch (msg->severity) { case CS_SV_INFORM: { CDB_ClientEx info(eDB_Info, (int) msg->msgnumber, "ctlib", msg->msgstring); hs->PostMsg(&info); break; } case CS_SV_RETRY_FAIL: { CDB_TimeoutEx to((int) msg->msgnumber, "ctlib", msg->msgstring); hs->PostMsg(&to); if(con) { CS_INT status; if((ct_con_props(con, CS_GET, CS_LOGIN_STATUS, (CS_VOID*)&status, CS_UNUSED, NULL) != CS_SUCCEED) || (!status)) return false; if(ct_cancel(con, (CS_COMMAND*)0, CS_CANCEL_ATTN) != CS_SUCCEED) return false; } else return false; break; } case CS_SV_CONFIG_FAIL: case CS_SV_API_FAIL: case CS_SV_INTERNAL_FAIL: { CDB_ClientEx err(eDB_Error, (int) msg->msgnumber, "ctlib", msg->msgstring); hs->PostMsg(&err); break; } default: { CDB_ClientEx ftl(eDB_Fatal, (int) msg->msgnumber, "ctlib", msg->msgstring); hs->PostMsg(&ftl); break; } } return true;}bool CTLibContext::CTLIB_srverr_handler(CS_CONTEXT* context, CS_CONNECTION* con, CS_SERVERMSG* msg){ if ((msg->severity == 0 && msg->msgnumber == 0) || msg->msgnumber == 5701 || msg->msgnumber == 5703) { return true; } CS_INT outlen; CPointerPot* p_pot = 0; CTL_Connection* link = 0; CDBHandlerStack* hs; if (con != 0 && ct_con_props(con, CS_GET, CS_USERDATA, (void*) &link, (CS_INT) sizeof(link), &outlen) == CS_SUCCEED && link != 0) { hs = &link->m_MsgHandlers; } else if (cs_config(context, CS_GET, CS_USERDATA, (void*) &p_pot, (CS_INT) sizeof(p_pot), &outlen) == CS_SUCCEED && p_pot != 0 && p_pot->NofItems() > 0) { CTLibContext* drv = (CTLibContext*) p_pot->Get(0); hs = &drv->m_CntxHandlers; } else { cerr << "Message from the server "; if (msg->svrnlen > 0) { cerr << "<" << msg->svrname << "> "; } cerr << "msg # " << msg->msgnumber << " severity: " << msg->severity << endl; if (msg->proclen > 0) { cerr << "Proc: " << msg->proc << " line: " << msg->line << endl; } if (msg->sqlstatelen > 1 && (msg->sqlstate[0] != 'Z' || msg->sqlstate[1] != 'Z')) { cerr << "SQL: " << msg->sqlstate << endl; } cerr << msg->text << endl; return true; } if (msg->msgnumber == 1205 /*DEADLOCK*/) { CDB_DeadlockEx dl(msg->svrname, msg->text); hs->PostMsg(&dl); } else { EDB_Severity sev = msg->severity < 10 ? eDB_Info : msg->severity == 10 ? eDB_Warning : msg->severity < 16 ? eDB_Error : msg->severity > 16 ? eDB_Fatal : eDB_Unknown; if (msg->proclen > 0) { CDB_RPCEx rpc(sev, (int) msg->msgnumber, msg->svrname, msg->text, msg->proc, (int) msg->line); hs->PostMsg(&rpc); } else if (msg->sqlstatelen > 1 && (msg->sqlstate[0] != 'Z' || msg->sqlstate[1] != 'Z')) { CDB_SQLEx sql(sev, (int) msg->msgnumber, msg->svrname, msg->text, (const char*) msg->sqlstate, (int) msg->line); hs->PostMsg(&sql); } else { CDB_DSEx m(sev, (int) msg->msgnumber, msg->svrname, msg->text); hs->PostMsg(&m); } } return true;}CS_CONNECTION* CTLibContext::x_ConnectToServer(const string& srv_name, const string& user_name, const string& passwd, TConnectionMode mode){ CS_CONNECTION* con; if (ct_con_alloc(m_Context, &con) != CS_SUCCEED) return 0; ct_callback(NULL, con, CS_SET, CS_CLIENTMSG_CB, (CS_VOID*) s_CTLIB_cterr_callback); ct_callback(NULL, con, CS_SET, CS_SERVERMSG_CB, (CS_VOID*) s_CTLIB_srverr_callback); char hostname[256]; if(gethostname(hostname, 256)) { strcpy(hostname, "UNKNOWN"); } else hostname[255]= '\0'; if (ct_con_props(con, CS_SET, CS_USERNAME, (void*) user_name.c_str(), CS_NULLTERM, NULL) != CS_SUCCEED || ct_con_props(con, CS_SET, CS_PASSWORD, (void*) passwd.c_str(), CS_NULLTERM, NULL) != CS_SUCCEED || ct_con_props(con, CS_SET, CS_APPNAME, (void*) m_AppName.c_str(), CS_NULLTERM, NULL) != CS_SUCCEED || ct_con_props(con, CS_SET, CS_HOSTNAME, (void*) hostname, CS_NULLTERM, NULL) != CS_SUCCEED) { ct_con_drop(con); return 0; } if ( !m_HostName.empty() ) { ct_con_props(con, CS_SET, CS_HOSTNAME, (void*) m_HostName.c_str(), CS_NULLTERM, NULL); } if (m_PacketSize > 0) { ct_con_props(con, CS_SET, CS_PACKETSIZE, (void*) &m_PacketSize, CS_UNUSED, NULL); } if (m_LoginRetryCount > 0) { ct_con_props(con, CS_SET, CS_RETRY_COUNT, (void*) &m_LoginRetryCount, CS_UNUSED, NULL); } if (m_LoginLoopDelay > 0) { ct_con_props(con, CS_SET, CS_LOOP_DELAY, (void*) &m_LoginLoopDelay, CS_UNUSED, NULL); } CS_BOOL flag = CS_TRUE; if ((mode & fBcpIn) != 0) { ct_con_props(con, CS_SET, CS_BULK_LOGIN, &flag, CS_UNUSED, NULL); } if ((mode & fPasswordEncrypted) != 0) { ct_con_props(con, CS_SET, CS_SEC_ENCRYPTION, &flag, CS_UNUSED, NULL); } if (ct_connect(con, const_cast<char*> (srv_name.c_str()), CS_NULLTERM) != CS_SUCCEED) { ct_con_drop(con); return 0; } return con;}///////////////////////////////////////////////////////////////////////////////// Miscellaneous//void g_CTLIB_GetRowCount(CS_COMMAND* cmd, int* cnt){ CS_INT n; CS_INT outlen; if (cnt && ct_res_info(cmd, CS_ROW_COUNT, &n, CS_UNUSED, &outlen) == CS_SUCCEED && n >= 0 && n != CS_NO_COUNT) { *cnt = (int) n; }}bool g_CTLIB_AssignCmdParam(CS_COMMAND* cmd, CDB_Object& param, const string& param_name, CS_DATAFMT& param_fmt, CS_SMALLINT indicator, bool declare_only){ {{ size_t l = param_name.copy(param_fmt.name, CS_MAX_NAME-1); param_fmt.name[l] = '\0'; }} CS_RETCODE ret_code; switch ( param.GetType() ) { case eDB_Int: { CDB_Int& par = dynamic_cast<CDB_Int&> (param); param_fmt.datatype = CS_INT_TYPE; if ( declare_only ) break; CS_INT value = (CS_INT) par.Value(); ret_code = ct_param(cmd, ¶m_fmt, (CS_VOID*) &value, CS_UNUSED, indicator); break; } case eDB_SmallInt: { CDB_SmallInt& par = dynamic_cast<CDB_SmallInt&> (param); param_fmt.datatype = CS_SMALLINT_TYPE; if ( declare_only )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -