Xmipp  v3.23.11-Nereus
Static Public Member Functions | Static Protected Member Functions | List of all members
sqlUtils Class Reference

#include <sql_utils.h>

Static Public Member Functions

static bool addColumns (const std::vector< MDLabel > &columns, sqlite3 *db, const std::string &table)
 
static void reportLastError (sqlite3 *db)
 
static bool select (size_t rowId, sqlite3 *db, const std::string &table, std::vector< MDObject > &values)
 
static bool select (sqlite3 *db, const std::string &table, const std::vector< MDObject > &columns, std::vector< std::vector< MDObject >> &rows)
 
template<typename T >
static bool select (const MDLabel &label, sqlite3 *db, const std::string &table, std::vector< T > &values)
 
static bool insert (const std::vector< std::vector< const MDObject *>> &records, sqlite3 *db, const std::string &table)
 
static bool insert (const std::vector< const MDObject *> &values, sqlite3 *db, const std::string &table)
 
static bool update (const std::vector< const MDObject *> &values, sqlite3 *db, const std::string &table, size_t id)
 

Static Protected Member Functions

static void beginTrans (sqlite3 *db)
 
static void endTrans (sqlite3 *db)
 
static void commitTrans (sqlite3 *db)
 
static bool checkError (sqlite3 *db)
 
static std::string createSelectQuery (const std::vector< MDObject > &values, const std::string &table)
 
static std::string createSelectQuery (size_t rowId, const std::vector< MDObject > &values, const std::string &table)
 
static std::string createInsertQuery (const std::vector< const MDObject *> &values, const std::string &table)
 
static std::string createUpdateQuery (const std::vector< const MDObject *> &values, const std::string &table, size_t id)
 

Detailed Description

Definition at line 34 of file sql_utils.h.

Member Function Documentation

◆ addColumns()

bool sqlUtils::addColumns ( const std::vector< MDLabel > &  columns,
sqlite3 *  db,
const std::string &  table 
)
static

Add a new column to a DB table. WARNING: this operation is typically 'very slow'. If you can, define all columns of the table at the construction time.

Parameters
columnsto be added
dbto be altered
tableto be altered
Returns
true on success

Definition at line 31 of file sql_utils.cpp.

32  {
33  // it seems that with columns, one cannot use data binding
34  auto query = "ALTER TABLE " + table + " ADD COLUMN ";
35  // FIXME currently, whole db is in one huge transaction. Finish whatever might be pending,
36  // do our business in a clean transaction and start a new transaction after (not to break the original code)
37  commitTrans(db);
38  beginTrans(db);
39 
40  for (auto c : columns) {
41  auto stmt = query + MDL::label2SqlColumn(c) + ";";
42  sqlite3_exec(db, stmt.c_str(), nullptr, nullptr, nullptr);
43  }
44 
45  endTrans(db);
46  beginTrans(db);
47  return checkError(db);
48 }
static void beginTrans(sqlite3 *db)
Definition: sql_utils.h:150
doublereal * c
static void endTrans(sqlite3 *db)
Definition: sql_utils.h:154
static void commitTrans(sqlite3 *db)
Definition: sql_utils.h:158
static bool checkError(sqlite3 *db)
Definition: sql_utils.cpp:331
static String label2SqlColumn(const MDLabel label)

◆ beginTrans()

static void sqlUtils::beginTrans ( sqlite3 *  db)
inlinestaticprotected

Definition at line 150 of file sql_utils.h.

