00001 #ifndef DBAPI_DRIVER___INTERFACES__HPP 00002 #define DBAPI_DRIVER___INTERFACES__HPP 00003 00004 /* $Id: interfaces.hpp 173033 2009-10-14 13:16:54Z ivanovp $ 00005 * =========================================================================== 00006 * 00007 * PUBLIC DOMAIN NOTICE 00008 * National Center for Biotechnology Information 00009 * 00010 * This software/database is a "United States Government Work" under the 00011 * terms of the United States Copyright Act. It was written as part of 00012 * the author's official duties as a United States Government employee and 00013 * thus cannot be copyrighted. This software/database is freely available 00014 * to the public for use. The National Library of Medicine and the U.S. 00015 * Government have not placed any restriction on its use or reproduction. 00016 * 00017 * Although all reasonable efforts have been taken to ensure the accuracy 00018 * and reliability of the software and data, the NLM and the U.S. 00019 * Government do not and cannot warrant the performance or results that 00020 * may be obtained by using this software or data. The NLM and the U.S. 00021 * Government disclaim all warranties, express or implied, including 00022 * warranties of performance, merchantability or fitness for any particular 00023 * purpose. 00024 * 00025 * Please cite the author in any work or product based on this material. 00026 * 00027 * =========================================================================== 00028 * 00029 * Author: Vladimir Soussov 00030 * 00031 * File Description: Data Server interfaces 00032 * 00033 */ 00034 00035 #include <corelib/ncbi_param.hpp> 00036 00037 #include <dbapi/driver/types.hpp> 00038 #include <dbapi/driver/exception.hpp> 00039 00040 #if defined(NCBI_OS_UNIX) 00041 # include <unistd.h> 00042 #endif 00043 00044 #include <map> 00045 00046 00047 /** @addtogroup DbInterfaces 00048 * 00049 * @{ 00050 */ 00051 00052 00053 BEGIN_NCBI_SCOPE 00054 00055 00056 class I_BaseCmd; 00057 00058 class I_DriverContext; 00059 00060 class I_Connection; 00061 class I_Result; 00062 class I_LangCmd; 00063 class I_RPCCmd; 00064 class I_BCPInCmd; 00065 class I_CursorCmd; 00066 class I_SendDataCmd; 00067 00068 class CDB_Connection; 00069 class CDB_Result; 00070 class CDB_LangCmd; 00071 class CDB_RPCCmd; 00072 class CDB_BCPInCmd; 00073 class CDB_CursorCmd; 00074 class CDB_SendDataCmd; 00075 class CDB_ResultProcessor; 00076 00077 class IConnValidator; 00078 00079 namespace impl 00080 { 00081 class CResult; 00082 class CBaseCmd; 00083 class CSendDataCmd; 00084 } 00085 00086 00087 class CDBParamVariant 00088 { 00089 public: 00090 enum ENameFormat { 00091 ePlainName, 00092 eQMarkName, // '...WHERE name=?' 00093 eNumericName, // '...WHERE name=:1' 00094 eNamedName, // '...WHERE name=:name' 00095 eFormatName, // ANSI C printf format codes, e.g. '...WHERE name=%s' 00096 eSQLServerName // '...WHERE name=@name' 00097 }; 00098 00099 public: 00100 CDBParamVariant(int pos); 00101 CDBParamVariant(unsigned int pos); 00102 CDBParamVariant(const char* name); 00103 CDBParamVariant(const string& name); 00104 ~CDBParamVariant(void); 00105 00106 public: 00107 bool IsPositional(void) const 00108 { 00109 return m_IsPositional; 00110 } 00111 unsigned int GetPosition(void) const 00112 { 00113 return m_Pos; 00114 } 00115 00116 00117 ENameFormat GetFormat(void) const 00118 { 00119 return m_Format; 00120 } 00121 string GetName(void) const 00122 { 00123 return m_Name; 00124 } 00125 string GetName(ENameFormat format) const; 00126 00127 static string MakePlainName(const char* name); 00128 00129 private: 00130 static string MakeName(const char* name, ENameFormat& format); 00131 00132 private: 00133 bool m_IsPositional; 00134 unsigned int m_Pos; 00135 ENameFormat m_Format; 00136 const string m_Name; 00137 }; 00138 00139 00140 00141 ///////////////////////////////////////////////////////////////////////////// 00142 /// 00143 /// CDBParams 00144 00145 class CDBParams 00146 { 00147 public: 00148 virtual ~CDBParams(void); 00149 00150 public: 00151 enum EDirection {eIn, eOut, eInOut}; 00152 00153 /// Get total number of columns in resultset. 00154 /// 00155 /// @return 00156 /// total number of columns in resultset 00157 virtual unsigned int GetNum(void) const = 0; 00158 00159 /// Get name of column. 00160 /// This method is returning const reference because meta-info MUST be 00161 /// cached for performance reasons. 00162 /// 00163 /// @param param 00164 /// Column number or name 00165 virtual const string& GetName( 00166 const CDBParamVariant& param, 00167 CDBParamVariant::ENameFormat format = 00168 CDBParamVariant::eSQLServerName) const = 0; 00169 00170 /// @brief 00171 /// 00172 /// @param param 00173 /// Column number or name 00174 /// 00175 /// @return 00176 /// Number of a columnn, which is corresponding to a name. 00177 virtual unsigned int GetIndex(const CDBParamVariant& param) const = 0; 00178 00179 /// Get maximum size in bytes for column. 00180 /// 00181 /// @param col 00182 /// Column number or name 00183 /// 00184 /// @return 00185 /// Max number of bytes needed to hold the returned data. 00186 virtual size_t GetMaxSize(const CDBParamVariant& param) const = 0; 00187 00188 /// Get data type for column in the resultset. 00189 /// 00190 /// @param param 00191 /// Column number or name 00192 virtual EDB_Type GetDataType(const CDBParamVariant& param) const = 0; 00193 00194 /// Get parameter's direction (in/out/inout). 00195 /// 00196 /// @param param 00197 /// Column number or name 00198 virtual EDirection GetDirection(const CDBParamVariant& param) const = 0; 00199 00200 /// This method stores pointer to data. 00201 /// 00202 /// @param param 00203 /// Column number or name 00204 /// 00205 /// @param value 00206 /// Binded object 00207 /// 00208 /// @param out_param 00209 /// true if this parameter is an output parameter 00210 virtual CDBParams& Bind( 00211 const CDBParamVariant& param, 00212 CDB_Object* value, 00213 bool out_param = false 00214 ); 00215 00216 /// This method stores copy of data. 00217 /// 00218 /// @param param 00219 /// Column number or name 00220 /// 00221 /// @param value 00222 /// Binded object 00223 /// 00224 /// @param out_param 00225 /// true if this parameter is an output parameter 00226 virtual CDBParams& Set( 00227 const CDBParamVariant& param, 00228 CDB_Object* value, 00229 bool out_param = false 00230 ); 00231 }; 00232 00233 00234 ///////////////////////////////////////////////////////////////////////////// 00235 /// 00236 /// CDBConnParams:: 00237 /// 00238 00239 class CDBConnParams 00240 { 00241 public: 00242 CDBConnParams(void); 00243 virtual ~CDBConnParams(void); 00244 00245 public: 00246 enum EServerType { 00247 eUnknown, //< Server type is not known 00248 eMySQL, //< MySQL server 00249 eSybaseOpenServer, //< Sybase Open server 00250 eSybaseSQLServer, //< Sybase SQL server 00251 eMSSqlServer //< Microsoft SQL server 00252 }; 00253 00254 virtual string GetDriverName(void) const = 0; 00255 virtual Uint4 GetProtocolVersion(void) const = 0; 00256 virtual EEncoding GetEncoding(void) const = 0; 00257 00258 virtual string GetServerName(void) const = 0; 00259 virtual string GetDatabaseName(void) const = 0; 00260 virtual string GetUserName(void) const = 0; 00261 virtual string GetPassword(void) const = 0; 00262 00263 virtual EServerType GetServerType(void) const = 0; 00264 virtual Uint4 GetHost(void) const = 0; 00265 virtual Uint2 GetPort(void) const = 0; 00266 00267 virtual CRef<IConnValidator> GetConnValidator(void) const = 0; 00268 00269 /// Parameters, which are not listed above explicitly, should be retrieved via 00270 /// SetParam() method. 00271 virtual string GetParam(const string& key) const = 0; 00272 00273 protected: 00274 void SetChildObj(const CDBConnParams& child_obj) const 00275 { 00276 _ASSERT(!m_ChildObj); 00277 m_ChildObj = &child_obj; 00278 } 00279 void ReleaseChildObj(void) const 00280 { 00281 m_ChildObj = NULL; 00282 } 00283 00284 protected: 00285 const CDBConnParams& GetThis(void) const 00286 { 00287 if (m_ChildObj) { 00288 return m_ChildObj->GetThis(); 00289 } 00290 00291 return *this; 00292 } 00293 00294 private: 00295 // Non-copyable. 00296 CDBConnParams(const CDBConnParams& other); 00297 CDBConnParams& operator =(const CDBConnParams& other); 00298 00299 private: 00300 mutable const CDBConnParams* m_ChildObj; 00301 00302 friend class CDBConnParamsDelegate; 00303 }; 00304 00305 00306 ///////////////////////////////////////////////////////////////////////////// 00307 class CDBConnParamsDelegate : public CDBConnParams 00308 { 00309 public: 00310 CDBConnParamsDelegate(const CDBConnParams& other); 00311 virtual ~CDBConnParamsDelegate(void); 00312 00313 public: 00314 virtual string GetDriverName(void) const; 00315 virtual Uint4 GetProtocolVersion(void) const; 00316 virtual EEncoding GetEncoding(void) const; 00317 00318 virtual string GetServerName(void) const; 00319 virtual string GetDatabaseName(void) const; 00320 virtual string GetUserName(void) const; 00321 virtual string GetPassword(void) const; 00322 00323 virtual EServerType GetServerType(void) const; 00324 virtual Uint4 GetHost(void) const; 00325 virtual Uint2 GetPort(void) const; 00326 00327 virtual CRef<IConnValidator> GetConnValidator(void) const; 00328 00329 virtual string GetParam(const string& key) const; 00330 00331 private: 00332 // Non-copyable. 00333 CDBConnParamsDelegate(const CDBConnParamsDelegate& other); 00334 CDBConnParamsDelegate& operator =(const CDBConnParamsDelegate& other); 00335 00336 private: 00337 const CDBConnParams& m_Other; 00338 }; 00339 00340 00341 ///////////////////////////////////////////////////////////////////////////// 00342 /// 00343 /// I_ITDescriptor:: 00344 /// 00345 /// Image or Text descriptor. 00346 /// 00347 00348 class I_ITDescriptor 00349 { 00350 public: 00351 virtual int DescriptorType(void) const = 0; 00352 virtual ~I_ITDescriptor(void); 00353 }; 00354 00355 00356 ///////////////////////////////////////////////////////////////////////////// 00357 /// 00358 /// EDB_ResType:: 00359 /// 00360 /// Type of result set 00361 /// 00362 00363 enum EDB_ResType { 00364 eDB_RowResult, 00365 eDB_ParamResult, 00366 eDB_ComputeResult, 00367 eDB_StatusResult, 00368 eDB_CursorResult 00369 }; 00370 00371 00372 ///////////////////////////////////////////////////////////////////////////// 00373 /// 00374 /// CParamStmt:: 00375 /// Parametrized statement. 00376 00377 class CParamStmt 00378 { 00379 public: 00380 CParamStmt(void); 00381 virtual ~CParamStmt(void); 00382 00383 public: 00384 /// Get meta-information about binded parameters. 00385 virtual CDBParams& GetBindParams(void) = 0; 00386 }; 00387 00388 ///////////////////////////////////////////////////////////////////////////// 00389 /// 00390 /// CParamRecordset:: 00391 /// Parametrized recordset. 00392 00393 class CParamRecordset : public CParamStmt 00394 { 00395 public: 00396 CParamRecordset(void); 00397 virtual ~CParamRecordset(void); 00398 00399 public: 00400 /// Get meta-information about defined parameters. 00401 virtual CDBParams& GetDefineParams(void) = 0; 00402 }; 00403 00404 00405 ///////////////////////////////////////////////////////////////////////////// 00406 /// 00407 /// I_BaseCmd:: 00408 /// 00409 /// Abstract base class for most "command" interface classes. 00410 /// 00411 00412 class I_BaseCmd : public CParamRecordset 00413 { 00414 public: 00415 I_BaseCmd(void); 00416 virtual ~I_BaseCmd(void); 00417 00418 public: 00419 /// Send command to the server 00420 virtual bool Send(void) = 0; 00421 /// Implementation-specific. 00422 /// @deprecated 00423 virtual bool WasSent(void) const = 0; 00424 00425 /// Cancel the command execution 00426 virtual bool Cancel(void) = 0; 00427 /// Implementation-specific. 00428 /// @deprecated 00429 virtual bool WasCanceled(void) const = 0; 00430 00431 /// Get result set 00432 virtual CDB_Result* Result(void) = 0; 00433 virtual bool HasMoreResults(void) const = 0; 00434 00435 // Check if command has failed 00436 virtual bool HasFailed(void) const = 0; 00437 00438 /// Get the number of rows affected by the command 00439 /// Special case: negative on error or if there is no way that this 00440 /// command could ever affect any rows (like PRINT). 00441 virtual int RowCount(void) const = 0; 00442 00443 /// Dump the results of the command 00444 /// if result processor is installed for this connection, it will be called for 00445 /// each result set 00446 virtual void DumpResults(void) = 0; 00447 }; 00448 00449 00450 ///////////////////////////////////////////////////////////////////////////// 00451 /// 00452 /// I_LangCmd:: 00453 /// I_RPCCmd:: 00454 /// I_BCPInCmd:: 00455 /// I_CursorCmd:: 00456 /// I_SendDataCmd:: 00457 /// 00458 /// "Command" interface classes. 00459 /// 00460 00461 00462 class I_LangCmd : public I_BaseCmd 00463 { 00464 public: 00465 I_LangCmd(void); 00466 virtual ~I_LangCmd(void); 00467 00468 protected: 00469 /// Add more text to the language command 00470 /// @deprecated 00471 virtual bool More(const string& query_text) = 0; 00472 }; 00473 00474 00475 00476 class I_RPCCmd : public I_BaseCmd 00477 { 00478 public: 00479 I_RPCCmd(void); 00480 virtual ~I_RPCCmd(void); 00481 00482 protected: 00483 /// Set the "recompile before execute" flag for the stored proc 00484 /// Implementation-specific. 00485 virtual void SetRecompile(bool recompile = true) = 0; 00486 00487 /// Get a name of the procedure. 00488 virtual const string& GetProcName(void) const = 0; 00489 }; 00490 00491 00492 00493 class I_BCPInCmd : public CParamStmt 00494 { 00495 public: 00496 I_BCPInCmd(void); 00497 virtual ~I_BCPInCmd(void); 00498 00499 protected: 00500 /// Send row to the server 00501 virtual bool SendRow(void) = 0; 00502 00503 /// Complete batch -- to store all rows transferred by far in this batch 00504 /// into the table 00505 virtual bool CompleteBatch(void) = 0; 00506 00507 /// Cancel the BCP command 00508 virtual bool Cancel(void) = 0; 00509 00510 /// Complete the BCP and store all rows transferred in last batch into 00511 /// the table 00512 virtual bool CompleteBCP(void) = 0; 00513 }; 00514 00515 00516 00517 class I_CursorCmd : public CParamRecordset 00518 { 00519 public: 00520 I_CursorCmd(void); 00521 virtual ~I_CursorCmd(void); 00522 00523 protected: 00524 /// Open the cursor. 00525 /// Return NULL if cursor resulted in no data. 00526 /// Throw exception on error. 00527 virtual CDB_Result* Open(void) = 0; 00528 00529 /// Update the last fetched row. 00530 /// NOTE: the cursor must be declared for update in CDB_Connection::Cursor() 00531 virtual bool Update(const string& table_name, const string& upd_query) = 0; 00532 00533 virtual bool UpdateTextImage(unsigned int item_num, CDB_Stream& data, 00534 bool log_it = true) = 0; 00535 00536 virtual CDB_SendDataCmd* SendDataCmd(unsigned int item_num, size_t size, 00537 bool log_it = true) = 0; 00538 /// Delete the last fetched row. 00539 /// NOTE: the cursor must be declared for delete in CDB_Connection::Cursor() 00540 virtual bool Delete(const string& table_name) = 0; 00541 00542 /// Get the number of fetched rows 00543 /// Special case: negative on error or if there is no way that this 00544 /// command could ever affect any rows (like PRINT). 00545 virtual int RowCount(void) const = 0; 00546 00547 /// Close the cursor. 00548 /// Return FALSE if the cursor is closed already (or not opened yet) 00549 virtual bool Close(void) = 0; 00550 }; 00551 00552 00553 00554 class I_SendDataCmd 00555 { 00556 public: 00557 I_SendDataCmd(void); 00558 virtual ~I_SendDataCmd(void); 00559 00560 protected: 00561 /// Send chunk of data to the server. 00562 /// Return number of bytes actually transferred to server. 00563 virtual size_t SendChunk(const void* pChunk, size_t nofBytes) = 0; 00564 virtual bool Cancel(void) = 0; 00565 }; 00566 00567 00568 00569 ///////////////////////////////////////////////////////////////////////////// 00570 /// 00571 /// I_Result:: 00572 /// 00573 00574 class I_Result 00575 { 00576 public: 00577 I_Result(void); 00578 virtual ~I_Result(void); 00579 00580 public: 00581 enum EGetItem {eAppendLOB, eAssignLOB}; 00582 00583 /// @brief 00584 /// Get type of the result 00585 /// 00586 /// @return 00587 /// Result type 00588 virtual EDB_ResType ResultType(void) const = 0; 00589 00590 /// Get meta-information about rows in resultset. 00591 virtual const CDBParams& GetDefineParams(void) const = 0; 00592 00593 /// Get # of items (columns) in the result 00594 /// @brief 00595 /// Get # of items (columns) in the result. 00596 /// 00597 /// @return 00598 /// Number of items (columns) in the result. 00599 virtual unsigned int NofItems(void) const = 0; 00600 00601 /// @brief 00602 /// Get name of a result item. 00603 /// 00604 /// @param item_num 00605 /// Number of item, starting from 0. 00606 /// 00607 /// @return 00608 /// NULL if "item_num" >= NofItems(), otherwise item name. 00609 virtual const char* ItemName(unsigned int item_num) const = 0; 00610 00611 /// @brief 00612 /// Get size (in bytes) of a result item. 00613 /// 00614 /// @param item_num 00615 /// Number of item, starting from 0. 00616 /// 00617 /// @return 00618 /// Return zero if "item_num" >= NofItems(). 00619 virtual size_t ItemMaxSize(unsigned int item_num) const = 0; 00620 00621 /// @brief 00622 /// Get datatype of a result item. 00623 /// 00624 /// @param item_num 00625 /// Number of item, starting from 0. 00626 /// 00627 /// @return 00628 /// Return 'eDB_UnsupportedType' if "item_num" >= NofItems(). 00629 virtual EDB_Type ItemDataType(unsigned int item_num) const = 0; 00630 00631 /// @brief 00632 /// Fetch next row 00633 /// 00634 /// @return 00635 /// - true if a record was fetched. 00636 /// - false if no more record can be fetched. 00637 virtual bool Fetch(void) = 0; 00638 00639 /// @brief 00640 /// Return current item number we can retrieve (0,1,...) 00641 /// 00642 /// @return 00643 /// Return current item number we can retrieve (0,1,...) 00644 /// Return "-1" if no more items left (or available) to read. 00645 virtual int CurrentItemNo(void) const = 0; 00646 00647 /// @brief 00648 /// Return number of columns in the recordset. 00649 /// 00650 /// @return 00651 /// number of columns in the recordset. 00652 virtual int GetColumnNum(void) const = 0; 00653 00654 /// @brief 00655 /// Get a result item (you can use either GetItem or ReadItem). 00656 /// 00657 /// @param item_buf 00658 /// If "item_buf" is not NULL, then use "*item_buf" (its type should be 00659 /// compatible with the type of retrieved item!) to retrieve the item to; 00660 /// otherwise allocate new "CDB_Object". 00661 /// In case of "CDB_Image" and "CDB_Text" data types value will be *appended* 00662 /// to the "item_buf" by default (policy == eAppendLOB). 00663 /// 00664 /// @param policy 00665 /// Data retrieval policy. If policy == eAppendLOB and "item_buf" is an 00666 /// object of CDB_Image or CDB_Text type, then data will be *appended* to 00667 /// the end of previously assigned data. If policy == eAssignLOB and "item_buf" is an 00668 /// object of CDB_Image or CDB_Text type, then new value will be *assigned* 00669 /// to the "item_buf" object. 00670 /// 00671 /// @return 00672 /// a result item 00673 /// 00674 /// @sa 00675 /// ReadItem, SkipItem 00676 virtual CDB_Object* GetItem(CDB_Object* item_buf = 0, EGetItem policy = eAppendLOB) = 0; 00677 00678 /// @brief 00679 /// Read a result item body (for text/image mostly). 00680 /// Throw an exception on any error. 00681 /// 00682 /// @param buffer 00683 /// Buffer to fill with data. 00684 /// @param buffer_size 00685 /// Buffere size. 00686 /// @param is_null 00687 /// Set "*is_null" to TRUE if the item is <NULL>. 00688 /// 00689 /// @return 00690 /// number of successfully read bytes. 00691 /// 00692 /// @sa 00693 /// GetItem, SkipItem 00694 virtual size_t ReadItem(void* buffer, size_t buffer_size, 00695 bool* is_null = 0) = 0; 00696 00697 /// @brief 00698 /// Get a descriptor for text/image column (for SendData). 00699 /// 00700 /// @return 00701 /// Return NULL if this result doesn't (or can't) have img/text descriptor. 00702 /// 00703 /// @note 00704 /// You need to call ReadItem (maybe even with buffer_size == 0) 00705 /// before calling this method! 00706 virtual I_ITDescriptor* GetImageOrTextDescriptor(void) = 0; 00707 00708 /// @brief 00709 /// Skip result item 00710 /// 00711 /// @return 00712 /// TRUE on success. 00713 /// 00714 /// @sa 00715 /// GetItem, ReadItem 00716 virtual bool SkipItem(void) = 0; 00717 }; 00718 00719 00720 ///////////////////////////////////////////////////////////////////////////// 00721 /// 00722 /// I_DriverContext:: 00723 /// 00724 00725 (NCBI_DBAPIDRIVER_EXPORT, 00726 bool, dbapi, conn_use_encrypt_data); 00727 typedef NCBI_PARAM_TYPE(dbapi, conn_use_encrypt_data) TDbapi_ConnUseEncryptData; 00728 00729 00730 class CDBConnParams; 00731 00732 class I_DriverContext 00733 { 00734 protected: 00735 I_DriverContext(void); 00736 00737 public: 00738 virtual ~I_DriverContext(void); 00739 00740 00741 /// Connection mode 00742 enum EConnectionMode { 00743 fBcpIn = 0x1, //< Enable BCP 00744 fPasswordEncrypted = 0x2, //< Encript password 00745 fDoNotConnect = 0x4 //< Use just connections from NotInUse pool 00746 // all driver-specific mode flags > 0x100 00747 }; 00748 00749 typedef int TConnectionMode; //< holds a binary OR of "EConnectionMode" 00750 00751 00752 /// Set login timeout. 00753 /// 00754 /// @param nof_secs 00755 /// Timeout in seconds. If "nof_secs" is zero or is "too big" 00756 /// (depends on the underlying DB API), then set the timeout to infinite. 00757 /// @return 00758 /// FALSE on error. 00759 /// 00760 /// @sa 00761 /// GetLoginTimeout() 00762 virtual bool SetLoginTimeout(unsigned int nof_secs = 0) = 0; 00763 00764 /// Set connection timeouts. 00765 /// 00766 /// @param nof_secs 00767 /// Timeout in seconds. If "nof_secs" is zero or is "too big" 00768 /// (depends on the underlying DB API), then set the timeout to infinite. 00769 /// @return 00770 /// FALSE on error. 00771 /// 00772 /// @sa 00773 /// GetTimeout() 00774 virtual bool SetTimeout(unsigned int nof_secs = 0) = 0; 00775 00776 /// Get login timeout 00777 /// 00778 /// @return 00779 /// Login timeout. 00780 /// @sa 00781 /// SetLoginTimeout() 00782 virtual unsigned int GetLoginTimeout(void) const = 0; 00783 00784 /// Get connection timeout 00785 /// 00786 /// @return 00787 /// Connection timeout. 00788 /// 00789 /// @sa 00790 /// SetTimeout() 00791 virtual unsigned int GetTimeout(void) const = 0; 00792 00793 /// Set maximal size for Text and Image objects. 00794 /// 00795 /// @param nof_bytes 00796 /// Maximal size for Text and Image objects. Text and Image objects 00797 /// exceeding this size will be truncated. 00798 /// @return 00799 /// FALSE on error (e.g. if "nof_bytes" is too big). 00800 virtual bool SetMaxTextImageSize(size_t nof_bytes) = 0; 00801 00802 00803 /// @brief 00804 /// Create new connection to specified server (or service) within this context. 00805 /// 00806 /// @param srv_name 00807 /// Server/Service name. This parameter can be a SERVER name (host name, 00808 /// DNS alias name, or one of the names found in interfaces file) or an 00809 /// IP address. This parameter can also be a SERVICE name, which is 00810 /// defined by Load Balancer framework. In order to enable using of LB 00811 /// service names you have to call DBLB_INSTALL_DEFAULT() macro before 00812 /// any other DBAPI method. 00813 /// @param user_name 00814 /// User name. 00815 /// @param passwd 00816 /// User password. 00817 /// @param mode 00818 /// Connection mode. 00819 /// @param reusable 00820 /// If set to TRUE, then return connection into a connection pool on deletion. 00821 /// @param pool_name 00822 /// Name of a pool to which this connection is going to belong. 00823 /// 00824 /// @return 00825 /// Connection object on success, NULL on error. 00826 /// 00827 /// NOTE: 00828 /// 00829 /// It is your responsibility to delete the returned connection object. 00830 /// reusable - controls connection pooling mechanism. If it is set to true 00831 /// then a connection will be added to a pool of connections instead of 00832 /// closing. 00833 /// 00834 /// srv_name, user_name and passwd may be set to empty string. 00835 /// 00836 /// If pool_name is provided then connection will be taken from a pool 00837 /// having this name if a pool is not empty. 00838 /// 00839 /// It is your responsibility to put connections with the same 00840 /// server/user/password values in a pool. 00841 /// 00842 /// If a pool name is not provided but a server name (srv_name) is provided 00843 /// instead then connection with the same name will be taken from a pool of 00844 /// connections if a pool is not empty. 00845 /// 00846 /// If a pool is empty then new connection will be created unless you passed 00847 /// mode = fDoNotConnect. In this case NULL will be returned. 00848 /// 00849 /// If you did not provide either a pool name or a server name then NULL will 00850 /// be returned. 00851 CDB_Connection* Connect( 00852 const string& srv_name, 00853 const string& user_name, 00854 const string& passwd, 00855 TConnectionMode mode, 00856 bool reusable = false, 00857 const string& pool_name = kEmptyStr); 00858 00859 /// @brief 00860 /// Create new connection to specified server (within this context). 00861 /// 00862 /// @param srv_name 00863 /// Server/Service name. This parameter can be a SERVER name (host name, 00864 /// DNS alias name, or one of the names found in interfaces file) or an 00865 /// IP address. This parameter can also be a SERVICE name, which is 00866 /// defined by Load Balancer framework. In order to enable using of LB 00867 /// service names you have to call DBLB_INSTALL_DEFAULT() macro before 00868 /// any other DBAPI method. 00869 /// @param user_name 00870 /// User name. 00871 /// @param passwd 00872 /// User password. 00873 /// @param validator 00874 /// Connection validation object. 00875 /// @param mode 00876 /// Connection mode. 00877 /// @param reusable 00878 /// If set to true put connection into a connection pool on deletion. 00879 /// @param pool_name 00880 /// Name of a pool to which this connection is going to belong. 00881 /// 00882 /// @return 00883 /// Connection object on success, NULL on error. 00884 /// 00885 /// NOTE: 00886 /// 00887 /// It is your responsibility to delete the returned connection object. 00888 /// reusable - controls connection pooling mechanism. If it is set to true 00889 /// then a connection will be added to a pool of connections instead of 00890 /// closing. 00891 /// 00892 /// srv_name, user_name and passwd may be set to empty string. 00893 /// 00894 /// If pool_name is provided then connection will be taken from a pool 00895 /// having this name if a pool is not empty. 00896 /// 00897 /// It is your responsibility to put connections with the same 00898 /// server/user/password values in a pool. 00899 /// 00900 /// If a pool name is not provided but a server name (srv_name) is provided 00901 /// instead then connection with the same name will be taken from a pool of 00902 /// connections if a pool is not empty. 00903 /// 00904 /// If a pool is empty then new connection will be created unless you passed 00905 /// mode = fDoNotConnect. In this case NULL will be returned. 00906 /// 00907 /// If you did not provide either a pool name or a server name then NULL will 00908 /// be returned. 00909 CDB_Connection* ConnectValidated( 00910 const string& srv_name, 00911 const string& user_name, 00912 const string& passwd, 00913 IConnValidator& validator, 00914 TConnectionMode mode = 0, 00915 bool reusable = false, 00916 const string& pool_name = kEmptyStr); 00917 00918 /// @brief 00919 /// Create connection object using Load Balancer / connection factory. 00920 /// 00921 /// @param params 00922 /// Connection parameters. 00923 /// 00924 /// @return 00925 /// Connection object. 00926 virtual CDB_Connection* MakeConnection(const CDBConnParams& params) = 0; 00927 00928 /// @brief 00929 /// Return number of currently open connections in this context. 00930 /// 00931 /// @param srv_name 00932 /// Server/Service name. If not empty, then return # of connection 00933 /// open to that server. 00934 /// @param pool_name 00935 /// Name of connection pool. 00936 /// 00937 /// @return 00938 /// Return number of currently open connections in this context. 00939 /// If "srv_name" is not NULL, then return # of conn. open to that server. 00940 virtual unsigned int NofConnections(const string& srv_name = "", 00941 const string& pool_name = "") 00942 const = 0; 00943 00944 /// @brief 00945 /// Add message handler "h" to process 'context-wide' (not bound 00946 /// to any particular connection) error messages. 00947 /// 00948 /// @param h 00949 /// Error message handler. 00950 /// @param ownership 00951 /// If set to eNoOwnership, it is user's responsibility to unregister 00952 /// and delete the error message handler. 00953 /// If set to eTakeOwnership, then DBAPI will take ownership of the 00954 /// error message handler and delete it itself. 00955 /// 00956 /// @sa 00957 /// PopCntxMsgHandler() 00958 virtual void PushCntxMsgHandler(CDB_UserHandler* h, 00959 EOwnership ownership = eNoOwnership) = 0; 00960 00961 /// @brief 00962 /// Remove message handler "h" and all handlers above it in the stack. 00963 /// 00964 /// @param h 00965 /// Error message handler to be removed. 00966 /// 00967 /// @sa 00968 /// PushCntxMsgHandler() 00969 virtual void PopCntxMsgHandler(CDB_UserHandler* h) = 0; 00970 00971 /// @brief 00972 /// Add `per-connection' err.message handler "h" to the stack of default 00973 /// handlers which are inherited by all newly created connections. 00974 /// 00975 /// @param h 00976 /// Error message handler. 00977 /// @param ownership 00978 /// If set to eNoOwnership, it is user's responsibility to unregister 00979 /// and delete the error message handler. 00980 /// If set to eTakeOwnership, then DBAPI will take ownership of the 00981 /// error message handler and delete it itself. 00982 /// 00983 /// @sa 00984 /// PopDefConnMsgHandler() 00985 virtual void PushDefConnMsgHandler(CDB_UserHandler* h, 00986 EOwnership ownership = eNoOwnership) = 0; 00987 00988 /// @brief 00989 /// Remove `per-connection' mess. handler "h" and all above it in the stack. 00990 /// 00991 /// @param h 00992 /// Error message handler. 00993 /// 00994 /// @sa 00995 /// PushDefConnMsgHandler() 00996 virtual void PopDefConnMsgHandler(CDB_UserHandler* h) = 0; 00997 00998 /// Report if the driver supports this functionality 00999 enum ECapability { 01000 eBcp, //< Is able to run BCP operations. 01001 eReturnITDescriptors, //< Is able to return ITDescriptor. 01002 eReturnComputeResults //< Is able to return compute results. 01003 }; 01004 01005 /// @brief 01006 /// Check if a driver is acle to provide necessary functionality. 01007 /// 01008 /// @param cpb 01009 /// Functionality to query about. 01010 /// 01011 /// @return 01012 /// - true if functionality is present 01013 /// - false if no such functionality. 01014 virtual bool IsAbleTo(ECapability cpb) const = 0; 01015 01016 /// @brief 01017 /// Close reusable deleted connections for specified server and/or pool. 01018 /// 01019 /// @param srv_name 01020 /// Server/Service name. 01021 /// @param pool_name 01022 /// Name of connection pool. 01023 virtual void CloseUnusedConnections(const string& srv_name = kEmptyStr, 01024 const string& pool_name = kEmptyStr) = 0; 01025 01026 /// @brief 01027 /// Set application name. 01028 /// 01029 /// @param app_name 01030 /// defines the application name that a connection will use when 01031 /// connecting to a server. 01032 /// 01033 /// @sa 01034 /// GetApplicationName() 01035 virtual void SetApplicationName(const string& app_name) = 0; 01036 01037 /// @brief 01038 /// Return application name. 01039 /// 01040 /// @return 01041 /// Application name. 01042 /// 01043 /// @sa 01044 /// SetApplicationName() 01045 virtual string GetApplicationName(void) const = 0; 01046 01047 /// @brief 01048 /// Set host name. 01049 /// 01050 /// @param host_name 01051 /// Host name 01052 /// 01053 /// @sa 01054 /// GetHostName() 01055 virtual void SetHostName(const string& host_name) = 0; 01056 01057 /// @brief 01058 /// Get host name. 01059 /// 01060 /// @return 01061 /// Host name. 01062 /// 01063 /// @sa 01064 /// SetHostName() 01065 virtual string GetHostName(void) const = 0; 01066 01067 protected: 01068 /// @brief 01069 /// Create connection object WITHOUT using of Load Balancer / connection factory. 01070 /// 01071 /// @param params 01072 /// Connection parameters. 01073 /// 01074 /// @return 01075 /// Connection object. 01076 virtual CDB_Connection* MakePooledConnection(const CDBConnParams& params) = 0; 01077 01078 private: 01079 friend class IDBConnectionFactory; 01080 }; 01081 01082 01083 01084 ///////////////////////////////////////////////////////////////////////////// 01085 /// 01086 /// I_Connection:: 01087 /// 01088 01089 class I_Connection 01090 { 01091 public: 01092 I_Connection(void); 01093 virtual ~I_Connection(void); 01094 01095 protected: 01096 /// @brief 01097 /// Check out if connection is alive. 01098 /// 01099 /// This function doesn't ping the server, 01100 /// it just checks the status of connection which was set by the last 01101 /// i/o operation. 01102 /// 01103 /// @return 01104 /// - true if connection is alive 01105 /// - false in other case. 01106 virtual bool IsAlive(void) = 0; 01107 01108 /// These methods: LangCmd(), RPC(), BCPIn(), Cursor() and SendDataCmd() 01109 /// create and return a "command" object, register it for later use with 01110 /// this (and only this!) connection. 01111 /// On error, an exception will be thrown (they never return NULL!). 01112 /// It is the user's responsibility to delete the returned "command" object. 01113 01114 /// Language command 01115 virtual CDB_LangCmd* LangCmd(const string& lang_query) = 0; 01116 /// Remote procedure call 01117 virtual CDB_RPCCmd* RPC(const string& rpc_name) = 0; 01118 /// "Bulk copy in" command 01119 virtual CDB_BCPInCmd* BCPIn(const string& table_name) = 0; 01120 /// Cursor 01121 virtual CDB_CursorCmd* Cursor(const string& cursor_name, 01122 const string& query, 01123 unsigned int batch_size) = 0; 01124 CDB_CursorCmd* Cursor(const string& cursor_name, 01125 const string& query) 01126 { 01127 return Cursor(cursor_name, query, 1); 01128 } 01129 /// @brief 01130 /// Create send-data command. 01131 /// 01132 /// @param desc 01133 /// Lob descriptor. 01134 /// @param data_size 01135 /// Maximal data size. 01136 /// @param log_it 01137 /// Log LOB operation if this value is set to true. 01138 /// 01139 /// @return 01140 /// Newly created send-data object. 01141 /// 01142 /// @sa SendData 01143 virtual CDB_SendDataCmd* SendDataCmd(I_ITDescriptor& desc, 01144 size_t data_size, 01145 bool log_it = true) = 0; 01146 01147 /// @brief 01148 /// Shortcut to send text and image to the server without using the 01149 /// "Send-data" command (SendDataCmd) 01150 /// 01151 /// @param desc 01152 /// Lob descriptor. 01153 /// @param lob 01154 /// Text or Image object. 01155 /// @param log_it 01156 /// Log LOB operation if this value is set to true. 01157 /// 01158 /// @return 01159 /// - true on success. 01160 /// 01161 /// @sa 01162 /// SendDataCmd 01163 virtual bool SendData(I_ITDescriptor& desc, CDB_Stream& lob, 01164 bool log_it = true) = 0; 01165 01166 /// @brief 01167 /// Reset the connection to the "ready" state (cancel all active commands) 01168 /// 01169 /// @return 01170 /// - true on success. 01171 virtual bool Refresh(void) = 0; 01172 01173 /// @brief 01174 /// Get the server name. 01175 /// 01176 /// @return 01177 /// Server/Service name. 01178 virtual const string& ServerName(void) const = 0; 01179 01180 /// @brief 01181 /// Get the user user. 01182 /// 01183 /// @return 01184 /// User name. 01185 virtual const string& UserName(void) const = 0; 01186 01187 /// @brief 01188 /// Get the password. 01189 /// 01190 /// @return 01191 /// Password value. 01192 virtual const string& Password(void) const = 0; 01193 01194 /// @brief 01195 /// Get the database name. 01196 /// 01197 /// @return 01198 /// Password value. 01199 virtual const string& DatabaseName(void) const = 0; 01200 01201 /// @brief 01202 /// Get the bitmask for the connection mode (BCP, secure login, ...) 01203 /// 01204 /// @return 01205 /// bitmask for the connection mode (BCP, secure login, ...) 01206 virtual I_DriverContext::TConnectionMode ConnectMode(void) const = 0; 01207 01208 /// @brief 01209 /// Check if this connection is a reusable one 01210 /// 01211 /// @return 01212 /// - true if this connection is a reusable one. 01213 virtual bool IsReusable(void) const = 0; 01214 01215 /// @brief 01216 /// Find out which connection pool this connection belongs to 01217 /// 01218 /// @return 01219 /// connection pool 01220 virtual const string& PoolName(void) const = 0; 01221 01222 /// @brief 01223 /// Get pointer to the driver context 01224 /// 01225 /// @return 01226 /// pointer to the driver context 01227 virtual I_DriverContext* Context(void) const = 0; 01228 01229 /// @brief 01230 /// Put the message handler into message handler stack 01231 /// 01232 /// @param h 01233 /// Error message handler. 01234 /// @param ownership 01235 /// If set to eNoOwnership, it is user's responsibility to unregister 01236 /// and delete the error message handler. 01237 /// If set to eTakeOwnership, then DBAPI will take ownership of the 01238 /// error message handler and delete it itself. 01239 /// 01240 /// @sa 01241 /// PopMsgHandler 01242 virtual void PushMsgHandler(CDB_UserHandler* h, 01243 EOwnership ownership = eNoOwnership) = 0; 01244 01245 /// @brief 01246 /// Remove the message handler (and all above it) from the stack 01247 /// 01248 /// @param h 01249 /// Error message handler. 01250 /// @sa 01251 /// PushMsgHandler 01252 virtual void PopMsgHandler(CDB_UserHandler* h) = 0; 01253 01254 /// @brief 01255 /// Set new result-processor. 01256 /// 01257 /// @param rp 01258 /// New result-processor. 01259 /// 01260 /// @return 01261 /// Old result-processor 01262 virtual CDB_ResultProcessor* SetResultProcessor(CDB_ResultProcessor* rp) = 0; 01263 01264 /// Abort the connection 01265 /// 01266 /// @return 01267 /// TRUE - if succeeded, FALSE if not 01268 /// 01269 /// @note 01270 /// Attention: it is not recommended to use this method unless you 01271 /// absolutely have to. The expected implementation is - close 01272 /// underlying file descriptor[s] without destroing any objects 01273 /// associated with a connection. 01274 /// 01275 /// @sa 01276 /// Close 01277 virtual bool Abort(void) = 0; 01278 01279 /// Close an open connection. 01280 /// This method will return connection (if it is created as reusable) to 01281 /// its connection pool 01282 /// 01283 /// @return 01284 /// TRUE - if succeeded, FALSE if not 01285 /// 01286 /// @sa 01287 /// Abort, I_DriverContext::Connect 01288 virtual bool Close(void) = 0; 01289 01290 /// @brief 01291 /// Set connection timeout. 01292 /// 01293 /// @param nof_secs 01294 /// Number of seconds. If "nof_secs" is zero or is "too big" 01295 /// (depends on the underlying DB API), then set the timeout to infinite. 01296 virtual void SetTimeout(size_t nof_secs) = 0; 01297 01298 public: 01299 // Deprecated legacy methods. 01300 01301 /// @deprecated 01302 CDB_LangCmd* LangCmd(const string& lang_query, unsigned int /*unused*/) 01303 { 01304 return LangCmd(lang_query); 01305 } 01306 /// @deprecated 01307 CDB_RPCCmd* RPC(const string& rpc_name, unsigned int /*unused*/) 01308 { 01309 return RPC(rpc_name); 01310 } 01311 /// @deprecated 01312 CDB_BCPInCmd* BCPIn(const string& table_name, unsigned int /*unused*/) 01313 { 01314 return BCPIn(table_name); 01315 } 01316 /// @deprecated 01317 CDB_CursorCmd* Cursor(const string& cursor_name, 01318 const string& query, 01319 unsigned int /*unused*/, 01320 unsigned int batch_size) 01321 { 01322 return Cursor(cursor_name, query, batch_size); 01323 } 01324 01325 }; 01326 01327 01328 END_NCBI_SCOPE 01329 01330 01331 01332 /* @} */ 01333 01334 01335 #endif /* DBAPI_DRIVER___INTERFACES__HPP */ 01336 01337
1.4.6
Modified on Mon Dec 07 16:20:35 2009 by modify_doxy.py rev. 173732