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 
12 #include "KVSQLiteResult.h"
13 #include "TSQLiteRow.h"
14 
15 #include <sqlite3.h>
16 
17 #include <thread>
18 #include <chrono>
19 using namespace std::chrono_literals;
20 
21 ClassImp(KVSQLiteResult);
22 
25 
26 
28 
29 KVSQLiteResult::KVSQLiteResult(void* result)
30 {
31  fResult = (sqlite3_stmt*) result;
32 
33  // RowCount is -1, as sqlite cannot determine RowCount beforehand:
34  fRowCount = -1;
35 }
36 
37 
40 
41 
43 
44 KVSQLiteResult::~KVSQLiteResult()
45 {
46  if (fResult)
47  Close();
48 }
49 
50 
53 
54 
56 
57 void KVSQLiteResult::Close(Option_t*)
58 {
59  if (!fResult)
60  return;
61 
62  sqlite3_finalize(fResult);
63  fResult = nullptr;
64 }
65 
66 
69 
70 
72 
73 Bool_t KVSQLiteResult::IsValid(Int_t field)
74 {
75  if (!fResult) {
76  Error("IsValid", "result set closed");
77  return kFALSE;
78  }
79  if (field < 0 || field >= GetFieldCount()) {
80  Error("IsValid", "field index out of bounds");
81  return kFALSE;
82  }
83  return kTRUE;
84 }
85 
86 
89 
90 
92 
93 Int_t KVSQLiteResult::GetFieldCount()
94 {
95  if (!fResult) {
96  Error("GetFieldCount", "result set closed");
97  return 0;
98  }
99  return sqlite3_column_count(fResult);
100 }
101 
102 
105 
106 
108 
109 const char* KVSQLiteResult::GetFieldName(Int_t field)
110 {
111  if (!fResult) {
112  Error("GetFieldName", "result set closed");
113  return nullptr;
114  }
115  return sqlite3_column_name(fResult, field);
116 }
117 
118 
122 
123 
125 
126 Int_t KVSQLiteResult::GetRowCount() const
127 {
128  return -1;
129 }
130 
131 
135 
136 
138 
139 TSQLRow* KVSQLiteResult::Next()
140 {
141  if (!fResult) {
142  Error("Next", "result set closed");
143  return nullptr;
144  }
145 
146  int ret = sqlite3_step(fResult);
147  if ((ret != SQLITE_DONE) && (ret != SQLITE_ROW)) {
148  if(ret == SQLITE_BUSY)
149  {
150  // retry the statement 4 times after sleeping
151  int i=0;
152  do
153  {
154  ++i;
155  Info("Next","DB locked: retry same statement processing in 50ms...(%d)",i);
156  std::this_thread::sleep_for(50ms);
157  ret = sqlite3_step(fResult);
158  }
159  while((i<4) && ((ret != SQLITE_DONE) && (ret != SQLITE_ROW)));
160  if ((ret != SQLITE_DONE) && (ret != SQLITE_ROW))
161  {
162  Error("Next", "SQL Error: %d %s", ret, sqlite3_errmsg(sqlite3_db_handle(fResult)));
163  return nullptr;
164  }
165  else
166  Info("Next","...statement processing successful!");
167  if (ret == SQLITE_DONE) {
168  // Finished executing, no other row!
169  return nullptr;
170  }
171  return new TSQLiteRow((void*) fResult, -1);
172  }
173  Error("Next", "SQL Error: %d %s", ret, sqlite3_errmsg(sqlite3_db_handle(fResult)));
174  return nullptr;
175  }
176  if (ret == SQLITE_DONE) {
177  // Finished executing, no other row!
178  return nullptr;
179  }
180  return new TSQLiteRow((void*) fResult, -1);
181 }
182 
183 
184 
int Int_t
bool Bool_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
void Error(const char *location, const char *fmt,...)
void Info(const char *location, const char *fmt,...)
ClassImp(TPyArg)