#include <sql_utils.h>
|
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) |
|
Definition at line 34 of file sql_utils.h.
◆ 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
-
columns | to be added |
db | to be altered |
table | to be altered |
- Returns
- true on success
Definition at line 31 of file sql_utils.cpp.
34 auto query =
"ALTER TABLE " + table +
" ADD COLUMN ";
40 for (
auto c : columns) {
42 sqlite3_exec(db, stmt.c_str(),
nullptr,
nullptr,
nullptr);
static void beginTrans(sqlite3 *db)
static void endTrans(sqlite3 *db)
static void commitTrans(sqlite3 *db)
static bool checkError(sqlite3 *db)
static String label2SqlColumn(const MDLabel label)
◆ beginTrans()
static void sqlUtils::beginTrans |
( |
sqlite3 * |
db | ) |
|
|
inlinestaticprotected |
Definition at line 150 of file sql_utils.h.
151 sqlite3_exec(db,
"BEGIN TRANSACTION",
nullptr,
nullptr,
nullptr);
◆ checkError()
bool sqlUtils::checkError |
( |
sqlite3 * |
db | ) |
|
|
staticprotected |
Definition at line 331 of file sql_utils.cpp.
332 auto err = sqlite3_errcode(db);
334 && 100 != err && 101 != err) {
335 auto msg = sqlite3_errmsg(db);
336 std::cerr <<
"SQLite3 error: " << err
◆ commitTrans()
static void sqlUtils::commitTrans |
( |
sqlite3 * |
db | ) |
|
|
inlinestaticprotected |
Definition at line 158 of file sql_utils.h.
159 sqlite3_exec(db,
"COMMIT TRANSACTION",
nullptr,
nullptr,
nullptr);
◆ 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.
239 std::stringstream cols;
240 std::stringstream vals;
241 const auto len = values.size();
242 for (
size_t i = 0;
i <
len; ++
i) {
245 if (len != (
i + 1)) {
250 std::stringstream ss;
251 ss <<
"INSERT INTO " << table
252 <<
" (" << cols.str() <<
")" 254 <<
" (" << vals.str() <<
");";
static String label2StrSql(const MDLabel label)
◆ 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.
171 std::stringstream cols;
172 const auto len = values.size();
173 for (
size_t i = 0;
i <
len; ++
i) {
175 if (len != (
i + 1)) {
179 std::stringstream ss;
182 <<
" FROM " << table <<
";";
static String label2StrSql(const MDLabel label)
◆ 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.
152 std::stringstream cols;
153 const auto len = values.size();
154 for (
size_t i = 0;
i <
len; ++
i) {
156 if (len != (
i + 1)) {
160 std::stringstream ss;
164 <<
" WHERE objID=" <<
id <<
";";
static String label2StrSql(const MDLabel label)
◆ 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.
190 std::stringstream cols;
191 const auto len = values.size();
192 for (
size_t i = 0;
i <
len; ++
i) {
195 if (len != (
i + 1)) {
199 std::stringstream ss;
200 ss <<
"UPDATE " << table <<
" SET " 202 <<
" WHERE objID=" <<
id <<
";";
static String label2StrSql(const MDLabel label)
◆ endTrans()
static void sqlUtils::endTrans |
( |
sqlite3 * |
db | ) |
|
|
inlinestaticprotected |
Definition at line 154 of file sql_utils.h.
155 sqlite3_exec(db,
"END TRANSACTION",
nullptr,
nullptr,
nullptr);
◆ 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
-
records | to be added (respective columns are expected to exists in the table) |
db | to be altered |
table | to be altered |
- Returns
- true on success
Definition at line 258 of file sql_utils.cpp.
260 if (0 == records.size()) {
264 const auto &rec = records.at(0);
266 sqlite3_stmt *stmt =
nullptr;
267 sqlite3_prepare_v2(db, query.c_str(), -1, &stmt,
nullptr);
273 const auto len = rec.size();
274 for (
const auto &r : records) {
276 for (
size_t i = 0;
i <
len; ++
i) {
277 bindValue(stmt,
i + 1, *r.at(
i));
281 sqlite3_clear_bindings(stmt);
284 sqlite3_finalize(stmt);
static void beginTrans(sqlite3 *db)
static void endTrans(sqlite3 *db)
static void commitTrans(sqlite3 *db)
static bool checkError(sqlite3 *db)
static std::string createInsertQuery(const std::vector< const MDObject *> &values, const std::string &table)
◆ 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
-
values | in the row to be added (respective columns are expected to exists in the table) |
db | to be altered |
table | to be altered |
- Returns
- true on success
Definition at line 291 of file sql_utils.cpp.
293 return insert(std::vector<std::vector<const MDObject*>>{values}, db, table);
static bool insert(const std::vector< std::vector< const MDObject *>> &records, sqlite3 *db, const std::string &table)
◆ reportLastError()
static void sqlUtils::reportLastError |
( |
sqlite3 * |
db | ) |
|
|
inlinestatic |
Report last error registered by the db to std::cerr
- Parameters
-
db | where the error occured |
Definition at line 52 of file sql_utils.h.
◆ 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
-
rowId | of the row |
db | to be read |
table | to be read |
values | will be stored here |
- Returns
- true on success
Definition at line 87 of file sql_utils.cpp.
95 sqlite3_mutex_enter(sqlite3_db_mutex(db));
99 sqlite3_stmt *stmt =
nullptr;
100 sqlite3_prepare_v2(db, query.c_str(), -1, &stmt,
nullptr);
104 for (
size_t i = 0;
i < values.size(); ++
i) {
105 extractValue(stmt,
i, values.at(
i));
110 sqlite3_finalize(stmt);
113 sqlite3_mutex_leave(sqlite3_db_mutex(db));
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)
◆ 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
-
db | to be read |
table | to be read to be read |
rows | will be stored here |
- Returns
- true on success
Definition at line 118 of file sql_utils.cpp.
124 sqlite3_stmt *stmt =
nullptr;
129 sqlite3_prepare_v2(db, query.c_str(), -1, &stmt,
nullptr);
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));
141 sqlite3_finalize(stmt);
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)
◆ 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
-
label | of the column |
db | to be read |
table | to be read |
values | will be stored here (appended to the end) |
- Returns
- true on success
Definition at line 90 of file sql_utils.h.
96 sqlite3_stmt *stmt =
nullptr;
101 sqlite3_prepare_v2(db, query.c_str(), -1, &stmt,
nullptr);
105 while (sqlite3_step(stmt) == SQLITE_ROW) {
106 extractValue(stmt, 0,
obj);
107 values.emplace_back(
obj.getValue2(T()));
111 sqlite3_finalize(stmt);
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)
◆ 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
-
values | in the row to be updated (respective columns are expected to exists in the table) |
db | to be altered |
table | to be altered |
id | of the row |
- Returns
- true on success
Definition at line 206 of file sql_utils.cpp.
208 if (values.empty()) {
213 sqlite3_stmt *stmt =
nullptr;
214 sqlite3_prepare_v2(db, query.c_str(), -1, &stmt,
nullptr);
221 for (
auto i = 0;
i < values.size(); ++
i) {
222 bindValue(stmt,
i + 1, *values.at(
i));
226 sqlite3_clear_bindings(stmt);
229 sqlite3_finalize(stmt);
static void beginTrans(sqlite3 *db)
static void endTrans(sqlite3 *db)
static void commitTrans(sqlite3 *db)
static bool checkError(sqlite3 *db)
static std::string createUpdateQuery(const std::vector< const MDObject *> &values, const std::string &table, size_t id)
The documentation for this class was generated from the following files: