32 #ifndef _QORUS_DATASOURCE_POOL_H
34 #define _QORUS_DATASOURCE_POOL_H
36 #include <qore/Datasource.h>
37 #include <qore/QoreThreadLock.h>
38 #include <qore/QoreCondition.h>
39 #include <qore/QoreString.h>
40 #include <qore/AbstractThreadResource.h>
42 #include "qore/intern/DatasourceStatementHelper.h"
43 #include "qore/intern/QoreSQLStatement.h"
49 typedef std::map<int, int> thread_use_t;
50 typedef std::deque<int> free_list_t;
53 class DatasourceConfig {
73 DLLLOCAL DatasourceConfig(
DBIDriver* n_driver,
const char* n_user,
const char* n_pass,
const char* n_db,
74 const char* n_encoding,
const char* n_host,
int n_port,
76 driver(n_driver), user(n_user ? n_user :
""), pass(n_pass ? n_pass :
""), db(n_db ? n_db :
""),
77 encoding(n_encoding ? n_encoding :
""), host(n_host ? n_host :
""), port(n_port),
78 opts(n_opts ? n_opts->hashRefSelf() : 0), q(n_q), arg(n_arg) {
81 DLLLOCAL DatasourceConfig(
const DatasourceConfig& old) :
82 driver(old.driver), user(old.user), pass(old.pass), db(old.db), encoding(old.encoding), host(old.host),
83 port(old.port), opts(old.opts ? old.opts->hashRefSelf() : nullptr),
84 q(old.q ? old.q->queueRefSelf() : nullptr), arg(old.arg.refSelf()) {
87 DLLLOCAL ~DatasourceConfig() {
126 friend class DatasourcePoolActionHelper;
131 free_list_t free_list;
148 DatasourceConfig config;
154 void addSQL(
const char* cmd,
const QoreString* sql);
173 DLLLOCAL DatasourcePool(
ExceptionSink* xsink,
DBIDriver* ndsl,
const char* user,
const char* pass,
const char* db,
const char* charset,
const char* hostname,
unsigned mn,
unsigned mx,
int port = 0,
const QoreHashNode* opts = 0, Queue* q =
nullptr,
QoreValue a =
QoreValue());
174 DLLLOCAL DatasourcePool(
const DatasourcePool& old,
ExceptionSink* xsink);
176 DLLLOCAL
virtual ~DatasourcePool();
200 DLLLOCAL
unsigned getMin()
const;
201 DLLLOCAL
unsigned getMax()
const;
207 DLLLOCAL
int getPendingPort()
const;
209 DLLLOCAL
const DBIDriver* getDriver ()
const {
212 DLLLOCAL
const char* getDriverName ()
const {
217 DLLLOCAL
int getCapabilities()
const;
224 DLLLOCAL
bool inTransaction();
235 DLLLOCAL DatasourceStatementHelper* helperRefSelfImpl() {
241 DLLLOCAL
virtual void helperDestructorImpl(QoreSQLStatement* s,
ExceptionSink* xsink) {
246 return getDS(new_transaction, xsink);
251 if (cmd == DAH_RELEASE) {
256 return getAllocatedDS();
259 DLLLOCAL
bool currentThreadInTransaction()
const {
261 return tmap.find(
q_gettid()) != tmap.end();
271 DLLLOCAL
void setErrorTimeout(
unsigned t_ms) {
272 tl_timeout_ms = t_ms;
275 DLLLOCAL
unsigned getErrorTimeout()
const {
276 return tl_timeout_ms;
282 class DatasourcePoolActionHelper {
291 DLLLOCAL DatasourcePoolActionHelper(DatasourcePool& n_dsp,
ExceptionSink* n_xsink,
char n_cmd = DAH_NOCHANGE) : dsp(n_dsp), xsink(n_xsink), new_ds(false), cmd(n_cmd) {
292 ds = dsp.getDS(new_ds, xsink);
300 DLLLOCAL ~DatasourcePoolActionHelper();
303 DLLLOCAL
void releaseNew() {
304 assert(cmd == DAH_ACQUIRE);
312 if (ds && !((cmd == DAH_RELEASE) || (new_ds && (cmd == DAH_NOCHANGE)) || ds->
wasConnectionAborted()))
313 dsp.addSQL(cmd == DAH_NOCHANGE ?
"select" :
"exec", sql);
317 DLLLOCAL
operator bool()
const {
return ds; }
319 DLLLOCAL
Datasource* operator*()
const {
return ds; }
320 DLLLOCAL
Datasource* operator->()
const {
return ds; }
DLLLOCAL void ref() const
increments the reference count of the object
Definition: AbstractPrivateData.h:51
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:65
DLLEXPORT void deref(ExceptionSink *xsink)
decrements the reference count and calls derefImpl() if there_can_be_only_one is false,...
base class for saving data using Qore's thread resource management system
Definition: AbstractThreadResource.h:51
this class provides the internal link to the database driver for Qore's DBI layer
Definition: DBI.h:354
the base class for accessing databases in Qore through a Qore DBI driver
Definition: Datasource.h:55
DLLEXPORT QoreValue getOption(const char *opt, ExceptionSink *xsink)
Returns the current value for the given option.
DLLEXPORT QoreValue getClientVersion(ExceptionSink *xsink) const
executes the "get_client_version" function of the driver, if any, and returns the result
DLLEXPORT bool wasConnectionAborted() const
returns the connection aborted status
DLLEXPORT const char * getDriverName() const
returns the name of the current DBI driver
DLLEXPORT QoreHashNode * getOptionHash() const
returns the valid options for this driver with descriptions and current values for the current dataso...
DLLEXPORT const DBIDriver * getDriver() const
returns the DBIDriver pointer used for this object
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT bool ROdereference() const
atomically decrements the reference count
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
provides access to thread-local storage
Definition: QoreThreadLocalStorage.h:44
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
base class for resolved call references
Definition: CallReferenceNode.h:109
provides an exception-safe way to manage locks in Qore, only to be used on the stack,...
Definition: QoreThreadLock.h:228
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
DLLEXPORT int q_gettid() noexcept
returns the current TID number
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275
DLLEXPORT void discard(ExceptionSink *xsink)
dereferences any contained AbstractQoreNode pointer and sets to 0; does not modify other values