Qore Programming Language  1.12.0
QoreSQLStatement.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSQLStatement.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2006 - 2022 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_QORESQLSTATEMENT_H
33 #define _QORE_QORESQLSTATEMENT_H
34 
35 #include "qore/intern/sql_statement_private.h"
36 
37 #include "qore/intern/DatasourceStatementHelper.h"
38 
39 #define STMT_IDLE 0
40 #define STMT_PREPARED 1
41 #define STMT_EXECED 2
42 #define STMT_DEFINED 3
43 
44 class DBActionHelper;
45 
46 class QoreSQLStatement : public AbstractPrivateData, public SQLStatement {
47  friend class DBActionHelper;
48 
49 protected:
50  DLLLOCAL static const char* stmt_statuses[];
51 
52  // Datasource used to prepare the statement
53  Datasource* stmtds = nullptr;
54  // helper object for acquiring a Datasource pointer
55  DatasourceStatementHelper* dsh;
56  // copy of SQL string
57  QoreString str;
58  // copy of prepare args
59  QoreListNode* prepare_args = nullptr;
60  // status
61  unsigned char status = STMT_IDLE;
62  // raw prepare flag
63  bool raw = false;
64  // valid flag
65  bool validp = false;
66 
67  DLLLOCAL int checkStatus(ExceptionSink* xsink, DBActionHelper& dba, int stat, const char* action);
68 
69  DLLLOCAL int closeIntern(ExceptionSink* xsink);
70  DLLLOCAL int execIntern(DBActionHelper& dba, ExceptionSink* xsink);
71  DLLLOCAL int execDescribeIntern(DBActionHelper& dba, ExceptionSink* xsink);
72  DLLLOCAL int defineIntern(ExceptionSink* xsink);
73  DLLLOCAL int prepareIntern(ExceptionSink* xsink);
74  DLLLOCAL int prepareArgs(bool n_raw, const QoreString& n_str, const QoreListNode* args, ExceptionSink* xsink);
75 
76 public:
77  DLLLOCAL QoreSQLStatement(DatasourceStatementHelper* dsh) : dsh(dsh->helperRefSelf()) {
78  }
79 
80  DLLLOCAL QoreSQLStatement(Datasource* ds, void* data, DatasourceStatementHelper* dsh, unsigned char status);
81 
82  DLLLOCAL ~QoreSQLStatement();
83 
85  DLLLOCAL virtual void deref(ExceptionSink* xsink);
86 
87  DLLLOCAL int prepare(const QoreString& n_str, const QoreListNode* args, ExceptionSink* xsink);
88 
89  DLLLOCAL int prepareRaw(const QoreString& n_str, ExceptionSink* xsink);
90 
91  DLLLOCAL int bind(const QoreListNode& l, ExceptionSink* xsink);
92  DLLLOCAL int bindPlaceholders(const QoreListNode& l, ExceptionSink* xsink);
93  DLLLOCAL int bindValues(const QoreListNode& l, ExceptionSink* xsink);
94 
95  DLLLOCAL int exec(const QoreListNode* args, ExceptionSink* xsink);
96 
97  DLLLOCAL int affectedRows(ExceptionSink* xsink);
98 
99  DLLLOCAL QoreHashNode* getOutput(ExceptionSink* xsink);
100 
101  DLLLOCAL QoreHashNode* getOutputRows(ExceptionSink* xsink);
102 
103  DLLLOCAL bool next(ExceptionSink* xsink);
104  DLLLOCAL bool valid();
105 
106  DLLLOCAL int define(ExceptionSink* xsink);
107 
108  DLLLOCAL QoreHashNode* fetchRow(ExceptionSink* xsink);
109 
110  DLLLOCAL QoreListNode* fetchRows(int rows, ExceptionSink* xsink);
111  DLLLOCAL QoreHashNode* fetchColumns(int rows, ExceptionSink* xsink);
112 
113  DLLLOCAL QoreHashNode* describe(ExceptionSink* xsink);
114 
115  DLLLOCAL int close(ExceptionSink* xsink);
116  DLLLOCAL int commit(ExceptionSink* xsink);
117  DLLLOCAL int rollback(ExceptionSink* xsink);
118  DLLLOCAL int beginTransaction(ExceptionSink* xsink);
119 
120  DLLLOCAL bool active() const;
121  DLLLOCAL bool currentThreadInTransaction(ExceptionSink* xsink);
122 
123  // called by the datasource object if the connection is lost or the transaction has been committed or rolled back
124  DLLLOCAL void transactionDone(bool clear, bool close, ExceptionSink* xsink);
125 
126  DLLLOCAL QoreStringNode* getSQL(ExceptionSink* xsink);
127 };
128 
129 #endif
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:65
the base class for accessing databases in Qore through a Qore DBI driver
Definition: Datasource.h:55
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
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
This is the public class for DBI drivers supporting Qore's new prepared statement API.
Definition: SQLStatement.h:38