void SQLiteGateWriterImpl::writeMatrixToDB(std::string label, MatrixPtr pMatrix) { std::string matrixValStr = getMatrixValueString(pMatrix); int rc = sqlite3_bind_text(m_pStatement, 1, label.c_str(), -1, SQLITE_STATIC); if(rc != SQLITE_OK) { fprintf(stderr, "Error 1 %s due to %s\n", sqlite3_errmsg(m_pDb), sqlite3_errstr(rc)); throw (rc); } rc = sqlite3_bind_text(m_pStatement, 2, matrixValStr.c_str(), -1, SQLITE_STATIC); if(rc != SQLITE_OK) { fprintf(stderr, "Error 2 %s due to %s\n", sqlite3_errmsg(m_pDb), sqlite3_errstr(rc)); throw (rc); } int result = sqlite3_step(m_pStatement); //Don't consider violate constrainst such as unique label a serious error //to forgive attempts add duplicated matrix if(result != SQLITE_DONE && result != SQLITE_CONSTRAINT) { fprintf(stderr, "Error 3 %s due to %s\n", sqlite3_errmsg(m_pDb), sqlite3_errstr(result)); throw (result); } sqlite3_reset(m_pStatement); }
bool Database::Open(const std::string &Path) { if (Inner != nullptr) Close(); int Err; if ((Err = sqlite3_initialize()) != SQLITE_OK) return LogError(sqlite3_errstr(Err), false); if (Err = sqlite3_open_v2(Path.c_str(), &Inner, SQLITE_OPEN_READWRITE, nullptr) != SQLITE_OK) { if (Err = sqlite3_open_v2(Path.c_str(), &Inner, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr) != SQLITE_OK) return LogError(sqlite3_errstr(Err), false); std::vector<std::string> TableStrings; TableStrings.push_back(User::CreationString); TableStrings.push_back(Server::CreationString); TableStrings.push_back(Chatroom::CreationString); for (unsigned int x = 0; x < TableStrings.size(); x++) { Statement s; if (!s.Prepare(Inner, TableStrings[x])) return false; if (!s.Execute()) return false; } } return true; }
static int prepare_statements(void) { int ret; #define FIND_TOP_HASHES \ "select digest, count(digest) from hashes group by digest having (count(digest) > 1) order by (count(digest)) desc;" ret = sqlite3_prepare_v2(gdb, FIND_TOP_HASHES, -1, &top_hashes_stmt, NULL); if (ret) { fprintf(stderr, "error %d while prepping hash search stmt: %s\n", ret, sqlite3_errstr(ret)); return ret; } #define FIND_FILES_COUNT \ "select count (distinct files.filename) from files INNER JOIN hashes on hashes.digest = ?1 AND files.subvol=hashes.subvol AND files.ino=hashes.ino;" ret = sqlite3_prepare_v2(gdb, FIND_FILES_COUNT, -1, &files_count_stmt, NULL); if (ret) { fprintf(stderr, "error %d while preparing file count stmt: %s\n", ret, sqlite3_errstr(ret)); return ret; } #define FIND_BLOCKS \ "select files.filename, hashes.loff, hashes.flags from files INNER JOIN hashes on hashes.digest = ?1 AND files.subvol=hashes.subvol AND files.ino=hashes.ino;" ret = sqlite3_prepare_v2(gdb, FIND_BLOCKS, -1, &find_blocks_stmt, NULL); if (ret) { fprintf(stderr, "error %d while prepping find blocks stmt: %s\n", ret, sqlite3_errstr(ret)); return ret; } return 0; }
void SqliteConnection::open() { int code = sqlite3_open(fileName_.c_str(), &handle_); if (code != SQLITE_OK) { SERVICE_LOG_LVL(ERROR, "SqliteConnection::open: cannot connect to file: " << fileName_ << ". Error code: " << code << " Description: " << sqlite3_errstr(code)); throw SqliteConnectionException(sqlite3_errstr(code)); } connected_ = true; }
void SqliteConnection::close() { if (handle_) { for (auto callback: onCloseCallbacks_) callback(this); int code = sqlite3_close(handle_); handle_ = nullptr; connected_ = false; if (code != SQLITE_OK) { SERVICE_LOG_LVL(ERROR, "SqliteConnection::close: cannot close db file: " << fileName_ << ". Error code: " << code << " Description: " << sqlite3_errstr(code)); throw SqliteConnectionException(sqlite3_errstr(code)); } } }
/** * @brief DB_Create * * @return */ int DB_Create() { int result; if (db_mode->integer == 1) { //result = sqlite3_open(":memory:", &db); // memory table, not shared see https://www.sqlite.org/inmemorydb.html result = sqlite3_open("file::memory:?cache=shared", &db); // In-memory databases with shared cache if (result != SQLITE_OK) { Com_Printf("... failed to create memory database - error: %s\n", sqlite3_errstr(result)); (void) sqlite3_close(db); return 1; } } else if (db_mode->integer == 2) { char *to_ospath; to_ospath = FS_BuildOSPath(Cvar_VariableString("fs_homepath"), db_url->string, ""); // FIXME: check for empty db_url to_ospath[strlen(to_ospath) - 1] = '\0'; result = sqlite3_open(to_ospath, &db); if (result != SQLITE_OK) { Com_Printf("... failed to create file database - error: %s\n", sqlite3_errstr(result)); (void) sqlite3_close(db); return 1; } } else { Com_Printf("... DB_Create failed - unknown mode\n"); return 1; } result = DB_Create_Schema(); if (result != 0) { Com_Printf("... failed to create database schema\n"); (void) sqlite3_close(db); return 1; } Com_Printf("... database %s created\n", db_url->string); return 0; }
extern void new_word(char *w, int slideid, char origin, short kw) { int code = SQLITE_DONE; if (w) { (void)sqlite3_reset(G_ins_word); if ((sqlite3_bind_text(G_ins_word, 1, w, -1, NULL) == SQLITE_OK) && (sqlite3_bind_int(G_ins_word, 2, slideid) == SQLITE_OK) && (sqlite3_bind_int(G_ins_word, 3, (int)origin) == SQLITE_OK) && (sqlite3_bind_int(G_ins_word, 4, (int)kw) == SQLITE_OK) && ((code = sqlite3_step(G_ins_word)) == SQLITE_DONE)) { return; } (void)sqlite3_reset(G_upd_word); if ((sqlite3_bind_text(G_upd_word, 1, w, -1, NULL) == SQLITE_OK) && (sqlite3_bind_int(G_upd_word, 2, slideid) == SQLITE_OK) && ((code = sqlite3_step(G_upd_word)) == SQLITE_DONE)) { return; } fprintf(stderr, "update word: %s (%d/code %d - %s)\n", (char *)sqlite3_errmsg(G_db), sqlite3_errcode(G_db), code, sqlite3_errstr(code)); } }
std::string Database::errorDescription(int code) const { if (!isOpening()) { return sqlite3_errstr(code); } return sqlite3_errmsg(_db); }
bool Statement::Reset() { int Err = sqlite3_reset(Inner); if (Err != SQLITE_OK) { LogError("Failed to reset statement."); return LogError(sqlite3_errstr(Err), false); } Err = sqlite3_clear_bindings(Inner); if (Err != SQLITE_OK) { LogError("Failed to clear bindings."); return LogError(sqlite3_errstr(Err), false); } return true; }
// Execute a one-step query with no expected result int Statement::exec() { if (false == mbDone) { const int ret = sqlite3_step(mStmtPtr); if (SQLITE_DONE == ret) // the statement has finished executing successfully { mbOk = false; mbDone = true; } else if (SQLITE_ROW == ret) { mbOk = false; mbDone = false; throw SQLite::Exception("exec() does not expect results. Use executeStep."); } else { mbOk = false; mbDone = false; throw SQLite::Exception(sqlite3_errstr(ret)); } } else { throw SQLite::Exception("Statement need to be reseted."); } // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE) return sqlite3_changes(mStmtPtr); }
vmod_exec(const struct vrt_ctx *ctx, struct vmod_priv *priv, VCL_STRING sql) { struct vmod_sqlite3_result r; struct vmod_sqlite3 *v; char *e, *p; CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); AN(priv); if (!priv->priv) { e = (char *)sqlite3_errstr(SQLITE_ERROR); p = WS_Copy(ctx->ws, e, -1); return (p); } CAST_OBJ_NOTNULL(v, priv->priv, VMOD_SQLITE3_MAGIC); r.u = WS_Reserve(ctx->ws, 0); r.p = ctx->ws->f; r.d = v->d; r.l = 0; r.r = 0; *(r.p) = '\0'; if (sqlite3_exec(v->db, sql, vmod_exec_cb, &r, &e) != SQLITE_OK) { WS_Release(ctx->ws, 0); p = WS_Copy(ctx->ws, e, -1); sqlite3_free(e); return (p); } WS_Release(ctx->ws, r.l + 1); return (r.p); }
// Execute a step of the query to fetch one row of results bool Statement::executeStep() { if (false == mbDone) { const int ret = sqlite3_step(mStmtPtr); if (SQLITE_ROW == ret) // one row is ready : call getColumn(N) to access it { mbOk = true; } else if (SQLITE_DONE == ret) // no (more) row ready : the query has finished executing { mbOk = false; mbDone = true; } else { mbOk = false; mbDone = false; throw SQLite::Exception(sqlite3_errstr(ret)); } } else { throw SQLite::Exception("Statement needs to be reseted."); } return mbOk; // true only if one row is accessible by getColumn(N) }
extern void new_word_as_is(char *w, int slideid, char origin, short kw) { int code = SQLITE_DONE; char *t; if (w && ((t = cleanup(w)) != (char *)NULL)) { (void)sqlite3_reset(G_ins_word_as_is); if ((sqlite3_bind_text(G_ins_word_as_is, 1, t, -1, NULL) == SQLITE_OK) && (sqlite3_bind_int(G_ins_word_as_is, 2, slideid) == SQLITE_OK) && (sqlite3_bind_int(G_ins_word_as_is, 3, (int)origin) == SQLITE_OK) && (sqlite3_bind_int(G_ins_word_as_is, 4, (int)kw) == SQLITE_OK) && ((code = sqlite3_step(G_ins_word_as_is)) == SQLITE_DONE)) { return; } (void)sqlite3_reset(G_upd_word_as_is); if ((sqlite3_bind_text(G_upd_word_as_is, 1, t, -1, NULL) == SQLITE_OK) && (sqlite3_bind_int(G_upd_word_as_is, 2, slideid) == SQLITE_OK) && ((code = sqlite3_step(G_upd_word_as_is)) == SQLITE_DONE)) { return; } if (t) { free(t); } fprintf(stderr, "update word: %s (%d/code %d - %s)\n", (char *)sqlite3_errmsg(G_db), sqlite3_errcode(G_db), code, sqlite3_errstr(code)); } }
int sqliterkCryptoDecode(sqliterk_codec *codec, int pgno, void *data) { int rc; int offset = 0; unsigned char *pdata = (unsigned char *) data; int page_sz = sqlcipher_codec_ctx_get_pagesize(codec); unsigned char *buffer = (unsigned char *) sqlcipher_codec_ctx_get_data(codec); rc = sqlcipher_codec_key_derive(codec); if (rc != SQLITE_OK) return rc; if (pgno == 1) { offset = 16; // FILE_HEADER_SZ memcpy(buffer, "SQLite format 3", 16); } rc = sqlcipher_page_cipher(codec, CIPHER_READ_CTX, pgno, CIPHER_DECRYPT, page_sz - offset, pdata + offset, buffer + offset); if (rc != SQLITE_OK) goto bail; memcpy(pdata, buffer, page_sz); return SQLITERK_OK; bail: sqliterkOSError(SQLITERK_DAMAGED, "Failed to decode page %d: %s", pgno, sqlite3_errstr(rc)); return rc; }
std::string err_str_(int rc) { #if SQLITE_VERSION_NUMBER < 3007015 return std::to_string(rc); #else return sqlite3_errstr(rc); #endif }
Cache::~Cache() { if (db) { int retVal = sqlite3_close_v2(db); if( SQLITE_OK != retVal ) { std::cerr << "Oops: failed to properly close database: " << sqlite3_errstr(retVal) << std::endl; } } }
BindException::BindException(sqlite3_stmt* statement, const std::string& parameter, int resultCode) { const auto errorString = sqlite3_errstr(resultCode); const auto query = sqlite3_sql(statement); std::stringstream what; what << errorString << " (" << resultCode << ") for parameter \"" << parameter << "\" of query \"" << query << "\""; m_what = what.str(); }
SqliteException::SqliteException(const std::string &func, int resultCode) : Exception( std::string("[") + func + "] " + resultToResultName(resultCode) + " (" + sqlite3_errstr(resultCode) + ")" ) { }
void object::close () { if (context) { error = sqlite3_close_v2 (context); if (error != SQLITE_OK) LOG_ERROR("database close error (%s)", sqlite3_errstr (error)); } }
// Execute backup step with a given number of source pages to be copied int Backup::executeStep(const int aNumPage /* = -1 */) { const int res = sqlite3_backup_step(mpSQLiteBackup, aNumPage); if (SQLITE_OK != res && SQLITE_DONE != res && SQLITE_BUSY != res && SQLITE_LOCKED != res) { throw SQLite::Exception(sqlite3_errstr(res), res); } return res; }
bool Cache::prepSqlite(const std::string &query) { int retVal = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, NULL); if(SQLITE_OK != retVal) { std::cerr << "Failed to prepare statement: " << query << " - Message:" << sqlite3_errstr(retVal) << std::endl; return false; } return true; }
static void db_statement_finalize(sqlite3_stmt *ppstmt) { int retval = sqlite3_finalize(ppstmt); if (SQLITE_OK != retval) { printlog("ERROR: finalizing sql statement: %s", sqlite3_errstr(retval)); qexit(EXIT_FAILURE); } }
bool Cache::finalizeSqlite() { int retVal = sqlite3_finalize(stmt); if(SQLITE_OK != retVal) { std::cerr << "Unable to finalize: " << sqlite3_errstr(retVal) << std::endl; return false; } return true; }
bool Cache::initSqlite() { int retVal = sqlite3_initialize(); if(SQLITE_OK != retVal) { std::cerr << "Can't initialize sqlite3: " << sqlite3_errstr(retVal) << std::endl; return false; } return true; }
bool Cache::stepSqlite(const std::string &errMsg) { int retVal = sqlite3_step(stmt); if(SQLITE_DONE != retVal) { std::cerr << errMsg << ": " << sqlite3_errstr(retVal) << std::endl; return false; } return true; }
int main( int argc, char *argv[] ) { /* open the database */ int result = sqlite3_open( dbname, &db ); int maxid; if ( result != SQLITE_OK ) { printf( "Failed to open database %s\n\r", sqlite3_errstr( result ) ); sqlite3_close( db ); return 1; } printf( "Opened db %s OK\n\r", dbname ); maxid = getmaxidevent( ); /* prepare the insert sql, leave stmt ready for loop */ result = sqlite3_prepare_v2( db, insert_sql, strlen( insert_sql ) + 1, &stmt, NULL ); if ( result != SQLITE_OK ) { printf( "Failed to prepare database %s\n\r", sqlite3_errstr( result ) ); sqlite3_close( db ); return 2; } printf( "SQL prepared ok\n\r" ); /* bind parameters */ sqlite3_bind_int( stmt, 1, maxid ); /* best done by autoincreement */ sqlite3_bind_int( stmt, 2, 41456 ); /* date */ sqlite3_bind_int( stmt, 3, 4 ); /* idvenue 4 = Echo */ sqlite3_bind_int( stmt, 4, 1 ); /* ideventtype - disco */ sqlite3_bind_text( stmt, 5, description, strlen( description ), SQLITE_STATIC ); printf( "Before - maxid = %d\n\r", maxid ); /* insert row by running one stmt */ result = sqlite3_step( stmt ); maxid = getmaxidevent( ); printf( "After - maxid = %d\n\r", maxid ); /* finish off */ sqlite3_close( db ); return 0; }
bool index_insert_entry( fp_data& fpd, UINT32 line, UINT32 column_start, const char *scope, id_type type, id_sub_type sub_type, const char *identifier) { bool retval = true; int result; sqlite3_stmt *stmt_insert_entry = cpd.stmt_insert_reference; if (sub_type == IST_DEFINITION) stmt_insert_entry = cpd.stmt_insert_definition; else if (sub_type == IST_DECLARATION) stmt_insert_entry = cpd.stmt_insert_declaration; result = sqlite3_bind_int64(stmt_insert_entry, 2, line); result |= sqlite3_bind_int64(stmt_insert_entry, 3, column_start); result |= sqlite3_bind_text(stmt_insert_entry, 4, scope, -1, SQLITE_STATIC); result |= sqlite3_bind_int(stmt_insert_entry, 5, (int) type); result |= sqlite3_bind_text(stmt_insert_entry, 6, identifier, -1, SQLITE_STATIC); if (result == SQLITE_OK) { result = sqlite3_step(stmt_insert_entry); if (result == SQLITE_DONE) { result = sqlite3_reset(stmt_insert_entry); } } if (result != SQLITE_OK) { const char *errstr = sqlite3_errstr(result); LOG_FMT(LERR, "index_insert_entry: access error (%d: %s)\n", result, errstr != NULL ? errstr : ""); retval = false; } return retval; }
void save_from_memory_to_file(const String &fn, sqlite3 *db) { auto ret = loadOrSaveDb(db, fn.c_str(), 1); if (ret != SQLITE_OK) { String error = "Can't save database: " + fn + " error: " + sqlite3_errstr(ret); sqlite3_close(db); throw std::runtime_error(error); } }
bool Statement::Execute() { int Err = sqlite3_step(Inner); if (Err != SQLITE_DONE) { Finalise(); return LogError(sqlite3_errstr(Err), false); } return true; }
bool Statement::Finalise() { int Err = sqlite3_finalize(Inner); if (Err != SQLITE_OK) { LogError("Failed to finalise statement."); return LogError(sqlite3_errstr(Err), false); } Inner = nullptr; return true; }