NCBI C++ ToolKit
dbapi_impl_connection.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef DBAPI_DRIVER_IMPL___DBAPI_IMPL_CONNECTION__HPP
2 #define DBAPI_DRIVER_IMPL___DBAPI_IMPL_CONNECTION__HPP
3 
4 
5 /* $Id: dbapi_impl_connection.hpp 102711 2024-06-28 14:39:22Z ucko $
6  * ===========================================================================
7  *
8  * PUBLIC DOMAIN NOTICE
9  * National Center for Biotechnology Information
10  *
11  * This software/database is a "United States Government Work" under the
12  * terms of the United States Copyright Act. It was written as part of
13  * the author's official duties as a United States Government employee and
14  * thus cannot be copyrighted. This software/database is freely available
15  * to the public for use. The National Library of Medicine and the U.S.
16  * Government have not placed any restriction on its use or reproduction.
17  *
18  * Although all reasonable efforts have been taken to ensure the accuracy
19  * and reliability of the software and data, the NLM and the U.S.
20  * Government do not and cannot warrant the performance or results that
21  * may be obtained by using this software or data. The NLM and the U.S.
22  * Government disclaim all warranties, express or implied, including
23  * warranties of performance, merchantability or fitness for any particular
24  * purpose.
25  *
26  * Please cite the author in any work or product based on this material.
27  *
28  * ===========================================================================
29  *
30  * Author: Sergey Sikorskiy
31  *
32  * File Description:
33  *
34  */
35 
38 
39 #include <corelib/ncbitime.hpp>
40 
42 
44 class CDB_Connection;
45 
46 namespace impl
47 {
48 
49 ////////////////////////////////////////////////////////////////////////////
50 class CDriverContext;
51 class CLangCmd;
52 class CRPCCmd;
53 class CBCPInCmd;
54 class CCursorCmd;
55 class CSendDataCmd;
56 class CCommand;
57 
58 /////////////////////////////////////////////////////////////////////////////
59 ///
60 /// CConnection::
61 ///
62 
64 {
65  friend class impl::CDriverContext;
66  friend class ncbi::CDBConnectionFactory;
67  friend class ncbi::CDB_Connection; // Because of AttachTo
68 
69 public:
71  const CDBConnParams& params,
72  bool isBCPable = false
73  );
74  virtual ~CConnection(void);
75 
77  {
78  return m_ResProc;
79  }
80 
82  {
83  _ASSERT(m_DriverContext);
84  return *m_DriverContext;
85  }
86  const CDriverContext& GetCDriverContext(void) const
87  {
88  _ASSERT(m_DriverContext);
89  return *m_DriverContext;
90  }
91 
92  bool IsMultibyteClientEncoding(void) const;
93  EEncoding GetClientEncoding(void) const;
94 
95  void SetExtraMsg(const string& msg) // const
96  {
97  m_ExceptionContext->extra_msg = msg;
98  }
99  const string& GetExtraMsg(void) const
100  {
101  return m_ExceptionContext->extra_msg;
102  }
103 
104 
105  const string& GetServerName(void) const
106  {
107  return ServerName();
108  }
109  const string& GetUserName(void) const
110  {
111  return UserName();
112  }
113  const string& GetPassword(void) const
114  {
115  return Password();
116  }
117  const string& GetDatabaseName(void) const;
118 
120  virtual const TDbgInfo& GetDbgInfo(void) const
121  {
122  return *m_ExceptionContext;
123  }
124 
125  const string& GetRequestedServer(void) const
126  {
127  return m_RequestedServer;
128  }
129 
130 public:
131  /// Check out if connection is alive (this function doesn't ping the server,
132  /// it just checks the status of connection which was set by the last
133  /// i/o operation)
134  virtual bool IsAlive(void) = 0;
135  bool IsOpeningFinished(void) const
136  {
137  return m_OpenFinished;
138  }
139  void FinishOpening(void)
140  {
141  m_OpenFinished = true;
142  }
143  bool IsValid(void) const
144  {
145  return m_Valid;
146  }
147 
148  /// These methods: LangCmd(), RPC(), BCPIn(), Cursor() and SendDataCmd()
149  /// create and return a "command" object, register it for later use with
150  /// this (and only this!) connection.
151  /// On error, an exception will be thrown (they never return NULL!).
152  /// It is the user's responsibility to delete the returned "command" object.
153 
154  /// Language command
155  virtual CDB_LangCmd* LangCmd(const string& lang_query) = 0;
156  /// Remote procedure call
157  virtual CDB_RPCCmd* RPC(const string& rpc_name) = 0;
158  /// "Bulk copy in" command
159  virtual CDB_BCPInCmd* BCPIn(const string& table_name) = 0; /// Cursor
160  virtual CDB_CursorCmd* Cursor(const string& cursor_name,
161  const string& query,
162  unsigned int batch_size = 1) = 0;
163  /// "Send-data" command
165  size_t data_size,
166  bool log_it = true,
167  bool dump_results = true)
168  = 0;
169 
170  /// Shortcut to send text and image to the server without using the
171  /// "Send-data" command (SendDataCmd)
172  virtual bool SendData(I_BlobDescriptor& desc, CDB_Stream& lob,
173  bool log_it = true) = 0;
174 
175  virtual void SetDatabaseName(const string& name);
176 
177  /// Reset the connection to the "ready" state (cancel all active commands)
178  virtual bool Refresh(void) = 0;
179  void Invalidate(void)
180  {
181  m_Valid = false;
182  }
183 
184  /// Get the server name, user login name, and password
185  const string& ServerName(void) const;
186  Uint4 Host(void) const;
187  Uint2 Port(void) const;
188  const string& UserName(void) const;
189  const string& Password(void) const;
190 
191  /// Get the bitmask for the connection mode (BCP, secure login, ...)
193 
194  /// Check if this connection is a reusable one
195  bool IsReusable(void) const;
196 
197  /// Find out which connection pool this connection belongs to
198  const string& PoolName(void) const;
199 
200  /// Get pointer to the driver context
201  I_DriverContext* Context(void) const;
202 
203  /// Put the message handler into message handler stack
204  void PushMsgHandler(CDB_UserHandler* h,
205  EOwnership ownership = eNoOwnership);
206 
207  /// Remove the message handler (and all above it) from the stack
208  void PopMsgHandler(CDB_UserHandler* h);
209 
210  CDB_ResultProcessor* SetResultProcessor(CDB_ResultProcessor* rp);
211 
212  /// abort the connection
213  /// Attention: it is not recommended to use this method unless you absolutely have to.
214  /// The expected implementation is - close underlying file descriptor[s] without
215  /// destroing any objects associated with a connection.
216  /// Returns: true - if succeed
217  /// false - if not
218  virtual bool Abort(void) = 0;
219 
220  /// Close an open connection.
221  /// Returns: true - if successfully closed an open connection.
222  /// false - if not
223  virtual bool Close(void) = 0;
224 
225  virtual void SetTimeout(size_t nof_secs) = 0;
226  virtual void SetCancelTimeout(size_t nof_secs) = 0;
227  virtual void SetBlobSize(size_t nof_bytes);
228  void SetTextImageSize(size_t nof_bytes) { SetBlobSize(nof_bytes); }
229 
230  virtual size_t GetTimeout(void) const;
231  virtual size_t GetCancelTimeout(void) const;
232 
233  virtual TSockHandle GetLowLevelHandle(void) const;
234 
235  virtual string GetDriverName(void) const;
236  virtual string GetVersionString(void) const;
237 
238  virtual unsigned int GetRowsInCurrentBatch(void) const { return 0; }
239 
240  CDBConnParams::EServerType GetServerType(void);
241  CDB_BigDateTime::ESyntax GetDateTimeSyntax(void);
242 
243  //
244  CDBConnParams::EServerType CalculateServerType(CDBConnParams::EServerType server_type);
245 
247  {
248  m_UserData.Reset(data);
249  }
250 
251 protected:
252  /// These methods to allow the children of CConnection to create
253  /// various command-objects
254  CDB_LangCmd* Create_LangCmd (CBaseCmd& lang_cmd );
255  CDB_RPCCmd* Create_RPCCmd (CBaseCmd& rpc_cmd );
256  CDB_BCPInCmd* Create_BCPInCmd (CBaseCmd& bcpin_cmd );
257 // CDB_CursorCmd* Create_CursorCmd (CCursorCmd& cursor_cmd );
258  CDB_CursorCmd* Create_CursorCmd (CBaseCmd& cursor_cmd );
259  CDB_SendDataCmd* Create_SendDataCmd (CSendDataCmd& senddata_cmd);
260 
261 protected:
262  void Release(void);
263  static CDB_Result* Create_Result(impl::CResult& result);
264 
265  const CDBHandlerStack& GetMsgHandlers(void) const
266  {
267  _ASSERT(m_MsgHandlers.GetSize() > 0);
268  return m_MsgHandlers;
269  }
271  {
272  _ASSERT(m_MsgHandlers.GetSize() > 0);
273  return m_MsgHandlers;
274  }
275 
276  void DropCmd(impl::CCommand& cmd);
277  void DeleteAllCommands(void);
278 
279 
280  //
281  void AttachTo(CDB_Connection* interface);
282  void ReleaseInterface(void);
283 
284  //
285  void DetachResultProcessor(void);
286 
287  void CheckCanOpen(void);
288  void MarkClosed(void);
289 
290  unsigned int GetReuseCount(void) const
291  {
292  return m_ReuseCount;
293  }
294 
295  //
296  bool IsBCPable(void) const
297  {
298  return m_BCPable;
299  }
300  bool HasSecureLogin(void) const
301  {
302  return m_SecureLogin;
303  }
304 
306  {
307  return *m_ExceptionContext;
308  }
309 
311  {
312  m_ServerType = type;
313  }
314 
316  {
317  _ASSERT( !m_OpenFinished );
318  return m_OpeningMsgHandlers;
319  }
320 
321  CObject* x_GetUserData(void) const
322  {
323  return const_cast<CObject*>(m_UserData.GetPointerOrNull());
324  }
325 
326  void x_RecordServer(const CDBServer& server);
327 
328  void x_DisableReuse(void);
329 
330 private:
331  typedef deque<impl::CCommand*> TCommandList;
332 
339 
343 
344  const string m_RequestedServer;
347  const string m_Passwd;
348  const string m_Pool;
349  unsigned int m_PoolMinSize;
350  unsigned int m_PoolMaxSize;
351  unsigned int m_PoolMaxConnUse; // Max number of time a connection is
352  // used before it is closed.
356  unsigned int m_ReuseCount;
359  bool m_Valid;
360  const bool m_BCPable; //< Does this connection support BCP (It is related to Context, actually)
361  const bool m_SecureLogin;
362  bool m_Opened;
363 };
364 
365 } // namespace impl
366 
368 
369 
370 
371 #endif /* DBAPI_DRIVER_IMPL___DBAPI_IMPL_CONNECTION__HPP */
CDBConnParams::
Definition: interfaces.hpp:258
IDBServiceMapper.
CObject –.
Definition: ncbiobj.hpp:180
CTimeSpan.
Definition: ncbitime.hpp:1313
CTime –.
Definition: ncbitime.hpp:296
I_BlobDescriptor::
Definition: interfaces.hpp:369
I_ConnectionExtra.
bool IsBCPable(void) const
virtual bool Close(void)=0
Close an open connection.
CDBHandlerStack & GetMsgHandlers(void)
virtual bool Abort(void)=0
abort the connection Attention: it is not recommended to use this method unless you absolutely have t...
virtual void SetTimeout(size_t nof_secs)=0
const string & GetRequestedServer(void) const
const CDriverContext & GetCDriverContext(void) const
const string & GetPassword(void) const
virtual CDB_SendDataCmd * SendDataCmd(I_BlobDescriptor &desc, size_t data_size, bool log_it=true, bool dump_results=true)=0
"Send-data" command
const CDBHandlerStack & GetMsgHandlers(void) const
CRef< TDbgInfo > m_ExceptionContext
const string & GetServerName(void) const
CDB_ResultProcessor * GetResultProcessor(void) const
virtual CDB_RPCCmd * RPC(const string &rpc_name)=0
Remote procedure call.
CDBConnParams::EServerType m_ServerType
const CDBHandlerStack & GetOpeningMsgHandlers(void) const
void SetTextImageSize(size_t nof_bytes)
CDriverContext * m_DriverContext
unsigned int GetReuseCount(void) const
void SetServerType(CDBConnParams::EServerType type)
bool HasSecureLogin(void) const
void SetExtraMsg(const string &msg)
CDBHandlerStack m_MsgHandlers
virtual void SetCancelTimeout(size_t nof_secs)=0
virtual bool IsAlive(void)=0
Check out if connection is alive (this function doesn't ping the server, it just checks the status of...
virtual CDB_BCPInCmd * BCPIn(const string &table_name)=0
"Bulk copy in" command
CDB_Exception::SContext TDbgInfo
const string & GetExtraMsg(void) const
virtual I_DriverContext::TConnectionMode ConnectMode(void) const =0
Get the bitmask for the connection mode (BCP, secure login, ...)
virtual bool Refresh(void)=0
Reset the connection to the "ready" state (cancel all active commands)
deque< impl::CCommand * > TCommandList
virtual unsigned int GetRowsInCurrentBatch(void) const
CDriverContext & GetCDriverContext(void)
const string & GetUserName(void) const
CInterfaceHook< CDB_Connection > m_Interface
virtual bool SendData(I_BlobDescriptor &desc, CDB_Stream &lob, bool log_it=true)=0
Shortcut to send text and image to the server without using the "Send-data" command (SendDataCmd)
void SetUserData(CObject *data)
bool IsOpeningFinished(void) const
CDBHandlerStack m_OpeningMsgHandlers
virtual CDB_CursorCmd * Cursor(const string &cursor_name, const string &query, unsigned int batch_size=1)=0
Cursor.
virtual const TDbgInfo & GetDbgInfo(void) const
CObject * x_GetUserData(void) const
TDbgInfo & SetDbgInfo(void)
CDB_ResultProcessor * m_ResProc
virtual CDB_LangCmd * LangCmd(const string &lang_query)=0
These methods: LangCmd(), RPC(), BCPIn(), Cursor() and SendDataCmd() create and return a "command" ob...
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
static const char table_name[]
Definition: bcp.c:249
static int type
Definition: getdata.c:31
char data[12]
Definition: iconv.c:80
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
EEncoding
Definition: ncbistr.hpp:199
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_DBAPIDRIVER_EXPORT
Definition: ncbi_export.h:392
Defines: CTimeFormat - storage class for time format.
static SLJIT_INLINE sljit_ins msg(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
static string query
Definition: type.c:6
#define _ASSERT
else result
Definition: token2.c:20
Modified on Fri Sep 20 14:58:27 2024 by modify_doxy.py rev. 669887