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

Go to the SVN repository for this file.

1 #ifndef DBAPI___DBAPI__HPP
2 #define DBAPI___DBAPI__HPP
3 
4 /* $Id: dbapi.hpp 102711 2024-06-28 14:39:22Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Michael Kholodov
30  *
31  * File Description: Database API interface
32  *
33  */
34 
35 /// @file dbapi.hpp
36 /// Defines the database API innterfaces for a variety of DBMS.
37 
38 #include <corelib/ncbiobj.hpp>
40 #include <dbapi/driver_mgr.hpp>
41 #include <dbapi/variant.hpp>
42 
43 
44 // Forward declaration.
46 class CVersionInfo;
48 
49 
50 /** @addtogroup DbAPI
51  *
52  * @{
53  */
54 
55 
57 
58 
59 /////////////////////////////////////////////////////////////////////////////
60 ///
61 /// EDataSource --
62 ///
63 /// Data source platform
64 ///
65 /// enum EDataSource {
66 /// eSybase,
67 /// eMsSql
68 /// };
69 
70 
71 
72 /////////////////////////////////////////////////////////////////////////////
73 ///
74 /// EAllowLog --
75 ///
76 /// Allow transaction log (general, to avoid using bools).
77 ///
78 enum EAllowLog {
79  eDisableLog, ///< Disables log.
80  eEnableLog ///< Enables log.
81 };
82 
83 
84 /////////////////////////////////////////////////////////////////////////////
85 ///
86 /// EBlobOStreamFlags --
87 ///
88 /// How to send BLOB data (generalization of EAllowLog).
89 ///
91  /// Use a (sub)transaction (committed once all data's been sent
92  /// successfully, rolled back for unrecoverable errors) to guard
93  /// against races that could let readers see incomplete blobs, and
94  /// to allow for replication as appropriate. NB: This approach can
95  /// yield local deadlocks in some circumstances, particularly if a
96  /// single application thread maintains multiple connections to the
97  /// database in question (due, for instance, to using methods that
98  /// establish temporary clones of the original connection).
100  fBOS_SkipLogging = 1 << 1 //< Don't request server-side logging.
101 };
102 
103 typedef int TBlobOStreamFlags; //< Binary OR of EBlobOStreamFlags
104 
105 
106 /////////////////////////////////////////////////////////////////////////////
107 ///
108 /// IResultSetMetaData --
109 ///
110 /// Interface class defines retrieving column information from a resultset,
111 /// such as total number of columns, type, name, etc.
112 
114 {
115 public:
116  /// Destructor.
117  ///
118  /// Clean up the metadata for the resultset.
119  virtual ~IResultSetMetaData(void);
120 
121  /// Convenience method to check whether a column with a given name
122  /// (or number) actually exists.
123  bool HasColumn(const CDBParamVariant& param) const;
124 
125  /// Get total number of columns in resultset.
126  virtual unsigned int GetTotalColumns(void) const = 0;
127 
128  /// Get data type for column in the resultset.
129  ///
130  /// @param param
131  /// Column number or name
132  virtual EDB_Type GetType(const CDBParamVariant& param) const = 0;
133 
134  /// Get maximum size in bytes for column.
135  ///
136  /// @param col
137  /// Column number
138  ///
139  /// @return
140  /// Max number of bytes needed to hold the returned data.
141  virtual int GetMaxSize (const CDBParamVariant& param) const = 0;
142 
143  /// Get name of column.
144  ///
145  /// @param param
146  /// Column number or name
147  virtual string GetName (const CDBParamVariant& param) const = 0;
148 
149  /// Get parameter's direction (in/out/inout).
150  ///
151  /// @param param
152  /// Column number or name
153  virtual CDBParams::EDirection GetDirection(const CDBParamVariant& param) const = 0;
154 };
155 
156 
157 
158 /////////////////////////////////////////////////////////////////////////////
159 ///
160 /// IResultSet --
161 ///
162 /// Used to retrieve a resultset from a query or cursor
163 
164 class IConnection;
165 
167 {
168 public:
169  /// Destructor.
170  ///
171  /// Clean up the resultset.
172  virtual ~IResultSet();
173 
174  /// Get result type.
175  ///
176  /// @sa
177  /// See in <dbapi/driver/interfaces.hpp> for the list of result types.
178  virtual EDB_ResType GetResultType() = 0;
179 
180  /// Get next row.
181  ///
182  /// NOTE: no results are fetched before first call to this function.
183  virtual bool Next() = 0;
184 
185  /// Retrieve a CVariant class describing the data stored in a given column.
186  /// Note that the index supplied is one-based, not zero-based; the first
187  /// column is column 1.
188  ///
189  /// @param param
190  /// Column number (one-based) or name
191  /// @return
192  /// All data (for BLOB data see below) is returned as CVariant.
193  virtual const CVariant& GetVariant(const CDBParamVariant& param) = 0;
194 
195  /// Disables column binding.
196  /// @note
197  /// When binding is disabled all columns must be read with Read()
198  /// method, GetVariant() method will always return NULL in this case.
199  ///
200  /// False by default.
201  /// @param
202  /// Disables column binding when set to true.
203  virtual void DisableBind(bool b) = 0;
204 
205  /// Bind blob to variant.
206  ///
207  /// If this mode is true, BLOB data is returned as CVariant
208  /// False by default.
209  /// @note
210  /// When binding of blobs to variant is disabled all columns in
211  /// resultset placed after first blob column must be read with Read()
212  /// method, GetVariant() method will always return NULL for these
213  /// columns.
214  ///
215  /// @param
216  /// Enables blob binding when set to true.
217  virtual void BindBlobToVariant(bool b) = 0;
218 
219  /// Read unformatted data.
220  ///
221  /// Reads unformatted data, returns bytes actually read.
222  /// Advances to next column as soon as data is read from the previous one.
223  /// Returns 0 when the column data is fully read
224  /// Valid only when the column binding is off (see DisableBind())
225  /// @param buf
226  /// Buffer to read data.
227  /// @param size
228  /// Amount of data to read.
229  /// @return
230  /// Actual number of bytes read.
231  virtual size_t Read(void* buf, size_t size) = 0;
232 
233  /// Determine if last column was NULL.
234  ///
235  /// Valid only when the column binding is off.
236  /// @return
237  /// Return true if the last column read was NULL.
238  /// @sa
239  /// DisableBind().
240  virtual bool WasNull() = 0;
241 
242  /// Get column number, currently available for Read()
243  ///
244  /// @return
245  /// Returns current item number we can retrieve (1,2,...) using Read()
246  /// Returns "0" if no more items left (or available) to read
247  virtual int GetColumnNo() = 0;
248 
249  /// Get total columns.
250  ///
251  /// @return
252  /// Returns total number of columns in the resultset
253  virtual unsigned int GetTotalColumns() = 0;
254 
255  /// Get Blob input stream.
256  ///
257  /// @param buf_size
258  /// buf_size is the size of internal buffer, default 4096.
259  virtual CNcbiIstream& GetBlobIStream(size_t buf_size = 0) = 0;
260 
261  /// Get Blob output stream. The existing connection is
262  /// cloned for writing blob.
263  ///
264  /// @param blob_size
265  /// blob_size is the size of the BLOB to be written.
266  /// @param flags
267  /// @see EBlobOStreamFlags.
268  /// @param buf_size
269  /// The size of internal buffer, default 4096.
270  /// @deprecated
271  /// Please use IStatement::GetBlobOStream instead.
272  /// @sa
273  /// IStatement::GetBlobOStream
275  virtual CNcbiOstream& GetBlobOStream(size_t blob_size,
277  size_t buf_size = 0) = 0;
278 
280  virtual CNcbiOstream& GetBlobOStream(size_t blob_size,
281  EAllowLog log_it,
282  size_t buf_size = 0);
283 
284  /// Get Blob output stream with explicit additional connection.
285  ///
286  /// @param conn
287  /// addtional connection used for writing blob (the above method
288  /// clones the existing connection implicitly)
289  /// @param blob_size
290  /// blob_size is the size of the BLOB to be written.
291  /// @param flags
292  /// @see EBlobOStreamFlags.
293  /// @param buf_size
294  /// The size of internal buffer, default 4096.
295  /// @deprecated
296  /// Please use IStatement::GetBlobOStream instead.
297  /// @sa
298  /// IStatement::GetBlobOStream
301  size_t blob_size,
303  size_t buf_size = 0) = 0;
304 
306  virtual CNcbiOstream& GetBlobOStream(IConnection *conn,
307  size_t blob_size,
308  EAllowLog log_it,
309  size_t buf_size = 0);
310 
311  /// Get a Blob Reader.
312  ///
313  /// @param
314  /// Pointer to the Blob Reader.
315  virtual IReader* GetBlobReader() = 0;
316 
317  /// Close resultset.
318  virtual void Close() = 0;
319 
320  /// Get Metadata.
321  ///
322  /// @return
323  /// Pointer to result metadata.
324  virtual const IResultSetMetaData* GetMetaData(EOwnership ownership = eNoOwnership) = 0;
325 };
326 
327 
328 
329 /////////////////////////////////////////////////////////////////////////////
330 ///
331 /// IStatement --
332 ///
333 /// Interface for a SQL statement
334 
335 class I_BlobDescriptor;
336 
338 {
339 public:
340  /// Destructor.
341  virtual ~IStatement();
342 
343  /// Get resulset.
344  ///
345  /// @return
346  /// Pointer to resultset. For statements with no resultset return 0.
347  virtual IResultSet* GetResultSet() = 0;
348 
349  /// Check for more results available.
350  ///
351  /// Each call advances to the next result and the current one
352  /// will be cancelled it not retrieved before next call.
353  /// The amount of retured results may be bigger than the expected amount
354  /// due to auxiliary results returned depending on the driver and server
355  /// platform.
356  ///
357  /// @return
358  /// Return true, if there are more results available.
359  virtual bool HasMoreResults() = 0;
360 
361  /// Check if the statement failed.
362  ///
363  /// @return
364  /// Return true, if the statement failed.
365  virtual bool Failed() = 0;
366 
367  /// Check if resultset has rows.
368  ///
369  /// @return
370  /// Return true, if resultset has rows.
371  virtual bool HasRows() = 0;
372 
373  /// Purge results.
374  ///
375  /// Calls fetch for every resultset received until
376  /// finished.
377  virtual void PurgeResults() = 0;
378 
379  /// Cancel statement.
380  ///
381  /// Rolls back current transaction.
382  virtual void Cancel() = 0;
383 
384  /// Close statement.
385  virtual void Close() = 0;
386 
387  /// Sends one or more SQL statements to the SQL server
388  ///
389  /// @param sql
390  /// SQL statement to execute.
391  virtual void SendSql(const string& sql) = 0;
392 
393  /// Sends one or more SQL statements to the SQL server (NOTE: replaced by
394  /// the SendSql())
395  ///
396  /// @param sql
397  /// SQL statement to execute.
398  /// @deprecated
399  /// Use SendSql() instead
400  virtual void Execute(const string& sql) = 0;
401 
402  /// Executes SQL statement with no results returned.
403  ///
404  /// All resultsets are discarded.
405  /// @param sql
406  /// SQL statement to execute.
407  virtual void ExecuteUpdate(const string& sql) = 0;
408 
409  /// Exectues SQL statement and returns the first resultset.
410  ///
411  /// If there is more than one resultset, the rest remain
412  /// pending unless either PurgeResults() is called or next statement
413  /// is run or the statement is closed.
414  /// NOTE: Provided only for queries containing a single sql statement returning rows.
415  /// @param sql
416  /// SQL statement to execute.
417  /// @return
418  /// Pointer to result set. Ownership of IResultSet* belongs to IStatement.
419  /// It is not allowed to use unique_ptr<> or other smart pointers to manage
420  /// life-time of IResultSet*.
421  virtual IResultSet* ExecuteQuery(const string& sql) = 0;
422 
423  /// Executes the last command (with changed parameters, if any).
424  virtual void ExecuteLast() = 0;
425 
426  /// Set input/output parameter.
427  ///
428  /// @param v
429  /// Parameter value.
430  /// @param name
431  /// Parameter name.
432  virtual void SetParam(const CVariant& v,
433  const CDBParamVariant& param) = 0;
434 
435  /// Clear parameter list.
436  virtual void ClearParamList() = 0;
437 
438  /// Get total of rows returned.
439  ///
440  /// Valid only after all rows are retrieved from a resultset.
441  /// Even then, can be -1 if the server didn't indicate a count.
442  virtual int GetRowCount() = 0;
443 
444  /// Get a writer for writing BLOBs using previously created
445  /// CDB_BlobDescriptor
446  /// @param d
447  /// Descriptor
448  /// @param blob_size
449  /// Size of BLOB to write
450  /// @param flags
451  /// @see EBlobOStreamFlags.
453  size_t blob_size,
454  TBlobOStreamFlags flags = 0) = 0;
455 
456  virtual IWriter* GetBlobWriter(I_BlobDescriptor &d,
457  size_t blob_size,
458  EAllowLog log_it);
459 
460  /// Get an ostream for writing BLOBs using previously created
461  /// CDB_BlobDescriptor
462  /// @param d
463  /// Descriptor
464  /// @param blob_size
465  /// Size of BLOB to write
466  /// @param flags
467  /// @see EBlobOStreamFlags.
468  /// @param buf_size
469  /// Buffer size, default 4096
471  size_t blob_size,
473  size_t buf_size = 0) = 0;
474 
475  virtual CNcbiOstream& GetBlobOStream(I_BlobDescriptor &d,
476  size_t blob_size,
477  EAllowLog log_it,
478  size_t buf_size = 0);
479 
480  /// Get the parent connection.
481  ///
482  /// If the original connections was cloned, returns cloned
483  /// connection.
484  virtual class IConnection* GetParentConn() = 0;
485 
486  /// Set auto-clear input parameter flag
487  ///
488  /// @param flag
489  /// auto-clear input parameter flag
490  /// In case when flag == true implicitly clear a statement's parameter list
491  /// after each Execute, ExecuteUpdate and ExecuteQuery call. Default value
492  //. is true.
493  virtual void SetAutoClearInParams(bool flag = true) = 0;
494 
495  /// Get auto-clear input parameter flag value
496  ///
497  /// @return
498  /// auto-clear input parameter flag value
499  virtual bool IsAutoClearInParams(void) const = 0;
500 
501  /// Get input parameters metadata.
502  ///
503  /// @return
504  /// Pointer to result metadata.
505  virtual const IResultSetMetaData& GetParamsMetaData(void) = 0;
506 };
507 
508 
509 /////////////////////////////////////////////////////////////////////////////
510 ///
511 /// ICallableStatement --
512 ///
513 /// Used for calling a stored procedure thru RPC call
514 
516 {
517 public:
518  /// Destructor.
519  virtual ~ICallableStatement();
520 
521  /// Execute stored procedure.
522  virtual void Execute() = 0;
523 
524  /// Executes stored procedure no results returned.
525  ///
526  /// NOTE: All resultsets are discarded.
527  virtual void ExecuteUpdate() = 0;
528 
529  /// Get return status from the stored procedure.
530  virtual int GetReturnStatus() = 0;
531 
532  /// Set input parameters.
533  ///
534  /// @param v
535  /// Parameter value.
536  /// @param name
537  /// Parameter name.
538  virtual void SetParam(const CVariant& v,
539  const CDBParamVariant& param) = 0;
540 
541  /// Set output parameter, which will be returned as resultset.
542  ///
543  /// NOTE: Use CVariant(EDB_Type type) constructor or
544  /// factory method CVariant::<type>(0) to create empty object
545  /// of a particular type.
546  /// @param v
547  /// Parameter value.
548  /// @param name
549  /// Parameter name.
550  virtual void SetOutputParam(const CVariant& v,
551  const CDBParamVariant& param) = 0;
552 
553 protected:
554  // Mask unused methods
555  virtual void SendSql(const string& /*sql*/);
556  virtual void Execute(const string& /*sql*/);
557  virtual void ExecuteUpdate(const string& /*sql*/);
558  virtual IResultSet* ExecuteQuery(const string& /*sql*/);
559 
560 };
561 
562 
563 /////////////////////////////////////////////////////////////////////////////
564 ///
565 /// ICursor --
566 ///
567 /// Interface for a cursor.
568 
570 {
571 public:
572  /// Destructor.
573  virtual ~ICursor();
574 
575  /// Set input parameter.
576  ///
577  /// @param v
578  /// Parameter value.
579  /// @param name
580  /// Parameter name.
581  virtual void SetParam(const CVariant& v,
582  const CDBParamVariant& param) = 0;
583 
584  /// Open cursor and get corresponding resultset.
585  virtual IResultSet* Open() = 0;
586 
587  /// Get output stream for BLOB updates, requires BLOB column number.
588  ///
589  /// @param col
590  /// Column number.
591  /// @param blob_size
592  /// blob_size is the size of the BLOB to be written.
593  /// @param flags
594  /// @see EBlobOStreamFlags.
595  /// @param buf_size
596  /// The size of internal buffer, default 4096.
597  /// @deprecated
598  /// Please use IStatement::GetBlobOStream instead.
599  /// @sa
600  /// IStatement::GetBlobOStream
602  virtual CNcbiOstream& GetBlobOStream(unsigned int col,
603  size_t blob_size,
605  size_t buf_size = 0) = 0;
606 
608  virtual CNcbiOstream& GetBlobOStream(unsigned int col,
609  size_t blob_size,
610  EAllowLog log_it,
611  size_t buf_size = 0);
612 
613  /// Get Blob Writer
614  ///
615  /// Implementation of IWriter interface
616  /// @param col
617  /// Column number.
618  /// @param blob_size
619  /// blob_size is the size of the BLOB to be written.
620  /// @param flags
621  /// @see EBlobOStreamFlags.
622  /// @deprecated
623  /// Please use IStatement::GetBlobWriter instead.
624  /// @sa
625  /// IStatement::GetBlobWriter
627  virtual IWriter* GetBlobWriter(unsigned int col,
628  size_t blob_size,
629  TBlobOStreamFlags flags = 0) = 0;
630 
632  virtual IWriter* GetBlobWriter(unsigned int col,
633  size_t blob_size,
634  EAllowLog log_it);
635 
636  /// Update statement for cursor.
637  ///
638  /// @param table
639  /// table name.
640  /// @param updateSql
641  /// SQL statement.
642  virtual void Update(const string& table, const string& updateSql) = 0;
643 
644  /// Delete statement for cursor.
645  ///
646  /// @param table
647  /// table name.
648  virtual void Delete(const string& table) = 0;
649 
650  /// Cancel cursor
651  virtual void Cancel() = 0;
652 
653  /// Close cursor
654  virtual void Close() = 0;
655 
656  /// Get the parent connection
657  ///
658  /// NOTE: If the original connections was cloned, returns cloned
659  /// connection.
660  virtual class IConnection* GetParentConn() = 0;
661 };
662 
663 
664 /////////////////////////////////////////////////////////////////////////////
665 ///
666 /// IBulkInsert --
667 ///
668 /// Interface for bulk insert
669 
671 {
672 public:
673  /// Destructor.
674  virtual ~IBulkInsert();
675 
676  /// Set hints by one call. Resets everything that was set by Add*Hint().
677  virtual void SetHints(CTempString hints) = 0;
678 
679  /// Type of hint that can be set.
680  enum EHints {
686  eFireTriggers
687  };
688 
689  /// Add hint with value.
690  /// Can be used with any hint type except eOrder and with e..PerBatch
691  /// value should be non-zero.
692  /// Resets everything that was set by SetHints().
693  virtual void AddHint(EHints hint, unsigned int value = 0) = 0;
694 
695  /// Add "ORDER" hint.
696  /// Resets everything that was set by SetHints().
697  virtual void AddOrderHint(CTempString columns) = 0;
698 
699  /// Bind column.
700  ///
701  /// @param col
702  /// Column number.
703  /// @param v
704  /// Variant value.
705  virtual void Bind(const CDBParamVariant& param, CVariant* v) = 0;
706 
707  /// Add row to the batch
708  virtual void AddRow() = 0;
709 
710  /// Store batch of rows
711  virtual void StoreBatch() = 0;
712 
713  /// Cancel bulk insert
714  virtual void Cancel() = 0;
715 
716  /// Complete batch
717  virtual void Complete() = 0;
718 
719  /// Close
720  virtual void Close() = 0;
721 };
722 
723 
724 
725 /////////////////////////////////////////////////////////////////////////////
726 ///
727 /// IConnection::
728 ///
729 /// Interface for a database connection.
730 
731 class IConnValidator;
732 
734 {
735 public:
736  /// Which connection mode.
737  enum EConnMode {
738  /// Bulk insert mode.
739  /// This value is not needed anymore because BCP mode is enabled
740  /// all the time in all drivers now.
741  eBulkInsert = I_DriverContext::fBcpIn,
742  /// Encrypted password mode.
743  ePasswordEncrypted = I_DriverContext::fPasswordEncrypted
744  };
745 
746  /// Destructor.
747  virtual ~IConnection();
748 
749  // Connection modes
750 
751  /// Set connection mode.
752  ///
753  /// @param mode
754  /// Mode to set to.
755  virtual void SetMode(EConnMode mode) = 0;
756 
757  /// Reset connection mode.
758  ///
759  /// @param mode
760  /// Mode to reset to.
761  virtual void ResetMode(EConnMode mode) = 0;
762 
763  /// Get mode mask.
764  virtual unsigned int GetModeMask() = 0;
765 
766  /// Force single connection mode, default false
767  ///
768  /// Disable this mode before using BLOB output streams
769  /// from IResultSet, because extra connection is needed
770  /// in this case.
771  virtual void ForceSingle(bool enable) = 0;
772 
773  /// Get parent datasource object.
774  virtual IDataSource* GetDataSource() = 0;
775 
776  /// Connect to a database.
777  ///
778  /// @param user
779  /// User name.
780  /// @param password
781  /// User's password.
782  /// @param server
783  /// Server to connect to.
784  /// @param database
785  /// Database to connect to.
786  virtual void Connect(const string& user,
787  const string& password,
788  const string& server,
789  const string& database = kEmptyStr) = 0;
790 
791  /// Connect to a database.
792  ///
793  /// @param params
794  /// Connection parameters. Parameters should include all necessary
795  /// settings because all info set via SetMode() or ResetMode() will
796  /// be ignored.
797  virtual void Connect(const CDBConnParams& params) = 0;
798 
799  /// Connect to a database using connect validator
800  ///
801  /// @param validator
802  /// Validator implementation class.
803  /// @param user
804  /// User name.
805  /// @param password
806  /// User's password.
807  /// @param server
808  /// Server to connect to.
809  /// @param database
810  /// Database to connect to.
811  virtual void ConnectValidated(IConnValidator& validator,
812  const string& user,
813  const string& password,
814  const string& server,
815  const string& database = kEmptyStr) = 0;
816 
817  /// Clone existing connection. All settings are copied except
818  /// message handlers
819  /// Set ownership to eTakeOwnership to prevent deleting
820  /// connection upon deleting parent object
822 
823  /// Set current database.
824  ///
825  /// @param name
826  /// Name of database to set to.
827  virtual void SetDatabase(const string& name) = 0;
828 
829  /// Get current database
830  virtual string GetDatabase() = 0;
831 
832  /// Check if the connection is alive
833  virtual bool IsAlive() = 0;
834 
835  // NEW INTERFACE: no additional connections created
836  // while using the next four methods.
837  // Objects obtained with these methods can't be used
838  // simultaneously (like opening cursor while a stored
839  // procedure is running on the same connection).
840 
841  /// Get statement object for regular SQL queries.
842  virtual IStatement* GetStatement() = 0;
843 
844  /// Get callable statement object for stored procedures.
845  ///
846  /// @param proc
847  /// Stored procedure name.
848  /// @param nofArgs
849  /// Number of arguments.
850  virtual ICallableStatement* GetCallableStatement(const string& proc) = 0;
853  {
854  return GetCallableStatement(proc);
855  }
856 
857  /// Get cursor object.
858  virtual ICursor* GetCursor(const string& name,
859  const string& sql,
860  int batchSize) = 0;
861  ICursor* GetCursor(const string& name,
862  const string& sql)
863  {
864  return GetCursor(name, sql, 1);
865  }
867  ICursor* GetCursor(const string& name,
868  const string& sql,
869  int,
870  int batchSize)
871  {
872  return GetCursor(name, sql, batchSize);
873  }
874 
875  /// Create bulk insert object.
876  ///
877  /// @param table_name
878  /// table name.
879  /// @param nof_cols
880  /// Number of columns.
881  virtual IBulkInsert* GetBulkInsert(const string& table_name) = 0;
883  IBulkInsert* GetBulkInsert(const string& table_name, unsigned int)
884  {
885  return GetBulkInsert(table_name);
886  }
887 
888  // END OF NEW INTERFACE
889 
890  /// Get statement object for regular SQL queries.
891  virtual IStatement* CreateStatement() = 0;
892 
893  /// Get callable statement object for stored procedures.
894  virtual ICallableStatement* PrepareCall(const string& proc) = 0;
896  ICallableStatement* PrepareCall(const string& proc, int)
897  {
898  return PrepareCall(proc);
899  }
900 
901  /// Get cursor object.
902  virtual ICursor* CreateCursor(const string& name,
903  const string& sql,
904  int batchSize) = 0;
905  ICursor* CreateCursor(const string& name,
906  const string& sql)
907  {
908  return CreateCursor(name, sql, 1);
909  }
911  ICursor* CreateCursor(const string& name,
912  const string& sql,
913  int,
914  int batchSize)
915  {
916  return CreateCursor(name, sql, batchSize);
917  }
918 
919  /// Create bulk insert object.
920  virtual IBulkInsert* CreateBulkInsert(const string& table_name) = 0;
922  IBulkInsert* CreateBulkInsert(const string& table_name, unsigned int)
923  {
924  return CreateBulkInsert(table_name);
925  }
926 
927  /// Close connecti
928  virtual void Close() = 0;
929 
930  /// Abort connection.
931  virtual void Abort() = 0;
932 
933  /// Set connection timeout.
934  /// NOTE: if "nof_secs" is zero or is "too big" (depends on the underlying
935  /// DB API), then set the timeout to infinite.
936  virtual void SetTimeout(size_t nof_secs) = 0;
937 
938  /// Set timeout for command cancellation and connection closing
939  virtual void SetCancelTimeout(size_t /*nof_secs*/) {}
940 
941  /// Get connection timeout.
942  virtual size_t GetTimeout(void) const;
943 
944  /// Get timeout for command cancellation and connection closing
945  virtual size_t GetCancelTimeout(void) const;
946 
947  /// If enabled, redirects all error messages
948  /// to CDB_MultiEx object (see below).
949  virtual void MsgToEx(bool v) = 0;
950 
951  /// Returns all error messages as a CDB_MultiEx object.
952  virtual CDB_MultiEx* GetErrorAsEx() = 0;
953 
954  /// Returns all error messages as a single string
955  virtual string GetErrorInfo() = 0;
956 
957  virtual const CVersionInfo& GetVersionInfo() const = 0;
958 
959  /// Returns the internal driver connection object
961 };
962 
963 
964 /////////////////////////////////////////////////////////////////////////////
965 ///
966 /// IDataSource --
967 ///
968 /// Interface for a datasource
969 
971 {
972  friend class CDriverManager;
973 
974 protected:
975  /// Protected Destructor.
976  ///
977  /// Prohibits explicit deletion.
978  /// Use CDriverManager::DestroyDs() call, instead.
979  virtual ~IDataSource();
980 
981 public:
982  // Get connection
983  // Set ownership to eTakeOwnership to prevent deleting
984  // connection upon deleting parent object
986 
987  /// Set login timeout.
988  virtual void SetLoginTimeout(unsigned int i) = 0;
989 
990  /// Set the output stream for server messages.
991  ///
992  /// Set it to zero to disable any output and collect
993  /// messages in CDB_MultiEx (see below).
994  /// @param out
995  /// Output stream to set to.
996  virtual void SetLogStream(ostream* out) = 0;
997 
998  /// Returns all server messages as a CDB_MultiEx object.
999  virtual CDB_MultiEx* GetErrorAsEx() = 0;
1000 
1001  /// Returns all server messages as a single string.
1002  virtual string GetErrorInfo() = 0;
1003 
1004  /// Returns the pointer to the general driver interface.
1006  virtual const I_DriverContext* GetDriverContext() const = 0;
1007 
1008  // app_name defines the application name that a connection will use when
1009  // connecting to a server.
1010  void SetApplicationName(const string& app_name);
1011  string GetApplicationName(void) const;
1012 };
1013 
1014 
1015 ////////////////////////////////////////////////////////////////////////////////
1016 inline
1018 {
1019  return DBAPI_MakeTrans(*connection.GetCDB_Connection());
1020 }
1021 
1022 
1024 
1025 
1026 /* @} */
1027 
1028 #endif /* DBAPI___DBAPI__HPP */
Helper class to allow safe initialization from higher-layer objects.
Definition: public.hpp:1058
CDBConnParams::
Definition: interfaces.hpp:258
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CVariant –.
Definition: variant.hpp:99
CVersionInfo –.
IBulkInsert –.
Definition: dbapi.hpp:671
ICallableStatement –.
Definition: dbapi.hpp:516
ICursor –.
Definition: dbapi.hpp:570
IDataSource –.
Definition: dbapi.hpp:971
A very basic data-read interface.
IResultSetMetaData –.
Definition: dbapi.hpp:114
A very basic data-write interface.
I_BlobDescriptor::
Definition: interfaces.hpp:369
Abstract reader-writer interface classes.
static uch flags
std::ofstream out("events_result.xml")
main entry point for tests
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
static const char table_name[]
Definition: bcp.c:249
static char sql[1024]
Definition: putdata.c:19
static const char * proc
Definition: stats.c:21
static const column_t columns[]
Definition: utf8_2.c:22
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
virtual IWriter * GetBlobWriter(unsigned int col, size_t blob_size, TBlobOStreamFlags flags=0)=0
Get Blob Writer.
virtual bool Next()=0
Get next row.
EConnMode
Which connection mode.
Definition: dbapi.hpp:737
virtual const I_DriverContext * GetDriverContext() const =0
virtual string GetName(const CDBParamVariant &param) const =0
Get name of column.
virtual size_t Read(void *buf, size_t size)=0
Read unformatted data.
ICursor * GetCursor(const string &name, const string &sql)
Definition: dbapi.hpp:861
virtual void Complete()=0
Complete batch.
virtual IResultSet * GetResultSet()=0
Get resulset.
virtual const CVersionInfo & GetVersionInfo() const =0
virtual bool Failed()=0
Check if the statement failed.
IBulkInsert * GetBulkInsert(const string &table_name, unsigned int)
Definition: dbapi.hpp:883
virtual void SetTimeout(size_t nof_secs)=0
Set connection timeout.
IBulkInsert * CreateBulkInsert(const string &table_name, unsigned int)
Definition: dbapi.hpp:922
virtual void DisableBind(bool b)=0
Disables column binding.
virtual void SetParam(const CVariant &v, const CDBParamVariant &param)=0
Set input/output parameter.
virtual CNcbiOstream & GetBlobOStream(unsigned int col, size_t blob_size, TBlobOStreamFlags flags=0, size_t buf_size=0)=0
Get output stream for BLOB updates, requires BLOB column number.
virtual void Close()=0
Close resultset.
virtual bool HasMoreResults()=0
Check for more results available.
virtual IDataSource * GetDataSource()=0
Get parent datasource object.
virtual void Execute()=0
Execute stored procedure.
virtual void Close()=0
Close connecti.
virtual IResultSet * ExecuteQuery(const string &sql)=0
Exectues SQL statement and returns the first resultset.
virtual void Execute(const string &sql)=0
Sends one or more SQL statements to the SQL server (NOTE: replaced by the SendSql())
virtual IStatement * CreateStatement()=0
Get statement object for regular SQL queries.
ICursor * CreateCursor(const string &name, const string &sql, int, int batchSize)
Definition: dbapi.hpp:911
virtual const IResultSetMetaData * GetMetaData(EOwnership ownership=eNoOwnership)=0
Get Metadata.
virtual void Close()=0
Close.
virtual void SetOutputParam(const CVariant &v, const CDBParamVariant &param)=0
Set output parameter, which will be returned as resultset.
virtual void ConnectValidated(IConnValidator &validator, const string &user, const string &password, const string &server, const string &database=kEmptyStr)=0
Connect to a database using connect validator.
virtual const IResultSetMetaData & GetParamsMetaData(void)=0
Get input parameters metadata.
virtual void MsgToEx(bool v)=0
If enabled, redirects all error messages to CDB_MultiEx object (see below).
virtual CDB_Connection * GetCDB_Connection()=0
Returns the internal driver connection object.
virtual IBulkInsert * CreateBulkInsert(const string &table_name)=0
Create bulk insert object.
virtual void AddOrderHint(CTempString columns)=0
Add "ORDER" hint.
virtual void StoreBatch()=0
Store batch of rows.
virtual EDB_ResType GetResultType()=0
Get result type.
virtual CNcbiIstream & GetBlobIStream(size_t buf_size=0)=0
Get Blob input stream.
virtual void SetMode(EConnMode mode)=0
Set connection mode.
virtual ICallableStatement * PrepareCall(const string &proc)=0
Get callable statement object for stored procedures.
virtual string GetErrorInfo()=0
Returns all error messages as a single string.
virtual void Update(const string &table, const string &updateSql)=0
Update statement for cursor.
virtual void ResetMode(EConnMode mode)=0
Reset connection mode.
virtual int GetRowCount()=0
Get total of rows returned.
virtual CNcbiOstream & GetBlobOStream(I_BlobDescriptor &d, size_t blob_size, TBlobOStreamFlags flags=0, size_t buf_size=0)=0
Get an ostream for writing BLOBs using previously created CDB_BlobDescriptor.
virtual EDB_Type GetType(const CDBParamVariant &param) const =0
Get data type for column in the resultset.
virtual bool IsAlive()=0
Check if the connection is alive.
virtual void Abort()=0
Abort connection.
virtual IReader * GetBlobReader()=0
Get a Blob Reader.
virtual IConnection * CloneConnection(EOwnership ownership=eNoOwnership)=0
Clone existing connection.
virtual void SetLoginTimeout(unsigned int i)=0
Set login timeout.
ICallableStatement * GetCallableStatement(const string &proc, int)
Definition: dbapi.hpp:852
virtual CDB_MultiEx * GetErrorAsEx()=0
Returns all server messages as a CDB_MultiEx object.
virtual unsigned int GetTotalColumns(void) const =0
Get total number of columns in resultset.
virtual void Bind(const CDBParamVariant &param, CVariant *v)=0
Bind column.
EHints
Type of hint that can be set.
Definition: dbapi.hpp:680
virtual IStatement * GetStatement()=0
Get statement object for regular SQL queries.
virtual void PurgeResults()=0
Purge results.
virtual void ClearParamList()=0
Clear parameter list.
virtual string GetErrorInfo()=0
Returns all server messages as a single string.
virtual ICursor * GetCursor(const string &name, const string &sql, int batchSize)=0
Get cursor object.
virtual void ForceSingle(bool enable)=0
Force single connection mode, default false.
virtual unsigned int GetTotalColumns()=0
Get total columns.
virtual void BindBlobToVariant(bool b)=0
Bind blob to variant.
ICallableStatement * PrepareCall(const string &proc, int)
Definition: dbapi.hpp:896
virtual const CVariant & GetVariant(const CDBParamVariant &param)=0
Retrieve a CVariant class describing the data stored in a given column.
virtual string GetDatabase()=0
Get current database.
virtual I_DriverContext * GetDriverContext()=0
Returns the pointer to the general driver interface.
ICursor * CreateCursor(const string &name, const string &sql)
Definition: dbapi.hpp:905
virtual ICallableStatement * GetCallableStatement(const string &proc)=0
Get callable statement object for stored procedures.
virtual ICursor * CreateCursor(const string &name, const string &sql, int batchSize)=0
Get cursor object.
virtual int GetMaxSize(const CDBParamVariant &param) const =0
Get maximum size in bytes for column.
virtual void ExecuteUpdate()=0
Executes stored procedure no results returned.
virtual void ExecuteLast()=0
Executes the last command (with changed parameters, if any).
virtual void SetParam(const CVariant &v, const CDBParamVariant &param)=0
Set input parameters.
virtual void SetHints(CTempString hints)=0
Set hints by one call. Resets everything that was set by Add*Hint().
virtual CDB_MultiEx * GetErrorAsEx()=0
Returns all error messages as a CDB_MultiEx object.
virtual void AddHint(EHints hint, unsigned int value=0)=0
Add hint with value.
virtual bool WasNull()=0
Determine if last column was NULL.
virtual void Cancel()=0
Cancel cursor.
ICursor * GetCursor(const string &name, const string &sql, int, int batchSize)
Definition: dbapi.hpp:867
virtual void SetAutoClearInParams(bool flag=true)=0
Set auto-clear input parameter flag.
virtual void Close()=0
Close statement.
virtual class IConnection * GetParentConn()=0
Get the parent connection.
virtual IResultSet * Open()=0
Open cursor and get corresponding resultset.
virtual void Connect(const string &user, const string &password, const string &server, const string &database=kEmptyStr)=0
Connect to a database.
virtual IBulkInsert * GetBulkInsert(const string &table_name)=0
Create bulk insert object.
virtual void Connect(const CDBConnParams &params)=0
Connect to a database.
virtual class IConnection * GetParentConn()=0
Get the parent connection.
virtual void SetDatabase(const string &name)=0
Set current database.
int TBlobOStreamFlags
Definition: dbapi.hpp:103
virtual IWriter * GetBlobWriter(I_BlobDescriptor &d, size_t blob_size, TBlobOStreamFlags flags=0)=0
Get a writer for writing BLOBs using previously created CDB_BlobDescriptor.
virtual int GetReturnStatus()=0
Get return status from the stored procedure.
virtual void Close()=0
Close cursor.
virtual bool HasRows()=0
Check if resultset has rows.
virtual void Delete(const string &table)=0
Delete statement for cursor.
virtual bool IsAutoClearInParams(void) const =0
Get auto-clear input parameter flag value.
virtual void Cancel()=0
Cancel statement.
virtual void AddRow()=0
Add row to the batch.
virtual unsigned int GetModeMask()=0
Get mode mask.
virtual void Cancel()=0
Cancel bulk insert.
virtual void SetParam(const CVariant &v, const CDBParamVariant &param)=0
Set input parameter.
virtual int GetColumnNo()=0
Get column number, currently available for Read()
virtual void SetCancelTimeout(size_t)
Set timeout for command cancellation and connection closing.
Definition: dbapi.hpp:939
EAllowLog
EDataSource –.
Definition: dbapi.hpp:78
virtual CNcbiOstream & GetBlobOStream(IConnection *conn, size_t blob_size, TBlobOStreamFlags flags=0, size_t buf_size=0)=0
Get Blob output stream with explicit additional connection.
virtual CDBParams::EDirection GetDirection(const CDBParamVariant &param) const =0
Get parameter's direction (in/out/inout).
virtual void SetLogStream(ostream *out)=0
Set the output stream for server messages.
CAutoTrans::CSubject DBAPI_MakeTrans(IConnection &connection)
Definition: dbapi.hpp:1017
virtual void ExecuteUpdate(const string &sql)=0
Executes SQL statement with no results returned.
virtual IConnection * CreateConnection(EOwnership ownership=eNoOwnership)=0
virtual CNcbiOstream & GetBlobOStream(size_t blob_size, TBlobOStreamFlags flags=0, size_t buf_size=0)=0
Get Blob output stream.
virtual void SendSql(const string &sql)=0
Sends one or more SQL statements to the SQL server.
EBlobOStreamFlags
EBlobOStreamFlags –.
Definition: dbapi.hpp:90
@ eRowsPerBatch
Definition: dbapi.hpp:682
@ eKilobytesPerBatch
Definition: dbapi.hpp:683
@ eCheckConstraints
Definition: dbapi.hpp:685
@ eDisableLog
Disables log.
Definition: dbapi.hpp:79
@ eEnableLog
Enables log.
Definition: dbapi.hpp:80
@ fBOS_UseTransaction
Use a (sub)transaction (committed once all data's been sent successfully, rolled back for unrecoverab...
Definition: dbapi.hpp:99
@ fBOS_SkipLogging
Definition: dbapi.hpp:100
EDB_ResType
EDB_ResType::
Definition: interfaces.hpp:386
EDB_Type
Definition: types.hpp:52
#define NCBI_DEPRECATED
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
#define kEmptyStr
Definition: ncbistr.hpp:123
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_DBAPI_EXPORT
Definition: ncbi_export.h:432
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
use only n Cassandra database for the lookups</td > n</tr > n< tr > n< td > yes</td > n< td > do not use tables BIOSEQ_INFO and BLOB_PROP in the Cassandra database
char * buf
int i
string Execute(const string &cmmd, const vector< string > &args, const string &data=kEmptyStr)
mdb_mode_t mode
Definition: lmdb++.h:38
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Modified on Wed Sep 04 15:04:28 2024 by modify_doxy.py rev. 669887