150  {
151  sqlite3_exec(db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
152  }

◆ checkError()

bool sqlUtils::checkError ( sqlite3 *  db)
staticprotected

Definition at line 331 of file sql_utils.cpp.

331  {
332  auto err = sqlite3_errcode(db);
333  if (0 != err
334  && 100 != err && 101 != err) { // probably not an error in case of the sqlite3_step (fingers crossed)
335  auto msg = sqlite3_errmsg(db);
336  std::cerr << "SQLite3 error: " << err
337  << "\n"
338  << msg << std::endl;
339  return false;
340  }
341 
342  return true;
343  }

◆ commitTrans()

static void sqlUtils::commitTrans ( sqlite3 *  db)
inlinestaticprotected

Definition at line 158 of file sql_utils.h.

158  {
159  sqlite3_exec(db, "COMMIT TRANSACTION", nullptr, nullptr, nullptr);
160  }

◆ createInsertQuery()

std::string sqlUtils::createInsertQuery ( const std::vector< const MDObject *> &  values,
const std::string &  table 
)
staticprotected

Definition at line 236 of file sql_utils.cpp.

238  {
239  std::stringstream cols;
240  std::stringstream vals;
241  const auto len = values.size();
242  for (size_t i = 0; i < len; ++i) {
243  cols << MDL::label2StrSql(values.at(i)->label);
244  vals << "?";
245  if (len != (i + 1)) {
246  cols << ", ";
247  vals << ", ";
248  }
249  }
250  std::stringstream ss;
251  ss << "INSERT INTO " << table
252  << " (" << cols.str() << ")"
253  << " VALUES "
254  << " (" << vals.str() << ");";
255  return ss.str();
256 }
static String label2StrSql(const MDLabel label)
#define i
#define len

◆ createSelectQuery() [1/2]

std::string sqlUtils::createSelectQuery ( const std::vector< MDObject > &  values,
const std::string &  table 
)
staticprotected

Create a query for selecting multiple values from a table

Definition at line 168 of file sql_utils.cpp.

170  {
171  std::stringstream cols;
172  const auto len = values.size();
173  for (size_t i = 0; i < len; ++i) {
174  cols << MDL::label2StrSql(values.at(i).label);
175  if (len != (i + 1)) {
176  cols << ", ";
177  }
178  }
179  std::stringstream ss;
180  ss << "SELECT "
181  << cols.str()
182  << " FROM " << table << ";";
183  return ss.str();
184 }
static String label2StrSql(const MDLabel label)
#define i
#define len

◆ createSelectQuery() [2/2]

std::string sqlUtils::createSelectQuery ( size_t  rowId,
const std::vector< MDObject > &  values,
const std::string &  table 
)
staticprotected

Create a query for selecting multiple values from a specific row

Definition at line 149 of file sql_utils.cpp.

151  {
152  std::stringstream cols;
153  const auto len = values.size();
154  for (size_t i = 0; i < len; ++i) {
155  cols << MDL::label2StrSql(values.at(i).label);
156  if (len != (i + 1)) {
157  cols << ", ";
158  }
159  }
160  std::stringstream ss;
161  ss << "SELECT "
162  << cols.str()
163  << " FROM " << table
164  << " WHERE objID=" << id << ";";
165  return ss.str();
166 }
static String label2StrSql(const MDLabel label)
#define i
#define len

◆ createUpdateQuery()

std::string sqlUtils::createUpdateQuery ( const std::vector< const MDObject *> &  values,
const std::string &  table,
size_t  id 
)
staticprotected

Create an update query for setting multiple values from a specific row

Definition at line 186 of file sql_utils.cpp.

189  {
190  std::stringstream cols;
191  const auto len = values.size();
192  for (size_t i = 0; i < len; ++i) {
193  cols << MDL::label2StrSql(values.at(i)->label);
194  cols << "=?";
195  if (len != (i + 1)) {
196  cols << ", ";
197  }
198  }
199  std::stringstream ss;
200  ss << "UPDATE " << table << " SET "
201  << cols.str()
202  << " WHERE objID=" << id << ";";
203  return ss.str();
204 }
static String label2StrSql(const MDLabel label)
#define i
#define len

◆ endTrans()

static void sqlUtils::endTrans ( sqlite3 *  db)
inlinestaticprotected

Definition at line 154 of file sql_utils.h.

154  {
155  sqlite3_exec(db, "END TRANSACTION", nullptr, nullptr, nullptr);
156  }

◆ insert() [1/2]

bool sqlUtils::insert ( const std::vector< std::vector< const MDObject *>> &  records,
sqlite3 *  db,
const std::string &  table 
)
static

Add multiple new rows into the table

Parameters
recordsto be added (respective columns are expected to exists in the table)
dbto be altered
tableto be altered
Returns
true on success

Definition at line 258 of file sql_utils.cpp.

259  {
260  if (0 == records.size()) {
261  return true;
262  }
263  // assuming all records are the same
264  const auto &rec = records.at(0);
265  auto query = createInsertQuery(rec, table);
266  sqlite3_stmt *stmt = nullptr;
267  sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr);
268  // FIXME currently, whole db is in one huge transaction. Finish whatever might be pending,
269  // do our business in a clean transaction and start a new transaction after (not to break the original code)
270  commitTrans(db);
271  beginTrans(db);
272 
273  const auto len = rec.size();
274  for (const auto &r : records) {
275  // bind proper values
276  for (size_t i = 0; i < len; ++i) {
277  bindValue(stmt, i + 1, *r.at(i));
278  }
279  // execute
280  sqlite3_step(stmt);
281  sqlite3_clear_bindings(stmt);
282  sqlite3_reset(stmt);
283  }
284  sqlite3_finalize(stmt);
285  endTrans(db);
286  beginTrans(db);
287 
288  return checkError(db);
289 }
static void beginTrans(sqlite3 *db)
Definition: sql_utils.h:150
static void endTrans(sqlite3 *db)
Definition: sql_utils.h:154
static void commitTrans(sqlite3 *db)
Definition: sql_utils.h:158
#define i
static bool checkError(sqlite3 *db)
Definition: sql_utils.cpp:331
#define len
static std::string createInsertQuery(const std::vector< const MDObject *> &values, const std::string &table)
Definition: sql_utils.cpp:236

◆ insert() [2/2]

bool sqlUtils::insert ( const std::vector< const MDObject *> &  values,
sqlite3 *  db,
const std::string &  table 
)
static

Add a single new row into the table

Parameters
valuesin the row to be added (respective columns are expected to exists in the table)
dbto be altered
tableto be altered
Returns
true on success

Definition at line 291 of file sql_utils.cpp.

292  {
293  return insert(std::vector<std::vector<const MDObject*>>{values}, db, table);
294 }
static bool insert(const std::vector< std::vector< const MDObject *>> &records, sqlite3 *db, const std::string &table)
Definition: sql_utils.cpp:258

◆ reportLastError()

static void sqlUtils::reportLastError ( sqlite3 *  db)
inlinestatic

Report last error registered by the db to std::cerr

Parameters
dbwhere the error occured

Definition at line 52 of file sql_utils.h.

52  {
53 
54  }

◆ select() [1/3]

bool sqlUtils::select ( size_t  rowId,
sqlite3 *  db,
const std::string &  table,
std::vector< MDObject > &  values 
)
static

Retrieve all values within a specific row

Parameters
rowIdof the row
dbto be read
tableto be read
valueswill be stored here
Returns
true on success

Definition at line 87 of file sql_utils.cpp.

89  {
90  // assuming all records are the same
91  auto query = createSelectQuery(rowId, values, table);
92 
93  // FIXME currently, whole db is in one huge transaction. Finish whatever might be pending,
94  // do our business in a clean transaction and start a new transaction after (not to break the original code)
95  sqlite3_mutex_enter(sqlite3_db_mutex(db)); // FIXME this should be only done on demand or not at all ...
96  commitTrans(db);
97  beginTrans(db);
98 
99  sqlite3_stmt *stmt = nullptr;
100  sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr);
101 
102  // execute
103  sqlite3_step(stmt);
104  for (size_t i = 0; i < values.size(); ++i) {
105  extractValue(stmt, i, values.at(i));
106  }
107 
108  sqlite3_reset(stmt);
109 
110  sqlite3_finalize(stmt);
111  endTrans(db);
112  beginTrans(db);
113  sqlite3_mutex_leave(sqlite3_db_mutex(db)); // FIXME this should be only done on demand or not at all ...
114 
115  return checkError(db);
116 }
static void beginTrans(sqlite3 *db)
Definition: sql_utils.h:150
static void endTrans(sqlite3 *db)
Definition: sql_utils.h:154
static void commitTrans(sqlite3 *db)
Definition: sql_utils.h:158
#define i
static bool checkError(sqlite3 *db)
Definition: sql_utils.cpp:331
static std::string createSelectQuery(const std::vector< MDObject > &values, const std::string &table)
Definition: sql_utils.cpp:168

◆ select() [2/3]

bool sqlUtils::select ( sqlite3 *  db,
const std::string &  table,
const std::vector< MDObject > &  columns,
std::vector< std::vector< MDObject >> &  rows 
)
static

Retrieve all rows from a table All MDObjects are expected to be in the same order

Parameters
dbto be read
tableto be read to be read
rowswill be stored here
Returns
true on success

Definition at line 118 of file sql_utils.cpp.

121  {
122  // assuming all records are the same
123  auto query = createSelectQuery(columns, table);
124  sqlite3_stmt *stmt = nullptr;
125  // FIXME currently, whole db is in one huge transaction. Finish whatever might be pending,
126  // do our business in a clean transaction and start a new transaction after (not to break the original code)
127  commitTrans(db);
128  beginTrans(db);
129  sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr);
130 
131  // execute, extract value from each row
132  while (sqlite3_step(stmt) == SQLITE_ROW) {
133  rows.emplace_back(columns);
134  auto &r = rows.back();
135  for (size_t i = 0; i < columns.size(); ++i) {
136  extractValue(stmt, i, r.at(i));
137  }
138  }
139  sqlite3_reset(stmt);
140 
141  sqlite3_finalize(stmt);
142  endTrans(db);
143  beginTrans(db);
144 
145  return checkError(db);
146 }
static void beginTrans(sqlite3 *db)
Definition: sql_utils.h:150
static void endTrans(sqlite3 *db)
Definition: sql_utils.h:154
static void commitTrans(sqlite3 *db)
Definition: sql_utils.h:158
#define i
static bool checkError(sqlite3 *db)
Definition: sql_utils.cpp:331
static std::string createSelectQuery(const std::vector< MDObject > &values, const std::string &table)
Definition: sql_utils.cpp:168

◆ select() [3/3]

template<typename T >
static bool sqlUtils::select ( const MDLabel label,
sqlite3 *  db,
const std::string &  table,
std::vector< T > &  values 
)
inlinestatic

Retrieve all values within a single column

Parameters
labelof the column
dbto be read
tableto be read
valueswill be stored here (appended to the end)
Returns
true on success

Definition at line 90 of file sql_utils.h.

92  {
93  // assuming all records are the same
94  auto query = createSelectQuery({label}, table);
95 
96  sqlite3_stmt *stmt = nullptr;
97  // FIXME currently, whole db is in one huge transaction. Finish whatever might be pending,
98  // do our business in a clean transaction and start a new transaction after (not to break the original code)
99  commitTrans(db);
100  beginTrans(db);
101  sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr);
102 
103  MDObject obj(label);
104  // execute, extract value from each row
105  while (sqlite3_step(stmt) == SQLITE_ROW) {
106  extractValue(stmt, 0, obj);
107  values.emplace_back(obj.getValue2(T()));
108  }
109 
110  sqlite3_reset(stmt);
111  sqlite3_finalize(stmt);
112  endTrans(db);
113  beginTrans(db);
114 
115  return checkError(db);
116  }
static void beginTrans(sqlite3 *db)
Definition: sql_utils.h:150
static void endTrans(sqlite3 *db)
Definition: sql_utils.h:154
static void commitTrans(sqlite3 *db)
Definition: sql_utils.h:158
static bool checkError(sqlite3 *db)
Definition: sql_utils.cpp:331
static std::string createSelectQuery(const std::vector< MDObject > &values, const std::string &table)
Definition: sql_utils.cpp:168
void(* obj)()

◆ update()

bool sqlUtils::update ( const std::vector< const MDObject *> &  values,
sqlite3 *  db,
const std::string &  table,
size_t  id 
)
static

Update a single row in the table

Parameters
valuesin the row to be updated (respective columns are expected to exists in the table)
dbto be altered
tableto be altered
idof the row
Returns
true on success

Definition at line 206 of file sql_utils.cpp.

207  {
208  if (values.empty()) {
209  return true;
210  }
211  // assuming all records are the same
212  auto query = createUpdateQuery(values, table, id);
213  sqlite3_stmt *stmt = nullptr;
214  sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr);
215  // FIXME currently, whole db is in one huge transaction. Finish whatever might be pending,
216  // do our business in a clean transaction and start a new transaction after (not to break the original code)
217  commitTrans(db);
218  beginTrans(db);
219 
220  // bind proper values
221  for (auto i = 0; i < values.size(); ++i) {
222  bindValue(stmt, i + 1, *values.at(i));
223  }
224  // execute
225  sqlite3_step(stmt);
226  sqlite3_clear_bindings(stmt);
227  sqlite3_reset(stmt);
228 
229  sqlite3_finalize(stmt);
230  endTrans(db);
231  beginTrans(db);
232 
233  return checkError(db);
234 }
static void beginTrans(sqlite3 *db)
Definition: sql_utils.h:150
static void endTrans(sqlite3 *db)
Definition: sql_utils.h:154
static void commitTrans(sqlite3 *db)
Definition: sql_utils.h:158
#define i
static bool checkError(sqlite3 *db)
Definition: sql_utils.cpp:331
static std::string createUpdateQuery(const std::vector< const MDObject *> &values, const std::string &table, size_t id)
Definition: sql_utils.cpp:186

The documentation for this class was generated from the following files: