39#ifndef _QORE_MANAGEDDATASOURCE_H
40#define _QORE_MANAGEDDATASOURCE_H
42#include "qore/intern/DatasourceStatementHelper.h"
43#include "qore/intern/QoreSQLStatement.h"
48#define DEFAULT_TL_TIMEOUT 120000
51 friend class DatasourceActionHelper;
52 friend class DatasourceLockHelper;
65 DLLLOCAL
int startDBAction(
ExceptionSink* xsink,
bool& new_transaction);
67 DLLLOCAL
bool endDBActionIntern(
char cmd = DAH_NOCHANGE,
bool new_transaction =
false);
69 DLLLOCAL
bool endDBAction(
char cmd = DAH_NOCHANGE,
bool new_transaction =
false);
72 DLLLOCAL
int grabLockIntern();
73 DLLLOCAL
void grabLockUnconditionalIntern();
76 DLLLOCAL
void releaseLock();
77 DLLLOCAL
void releaseLockIntern();
78 DLLLOCAL
void forceReleaseLockIntern();
79 DLLLOCAL
void finish_transaction();
82 DLLLOCAL
virtual ~ManagedDatasource() {
86 DLLLOCAL ManagedDatasource(
DBIDriver *ndsl) :
Datasource(ndsl, this), tl_timeout_ms(DEFAULT_TL_TIMEOUT) {
89 DLLLOCAL ManagedDatasource(
const ManagedDatasource& old) :
Datasource(old, this), tl_timeout_ms(old.tl_timeout_ms) {
95 DLLLOCAL
virtual void deref();
111 DLLLOCAL
void setPendingUsername(
const char* u);
112 DLLLOCAL
void setPendingPassword(
const char* p);
113 DLLLOCAL
void setPendingDBName(
const char* d);
114 DLLLOCAL
void setPendingDBEncoding(
const char* c);
115 DLLLOCAL
void setPendingHostName(
const char* h);
116 DLLLOCAL
void setPendingPort(
int port);
122 DLLLOCAL
int getPendingPort()
const;
123 DLLLOCAL
void setTransactionLockTimeout(
int t_ms);
124 DLLLOCAL
int getTransactionLockTimeout()
const;
128 using Datasource::setAutoCommit;
132 DLLLOCAL ManagedDatasource* copy();
142 DLLLOCAL
int transactionTid()
const {
146 DLLLOCAL
bool currentThreadInTransaction()
const {
157 DLLLOCAL
virtual DatasourceStatementHelper* helperRefSelfImpl() {
163 DLLLOCAL
virtual void helperDestructorImpl(QoreSQLStatement* s,
ExceptionSink* xsink) {
168 if (!startDBAction(xsink, new_transaction))
172 return tid ==
q_gettid() ? this : 0;
178 if (cmd == DAH_RELEASE) {
181 return endDBAction(cmd, new_transaction) ? this : 0;
185class DatasourceActionHelper {
187 ManagedDatasource& ds;
188 bool ok, new_transaction;
192 DLLLOCAL DatasourceActionHelper(ManagedDatasource& n_ds,
ExceptionSink* xsink,
char n_cmd = DAH_NOCHANGE) :
193 ds(n_ds), ok(n_cmd == DAH_NOCONN ? !ds.acquireLock(xsink) : !ds.startDBAction(xsink, new_transaction)), cmd(n_cmd) {
194 if (cmd == DAH_NOCONN) {
195 new_transaction =
false;
199 DLLLOCAL ~DatasourceActionHelper();
201 DLLLOCAL
bool newTransaction()
const {
return new_transaction; }
203 DLLLOCAL
operator bool()
const {
return ok; }
206class DatasourceLockHelper {
208 ManagedDatasource& ds;
209 bool valid, had_lock;
212 DLLLOCAL DatasourceLockHelper(ManagedDatasource& n_ds,
ExceptionSink* xsink) : ds(n_ds) {
215 valid = !ds.grabLock(xsink);
220 DLLLOCAL ~DatasourceLockHelper() {
223 ds.releaseLockIntern();
228 DLLLOCAL
operator bool()
const {
return valid; }
DLLLOCAL void ref() const
increments the reference count of the object
Definition: AbstractPrivateData.h:51
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:367
the base class for accessing databases in Qore through a Qore DBI driver
Definition: Datasource.h:55
DLLEXPORT QoreHashNode * getOptionHash() const
returns the valid options for this driver with descriptions and current values for the current dataso...
DLLEXPORT QoreStringNode * getConfigString() const
returns a string representing the configuration of the current object
DLLEXPORT int autoCommit(ExceptionSink *xsink)
called from subclasses when releasing the transaction lock
DLLEXPORT QoreHashNode * getConfigHash() const
returns a hash representing the configuration of the current object
DLLEXPORT int close()
closes the connection
DLLEXPORT QoreValue execRaw(const QoreString *query_str, const QoreListNode *args, ExceptionSink *xsink)
executes SQL throught the "execRaw" function of the DBI driver and returns the result,...
DLLEXPORT Datasource * copy() const
returns a copy of this object with the same DBIDriver and pending connection values
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
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
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 a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
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:276