📄 connection.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: connection.cpp,v $ * PRODUCTION Revision 1000.1 2004/06/01 19:19:34 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.17 * PRODUCTION * =========================================================================== *//* $Id: connection.cpp,v 1000.1 2004/06/01 19:19:34 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Vladimir Soussov * * File Description: CTLib connection * */#include <ncbi_pch.hpp>#include <dbapi/driver/ctlib/interfaces.hpp>#include <string.h>BEGIN_NCBI_SCOPECTL_Connection::CTL_Connection(CTLibContext* cntx, CS_CONNECTION* con, bool reusable, const string& pool_name){ m_Link = con; m_Context = cntx; m_Reusable = reusable; m_Pool = pool_name; CTL_Connection* link = this; ct_con_props(m_Link, CS_SET, CS_USERDATA, &link, (CS_INT) sizeof(link), NULL); // retrieve the connection attributes CS_INT outlen(0); char buf[512]; ct_con_props(m_Link, CS_GET, CS_SERVERNAME, buf, (CS_INT) (sizeof(buf) - 1), &outlen); if((outlen > 0) && (buf[outlen-1] == '\0')) --outlen; m_Server.append(buf, (size_t) outlen); ct_con_props(m_Link, CS_GET, CS_USERNAME, buf, (CS_INT) (sizeof(buf) - 1), &outlen); if((outlen > 0) && (buf[outlen-1] == '\0')) --outlen; m_User.append(buf, (size_t) outlen); ct_con_props(m_Link, CS_GET, CS_PASSWORD, buf, (CS_INT) (sizeof(buf) - 1), &outlen); if((outlen > 0) && (buf[outlen-1] == '\0')) --outlen; m_Passwd.append(buf, (size_t) outlen); CS_BOOL flag; ct_con_props(m_Link, CS_GET, CS_BULK_LOGIN, &flag, CS_UNUSED, &outlen); m_BCPable = (flag == CS_TRUE); ct_con_props(m_Link, CS_GET, CS_SEC_ENCRYPTION, &flag, CS_UNUSED, &outlen); m_SecureLogin = (flag == CS_TRUE); m_ResProc= 0;}bool CTL_Connection::IsAlive(){ CS_INT status; if (ct_con_props(m_Link, CS_GET, CS_CON_STATUS, &status, CS_UNUSED, 0) != CS_SUCCEED) return false; return (status & CS_CONSTAT_CONNECTED) != 0 && (status & CS_CONSTAT_DEAD ) == 0;}CDB_LangCmd* CTL_Connection::LangCmd(const string& lang_query, unsigned int nof_params){ CS_COMMAND* cmd; switch ( ct_cmd_alloc(m_Link, &cmd) ) { case CS_SUCCEED: break; case CS_FAIL: throw CDB_ClientEx(eDB_Fatal, 110001, "CTL_Connection::LangCmd", "ct_cmd_alloc failed"); case CS_BUSY: throw CDB_ClientEx(eDB_Fatal, 110002, "CTL_Connection::LangCmd", "the connection is busy"); } CTL_LangCmd* lcmd = new CTL_LangCmd(this, cmd, lang_query, nof_params); m_CMDs.Add(lcmd); return Create_LangCmd(*lcmd);}CDB_RPCCmd* CTL_Connection::RPC(const string& rpc_name, unsigned int nof_args){ CS_COMMAND* cmd; switch ( ct_cmd_alloc(m_Link, &cmd) ) { case CS_SUCCEED: break; case CS_FAIL: throw CDB_ClientEx(eDB_Fatal, 110001, "CTL_Connection::RPC", "ct_cmd_alloc failed"); case CS_BUSY: throw CDB_ClientEx(eDB_Fatal, 110002, "CTL_Connection::RPC", "the connection is busy"); } CTL_RPCCmd* rcmd = new CTL_RPCCmd(this, cmd, rpc_name, nof_args); m_CMDs.Add(rcmd); return Create_RPCCmd(*rcmd);}CDB_BCPInCmd* CTL_Connection::BCPIn(const string& table_name, unsigned int nof_columns){ if (!m_BCPable) { throw CDB_ClientEx(eDB_Error, 110003, "CTL_Connection::BCPIn", "No bcp on this connection"); } CS_BLKDESC* cmd; if (blk_alloc(m_Link, BLK_VERSION_100, &cmd) != CS_SUCCEED) { throw CDB_ClientEx(eDB_Fatal, 110004, "CTL_Connection::BCPIn", "blk_alloc failed"); } CTL_BCPInCmd* bcmd = new CTL_BCPInCmd(this, cmd, table_name, nof_columns); m_CMDs.Add(bcmd); return Create_BCPInCmd(*bcmd);}CDB_CursorCmd* CTL_Connection::Cursor(const string& cursor_name, const string& query, unsigned int nof_params, unsigned int batch_size){ CS_COMMAND* cmd; switch ( ct_cmd_alloc(m_Link, &cmd) ) { case CS_SUCCEED: break; case CS_FAIL: throw CDB_ClientEx(eDB_Fatal, 110001, "CTL_Connection::Cursor", "ct_cmd_alloc failed"); case CS_BUSY: throw CDB_ClientEx(eDB_Fatal, 110002, "CTL_Connection::Cursor", "the connection is busy"); } CTL_CursorCmd* ccmd = new CTL_CursorCmd(this, cmd, cursor_name, query, nof_params, batch_size); m_CMDs.Add(ccmd); return Create_CursorCmd(*ccmd);}CDB_SendDataCmd* CTL_Connection::SendDataCmd(I_ITDescriptor& descr_in, size_t data_size, bool log_it){ if ( !data_size ) { throw CDB_ClientEx(eDB_Fatal, 110092, "CTL_Connection::SendDataCmd", "wrong (zero) data size"); } I_ITDescriptor* p_desc= 0; // check what type of descriptor we've got if(descr_in.DescriptorType() != CTL_ITDESCRIPTOR_TYPE_MAGNUM) { // this is not a native descriptor p_desc= x_GetNativeITDescriptor (dynamic_cast<CDB_ITDescriptor&> (descr_in)); if(p_desc == 0) return 0; } C_ITDescriptorGuard d_guard(p_desc); CS_COMMAND* cmd; switch ( ct_cmd_alloc(m_Link, &cmd) ) { case CS_SUCCEED: break; case CS_FAIL: throw CDB_ClientEx(eDB_Fatal, 110001, "CTL_Connection::SendDataCmd", "ct_cmd_alloc failed"); case CS_BUSY: throw CDB_ClientEx(eDB_Fatal, 110002, "CTL_Connection::SendDataCmd", "the connection is busy"); } if (ct_command(cmd, CS_SEND_DATA_CMD, 0, CS_UNUSED, CS_COLUMN_DATA) != CS_SUCCEED) { ct_cmd_drop(cmd); throw CDB_ClientEx(eDB_Fatal, 110093, "CTL_Connection::SendDataCmd", "ct_command failed"); } CTL_ITDescriptor& desc = p_desc? dynamic_cast<CTL_ITDescriptor&>(*p_desc) : dynamic_cast<CTL_ITDescriptor&> (descr_in); // desc.m_Desc.datatype = CS_TEXT_TYPE; desc.m_Desc.total_txtlen = (CS_INT)data_size; desc.m_Desc.log_on_update = log_it ? CS_TRUE : CS_FALSE; if (ct_data_info(cmd, CS_SET, CS_UNUSED, &desc.m_Desc) != CS_SUCCEED) { ct_cancel(0, cmd, CS_CANCEL_ALL); ct_cmd_drop(cmd); throw CDB_ClientEx(eDB_Fatal, 110093, "CTL_Connection::SendDataCmd", "ct_data_info failed"); } CTL_SendDataCmd* sd_cmd = new CTL_SendDataCmd(this, cmd, data_size); m_CMDs.Add(sd_cmd); return Create_SendDataCmd(*sd_cmd);}bool CTL_Connection::SendData(I_ITDescriptor& desc, CDB_Image& img, bool log_it){ return x_SendData(desc, img, log_it);}bool CTL_Connection::SendData(I_ITDescriptor& desc, CDB_Text& txt, bool log_it){ return x_SendData(desc, txt, log_it);}bool CTL_Connection::Refresh(){ // close all commands first while(m_CMDs.NofItems() > 0) { CDB_BaseEnt* pCmd = static_cast<CDB_BaseEnt*> (m_CMDs.Get(0)); try { delete pCmd; } catch (CDB_Exception& ) { } m_CMDs.Remove((int) 0); } // cancel all pending commands if (ct_cancel(m_Link, 0, CS_CANCEL_ALL) != CS_SUCCEED) return false; // check the connection status CS_INT status; if (ct_con_props(m_Link, CS_GET, CS_CON_STATUS, &status, CS_UNUSED, 0) != CS_SUCCEED) return false; return (status & CS_CONSTAT_CONNECTED) != 0 && (status & CS_CONSTAT_DEAD ) == 0;}const string& CTL_Connection::ServerName() const{ return m_Server;}const string& CTL_Connection::UserName() const{ return m_User;}const string& CTL_Connection::Password() const{ return m_Passwd;}I_DriverContext::TConnectionMode CTL_Connection::ConnectMode() const{ I_DriverContext::TConnectionMode mode = 0; if ( m_BCPable ) { mode |= I_DriverContext::fBcpIn; } if ( m_SecureLogin ) { mode |= I_DriverContext::fPasswordEncrypted; } return mode;}bool CTL_Connection::IsReusable() const{ return m_Reusable;}const string& CTL_Connection::PoolName() const{ return m_Pool;}I_DriverContext* CTL_Connection::Context() const{ return const_cast<CTLibContext*> (m_Context);}void CTL_Connection::PushMsgHandler(CDB_UserHandler* h){ m_MsgHandlers.Push(h);}void CTL_Connection::PopMsgHandler(CDB_UserHandler* h){ m_MsgHandlers.Pop(h);}CDB_ResultProcessor* CTL_Connection::SetResultProcessor(CDB_ResultProcessor* rp){ CDB_ResultProcessor* r= m_ResProc; m_ResProc= rp; return r;}void CTL_Connection::Release(){ m_BR = 0; // close all commands first while(m_CMDs.NofItems() > 0) { CDB_BaseEnt* pCmd = static_cast<CDB_BaseEnt*> (m_CMDs.Get(0)); try { delete pCmd; } catch (CDB_Exception& ) { } m_CMDs.Remove((int) 0); }}CTL_Connection::~CTL_Connection(){ if (!Refresh() || ct_close(m_Link, CS_UNUSED) != CS_SUCCEED) { ct_close(m_Link, CS_FORCE_CLOSE); } ct_con_drop(m_Link);}void CTL_Connection::DropCmd(CDB_BaseEnt& cmd){ m_CMDs.Remove(static_cast<TPotItem> (&cmd));}bool CTL_Connection::x_SendData(I_ITDescriptor& descr_in, CDB_Stream& img, bool log_it){ CS_INT size = (CS_INT) img.Size(); if ( !size ) return false; I_ITDescriptor* p_desc= 0; // check what type of descriptor we've got if(descr_in.DescriptorType() != CTL_ITDESCRIPTOR_TYPE_MAGNUM) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -