Qore Programming Language  0.8.3
include/qore/DBI.h (4311)
Go to the documentation of this file.
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */
00002 /*
00003   DBI.h
00004 
00005   Qore Programming Language
00006 
00007   Copyright 2003 - 2011 David Nichols
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 */
00023 
00024 #ifndef _QORE_DBI_H
00025 #define _QORE_DBI_H
00026 
00028 
00029 // DBI Driver capabilities
00030 #define DBI_CAP_NONE                     0
00031 #define DBI_CAP_TIME_ZONE_SUPPORT        (1 << 0)
00032 #define DBI_CAP_CHARSET_SUPPORT          (1 << 1)
00033 #define DBI_CAP_TRANSACTION_MANAGEMENT   (1 << 2)
00034 #define DBI_CAP_STORED_PROCEDURES        (1 << 3)
00035 #define DBI_CAP_LOB_SUPPORT              (1 << 4)
00036 #define DBI_CAP_BIND_BY_VALUE            (1 << 5)
00037 #define DBI_CAP_BIND_BY_PLACEHOLDER      (1 << 6)
00038 #define DBI_CAP_HAS_EXECRAW              (1 << 7)
00039 #define DBI_CAP_HAS_STATEMENT            (1 << 8)
00040 #define DBI_CAP_HAS_SELECT_ROW           (1 << 9)
00041 
00042 #define BN_PLACEHOLDER  0
00043 #define BN_VALUE        1
00044 
00045 #define DBI_DEFAULT_STR_LEN 512
00046 
00047 // DBI method codes
00048 #define QDBI_METHOD_OPEN                      1
00049 #define QDBI_METHOD_CLOSE                     2
00050 #define QDBI_METHOD_SELECT                    3
00051 #define QDBI_METHOD_SELECT_ROWS               4
00052 #define QDBI_METHOD_EXEC                      5
00053 #define QDBI_METHOD_COMMIT                    6
00054 #define QDBI_METHOD_ROLLBACK                  7
00055 #define QDBI_METHOD_BEGIN_TRANSACTION         8
00056 #define QDBI_METHOD_ABORT_TRANSACTION_START   9
00057 #define QDBI_METHOD_GET_SERVER_VERSION       10
00058 #define QDBI_METHOD_GET_CLIENT_VERSION       11
00059 #define QDBI_METHOD_EXECRAW                  12
00060 #define QDBI_METHOD_STMT_PREPARE             13
00061 #define QDBI_METHOD_STMT_PREPARE_RAW         14
00062 #define QDBI_METHOD_STMT_BIND                15
00063 #define QDBI_METHOD_STMT_BIND_PLACEHOLDERS   16
00064 #define QDBI_METHOD_STMT_BIND_VALUES         17
00065 #define QDBI_METHOD_STMT_EXEC                18
00066 #define QDBI_METHOD_STMT_FETCH_ROW           19
00067 #define QDBI_METHOD_STMT_FETCH_ROWS          20
00068 #define QDBI_METHOD_STMT_FETCH_COLUMNS       21
00069 #define QDBI_METHOD_STMT_NEXT                22
00070 #define QDBI_METHOD_STMT_CLOSE               23
00071 #define QDBI_METHOD_STMT_AFFECTED_ROWS       24
00072 #define QDBI_METHOD_STMT_GET_OUTPUT          25
00073 #define QDBI_METHOD_STMT_GET_OUTPUT_ROWS     26
00074 #define QDBI_METHOD_STMT_DEFINE              27
00075 #define QDBI_METHOD_SELECT_ROW               28
00076 
00077 #define QDBI_VALID_CODES 28
00078 
00079 class Datasource;
00080 class ExceptionSink;
00081 class QoreString;
00082 class QoreListNode;
00083 class AbstractQoreNode;
00084 class QoreHashNode;
00085 class QoreNamespace;
00086 class SQLStatement;
00087 
00088 // DBI method signatures - note that only get_client_version uses a "const Datasource" 
00089 // the others do not so that automatic reconnects can be supported (which will normally
00090 // require writing to the Datasource)
00091 
00093 
00097 typedef int (*q_dbi_open_t)(Datasource *ds, ExceptionSink *xsink);
00098 
00100 
00104 typedef int (*q_dbi_close_t)(Datasource *ds);
00105 
00107 
00114 typedef AbstractQoreNode *(*q_dbi_select_t)(Datasource *ds, const QoreString *str, const QoreListNode *args, ExceptionSink *xsink);
00115 
00117 
00124 typedef AbstractQoreNode *(*q_dbi_select_rows_t)(Datasource *ds, const QoreString *str, const QoreListNode *args, ExceptionSink *xsink);
00125 
00127 
00135 typedef QoreHashNode *(*q_dbi_select_row_t)(Datasource *ds, const QoreString *str, const QoreListNode *args, ExceptionSink *xsink);
00136 
00138 
00145 typedef AbstractQoreNode *(*q_dbi_exec_t)(Datasource *ds, const QoreString *str, const QoreListNode *args, ExceptionSink *xsink);
00146 
00148 
00154 typedef AbstractQoreNode *(*q_dbi_execraw_t)(Datasource *ds, const QoreString *str, ExceptionSink *xsink);
00155 
00157 
00162 typedef int (*q_dbi_commit_t)(Datasource *ds, ExceptionSink *xsink);
00163 
00165 
00170 typedef int (*q_dbi_rollback_t)(Datasource *ds, ExceptionSink *xsink);
00171 
00173 
00178 typedef int (*q_dbi_begin_transaction_t)(Datasource *ds, ExceptionSink *xsink);
00179 
00181 
00186 typedef int (*q_dbi_abort_transaction_start_t)(Datasource *ds, ExceptionSink *xsink);
00187 
00189 
00194 typedef AbstractQoreNode *(*q_dbi_get_server_version_t)(Datasource *ds, ExceptionSink *xsink);
00195 
00197 
00202 typedef AbstractQoreNode *(*q_dbi_get_client_version_t)(const Datasource *ds, ExceptionSink *xsink);
00203 
00204 // FIXME: document
00206 
00208 typedef int (*q_dbi_stmt_prepare_t)(SQLStatement *stmt, const QoreString &str, const QoreListNode *args, ExceptionSink *xsink);
00209 
00211 
00213 typedef int (*q_dbi_stmt_prepare_raw_t)(SQLStatement *stmt, const QoreString &str, ExceptionSink *xsink);
00214 
00216 
00218 typedef int (*q_dbi_stmt_bind_t)(SQLStatement *stmt, const QoreListNode &l, ExceptionSink *xsink);
00219 
00221 
00223 typedef int (*q_dbi_stmt_exec_t)(SQLStatement *stmt, ExceptionSink *xsink);
00224 
00226 
00228 typedef int (*q_dbi_stmt_affected_rows_t)(SQLStatement *stmt, ExceptionSink *xsink);
00229 
00231 
00233 typedef QoreHashNode *(*q_dbi_stmt_get_output_t)(SQLStatement *stmt, ExceptionSink *xsink);
00234 
00236 
00238 typedef QoreHashNode *(*q_dbi_stmt_get_output_rows_t)(SQLStatement *stmt, ExceptionSink *xsink);
00239 
00240 typedef int (*q_dbi_stmt_define_t)(SQLStatement *stmt, ExceptionSink *xsink);
00241 typedef QoreHashNode *(*q_dbi_stmt_fetch_row_t)(SQLStatement *stmt, ExceptionSink *xsink);
00242 typedef QoreHashNode *(*q_dbi_stmt_fetch_columns_t)(SQLStatement *stmt, int rows, ExceptionSink *xsink);
00243 typedef QoreListNode *(*q_dbi_stmt_fetch_rows_t)(SQLStatement *stmt, int rows, ExceptionSink *xsink);
00244 typedef bool (*q_dbi_stmt_next_t)(SQLStatement *stmt, ExceptionSink *xsink);
00245 typedef int (*q_dbi_stmt_close_t)(SQLStatement *stmt, ExceptionSink *xsink);
00246 
00247 typedef std::pair<int, void *> qore_dbi_method_t;
00248 
00249 typedef safe_dslist<qore_dbi_method_t> dbi_method_list_t;
00250 
00252 
00254 class qore_dbi_method_list {
00255 private:
00256    struct qore_dbi_mlist_private *priv; // private implementation
00257 
00258    // not implemented
00259    DLLLOCAL qore_dbi_method_list(const qore_dbi_method_list&);
00260    DLLLOCAL qore_dbi_method_list& operator=(const qore_dbi_method_list&);
00261 
00262 public:
00263    DLLEXPORT qore_dbi_method_list();
00264    DLLEXPORT ~qore_dbi_method_list();
00265 
00266    // covers open, commit, rollback, and begin transaction
00267    DLLEXPORT void add(int code, q_dbi_open_t method);
00268    // for close
00269    DLLEXPORT void add(int code, q_dbi_close_t method);
00270    // covers select, select_rows, and exec
00271    DLLEXPORT void add(int code, q_dbi_select_t method);
00272    // covers select_row
00273    DLLEXPORT void add(int code, q_dbi_select_row_t method);
00274    // covers execRaw
00275    DLLEXPORT void add(int code, q_dbi_execraw_t method);
00276    // covers get_server_version
00277    DLLEXPORT void add(int code, q_dbi_get_server_version_t method);
00278    // covers get_client_version
00279    DLLEXPORT void add(int code, q_dbi_get_client_version_t method);
00280 
00281    // covers prepare
00282    DLLEXPORT void add(int code, q_dbi_stmt_prepare_t method);
00283    // covers prepare_raw
00284    DLLEXPORT void add(int code, q_dbi_stmt_prepare_raw_t method);
00285    // covers bind, bind_placeholders, bind_values
00286    DLLEXPORT void add(int code, q_dbi_stmt_bind_t method);
00287    // covers exec, close, affected_rows, and define
00288    DLLEXPORT void add(int code, q_dbi_stmt_exec_t method);
00289    // covers fetch_row, get_output, and get_output_rows
00290    DLLEXPORT void add(int code, q_dbi_stmt_fetch_row_t method);
00291    // covers fetch_columns
00292    DLLEXPORT void add(int code, q_dbi_stmt_fetch_columns_t method);
00293    // covers fetch_rows
00294    DLLEXPORT void add(int code, q_dbi_stmt_fetch_rows_t method);
00295    // covers next
00296    DLLEXPORT void add(int code, q_dbi_stmt_next_t method);
00297 
00298    // internal interface
00299    DLLLOCAL dbi_method_list_t *getMethods() const;
00300 };
00301 
00303 
00308 class DBIDriver {
00309 private:
00311    struct qore_dbi_private *priv;
00312 
00314    DLLLOCAL DBIDriver(const DBIDriver&);
00316    DLLLOCAL DBIDriver& operator=(const DBIDriver&);
00317 
00318 public:
00320 
00323    DLLEXPORT const char *getName() const;
00324 
00325    DLLLOCAL DBIDriver(const char *name, const dbi_method_list_t &methods, int cps);
00326    DLLLOCAL ~DBIDriver();
00327    DLLLOCAL int init(Datasource *ds, ExceptionSink *xsink) const;
00328    DLLLOCAL int close(Datasource *ds) const;
00329    DLLLOCAL AbstractQoreNode *select(Datasource *ds, const QoreString *sql, const QoreListNode *args, ExceptionSink *xsink) const;
00330    DLLLOCAL AbstractQoreNode *selectRows(Datasource *ds, const QoreString *sql, const QoreListNode *args, ExceptionSink *xsink) const;
00331    DLLLOCAL QoreHashNode *selectRow(Datasource *ds, const QoreString *sql, const QoreListNode *args, ExceptionSink *xsink) const;
00332    DLLLOCAL AbstractQoreNode *execSQL(Datasource *ds, const QoreString *sql, const QoreListNode *args, ExceptionSink *xsink) const;
00333    DLLLOCAL AbstractQoreNode *execRawSQL(Datasource *ds, const QoreString *sql, ExceptionSink *xsink) const;
00334    DLLLOCAL int commit(Datasource *, ExceptionSink *xsink) const;
00335    DLLLOCAL int rollback(Datasource *, ExceptionSink *xsink) const;
00336    DLLLOCAL int autoCommit(Datasource *, ExceptionSink *xsink) const;
00337    DLLLOCAL int beginTransaction(Datasource *, ExceptionSink *xsink) const;
00338    DLLLOCAL int abortTransactionStart(Datasource *, ExceptionSink *xsink) const;
00339    DLLLOCAL AbstractQoreNode *getServerVersion(Datasource *, ExceptionSink *xsink) const;
00340    DLLLOCAL AbstractQoreNode *getClientVersion(const Datasource *, ExceptionSink *xsink) const;
00341 
00342    DLLLOCAL int stmt_prepare(SQLStatement *stmt, const QoreString &str, const QoreListNode *args, ExceptionSink *xsink) const;
00343    DLLLOCAL int stmt_prepare_raw(SQLStatement *stmt, const QoreString &str, ExceptionSink *xsink) const;
00344    DLLLOCAL int stmt_bind(SQLStatement *stmt, const QoreListNode &l, ExceptionSink *xsink) const;
00345    DLLLOCAL int stmt_bind_placeholders(SQLStatement *stmt, const QoreListNode &l, ExceptionSink *xsink) const;
00346    DLLLOCAL int stmt_bind_values(SQLStatement *stmt, const QoreListNode &l, ExceptionSink *xsink) const;
00347    DLLLOCAL int stmt_exec(SQLStatement *stmt, ExceptionSink *xsink) const;
00348    DLLLOCAL int stmt_affected_rows(SQLStatement *stmt, ExceptionSink *xsink) const;
00349    DLLLOCAL int stmt_define(SQLStatement *stmt, ExceptionSink *xsink) const;
00350    DLLLOCAL QoreHashNode *stmt_get_output(SQLStatement *stmt, ExceptionSink *xsink) const;
00351    DLLLOCAL QoreHashNode *stmt_get_output_rows(SQLStatement *stmt, ExceptionSink *xsink) const;
00352 
00353    DLLLOCAL QoreHashNode *stmt_fetch_row(SQLStatement *stmt, ExceptionSink *xsink) const;
00354    DLLLOCAL QoreListNode *stmt_fetch_rows(SQLStatement *stmt, int rows, ExceptionSink *xsink) const;
00355    DLLLOCAL QoreHashNode *stmt_fetch_columns(SQLStatement *stmt, int rows, ExceptionSink *xsink) const;
00356    DLLLOCAL bool stmt_next(SQLStatement *stmt, ExceptionSink *xsink) const;
00357    DLLLOCAL int stmt_close(SQLStatement *stmt, ExceptionSink *xsink) const;
00358 
00359    DLLLOCAL bool hasStatementAPI() const;
00360 
00361    DLLLOCAL int getCaps() const;
00362    DLLLOCAL QoreListNode *getCapList() const;
00363 };
00364 
00365 struct qore_dbi_dlist_private;
00366 
00368 
00372 class DBIDriverList {
00373 private:
00375    struct qore_dbi_dlist_private *priv;
00376 
00377    DLLLOCAL DBIDriver *find_intern(const char *name) const;
00378 
00379 public:
00381 
00387    DLLEXPORT class DBIDriver *registerDriver(const char *name, const struct qore_dbi_method_list &methods, int caps);
00388 
00390 
00395    DLLEXPORT DBIDriver *find(const char *name) const;
00396 
00398 
00404    DLLEXPORT DBIDriver *find(const char *name, ExceptionSink *xsink) const;
00405 
00406    DLLLOCAL DBIDriverList();
00407    DLLLOCAL ~DBIDriverList();
00408    DLLLOCAL QoreListNode *getDriverList() const;
00409 };
00410 
00412 DLLEXPORT extern DBIDriverList DBI;
00413 
00415 DLLEXPORT QoreHashNode *parseDatasource(const char *ds, ExceptionSink *xsink);
00416 
00418 DLLEXPORT void DBI_concat_numeric(QoreString *str, const AbstractQoreNode *v);
00419 
00421 
00424 DLLEXPORT int DBI_concat_string(QoreString *str, const AbstractQoreNode *v, ExceptionSink *xsink);
00425 
00426 DLLLOCAL void init_dbi_functions();
00427 DLLLOCAL QoreNamespace *getSQLNamespace();
00428 
00429 #endif  // _QORE_DBI_H
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines