wi_sqlite3_statement_t * wi_sqlite3_prepare_statement(wi_sqlite3_database_t *database, wi_string_t *query, ...) { wi_sqlite3_statement_t *statement; va_list ap; statement = wi_autorelease(wi_runtime_create_instance(_wi_sqlite3_statement_runtime_id, sizeof(wi_sqlite3_statement_t))); statement->query = wi_retain(query); wi_recursive_lock_lock(database->lock); if( #ifdef HAVE_SQLITE3_PREPARE_V2 sqlite3_prepare_v2 #else sqlite3_prepare #endif (database->database, wi_string_cstring(query), wi_string_length(query), &statement->statement, NULL) == SQLITE_OK) { va_start(ap, query); _wi_sqlite3_bind_statement(statement, ap); va_end(ap); } else { wi_error_set_sqlite3_error_with_description(database->database, wi_description(statement)); statement = NULL; } wi_recursive_lock_unlock(database->lock); return statement; }
void wi_error_set_sqlite3_error(void *db) { wi_error_set_sqlite3_error_with_description(db, NULL); }
wi_dictionary_t * wi_sqlite3_fetch_statement_results(wi_sqlite3_database_t *database, wi_sqlite3_statement_t *statement) { wi_mutable_dictionary_t *results; wi_runtime_instance_t *instance; int i, count, length, result; wi_recursive_lock_lock(database->lock); result = sqlite3_step(statement->statement); switch(result) { case SQLITE_DONE: results = wi_dictionary(); sqlite3_finalize(statement->statement); statement->statement = NULL; break; case SQLITE_ROW: results = wi_mutable_dictionary(); count = sqlite3_column_count(statement->statement); for(i = 0; i < count; i++) { switch(sqlite3_column_type(statement->statement, i)) { case SQLITE_INTEGER: instance = wi_number_with_int64(sqlite3_column_int64(statement->statement, i)); break; case SQLITE_FLOAT: instance = wi_number_with_double(sqlite3_column_double(statement->statement, i)); break; case SQLITE_TEXT: instance = wi_string_with_cstring((const char *) sqlite3_column_text(statement->statement, i)); break; case SQLITE_BLOB: length = sqlite3_column_bytes(statement->statement, i); instance = wi_data_with_bytes(sqlite3_column_blob(statement->statement, i), length); break; case SQLITE_NULL: instance = wi_null(); break; default: instance = NULL; break; } if(instance) wi_mutable_dictionary_set_data_for_key(results, instance, wi_string_with_cstring(sqlite3_column_name(statement->statement, i))); } wi_runtime_make_immutable(results); break; default: wi_error_set_sqlite3_error_with_description(database->database, wi_description(statement)); sqlite3_finalize(statement->statement); statement->statement = NULL; results = NULL; break; } wi_recursive_lock_unlock(database->lock); return results; }