TransactionResult::Enum RunCommitTransaction() { TransactionResult::Enum tret; for (TouchedFilesList::iterator p = TouchedFiles.begin(), end = TouchedFiles.end(); p != end; ++p) { tret = MarkAsProcessed(*p); if (tret != TransactionResult::COMMIT) { return tret; } } Statement stmt; tret = stmt.TxnPrepare (*DB, "INSERT INTO files (path, mtime, size) VALUES (?, ?, ?)"); if (tret != TransactionResult::COMMIT) { return tret; } for (FTableMap::const_iterator p = FTable.begin(), end = FTable.end(); p != end; ++p) { const FileIdentification &FI(p->second->Ident); if (p->first != FI.Path) { // This is a duplicate, secondary entry. Avoid // shadowing the real entry. continue; } sqlite3_reset(stmt.Ptr); sqlite3_bind_text(stmt.Ptr, 1, FI.Path.data(), FI.Path.size(), SQLITE_TRANSIENT); sqlite3_bind_int64(stmt.Ptr, 2, FI.Mtime); sqlite3_bind_int64(stmt.Ptr, 3, FI.Size); if (sqlite3_step(stmt.Ptr) != SQLITE_DONE) { return DB->SetTransactionError(sqlite3_sql(stmt.Ptr)); } p->second->ID = sqlite3_last_insert_rowid(DB->Ptr); } tret = stmt.TxnPrepare (*DB, "INSERT INTO reports (file, line, column, tool, message) " "VALUES (?, ?, ?, ?, ?);"); if (tret != TransactionResult::COMMIT) { return tret; } for (std::vector<Report>::const_iterator p = Reports.begin(), end = Reports.end(); p != end; ++p) { sqlite3_reset(stmt.Ptr); sqlite3_bind_int64(stmt.Ptr, 1, p->FI->ID); sqlite3_bind_int64(stmt.Ptr, 2, p->Line); sqlite3_bind_int64(stmt.Ptr, 3, p->Column); sqlite3_bind_text(stmt.Ptr, 4, p->Tool.data(), p->Tool.size(), SQLITE_TRANSIENT); sqlite3_bind_text(stmt.Ptr, 5, p->Message.data(), p->Message.size(), SQLITE_TRANSIENT); if (sqlite3_step(stmt.Ptr) != SQLITE_DONE) { return DB->SetTransactionError(sqlite3_sql(stmt.Ptr)); } } return TransactionResult::COMMIT; }
KDbSqlResult* SqlitePreparedStatement::execute( KDbPreparedStatement::Type type, const KDbField::List& selectFieldList, KDbFieldList* insertFieldList, const KDbPreparedStatementParameters& parameters, bool *resultOwned) { Q_UNUSED(insertFieldList); Q_UNUSED(resultOwned); if (!m_sqlResult->prepared_st) { return nullptr; } int par = 1; // par.index counted from 1 KDbField::ListIterator itFields(selectFieldList.constBegin()); for (QList<QVariant>::ConstIterator it = parameters.constBegin(); itFields != selectFieldList.constEnd(); it += (it == parameters.constEnd() ? 0 : 1), ++itFields, par++) { if (!bindValue(*itFields, it == parameters.constEnd() ? QVariant() : *it, par)) return nullptr; } //real execution const int res = sqlite3_step(m_sqlResult->prepared_st); if (type == KDbPreparedStatement::InsertStatement) { const bool ok = res == SQLITE_DONE; if (ok) { m_result = KDbResult(); } else { m_result.setServerErrorCode(res); storeResult(&m_result); sqliteWarning() << m_result << QString::fromLatin1(sqlite3_sql(m_sqlResult->prepared_st)); } (void)sqlite3_reset(m_sqlResult->prepared_st); return m_sqlResult.data(); } else if (type == KDbPreparedStatement::SelectStatement) { //! @todo fetch result const bool ok = res == SQLITE_ROW; storeResult(&m_result); if (ok) { m_result = KDbResult(); } else { m_result.setServerErrorCode(res); storeResult(&m_result); sqliteWarning() << m_result << QString::fromLatin1(sqlite3_sql(m_sqlResult->prepared_st)); } (void)sqlite3_reset(m_sqlResult->prepared_st); return m_sqlResult.data(); } return nullptr; }
/* ** Rerun the compilation of a statement after a schema change. ** ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, ** if the statement cannot be recompiled because another connection has ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error ** occurs, return SQLITE_SCHEMA. */ int sqlite3Reprepare(Vdbe *p){ int rc; sqlite3_stmt *pNew; const char *zSql; sqlite3 *db; assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); zSql = sqlite3_sql((sqlite3_stmt *)p); assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ db = sqlite3VdbeDb(p); assert( sqlite3_mutex_held(db->mutex) ); rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0); if( rc ){ if( rc==SQLITE_NOMEM ){ db->mallocFailed = 1; } assert( pNew==0 ); return (rc==SQLITE_LOCKED) ? SQLITE_LOCKED : SQLITE_SCHEMA; }else{ assert( pNew!=0 ); } sqlite3VdbeSwap((Vdbe*)pNew, p); sqlite3TransferBindings(pNew, (sqlite3_stmt*)p); sqlite3VdbeResetStepResult((Vdbe*)pNew); sqlite3VdbeFinalize((Vdbe*)pNew); return SQLITE_OK; }
TransactionResult::Enum MarkAsProcessed(const std::string &Path) { std::string Absolute; if (!ResolvePath(Path.c_str(), Absolute)) { DB->ErrorMessage = "could not find file on disk: "; DB->ErrorMessage += Path; return TransactionResult::ERROR; } Statement SQL; TransactionResult::Enum tret = SQL.TxnPrepare(*DB, "SELECT id FROM files " "WHERE path = ? ORDER BY id DESC LIMIT 1"); if (tret != TransactionResult::COMMIT) { return tret; } sqlite3_bind_text(SQL.Ptr, 1, Absolute.data(), Absolute.size(), SQLITE_TRANSIENT); int ret = sqlite3_step(SQL.Ptr); if (ret == SQLITE_DONE) { // File is not in the database. No need to mask older errors. return TransactionResult::COMMIT; } if (ret != SQLITE_ROW) { return DB->SetTransactionError(sqlite3_sql(SQL.Ptr)); } // Add an entry for the file, hiding the previous reports. // TODO: Only hide changed files? What about plugin changes? if (Resolve(Path) == NULL) { return TransactionResult::ERROR; } return TransactionResult::COMMIT; }
char * dbt2_sql_getvalue(struct db_context_t *dbc, struct sql_result_t * sql_result, int field) { char * tmp; tmp= NULL; if (sql_result->query_running && field < sql_result->num_fields) { tmp = strndup(sqlite3_column_text(sql_result->pStmt, field), sqlite3_column_bytes(sql_result->pStmt, field)); if (!tmp) { #ifdef DEBUG_QUERY LOG_ERROR_MESSAGE("dbt2_sql_getvalue: var[%d]=NULL\n", field); #endif } } else { #ifdef DEBUG_QUERY LOG_ERROR_MESSAGE("dbt2_sql_getvalue: POSSIBLE NULL VALUE or ERROR\nQuery: %s\nField: %d from %d", sqlite3_sql(sql_result->pStmt), field, sql_result->num_fields); #endif } return tmp; }
inline CFStringRef SQLite3StatementCreateSQLString(SQLite3StatementRef statement) { CFStringRef sql = NULL; if (statement) { sql = CFStringCreateWithCString(statement->allocator, sqlite3_sql(statement->stmt), kCFStringEncodingUTF8); } return sql; }
bool Statement::Step() { #if ORTHANC_SQLITE_STANDALONE != 1 VLOG(1) << "SQLite::Statement::Step " << sqlite3_sql(GetStatement()); #endif return CheckError(sqlite3_step(GetStatement()), ErrorCode_SQLiteCannotStep) == SQLITE_ROW; }
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(); }
bool Statement::Run() { #if ORTHANC_SQLITE_STANDALONE != 1 VLOG(1) << "SQLite::Statement::Run " << sqlite3_sql(GetStatement()); #endif return CheckError(sqlite3_step(GetStatement())) == SQLITE_DONE; }
void Handle::setupTrace() { unsigned flag = 0; if (m_sqlTrace) { flag |= SQLITE_TRACE_STMT; } if (m_performanceTrace) { flag |= SQLITE_TRACE_PROFILE; } if (flag > 0) { sqlite3_trace_v2( (sqlite3 *) m_handle, flag, [](unsigned int flag, void *M, void *P, void *X) -> int { Handle *handle = (Handle *) M; sqlite3_stmt *stmt = (sqlite3_stmt *) P; switch (flag) { case SQLITE_TRACE_STMT: { const char *sql = sqlite3_sql(stmt); if (sql) { handle->reportSQL(sql); } } break; case SQLITE_TRACE_PROFILE: { sqlite3_int64 *cost = (sqlite3_int64 *) X; const char *sql = sqlite3_sql(stmt); //report last trace if (!handle->shouldPerformanceAggregation()) { handle->reportPerformance(); } if (sql) { handle->addPerformanceTrace(sql, *cost); } } break; default: break; } return SQLITE_OK; }, this); } else { sqlite3_trace_v2((sqlite3 *) m_handle, 0, nullptr, nullptr); } }
void Statement::execWithoutResult() { if (begin() != end()) { std::string sql = sqlite3_sql(impl->stmt); throw QueryReturnedRows(sql); } }
Row Statement::execWithSingleResult() { auto first = begin(); if (first == end()) { std::string sql = sqlite3_sql(impl->stmt); throw QueryReturnedNoRows(sql); } return *first; }
static void sql_error(const char *file, int line, sqlite3_stmt *stmt) { (void) fprintf( stderr, "%s/%d: %s (%d): %s\n", file, line, sqlite3_errmsg(ctx.db), sqlite3_errcode(ctx.db), sqlite3_sql(stmt) ); (void) sqlite3_clear_bindings(stmt); }
static bool execute(sqlite3_stmt *query) { int result = sqlite3_step(query); if (result == SQLITE_DONE || result == SQLITE_ROW) { result = true; } else { musicd_log(LOG_ERROR, "library", "sqlite3_step failed for '%s'", sqlite3_sql(query)); result = false; } sqlite3_finalize(query); return result; }
bool StatementHandle::step() { int rc = sqlite3_step((sqlite3_stmt *) m_stmt); if (rc == SQLITE_ROW || rc == SQLITE_OK || rc == SQLITE_DONE) { m_error.reset(); return rc == SQLITE_ROW; } sqlite3 *handle = sqlite3_db_handle((sqlite3_stmt *) m_stmt); Error::ReportSQLite( m_handle.getTag(), m_handle.path, Error::HandleOperation::Step, rc, sqlite3_extended_errcode(handle), sqlite3_errmsg(handle), sqlite3_sql((sqlite3_stmt *) m_stmt), &m_error); return false; }
static int64_t execute_scalar(sqlite3_stmt *query) { int64_t result = sqlite3_step(query); if (result == SQLITE_ROW) { result = sqlite3_column_int(query, 0); } else if (result == SQLITE_DONE) { result = 0; } else { musicd_log(LOG_ERROR, "library", "sqlite3_step failed for '%s'", sqlite3_sql(query)); result = -1; } sqlite3_finalize(query); return result; }
int sqlite3_run_query(sqlite3* DB, sqlite3_stmt *stmt, unsigned retries) { int src = SQLITE_BUSY; for (unsigned i = 0; i < retries; i++) { src = sqlite3_step(stmt); if (src != SQLITE_BUSY && src != SQLITE_LOCKED) { break; } usleep(200); } if ((src!=SQLITE_DONE) && (src!=SQLITE_ROW)) { fprintf(stderr,"sqlite3_run_query failed: %s: %s\n", sqlite3_sql(stmt), sqlite3_errmsg(DB)); } return src; }
int SP_HiveDBSchema :: init( const char * dbname, const char * ddl ) { mTableList = new SP_NKVector(); mDBName = strdup( dbname ); int ret = 0; sqlite3 * handle = NULL; sqlite3_open( ":memory:", &handle ); for( ; NULL != ddl && 0 == ret; ) { sqlite3_stmt * stmt = NULL; const char * next = NULL; ret = sqlite3_prepare_v2( handle, ddl, strlen( ddl ), &stmt, &next ); if( SQLITE_OK == ret && NULL != stmt ) { const char * sql = sqlite3_sql( stmt ); char table[ 256 ] = { 0 }; if( 0 == createTable( handle, sql, table, sizeof( table ) ) ) { SP_HiveTableSchema * schema = new SP_HiveTableSchema(); if( 0 == schema->init( handle, table, sql ) ) { SP_NKLog::log( LOG_DEBUG, "INIT: parse ddl ok, [%s]%s", dbname, sql ); mTableList->append( schema ); } else { ret = -1; SP_NKLog::log( LOG_ERR, "ERROR: invalid ddl, [%s]%s", dbname, sql ); delete schema; } } else { SP_NKLog::log( LOG_ERR, "ERROR: invalid ddl, [%s]%s", dbname, sql ); ret = -1; } } ddl = stmt ? next : NULL; if( NULL != stmt ) sqlite3_finalize( stmt ); } sqlite3_close( handle ); return ret; }
jstring JNICALL Java_com_baidu_javalite_PrepareStmt_sqlite3_1sql(JNIEnv *env, jclass cls, jlong handle) { if (handle == 0) { throwSqliteException(env, "handle is NULL"); return 0; } sqlite3_stmt* stmt = (sqlite3_stmt*) handle; const char* csql = sqlite3_sql(stmt); if (csql == 0) { return (*env)->NewStringUTF(env, ""); } else { return (*env)->NewStringUTF(env, csql); } }
static int sql_exec_one_statement( sqlite3_stmt *statement, async_sqlite3_command *async_command, int *term_count_p, int *term_allocated_p, ErlDrvTermData **dataset_p) { int column_count = sqlite3_column_count(statement); int row_count = 0, next_row; int base_term_count; sqlite3_drv_t *drv = async_command->driver_data; ptr_list **ptrs_p = &(async_command->ptrs); ptr_list **binaries_p = &(async_command->binaries); // printf("\nsql_exec_one_statement. SQL:\n%s\n Term count: %d, terms alloc: %d\n", sqlite3_sql(statement), *term_count_p, *term_allocated_p); int i; if (column_count > 0) { *term_count_p += 2; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 2] = ERL_DRV_ATOM; (*dataset_p)[*term_count_p - 1] = drv->atom_columns; base_term_count = *term_count_p; get_columns( drv, statement, column_count, base_term_count, term_count_p, term_allocated_p, dataset_p); *term_count_p += 4; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[base_term_count + column_count * 3 + 3] = ERL_DRV_TUPLE; (*dataset_p)[base_term_count + column_count * 3 + 4] = 2; (*dataset_p)[base_term_count + column_count * 3 + 5] = ERL_DRV_ATOM; (*dataset_p)[base_term_count + column_count * 3 + 6] = drv->atom_rows; } #ifdef DEBUG fprintf(drv->log, "Exec: %s\n", sqlite3_sql(statement)); fflush(drv->log); #endif while ((next_row = sqlite3_step(statement)) == SQLITE_ROW) { for (i = 0; i < column_count; i++) { #ifdef DEBUG fprintf(drv->log, "Column %d type: %d\n", i, sqlite3_column_type(statement, i)); fflush(drv->log); #endif switch (sqlite3_column_type(statement, i)) { case SQLITE_INTEGER: { ErlDrvSInt64 *int64_ptr = driver_alloc(sizeof(ErlDrvSInt64)); *int64_ptr = (ErlDrvSInt64) sqlite3_column_int64(statement, i); *ptrs_p = add_to_ptr_list(*ptrs_p, int64_ptr); *term_count_p += 2; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 2] = ERL_DRV_INT64; (*dataset_p)[*term_count_p - 1] = (ErlDrvTermData) int64_ptr; break; } case SQLITE_FLOAT: { double *float_ptr = driver_alloc(sizeof(double)); *float_ptr = sqlite3_column_double(statement, i); *ptrs_p = add_to_ptr_list(*ptrs_p, float_ptr); *term_count_p += 2; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 2] = ERL_DRV_FLOAT; (*dataset_p)[*term_count_p - 1] = (ErlDrvTermData) float_ptr; break; } case SQLITE_BLOB: { int bytes = sqlite3_column_bytes(statement, i); ErlDrvBinary* binary = driver_alloc_binary(bytes); binary->orig_size = bytes; memcpy(binary->orig_bytes, sqlite3_column_blob(statement, i), bytes); *binaries_p = add_to_ptr_list(*binaries_p, binary); *term_count_p += 8; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 8] = ERL_DRV_ATOM; (*dataset_p)[*term_count_p - 7] = drv->atom_blob; (*dataset_p)[*term_count_p - 6] = ERL_DRV_BINARY; (*dataset_p)[*term_count_p - 5] = (ErlDrvTermData) binary; (*dataset_p)[*term_count_p - 4] = bytes; (*dataset_p)[*term_count_p - 3] = 0; (*dataset_p)[*term_count_p - 2] = ERL_DRV_TUPLE; (*dataset_p)[*term_count_p - 1] = 2; break; } case SQLITE_TEXT: { int bytes = sqlite3_column_bytes(statement, i); ErlDrvBinary* binary = driver_alloc_binary(bytes); binary->orig_size = bytes; memcpy(binary->orig_bytes, sqlite3_column_blob(statement, i), bytes); *binaries_p = add_to_ptr_list(*binaries_p, binary); *term_count_p += 4; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 4] = ERL_DRV_BINARY; (*dataset_p)[*term_count_p - 3] = (ErlDrvTermData) binary; (*dataset_p)[*term_count_p - 2] = bytes; (*dataset_p)[*term_count_p - 1] = 0; break; } case SQLITE_NULL: { *term_count_p += 2; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 2] = ERL_DRV_ATOM; (*dataset_p)[*term_count_p - 1] = drv->atom_null; break; } } } *term_count_p += 2; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 2] = ERL_DRV_TUPLE; (*dataset_p)[*term_count_p - 1] = column_count; row_count++; } if (next_row == SQLITE_BUSY) { return_error(drv, SQLITE_BUSY, "SQLite3 database is busy", dataset_p, term_count_p, term_allocated_p, &async_command->error_code); async_command->finalize_statement_on_free = 1; return next_row; } if (next_row != SQLITE_DONE) { return_error(drv, next_row, sqlite3_errmsg(drv->db), dataset_p, term_count_p, term_allocated_p, &async_command->error_code); async_command->finalize_statement_on_free = 1; return next_row; } if (column_count > 0) { *term_count_p += 3+2+3; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 8] = ERL_DRV_NIL; (*dataset_p)[*term_count_p - 7] = ERL_DRV_LIST; (*dataset_p)[*term_count_p - 6] = row_count + 1; (*dataset_p)[*term_count_p - 5] = ERL_DRV_TUPLE; (*dataset_p)[*term_count_p - 4] = 2; (*dataset_p)[*term_count_p - 3] = ERL_DRV_NIL; (*dataset_p)[*term_count_p - 2] = ERL_DRV_LIST; (*dataset_p)[*term_count_p - 1] = 3; } else if (sql_is_insert(sqlite3_sql(statement))) { ErlDrvSInt64 *rowid_ptr = driver_alloc(sizeof(ErlDrvSInt64)); *rowid_ptr = (ErlDrvSInt64) sqlite3_last_insert_rowid(drv->db); *ptrs_p = add_to_ptr_list(*ptrs_p, rowid_ptr); *term_count_p += 6; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 6] = ERL_DRV_ATOM; (*dataset_p)[*term_count_p - 5] = drv->atom_rowid; (*dataset_p)[*term_count_p - 4] = ERL_DRV_INT64; (*dataset_p)[*term_count_p - 3] = (ErlDrvTermData) rowid_ptr; (*dataset_p)[*term_count_p - 2] = ERL_DRV_TUPLE; (*dataset_p)[*term_count_p - 1] = 2; } else { *term_count_p += 2; if (*term_count_p > *term_allocated_p) { *term_allocated_p = max(*term_count_p, *term_allocated_p*2); *dataset_p = driver_realloc(*dataset_p, sizeof(ErlDrvTermData) * *term_allocated_p); } (*dataset_p)[*term_count_p - 2] = ERL_DRV_ATOM; (*dataset_p)[*term_count_p - 1] = drv->atom_ok; } #ifdef DEBUG fprintf(drv->log, "Total term count: %p %d, rows count: %dx%d\n", statement, *term_count_p, column_count, row_count); fflush(drv->log); #endif async_command->finalize_statement_on_free = 1; return 0; }
int proxy_db_close(pool *p, const char *schema_name) { if (p == NULL) { errno = EINVAL; return -1; } if (proxy_dbh != NULL) { pool *tmp_pool; sqlite3_stmt *pstmt; int res; tmp_pool = make_sub_pool(p); /* If we're given a schema name, then just detach that schema from the * database handle. */ if (schema_name != NULL) { const char *stmt; stmt = pstrcat(tmp_pool, "DETACH DATABASE ", schema_name, ";", NULL); res = sqlite3_exec(proxy_dbh, stmt, NULL, NULL, NULL); if (res != SQLITE_OK) { pr_trace_msg(trace_channel, 2, "error detaching '%s' from existing SQLite handle using '%s': %s", schema_name, stmt, sqlite3_errmsg(proxy_dbh)); destroy_pool(tmp_pool); errno = EPERM; return -1; } destroy_pool(tmp_pool); return 0; } /* Make sure to close/finish any prepared statements associated with * the database. */ pstmt = sqlite3_next_stmt(proxy_dbh, NULL); while (pstmt != NULL) { sqlite3_stmt *next; const char *sql; pr_signals_handle(); next = sqlite3_next_stmt(proxy_dbh, pstmt); sql = pstrdup(tmp_pool, sqlite3_sql(pstmt)); res = sqlite3_finalize(pstmt); if (res != SQLITE_OK) { pr_trace_msg(trace_channel, 2, "error finishing prepared statement '%s': %s", sql, sqlite3_errmsg(proxy_dbh)); } else { pr_trace_msg(trace_channel, 18, "finished prepared statement '%s'", sql); } pstmt = next; } destroy_pool(tmp_pool); res = sqlite3_close(proxy_dbh); if (res != SQLITE_OK) { pr_trace_msg(trace_channel, 2, "error closing SQLite database: %s", sqlite3_errmsg(proxy_dbh)); errno = EPERM; return -1; } pr_trace_msg(trace_channel, 18, "%s", "closed SQLite database"); proxy_dbh = NULL; } pr_table_empty(prepared_stmts); pr_table_free(prepared_stmts); prepared_stmts = NULL; return 0; }
SqlException::SqlException(sqlite3_stmt* statement, const std::string& message) : SqlException(sqlite3_db_handle(statement), sqlite3_sql(statement), message) { }
/* ** Implementation of the sha3_query(SQL,SIZE) function. ** ** This function compiles and runs the SQL statement(s) given in the ** argument. The results are hashed using a SIZE-bit SHA3. The default ** size is 256. ** ** The format of the byte stream that is hashed is summarized as follows: ** ** S<n>:<sql> ** R ** N ** I<int> ** F<ieee-float> ** B<size>:<bytes> ** T<size>:<text> ** ** <sql> is the original SQL text for each statement run and <n> is ** the size of that text. The SQL text is UTF-8. A single R character ** occurs before the start of each row. N means a NULL value. ** I mean an 8-byte little-endian integer <int>. F is a floating point ** number with an 8-byte little-endian IEEE floating point value <ieee-float>. ** B means blobs of <size> bytes. T means text rendered as <size> ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII ** text integers. ** ** For each SQL statement in the X input, there is one S segment. Each ** S segment is followed by zero or more R segments, one for each row in the ** result set. After each R, there are one or more N, I, F, B, or T segments, ** one for each column in the result set. Segments are concatentated directly ** with no delimiters of any kind. */ static void sha3QueryFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ sqlite3 *db = sqlite3_context_db_handle(context); const char *zSql = (const char*)sqlite3_value_text(argv[0]); sqlite3_stmt *pStmt = 0; int nCol; /* Number of columns in the result set */ int i; /* Loop counter */ int rc; int n; const char *z; SHA3Context cx; int iSize; if( argc==1 ){ iSize = 256; }else{ iSize = sqlite3_value_int(argv[1]); if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " "384 512", -1); return; } } if( zSql==0 ) return; SHA3Init(&cx, iSize); while( zSql[0] ){ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); if( rc ){ char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", zSql, sqlite3_errmsg(db)); sqlite3_finalize(pStmt); sqlite3_result_error(context, zMsg, -1); sqlite3_free(zMsg); return; } if( !sqlite3_stmt_readonly(pStmt) ){ char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); sqlite3_finalize(pStmt); sqlite3_result_error(context, zMsg, -1); sqlite3_free(zMsg); return; } nCol = sqlite3_column_count(pStmt); z = sqlite3_sql(pStmt); n = (int)strlen(z); hash_step_vformat(&cx,"S%d:",n); SHA3Update(&cx,(unsigned char*)z,n); /* Compute a hash over the result of the query */ while( SQLITE_ROW==sqlite3_step(pStmt) ){ SHA3Update(&cx,(const unsigned char*)"R",1); for(i=0; i<nCol; i++){ switch( sqlite3_column_type(pStmt,i) ){ case SQLITE_NULL: { SHA3Update(&cx, (const unsigned char*)"N",1); break; } case SQLITE_INTEGER: { sqlite3_uint64 u; int j; unsigned char x[9]; sqlite3_int64 v = sqlite3_column_int64(pStmt,i); memcpy(&u, &v, 8); for(j=8; j>=1; j--){ x[j] = u & 0xff; u >>= 8; } x[0] = 'I'; SHA3Update(&cx, x, 9); break; } case SQLITE_FLOAT: { sqlite3_uint64 u; int j; unsigned char x[9]; double r = sqlite3_column_double(pStmt,i); memcpy(&u, &r, 8); for(j=8; j>=1; j--){ x[j] = u & 0xff; u >>= 8; } x[0] = 'F'; SHA3Update(&cx,x,9); break; } case SQLITE_TEXT: { int n2 = sqlite3_column_bytes(pStmt, i); const unsigned char *z2 = sqlite3_column_text(pStmt, i); hash_step_vformat(&cx,"T%d:",n2); SHA3Update(&cx, z2, n2); break; } case SQLITE_BLOB: { int n2 = sqlite3_column_bytes(pStmt, i); const unsigned char *z2 = sqlite3_column_blob(pStmt, i); hash_step_vformat(&cx,"B%d:",n2); SHA3Update(&cx, z2, n2); break; } } } } sqlite3_finalize(pStmt); } sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); }
static int rhizome_server_sql_query_fill_buffer(rhizome_http_request *r, char *table, char *column) { unsigned char blob_value[r->source_record_size*2+1]; if (debug & DEBUG_RHIZOME_TX) DEBUGF("populating with sql rows at offset %d",r->buffer_length); if (r->source_index>=r->source_count) { /* All done */ return 0; } int record_count=(r->buffer_size-r->buffer_length)/r->source_record_size; if (record_count<1) { if (debug & DEBUG_RHIZOME_TX) DEBUGF("r->buffer_size=%d, r->buffer_length=%d, r->source_record_size=%d", r->buffer_size, r->buffer_length, r->source_record_size); return WHY("Not enough space to fit any records"); } sqlite3_stmt *statement = sqlite_prepare("%s LIMIT %lld,%d", r->source, r->source_index, record_count); if (!statement) return -1; if (debug & DEBUG_RHIZOME_TX) DEBUG(sqlite3_sql(statement)); sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT; while( r->buffer_length + r->source_record_size < r->buffer_size && sqlite_step_retry(&retry, statement) == SQLITE_ROW ) { r->source_index++; if (sqlite3_column_count(statement)!=2) { sqlite3_finalize(statement); return WHY("sqlite3 returned multiple columns for a single column query"); } sqlite3_blob *blob; const unsigned char *value; int column_type=sqlite3_column_type(statement, 0); switch(column_type) { case SQLITE_TEXT: value=sqlite3_column_text(statement, 0); break; case SQLITE_BLOB: if (debug & DEBUG_RHIZOME_TX) DEBUGF("table='%s',col='%s',rowid=%lld", table, column, sqlite3_column_int64(statement,1)); int ret; int64_t rowid = sqlite3_column_int64(statement, 1); do ret = sqlite3_blob_open(rhizome_db, "main", table, column, rowid, 0 /* read only */, &blob); while (sqlite_code_busy(ret) && sqlite_retry(&retry, "sqlite3_blob_open")); if (!sqlite_code_ok(ret)) { WHYF("sqlite3_blob_open() failed, %s", sqlite3_errmsg(rhizome_db)); continue; } sqlite_retry_done(&retry, "sqlite3_blob_open"); if (sqlite3_blob_read(blob,&blob_value[0], /* copy number of bytes based on whether we need to de-hex the string or not */ r->source_record_size*(1+(r->source_flags&1)),0) !=SQLITE_OK) { WHYF("sqlite3_blob_read() failed, %s", sqlite3_errmsg(rhizome_db)); sqlite3_blob_close(blob); continue; } value = blob_value; sqlite3_blob_close(blob); break; default: /* improper column type, so don't include in report */ WHYF("Bad column type %d", column_type); continue; } if (r->source_flags&1) { /* hex string to be converted */ int i; for(i=0;i<r->source_record_size;i++) /* convert the two nybls and make a byte */ r->buffer[r->buffer_length+i] =(hexvalue(value[i<<1])<<4)|hexvalue(value[(i<<1)+1]); } else /* direct binary value */ bcopy(value,&r->buffer[r->buffer_length],r->source_record_size); r->buffer_length+=r->source_record_size; } sqlite3_finalize(statement); return 0; }
/** @SYMTestCaseID PDS-SQLITE3-UT-4039 @SYMTestCaseDesc SQLITE3 - blob API tests. List of called SQLITE3 functions: - sqlite3_bind_zeroblob; - sqlite3_blob_bytes; - sqlite3_blob_close; - sqlite3_blob_open; - sqlite3_blob_read; - sqlite3_blob_write; - sqlite3_sql; @SYMTestPriority High @SYMTestActions SQLITE3 - blob API tests. @SYMTestExpectedResults Test must not fail @SYMREQ REQ10424 */ static void TestSqliteBlobApi() { int err; const char* tail = 0; sqlite3_blob* blob = 0; int bytes = 0; const char KBlobData[] = "ABCDEFGH"; char sql[100]; const int KBlobLen = strlen(KBlobData); const char* sql2 = 0; const char KSqlFmt[] = "UPDATE BlobTbl SET B=:Prm WHERE ROWID=1"; TEST(TheDb != 0); TEST(!TheStmt); /* Create the table, insert one record with a blob */ err = sqlite3_exec(TheDb, "CREATE TABLE BlobTbl(I INTEGER PRIMARY KEY, B BLOB)", 0, 0, 0); TEST2(err, SQLITE_OK); sprintf(sql, "INSERT INTO BlobTbl VALUES(1, zeroblob(%d))", KBlobLen); err = sqlite3_exec(TheDb, sql, 0, 0, 0); TEST2(err, SQLITE_OK); err = sqlite3_prepare_v2(TheDb, KSqlFmt, -1, &TheStmt, &tail); TEST2(err, SQLITE_OK); TEST(TheStmt != 0); sql2 = sqlite3_sql(TheStmt); TEST(sql2 != NULL); err = strcmp(sql2, KSqlFmt); TEST2(err, 0); err = sqlite3_bind_zeroblob(TheStmt, 1, KBlobLen); TEST2(err, SQLITE_OK); err = sqlite3_step(TheStmt); TEST2(err, SQLITE_DONE); err = sqlite3_finalize(TheStmt); TEST2(err, SQLITE_OK); TheStmt = 0; /* Open the blob and write to it */ err = sqlite3_blob_open(TheDb, "main", "BlobTbl", "B", 1, 1, &blob); TEST2(err, SQLITE_OK); TEST(blob != 0); bytes = sqlite3_blob_bytes(blob); TEST2(bytes, KBlobLen); err = sqlite3_blob_write(blob, KBlobData, KBlobLen, 0); TEST2(err, SQLITE_OK); err = sqlite3_blob_close(blob); TEST2(err, SQLITE_OK); blob = 0; /* Open the blob and read from it */ err = sqlite3_blob_open(TheDb, "main", "BlobTbl", "B", 1, 1, &blob); TEST2(err, SQLITE_OK); TEST(blob != 0); bytes = sqlite3_blob_bytes(blob); TEST2(bytes, KBlobLen); err = sqlite3_blob_read(blob, sql, KBlobLen, 0); TEST2(err, SQLITE_OK); sql[bytes] = 0; err = sqlite3_blob_close(blob); TEST2(err, SQLITE_OK); blob = 0; err = strcmp(sql, KBlobData); TEST2(err, 0); err = sqlite3_exec(TheDb, "DROP TABLE BlobTbl", 0, 0, 0); TEST2(err, SQLITE_OK); }
DLL_FUNCTION(const char*) BU_SQLite_SQL(sqlite3_stmt* pStmt) { #pragma comment(linker, "/EXPORT:BU_SQLite_SQL=_BU_SQLite_SQL@4") return sqlite3_sql(pStmt); }
std::string Statement::GetOriginalSQLStatement() { return std::string(sqlite3_sql(GetStatement())); }
char * machine_script(Pointer stmt) { return (char *) sqlite3_sql((sqlite3_stmt*) stmt); }
const char* Database::Query::getSql() { return _stmt ? sqlite3_sql(_stmt) : "<finalized>"; }
int serverMain(void) { int rc, signal; pthread_t thread_listen, thread_answer; running = 1; signal = SIGTERM; rc = EX_SOFTWARE; if (!sqlite3_threadsafe()) { fprintf(stderr, "thread-safe sqlite3 required\n"); goto error0; } if (pthreadInit()) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error0; } if ((service_addr = socketAddressNew("0.0.0.0", port)) == NULL) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error1; } if ((service = socketOpen(service_addr, 0)) == NULL) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error2; } if (socketSetReuse(service, 1)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error2; } if (socketBind(service, &service->address)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error2; } if (serverSignalsInit(&signals)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error3; } if (queueInit(&queries_unused)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error4; } if (queueInit(&queries_waiting)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error5; } if (sqlite3_open(database_path, &db) != SQLITE_OK) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error6; } if (create_database(db)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error7; } if (sqlite3_prepare_v2(db, SQL_SELECT_ONE, -1, &db_select_one, NULL) != SQLITE_OK) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error7; } if (0 < debug) syslog(LOG_DEBUG, "sql=\"%s\"", sqlite3_sql(db_select_one)); if (pthread_create(&thread_answer, NULL, answer_thread, NULL)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error8; } if (pthread_create(&thread_listen, NULL, listen_thread, NULL)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error9; } syslog(LOG_INFO, "ready"); signal = serverSignalsLoop(&signals); syslog(LOG_INFO, "signal %d, terminating process", signal); running = 0; rc = EXIT_SUCCESS; (void) pthread_cancel(thread_answer); (void) pthread_join(thread_answer, NULL); error9: (void) pthread_cancel(thread_listen); (void) pthread_join(thread_listen, NULL); error8: sqlite3_finalize(db_select_one); error7: sqlite3_close(db); error6: queueFini(&queries_waiting); error5: queueFini(&queries_unused); error4: serverSignalsFini(&signals); error3: socketClose(service); error2: free(service_addr); error1: pthreadFini(); error0: syslog(LOG_INFO, "signal %d, terminated", signal); return rc; }