20 #include "KVSQLiteStatement.h"
21 #include "KVSQLiteResult.h"
31 using namespace std::chrono_literals;
49 unsigned long bindParamcount = sqlite3_bind_parameter_count(fStmt->fRes);
51 if (bindParamcount > 0) {
53 fNumPars = bindParamcount;
57 fNumPars = sqlite3_column_count(fStmt->fRes);
83 sqlite3_finalize(fStmt->fRes);
86 fStmt->fRes =
nullptr;
87 fStmt->fConn =
nullptr;
97 #define CheckStmt(method, res) \
101 SetError(-1,"Statement handle is 0",method); \
110 #define CheckErrNo(method, force, res) \
112 int stmterrno = sqlite3_errcode(fStmt->fConn); \
113 if ((stmterrno!=0) || force) { \
114 const char* stmterrmsg = sqlite3_errmsg(fStmt->fConn); \
115 if (stmterrno==0) { stmterrno = -1; stmterrmsg = "SQLite statement error"; } \
116 SetError(stmterrno, stmterrmsg, method); \
125 #define CheckGetField(method, res) \
128 if (!IsResultSetMode()) { \
129 SetError(-1,"Cannot get statement parameters",method); \
132 if ((npar<0) || (npar>=fNumPars)) { \
133 SetError(-1,Form("Invalid parameter number %d", npar),method); \
145 if (res == SQLITE_RANGE) {
146 SetError(-1,
Form(
"SQLite parameter out of bounds, error: %d %s", res, sqlite3_errmsg(fStmt->fConn)), method);
149 if (res != SQLITE_OK) {
150 SetError(-1,
Form(
"SQLite error code during parameter binding, error: %d %s", res, sqlite3_errmsg(fStmt->fConn)), method);
165 CheckStmt(
"Process",
kFALSE);
167 int res = sqlite3_step(fStmt->fRes);
168 if ((res != SQLITE_DONE) && (res != SQLITE_ROW)) {
169 if(res == SQLITE_BUSY)
176 Info(
"Process",
"DB locked: retry same statement processing in 50ms...(%d)",i);
177 std::this_thread::sleep_for(50ms);
178 res = sqlite3_step(fStmt->fRes);
180 while((i<4) && ((res != SQLITE_DONE) && (res != SQLITE_ROW)));
181 if ((res != SQLITE_DONE) && (res != SQLITE_ROW))
183 SetError(-1,
Form(
"SQLite error code during statement-stepping: %d %s", res, sqlite3_errmsg(fStmt->fConn)),
"Process");
189 SetError(-1,
Form(
"SQLite error code during statement-stepping: %d %s", res, sqlite3_errmsg(fStmt->fConn)),
"Process");
196 if (res == SQLITE_DONE) {
197 sqlite3_reset(fStmt->fRes);
200 if (IsResultSetMode()) {
205 if (IsSetParsMode()) {
210 if (res == SQLITE_ROW) {
229 CheckStmt(
"GetNumAffectedRows",
kFALSE);
231 return (
Int_t) sqlite3_changes(fStmt->fConn);
243 CheckStmt(
"GetNumParameters", -1);
245 Int_t res = sqlite3_bind_parameter_count(fStmt->fRes);
247 CheckErrNo(
"GetNumParameters",
kFALSE, -1);
265 CheckStmt(
"StoreResult",
kFALSE);
291 if (!IsResultSetMode() || (nfield < 0) || (nfield >= sqlite3_column_count(fStmt->fRes)))
294 return sqlite3_column_name(fStmt->fRes, nfield);
308 if (!fStmt || !IsResultSetMode())
return kFALSE;
310 if (fIterationCount == 0) {
335 if (!IsSetParsMode()) {
336 SetError(-1,
"Cannot call for that statement",
"NextIteration");
340 if (fIterationCount == 0) {
362 CheckGetField(
"ConvertToString",
"");
364 return reinterpret_cast<const char*
>(sqlite3_column_text(fStmt->fRes, npar));
376 CheckGetField(
"ConvertToNumeric", -1);
378 return (
long double) sqlite3_column_double(fStmt->fRes, npar);
390 CheckGetField(
"IsNull",
kFALSE);
392 return (sqlite3_column_type(fStmt->fRes, npar) == SQLITE_NULL);
404 CheckGetField(
"GetInt", -1);
406 return (
Int_t) sqlite3_column_int(fStmt->fRes, npar);
418 CheckGetField(
"GetUInt", 0);
420 return (
UInt_t) sqlite3_column_int(fStmt->fRes, npar);
432 CheckGetField(
"GetLong", -1);
434 return (
Long_t) sqlite3_column_int64(fStmt->fRes, npar);
446 CheckGetField(
"GetLong64", -1);
448 return (
Long64_t) sqlite3_column_int64(fStmt->fRes, npar);
460 CheckGetField(
"GetULong64", 0);
462 return (
ULong64_t) sqlite3_column_int64(fStmt->fRes, npar);
474 CheckGetField(
"GetDouble", -1);
476 return (
Double_t) sqlite3_column_double(fStmt->fRes, npar);
488 CheckGetField(
"GetString",
"");
490 return reinterpret_cast<const char*
>(sqlite3_column_text(fStmt->fRes, npar));
504 CheckGetField(
"GetBinary",
kFALSE);
509 size_t sz = sqlite3_column_bytes(fStmt->fRes, npar);
511 delete [](
unsigned char*) mem;
512 mem = (
void*)
new unsigned char[sz];
516 memcpy(mem, sqlite3_column_blob(fStmt->fRes, npar), sz);
530 CheckGetField(
"GetDate",
kFALSE);
532 TString val =
reinterpret_cast<const char*
>(sqlite3_column_text(fStmt->fRes, npar));
535 month =
d.GetMonth();
550 CheckGetField(
"GetTime",
kFALSE);
552 TString val =
reinterpret_cast<const char*
>(sqlite3_column_text(fStmt->fRes, npar));
570 CheckGetField(
"GetDatime",
kFALSE);
572 TString val =
reinterpret_cast<const char*
>(sqlite3_column_text(fStmt->fRes, npar));
575 month =
d.GetMonth();
594 CheckGetField(
"GetTimestamp",
kFALSE);
596 TString val =
reinterpret_cast<const char*
>(sqlite3_column_text(fStmt->fRes, npar));
603 month =
d.GetMonth();
610 frac = (
Int_t)(atof(s_frac.
Data()) * 1.E3);
624 int res = sqlite3_bind_null(fStmt->fRes, npar + 1);
626 return CheckBindError(
"SetNull", res);
638 int res = sqlite3_bind_int(fStmt->fRes, npar + 1,
value);
640 return CheckBindError(
"SetInt", res);
653 int res = sqlite3_bind_int(fStmt->fRes, npar + 1, (
Int_t)
value);
655 return CheckBindError(
"SetUInt", res);
667 int res = sqlite3_bind_int64(fStmt->fRes, npar + 1,
value);
669 return CheckBindError(
"SetLong", res);
681 int res = sqlite3_bind_int64(fStmt->fRes, npar + 1,
value);
683 return CheckBindError(
"SetLong64", res);
696 int res = sqlite3_bind_int64(fStmt->fRes, npar + 1, (
Long64_t)
value);
698 return CheckBindError(
"SetULong64", res);
710 int res = sqlite3_bind_double(fStmt->fRes, npar + 1,
value);
712 return CheckBindError(
"SetDouble", res);
724 int res = sqlite3_bind_text(fStmt->fRes, npar + 1,
value, maxsize, SQLITE_TRANSIENT);
726 return CheckBindError(
"SetString", res);
741 SetError(-1,
"Passing negative value to size for BLOB to SQLite would cause undefined behaviour, refusing it!",
"SetBinary");
745 int res = sqlite3_bind_blob(fStmt->fRes, npar + 1, mem, (
size_t)
size, SQLITE_TRANSIENT);
747 return CheckBindError(
"SetBinary", res);
760 int res = sqlite3_bind_text(fStmt->fRes, npar + 1, (
char*)
d.AsSQLString(), -1, SQLITE_TRANSIENT);
762 return CheckBindError(
"SetDate", res);
776 int res = sqlite3_bind_text(fStmt->fRes, npar + 1, (
char*)
d.AsSQLString(), -1, SQLITE_TRANSIENT);
778 return CheckBindError(
"SetTime", res);
792 int res = sqlite3_bind_text(fStmt->fRes, npar + 1, (
char*)
d.AsSQLString(), -1, SQLITE_TRANSIENT);
794 return CheckBindError(
"SetDatime", res);
810 value.Form(
"%s.%03d", (
char*)
d.AsSQLString(), frac);
812 int res = sqlite3_bind_text(fStmt->fRes, npar + 1,
value.Data(), -1, SQLITE_TRANSIENT);
814 return CheckBindError(
"SetTimestamp", res);
size_t size(const MatrixT &matrix)
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char * Form(const char *fmt,...)
Modified copy of TSQLiteStatement.
Bool_t NextIteration() final
Bool_t SetLong(Int_t npar, Long_t value) final
Set parameter value as long.
Bool_t SetDate(Int_t npar, Int_t year, Int_t month, Int_t day) final
Set parameter value as date.
Bool_t GetBinary(Int_t npar, void *&mem, Long_t &size) final
virtual ~KVSQLiteStatement()
Destructor.
TDatime GetTimestamp(Int_t)
Bool_t SetInt(Int_t npar, Int_t value) final
Set parameter value as integer.
Bool_t GetTime(Int_t npar, Int_t &hour, Int_t &min, Int_t &sec) final
Return field as time.
Bool_t GetDatime(Int_t npar, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec) final
Return field value as date & time.
Bool_t SetString(Int_t npar, const char *value, Int_t maxsize=256) final
Set parameter value as string.
Bool_t SetBinary(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000) final
Bool_t NextResultRow() final
Shift cursor to next row in result set.
const char * GetString(Int_t npar) final
Return field value as string.
ULong64_t GetULong64(Int_t npar) final
Return field value as unsigned 64-bit integer.
Double_t GetDouble(Int_t npar) final
Return field value as double.
const char * GetFieldName(Int_t nfield) final
Returns field name in result set.
const char * ConvertToString(Int_t npar)
Convert field value to string.
Bool_t GetDate(Int_t npar, Int_t &year, Int_t &month, Int_t &day) final
Return field value as date.
Int_t GetNumFields() final
Return number of fields in result set.
Long64_t GetLong64(Int_t npar) final
Get long64.
Bool_t IsNull(Int_t npar) final
Checks if field value is null.
Bool_t SetULong64(Int_t npar, ULong64_t value) final
Bool_t SetNull(Int_t npar) final
Set NULL as parameter value.
Bool_t StoreResult() final
Int_t GetInt(Int_t npar) final
Get integer.
Bool_t SetLong64(Int_t npar, Long64_t value) final
Set parameter value as 64-bit integer.
Bool_t SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec) final
Set parameter value as time.
void Close(Option_t *="") final
Close statement.
long double ConvertToNumeric(Int_t npar)
Convert field to numeric.
Bool_t SetDatime(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec) final
Set parameter value as date & time.
Bool_t CheckBindError(const char *method, int res)
Long_t GetLong(Int_t npar) final
Get long.
Int_t GetNumAffectedRows() final
UInt_t GetUInt(Int_t npar) final
Get unsigned integer.
Bool_t SetTimestamp(Int_t, const TDatime &)
Bool_t Process() final
Process statement.
Int_t GetNumParameters() final
Return number of statement parameters.
Bool_t SetUInt(Int_t npar, UInt_t value) final
Bool_t SetDouble(Int_t npar, Double_t value) final
Set parameter value as double value.
const char * Data() const
Ssiz_t Last(char c) const
const char * Data() const
unsigned long long ULong64_t
double min(double x, double y)