Qore Programming Language  0.8.11.1
Datasource.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  Datasource.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2014 David Nichols
8 
9  The Datasource class provides the low-level interface to Qore DBI drivers.
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_DATASOURCE_H
35 
36 #define _QORE_DATASOURCE_H
37 
38 #include <qore/QoreThreadLock.h>
39 #include <qore/QoreQueue.h>
40 
41 #include <string>
42 
43 class DBIDriver;
44 
46 
54 class Datasource {
55  friend class QoreSQLStatement;
56 
57 private:
58  struct qore_ds_private *priv; // private implementation
59 
61  DLLLOCAL Datasource& operator=(const Datasource&);
62 
64  DLLLOCAL AbstractQoreNode* exec_internal(bool doBind, const QoreString* query_str, const QoreListNode* args, ExceptionSink* xsink);
65 
66 protected:
68  DLLEXPORT void freeConnectionValues();
69 
71  DLLEXPORT void setConnectionValues();
72 
74  DLLEXPORT void setTransactionStatus(bool);
75 
77  DLLEXPORT void setPendingConnectionValues(const Datasource* other);
78 
80  DLLEXPORT int beginImplicitTransaction(ExceptionSink* xsink);
81 
82 public:
84 
87  DLLEXPORT Datasource(DBIDriver* driver);
88 
90  DLLEXPORT Datasource(const Datasource& old);
91 
93  DLLEXPORT virtual ~Datasource();
94 
96 
99  DLLEXPORT bool getAutoCommit() const;
100 
102 
105  DLLEXPORT const char* getUsername() const;
106 
108 
111  DLLEXPORT const char* getPassword() const;
112 
114 
117  DLLEXPORT const char* getDBName() const;
118 
120 
123  DLLEXPORT const char* getDBEncoding() const;
124 
126 
129  DLLEXPORT const char* getOSEncoding() const;
130 
132 
135  DLLEXPORT const char* getHostName() const;
136 
138 
141  DLLEXPORT int getPort() const;
142 
144 
147  DLLEXPORT const std::string& getUsernameStr() const;
148 
150 
153  DLLEXPORT const std::string& getPasswordStr() const;
154 
156 
159  DLLEXPORT const std::string& getDBNameStr() const;
160 
162 
165  DLLEXPORT const std::string& getDBEncodingStr() const;
166 
168 
171  DLLEXPORT const std::string& getHostNameStr() const;
172 
174  DLLEXPORT void* getPrivateData() const;
175 
177  template <typename T>
178  DLLLOCAL T* getPrivateData() const {
179  return reinterpret_cast<T*>(getPrivateData());
180  }
181 
183  template <typename T>
184  DLLLOCAL T& getPrivateDataRef() const {
185  return *getPrivateData<T>();
186  }
187 
189 
192  DLLEXPORT void setPrivateData(void* data);
193 
195 
198  DLLEXPORT void setDBEncoding(const char* name);
199 
201  DLLEXPORT const QoreEncoding* getQoreEncoding() const;
202 
204 
207  DLLEXPORT void setQoreEncoding(const QoreEncoding* enc);
208 
210 
213  DLLEXPORT void setQoreEncoding(const char* name);
214 
216 
219  DLLEXPORT void setPendingUsername(const char* u);
220 
222 
225  DLLEXPORT void setPendingPassword(const char* p);
226 
228 
231  DLLEXPORT void setPendingDBName(const char* d);
232 
234 
237  DLLEXPORT void setPendingDBEncoding(const char* c);
238 
240 
243  DLLEXPORT void setPendingHostName(const char* h);
244 
246 
249  DLLEXPORT void setPendingPort(int port);
250 
251  DLLEXPORT void setAutoCommit(bool ac);
252 
254 
257  DLLEXPORT int open(ExceptionSink* xsink);
258 
260 
265  DLLEXPORT AbstractQoreNode* select(const QoreString* query_str, const QoreListNode* args, ExceptionSink* xsink);
266 
268 
273  DLLEXPORT AbstractQoreNode* selectRows(const QoreString* query_str, const QoreListNode* args, ExceptionSink* xsink);
274 
276 
283  DLLEXPORT QoreHashNode* selectRow(const QoreString* query_str, const QoreListNode* args, ExceptionSink* xsink);
284 
286 
293  DLLEXPORT AbstractQoreNode* exec(const QoreString* query_str, const QoreListNode* args, ExceptionSink* xsink);
294 
296 
306  DLLEXPORT AbstractQoreNode* execRaw(const QoreString* query_str, const QoreListNode* args, ExceptionSink* xsink);
307 
309 
316  DLLEXPORT AbstractQoreNode* execRaw(const QoreString* query_str, ExceptionSink* xsink);
317 
319 
326  DLLEXPORT QoreHashNode* describe(const QoreString* query_str, const QoreListNode* args, ExceptionSink* xsink);
327 
329 
333  DLLEXPORT int commit(ExceptionSink* xsink);
334 
336 
340  DLLEXPORT int rollback(ExceptionSink* xsink);
341 
343  DLLEXPORT int close();
344 
346  DLLEXPORT void reset(ExceptionSink* xsink);
347 
349 
352  DLLEXPORT QoreListNode* getCapabilityList() const;
353 
355 
358  DLLEXPORT int getCapabilities() const;
359 
361 
363  DLLEXPORT QoreStringNode* getPendingUsername() const;
364 
366 
368  DLLEXPORT QoreStringNode* getPendingPassword() const;
369 
371 
373  DLLEXPORT QoreStringNode* getPendingDBName() const;
374 
376 
378  DLLEXPORT QoreStringNode* getPendingDBEncoding() const;
379 
381 
384  DLLEXPORT QoreStringNode* getPendingHostName() const;
385 
387 
390  DLLEXPORT int getPendingPort() const;
391 
397  DLLEXPORT int beginTransaction(ExceptionSink* xsink);
398 
400 
403  DLLEXPORT bool isInTransaction() const;
404 
406 
410  DLLEXPORT bool activeTransaction() const;
411 
413 
416  DLLEXPORT bool isOpen() const;
417 
419 
422  DLLEXPORT Datasource* copy() const;
423 
425  DLLEXPORT const char* getDriverName() const;
426 
428 
433 
435 
438  DLLEXPORT AbstractQoreNode* getClientVersion(ExceptionSink* xsink) const;
439 
441 
444  DLLEXPORT const DBIDriver* getDriver() const;
445 
447 
451  DLLEXPORT void connectionAborted();
452 
454 
456  DLLEXPORT bool wasConnectionAborted() const;
457 
459 
463  DLLEXPORT int autoCommit(ExceptionSink* xsink);
464 
466 
473  DLLEXPORT QoreHashNode* getOptionHash() const;
474 
476 
482  DLLEXPORT QoreHashNode* getCurrentOptionHash() const;
483 
485 
491  DLLEXPORT int setOption(const char* opt, const AbstractQoreNode* val, ExceptionSink* xsink);
492 
494 
501  DLLEXPORT AbstractQoreNode* getOption(const char* opt, ExceptionSink* xsink);
502 
504 
512  DLLEXPORT const QoreHashNode* getConnectOptions() const;
513 
515 
518  DLLEXPORT QoreHashNode* getConfigHash() const;
519 
521 
524  DLLEXPORT QoreStringNode* getConfigString() const;
525 
527 
530  DLLEXPORT void setEventQueue(Queue* q, AbstractQoreNode* arg, ExceptionSink* xsink);
531 
533 
535  DLLEXPORT QoreHashNode* getEventQueueHash(Queue*& q, int event_code) const;
536 };
537 
538 #endif // _QORE_DATASOURCE_H
539 
DLLEXPORT bool getAutoCommit() const
returns the autocommit status
DLLEXPORT void setPendingConnectionValues(const Datasource *other)
copies all pending connection values to another Datasource
DLLEXPORT const DBIDriver * getDriver() const
returns the DBIDriver pointer used for this object
DLLLOCAL T * getPrivateData() const
returns the private DBI-specific data structure for this object
Definition: Datasource.h:178
DLLEXPORT QoreHashNode * getEventQueueHash(Queue *&q, int event_code) const
returns an event hash with only default information in it or 0 if no event queue is set ...
DLLEXPORT QoreStringNode * getPendingUsername() const
returns the pending username for the next connection
DLLEXPORT AbstractQoreNode * getOption(const char *opt, ExceptionSink *xsink)
Returns the current value for the given option.
DLLEXPORT QoreStringNode * getPendingDBName() const
returns the pending database (or schema) name for the next connection
DLLEXPORT void setPendingPort(int port)
sets the port number to be used for the next connection
DLLEXPORT const std::string & getPasswordStr() const
returns the password used for the last connection
defines string encoding functions in Qore
Definition: QoreEncoding.h:80
DLLEXPORT const QoreHashNode * getConnectOptions() const
returns the valid options for this driver with descriptions and current values for the current dataso...
DLLEXPORT bool wasConnectionAborted() const
returns the connection aborted status
DLLEXPORT int commit(ExceptionSink *xsink)
commits the current transaction to the database
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:49
virtual DLLEXPORT ~Datasource()
the Datasource is closed if it's still open and the object is destroyed
DLLEXPORT void setPendingUsername(const char *u)
sets the username to be used for the next connection
DLLEXPORT int autoCommit(ExceptionSink *xsink)
called from subclasses when releasing the transaction lock
DLLEXPORT void freeConnectionValues()
frees all connection values
DLLEXPORT const std::string & getUsernameStr() const
returns the username used for the last connection
DLLEXPORT const QoreEncoding * getQoreEncoding() const
returns the QoreEncoding pointer used for this connection
DLLEXPORT bool activeTransaction() const
returns true if a transaction is in progress and DB commands have been issued since the transaction w...
DLLEXPORT QoreStringNode * getPendingDBEncoding() const
returns the pending database-specific character encoding name for the next connection ...
DLLEXPORT void setPendingHostName(const char *h)
sets the hostname to be used for the next connection
DLLEXPORT QoreStringNode * getConfigString() const
returns a string representing the configuration of the current object
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:55
DLLEXPORT void reset(ExceptionSink *xsink)
closes and opens the connection
DLLLOCAL T & getPrivateDataRef() const
returns the private DBI-specific data structure for this object
Definition: Datasource.h:184
DLLEXPORT const char * getPassword() const
returns the password used for the last connection
this class provides the internal link to the database driver for Qore's DBI layer ...
Definition: DBI.h:356
DLLEXPORT const std::string & getDBEncodingStr() const
returns the database-specific character encoding name used for the last connection ...
DLLEXPORT void setPendingDBName(const char *d)
sets the database (or schema) name to be used for the next connection
DLLEXPORT const std::string & getDBNameStr() const
returns the database (or schema) name used for the last connection
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:50
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:48
DLLEXPORT const char * getDriverName() const
returns the name of the current DBI driver
DLLEXPORT int setOption(const char *opt, const AbstractQoreNode *val, ExceptionSink *xsink)
sets an option for the datasource
DLLEXPORT bool isInTransaction() const
returns the transaction status flag
DLLEXPORT AbstractQoreNode * execRaw(const QoreString *query_str, const QoreListNode *args, ExceptionSink *xsink)
executes SQL throught the "execRaw" function of the DBI driver and returns the result, makes an implicit connection if necessary
DLLEXPORT int beginTransaction(ExceptionSink *xsink)
DLLEXPORT QoreStringNode * getPendingPassword() const
returns the pending password for the next connection
DLLEXPORT const char * getDBName() const
returns the database (or schema) name used for the last connection
DLLEXPORT QoreHashNode * getCurrentOptionHash() const
returns the options currently set for this object
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT void setPendingPassword(const char *p)
sets the password to be used for the next connection
DLLEXPORT Datasource(DBIDriver *driver)
creates the object and binds it to a particular DBIDriver
DLLEXPORT void connectionAborted()
should be called by the DBIDriver if the connection to the server is lost
DLLEXPORT int rollback(ExceptionSink *xsink)
rolls back the current transaction to the database
DLLEXPORT QoreHashNode * describe(const QoreString *query_str, const QoreListNode *args, ExceptionSink *xsink)
executes SQL that returns a result set and then returns a hash description of the result set ...
DLLEXPORT void setConnectionValues()
copies pending values to current values
DLLEXPORT const char * getDBEncoding() const
returns the database-specific character encoding name used for the last connection ...
DLLEXPORT void setQoreEncoding(const QoreEncoding *enc)
sets the QoreEncoding used for this connection
the base class for accessing databases in Qore through a Qore DBI driver
Definition: Datasource.h:54
DLLEXPORT AbstractQoreNode * getClientVersion(ExceptionSink *xsink) const
executes the "get_client_version" function of the driver, if any, and returns the result ...
DLLEXPORT AbstractQoreNode * selectRows(const QoreString *query_str, const QoreListNode *args, ExceptionSink *xsink)
executes SQL throught the "selectRows" function of the DBI driver and returns the result...
DLLEXPORT const char * getOSEncoding() const
returns the OS (or Qore) character encoding name used for the last connection
DLLEXPORT AbstractQoreNode * select(const QoreString *query_str, const QoreListNode *args, ExceptionSink *xsink)
executes SQL throught the "select" function of the DBI driver and returns the result, makes an implicit connection if necessary
DLLEXPORT QoreListNode * getCapabilityList() const
returns a QoreListNode object of all capability strings of the current driver, the caller owns the re...
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
DLLEXPORT int getPort() const
returns the port number used for the last connection
DLLEXPORT int beginImplicitTransaction(ExceptionSink *xsink)
calls the "begin_implicit_transaction" DBI method if it exists
DLLEXPORT bool isOpen() const
returns true if the connection is currently open
DLLEXPORT AbstractQoreNode * getServerVersion(ExceptionSink *xsink)
executes the "get_server_version" function of the driver, if any, and returns the result...
DLLEXPORT Datasource * copy() const
returns a copy of this object with the same DBIDriver and pending connection values ...
DLLEXPORT const char * getUsername() const
returns the username used for the last connection
DLLEXPORT void setPendingDBEncoding(const char *c)
sets the database-specific name of the character-encoding to be used for the next connection ...
DLLEXPORT int getCapabilities() const
returns the capability mask of the current driver
DLLEXPORT int getPendingPort() const
returns the pending port number for the next connection
DLLEXPORT void setEventQueue(Queue *q, AbstractQoreNode *arg, ExceptionSink *xsink)
sets an event queue for datasource events
DLLEXPORT QoreHashNode * selectRow(const QoreString *query_str, const QoreListNode *args, ExceptionSink *xsink)
executes SQL throught the "selectRow" function of the DBI driver and returns the result, makes an implicit connection if necessary
DLLEXPORT void setTransactionStatus(bool)
sets the transaction status
DLLEXPORT void setPrivateData(void *data)
sets the private DBI-specific data structure for this object
DLLEXPORT AbstractQoreNode * exec(const QoreString *query_str, const QoreListNode *args, ExceptionSink *xsink)
executes SQL throught the "exec" function of the DBI driver and returns the result, makes an implicit connection if necessary
DLLEXPORT int open(ExceptionSink *xsink)
opens a connection to the database
DLLEXPORT QoreHashNode * getOptionHash() const
returns the valid options for this driver with descriptions and current values for the current dataso...
DLLEXPORT const std::string & getHostNameStr() const
returns the host name used for the last connection
DLLEXPORT QoreHashNode * getConfigHash() const
returns a hash representing the configuration of the current object
DLLEXPORT void setDBEncoding(const char *name)
sets the database-specific character encoding name used for the current connection ...
DLLEXPORT void * getPrivateData() const
returns the private DBI-specific data structure for this object
DLLEXPORT const char * getHostName() const
returns the host name used for the last connection
DLLEXPORT QoreStringNode * getPendingHostName() const
returns the pending host name for the next connection
DLLEXPORT int close()
closes the connection