📄 context.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: context.cpp,v $ * PRODUCTION Revision 1000.4 2004/06/16 17:20:27 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.31 * PRODUCTION * =========================================================================== *//* $Id: context.cpp,v 1000.4 2004/06/16 17:20:27 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: Driver for DBLib server * */#include <ncbi_pch.hpp>#include <corelib/ncbimtx.hpp>#ifndef USE_MS_DBLIB# include <dbapi/driver/dblib/interfaces.hpp># include <dbapi/driver/dblib/interfaces_p.hpp>#else# include <dbapi/driver/msdblib/interfaces.hpp># include <dbapi/driver/msdblib/interfaces_p.hpp>#endif#include <dbapi/driver/util/numeric_convert.hpp>BEGIN_NCBI_SCOPE///////////////////////////////////////////////////////////////////////////////// CDBLibContext:://extern "C" {#ifdef MS_DBLIB_IN_USE static int s_DBLIB_err_callback (DBPROCESS* dblink, int severity, int dberr, int oserr, const char* dberrstr, const char* oserrstr) { return CDBLibContext::DBLIB_dberr_handler (dblink, severity, dberr, oserr, dberrstr? dberrstr : "", oserrstr? oserrstr : ""); } static int s_DBLIB_msg_callback (DBPROCESS* dblink, DBINT msgno, int msgstate, int severity, const char* msgtxt, const char* srvname, const char* procname, unsigned short line) { CDBLibContext::DBLIB_dbmsg_handler (dblink, msgno, msgstate, severity, msgtxt? msgtxt : "", srvname? srvname : "", procname? procname : "", line); return 0; }#else static int CS_PUBLIC s_DBLIB_err_callback (DBPROCESS* dblink, int severity, int dberr, int oserr, char* dberrstr, char* oserrstr) { return CDBLibContext::DBLIB_dberr_handler (dblink, severity, dberr, oserr, dberrstr? dberrstr : "", oserrstr? oserrstr : ""); } static int CS_PUBLIC s_DBLIB_msg_callback (DBPROCESS* dblink, DBINT msgno, int msgstate, int severity, char* msgtxt, char* srvname, char* procname, int line) { CDBLibContext::DBLIB_dbmsg_handler (dblink, msgno, msgstate, severity, msgtxt? msgtxt : "", srvname? srvname : "", procname? procname : "", line); return 0; }#endif}CDBLibContext* CDBLibContext::m_pDBLibContext = 0;CDBLibContext::CDBLibContext(DBINT version) : m_AppName("DBLibDriver"), m_HostName(""), m_PacketSize(0){ DEFINE_STATIC_FAST_MUTEX(xMutex); CFastMutexGuard mg(xMutex); if (m_pDBLibContext != 0) { throw CDB_ClientEx(eDB_Error, 200000, "CDBLibContext::CDBLibContext", "You cannot use more than one dblib context " "concurrently"); }#ifdef MS_DBLIB_IN_USE if (dbinit() == NULL || version == 31415)#else if (dbinit() != SUCCEED || dbsetversion(version) != SUCCEED)#endif { throw CDB_ClientEx(eDB_Fatal, 200001, "CDBLibContext::CDBLibContext", "dbinit failed"); } dberrhandle(s_DBLIB_err_callback); dbmsghandle(s_DBLIB_msg_callback); m_pDBLibContext = this; m_Login = dblogin();}bool CDBLibContext::SetLoginTimeout(unsigned int nof_secs){ return dbsetlogintime(nof_secs) == SUCCEED;}bool CDBLibContext::SetTimeout(unsigned int nof_secs){ return dbsettime(nof_secs) == SUCCEED;}bool CDBLibContext::SetMaxTextImageSize(size_t nof_bytes){ char s[64]; sprintf(s, "%lu", (unsigned long) nof_bytes);#ifdef MS_DBLIB_IN_USE return dbsetopt(0, DBTEXTLIMIT, s) == SUCCEED;#else return dbsetopt(0, DBTEXTLIMIT, s, -1) == SUCCEED;#endif}CDB_Connection* CDBLibContext::Connect(const string& srv_name, const string& user_name, const string& passwd, TConnectionMode mode, bool reusable, const string& pool_name){ CDBL_Connection* t_con; // DEFINE_STATIC_FAST_MUTEX(xMutex); CFastMutexGuard mg(m_Mtx); if (reusable && m_NotInUse.NofItems() > 0) { // try to reuse connection if (!pool_name.empty()) { // try to use pool name int n = m_NotInUse.NofItems(); while (n--) { t_con = static_cast<CDBL_Connection*> (m_NotInUse.Get(n)); if (pool_name.compare(t_con->PoolName()) == 0) { m_NotInUse.Remove(n); if(t_con->Refresh()) { m_InUse.Add((TPotItem) t_con); return Create_Connection(*t_con); } delete t_con; } } } else { if (srv_name.empty()) return 0; int n = m_NotInUse.NofItems(); // try to use server name while (n--) { t_con = static_cast<CDBL_Connection*> (m_NotInUse.Get(n)); if (srv_name.compare(t_con->ServerName()) == 0) { m_NotInUse.Remove(n); if(t_con->Refresh()) { m_InUse.Add((TPotItem) t_con); return Create_Connection(*t_con); } delete t_con; } } } } if((mode & fDoNotConnect) != 0) return 0; // new connection needed if (srv_name.empty() || user_name.empty() || passwd.empty()) { throw CDB_ClientEx(eDB_Error, 200010, "CDBLibContext::Connect", "Insufficient info/credentials to connect"); } DBPROCESS* dbcon = x_ConnectToServer(srv_name, user_name, passwd, mode); if (!dbcon) { throw CDB_ClientEx(eDB_Error, 200011, "CDBLibContext::Connect", "Cannot connect to server"); }#ifdef MS_DBLIB_IN_USE dbsetopt(dbcon, DBTEXTLIMIT, "0" ); // No limit dbsetopt(dbcon, DBTEXTSIZE , "2147483647" ); // 0x7FFFFFFF#endif t_con = new CDBL_Connection(this, dbcon, reusable, pool_name); t_con->m_MsgHandlers = m_ConnHandlers; t_con->m_Server = srv_name; t_con->m_User = user_name; t_con->m_Passwd = passwd; t_con->m_BCPAble = (mode & fBcpIn) != 0; t_con->m_SecureLogin = (mode & fPasswordEncrypted) != 0; m_InUse.Add((TPotItem) t_con); return Create_Connection(*t_con);}bool CDBLibContext::IsAbleTo(ECapability cpb) const{ switch(cpb) { case eBcp: case eReturnITDescriptors: case eReturnComputeResults: return true; default: break; } return false;}CDBLibContext::~CDBLibContext(){ CDBL_Connection* t_con; // close all connections first for (int i = m_NotInUse.NofItems(); i--; ) { t_con = static_cast<CDBL_Connection*> (m_NotInUse.Get(i)); delete t_con; } for (int i = m_InUse.NofItems(); i--; ) { t_con = static_cast<CDBL_Connection*> (m_InUse.Get(i)); delete t_con; }#ifdef MS_DBLIB_IN_USE dbfreelogin(m_Login);#else dbloginfree(m_Login);#endif dbexit(); m_pDBLibContext = 0;}void CDBLibContext::DBLIB_SetApplicationName(const string& app_name){ m_AppName = app_name;}void CDBLibContext::DBLIB_SetHostName(const string& host_name){ m_HostName = host_name;}void CDBLibContext::DBLIB_SetPacketSize(int p_size){ m_PacketSize = (short) p_size;}bool CDBLibContext::DBLIB_SetMaxNofConns(int n){ return dbsetmaxprocs(n) == SUCCEED;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -