KaliVeda
Toolkit for HIC analysis
KVSQLiteResult.cxx
1 // @(#)root/sqlite:$Id$
2 // Author: o.freyermuth <o.f@cern.ch>, 01/06/2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 #include "KVSQLiteResult.h"
12 #include "TSQLiteRow.h"
13 
14 #include <sqlite3.h>
15 
16 #include <thread>
17 #include <chrono>
18 using namespace std::chrono_literals;
19 
21 
24 
25 
27 
29 {
30  fResult = (sqlite3_stmt*) result;
31 
32  // RowCount is -1, as sqlite cannot determine RowCount beforehand:
33  fRowCount = -1;
34 }
35 
36 
39 
40 
42 
44 {
45  if (fResult)
46  Close();
47 }
48 
49 
52 
53 
55 
57 {
58  if (!fResult)
59  return;
60 
61  sqlite3_finalize(fResult);
62  fResult = nullptr;
63 }
64 
65 
68 
69 
71 
73 {
74  if (!fResult) {
75  Error("IsValid", "result set closed");
76  return kFALSE;
77  }
78  if (field < 0 || field >= GetFieldCount()) {
79  Error("IsValid", "field index out of bounds");
80  return kFALSE;
81  }
82  return kTRUE;
83 }
84 
85 
88 
89 
91 
93 {
94  if (!fResult) {
95  Error("GetFieldCount", "result set closed");
96  return 0;
97  }
98  return sqlite3_column_count(fResult);
99 }
100 
101 
104 
105 
107 
109 {
110  if (!fResult) {
111  Error("GetFieldName", "result set closed");
112  return nullptr;
113  }
114  return sqlite3_column_name(fResult, field);
115 }
116 
117 
121 
122 
124 
126 {
127  return -1;
128 }
129 
130 
134 
135 
137 
139 {
140  if (!fResult) {
141  Error("Next", "result set closed");
142  return nullptr;
143  }
144 
145  int ret = sqlite3_step(fResult);
146  if ((ret != SQLITE_DONE) && (ret != SQLITE_ROW)) {
147  if(ret == SQLITE_BUSY)
148  {
149  // retry the statement 4 times after sleeping
150  int i=0;
151  do
152  {
153  ++i;
154  Info("Next","DB locked: retry same statement processing in 50ms...(%d)",i);
155  std::this_thread::sleep_for(50ms);
156  ret = sqlite3_step(fResult);
157  }
158  while((i<4) && ((ret != SQLITE_DONE) && (ret != SQLITE_ROW)));
159  if ((ret != SQLITE_DONE) && (ret != SQLITE_ROW))
160  {
161  Error("Next", "SQL Error: %d %s", ret, sqlite3_errmsg(sqlite3_db_handle(fResult)));
162  return nullptr;
163  }
164  else
165  Info("Next","...statement processing successful!");
166  if (ret == SQLITE_DONE) {
167  // Finished executing, no other row!
168  return nullptr;
169  }
170  return new TSQLiteRow((void*) fResult, -1);
171  }
172  Error("Next", "SQL Error: %d %s", ret, sqlite3_errmsg(sqlite3_db_handle(fResult)));
173  return nullptr;
174  }
175  if (ret == SQLITE_DONE) {
176  // Finished executing, no other row!
177  return nullptr;
178  }
179  return new TSQLiteRow((void*) fResult, -1);
180 }
181 
182 
183 
int Int_t
bool Bool_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Modified copy of TSQLiteResult.
TSQLRow * Next() final
const char * GetFieldName(Int_t field) final
Get name of specified field.
KVSQLiteResult(void *result)
SQLite query result.
void Close(Option_t *opt="") final
Close query result.
~KVSQLiteResult()
Cleanup SQLite query result.
Bool_t IsValid(Int_t field)
Check if result set is open and field index within range.
Int_t GetFieldCount() final
Get number of fields in result.
Int_t GetRowCount() const final
void Error(const char *location, const char *fmt,...)
void Info(const char *location, const char *fmt,...)
ClassImp(TPyArg)