29 #include "../xmipp_error.h" 32 sqlite3 *db,
const std::string &table) {
34 auto query =
"ALTER TABLE " + table +
" ADD COLUMN ";
40 for (
auto c : columns) {
42 sqlite3_exec(db, stmt.c_str(),
nullptr,
nullptr,
nullptr);
50 void sqlUtils::extractValue(sqlite3_stmt *stmt,
const int position,
MDObject &valueOut)
52 switch (valueOut.
type)
55 valueOut.
data.
boolValue = sqlite3_column_int(stmt, position) == 1;
58 valueOut.
data.
intValue = sqlite3_column_int(stmt, position);
69 ss << sqlite3_column_text(stmt, position);
78 ss << sqlite3_column_text(stmt, position);
88 sqlite3 *db,
const std::string &table,
89 std::vector<MDObject> &values) {
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));
119 sqlite3 *db,
const std::string &table,
120 const std::vector<MDObject> &columns,
121 std::vector<std::vector<MDObject>> &rows) {
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);
150 const std::vector<MDObject> &values,
151 const std::string &table) {
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 <<
";";
169 const std::vector<MDObject> &values,
170 const std::string &table) {
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 <<
";";
187 const std::vector<const MDObject*> &values,
188 const std::string &table,
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 <<
";";
207 sqlite3 *db,
const std::string &table,
size_t id) {
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);
237 const std::vector<const MDObject*> &values,
238 const std::string &table) {
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() <<
");";
259 sqlite3 *db,
const std::string &table) {
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);
292 sqlite3 *db,
const std::string &table) {
293 return insert(std::vector<std::vector<const MDObject*>>{values}, db, table);
296 int sqlUtils::bindValue(sqlite3_stmt *stmt,
const int position,
const MDObject &valueIn)
304 std::cerr <<
"WARNING!!! valueIn.failed = True, binding NULL" << std::endl;
306 return sqlite3_bind_null(stmt, position);
310 switch (valueIn.
type)
313 return sqlite3_bind_int(stmt, position, valueIn.
data.
boolValue ? 1 : 0);
315 return sqlite3_bind_int(stmt, position, valueIn.
data.
intValue);
321 return sqlite3_bind_text(stmt, position, valueIn.
data.
stringValue->c_str(), -1, SQLITE_TRANSIENT);
324 return sqlite3_bind_text(stmt, position, valueIn.
toString(
false,
true).c_str(), -1, SQLITE_TRANSIENT);
332 auto err = sqlite3_errcode(db);
334 && 100 != err && 101 != err) {
335 auto msg = sqlite3_errmsg(db);
336 std::cerr <<
"SQLite3 error: " << err
static void beginTrans(sqlite3 *db)
static bool insert(const std::vector< std::vector< const MDObject *>> &records, sqlite3 *db, const std::string &table)
#define REPORT_ERROR(nerr, ErrormMsg)
static void endTrans(sqlite3 *db)
static void commitTrans(sqlite3 *db)
static String label2StrSql(const MDLabel label)
bool fromStream(std::istream &is, bool fromString=false)
static bool update(const std::vector< const MDObject *> &values, sqlite3 *db, const std::string &table, size_t id)
Incorrect argument received.
static bool checkError(sqlite3 *db)
static std::string createUpdateQuery(const std::vector< const MDObject *> &values, const std::string &table, size_t id)
String toString(bool withFormat=false, bool isSql=false) const
static std::string createSelectQuery(const std::vector< MDObject > &values, const std::string &table)
static String label2SqlColumn(const MDLabel label)
static bool select(size_t rowId, sqlite3 *db, const std::string &table, std::vector< MDObject > &values)
static std::string createInsertQuery(const std::vector< const MDObject *> &values, const std::string &table)
static bool addColumns(const std::vector< MDLabel > &columns, sqlite3 *db, const std::string &table)