static int invalid_op(apr_pool_t* pool, apr_dbd_t* handle, const apr_dbd_driver_t* driver) { int rv = 0; int nrows; const char *statement = "INSERT into apr_dbd_test1 (col2) values ('foo')" ; rv = apr_dbd_query(driver, handle, &nrows, statement); printf("invalid op returned %d (should be nonzero). Error msg follows\n", rv); printf("'%s'\n", apr_dbd_error(driver, handle, rv)); statement = "INSERT into apr_dbd_test (col1, col2) values ('bar', 'foo')" ; rv = apr_dbd_query(driver, handle, &nrows, statement); printf("valid op returned %d (should be zero; error shouldn't affect subsequent ops)\n", rv); return rv; }
/* ============================================================================= db:query(statement): Executes the given database query and returns the number of rows affected. If an error is encountered, returns nil as the first parameter and the error message as the second. ============================================================================= */ int lua_db_query(lua_State *L) { /*~~~~~~~~~~~~~~~~~~~~~~~*/ lua_db_handle *db = 0; apr_status_t rc = 0; int x = 0; const char *statement; /*~~~~~~~~~~~~~~~~~~~~~~~*/ luaL_checktype(L, 3, LUA_TSTRING); statement = lua_tostring(L, 3); db = lua_get_db_handle(L); if (db && db->alive) rc = apr_dbd_query(db->driver, db->handle, &x, statement); else { rc = 0; x = -1; } if (rc == APR_SUCCESS) lua_pushnumber(L, x); else { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ const char *err = apr_dbd_error(db->driver, db->handle, rc); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ lua_pushnil(L); if (err) { lua_pushstring(L, err); return 2; } } return 1; }
static int drop_table(apr_pool_t* pool, apr_dbd_t* handle, const apr_dbd_driver_t* driver) { int rv = 0; int nrows; const char *statement = "DROP TABLE apr_dbd_test" ; rv = apr_dbd_query(driver, handle, &nrows, statement); return rv; }
static void test_statement(abts_case *tc, apr_dbd_t* handle, const apr_dbd_driver_t* driver, const char* sql) { int nrows; apr_status_t rv; rv = apr_dbd_query(driver, handle, &nrows, sql); ABTS_ASSERT(tc, sql, rv == APR_SUCCESS); }
int MetadataStorage::execute_query(std::string query) { int nrows = -1; int status = apr_dbd_query(driver_, handle_.get(), &nrows, query.c_str()); if (status != 0 && status != 21) { // generate error and throw (*logger_)(AKU_LOG_ERROR, "Error executing query"); throw std::runtime_error(apr_dbd_error(driver_, handle_.get(), status)); } return nrows; }
static int create_table(apr_pool_t* pool, apr_dbd_t* handle, const apr_dbd_driver_t* driver) { int rv = 0; int nrows; const char *statement = "CREATE TABLE apr_dbd_test (" "col1 varchar(40) not null," "col2 varchar(40)," "col3 integer)" ; rv = apr_dbd_query(driver, handle, &nrows, statement); return rv; }
static int insert_rows(apr_pool_t* pool, apr_dbd_t* handle, const apr_dbd_driver_t* driver) { int i; int rv = 0; int nrows; int nerrors = 0; const char *statement = "INSERT into apr_dbd_test (col1) values ('foo');" "INSERT into apr_dbd_test values ('wibble', 'other', 5);" "INSERT into apr_dbd_test values ('wibble', 'nothing', 5);" "INSERT into apr_dbd_test values ('qwerty', 'foo', 0);" "INSERT into apr_dbd_test values ('asdfgh', 'bar', 1);" ; rv = apr_dbd_query(driver, handle, &nrows, statement); if (rv) { const char* stmt[] = { "INSERT into apr_dbd_test (col1) values ('foo');", "INSERT into apr_dbd_test values ('wibble', 'other', 5);", "INSERT into apr_dbd_test values ('wibble', 'nothing', 5);", "INSERT into apr_dbd_test values ('qwerty', 'foo', 0);", "INSERT into apr_dbd_test values ('asdfgh', 'bar', 1);", NULL }; printf("Compound insert failed; trying statements one-by-one\n") ; for (i=0; stmt[i] != NULL; ++i) { statement = stmt[i]; rv = apr_dbd_query(driver, handle, &nrows, statement); if (rv) { nerrors++; } } if (nerrors) { printf("%d single inserts failed too.\n", nerrors) ; } } return rv; }
static void insert_data(abts_case *tc, apr_dbd_t* handle, const apr_dbd_driver_t* driver, int count) { apr_pool_t* pool = p; const char* sql = "INSERT INTO apr_dbd_test VALUES('%d', '%d', %d)"; char* sqf = NULL; int i; int nrows; apr_status_t rv; for (i=0; i<count; i++) { sqf = apr_psprintf(pool, sql, i, i, i); rv = apr_dbd_query(driver, handle, &nrows, sqf); ABTS_ASSERT(tc, sqf, rv == APR_SUCCESS); ABTS_ASSERT(tc, sqf, 1 == nrows); } }
/* Run an insert query and return a categorized error or success */ static logsql_query_ret log_sql_dbd_query(request_rec *r,logsql_dbconnection *db, const char *query) { int ret; const char *err; int affected; // Acquire a DBD connection from mod_dbd ap_dbd_t *dbd = log_sql_dbd_getconnection(r); if (!dbd) return LOGSQL_QUERY_NOLINK; // Run the query ret = apr_dbd_query(dbd->driver, dbd->handle, &affected, query); if (ret == 0) { return LOGSQL_QUERY_SUCCESS; } else { // attempt to detect error message err = apr_dbd_error(dbd->driver, dbd->handle, ret); log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "DB Returned error: (%d) %s", ret, err); // Unable to check if "NO SUCH TABLE" due to apr_dbd not mapping error codes to a standard set. return LOGSQL_QUERY_FAIL; } }
static int test_transactions(apr_pool_t* pool, apr_dbd_t* handle, const apr_dbd_driver_t* driver) { int rv = 0; int nrows; apr_dbd_transaction_t *trans = NULL; const char* statement; /* trans 1 - error out early */ printf("Transaction 1\n"); rv = apr_dbd_transaction_start(driver, pool, handle, &trans); if (rv) { printf("Start transaction failed!\n%s\n", apr_dbd_error(driver, handle, rv)); return rv; } statement = "UPDATE apr_dbd_test SET col2 = 'failed'"; rv = apr_dbd_query(driver, handle, &nrows, statement); if (rv) { printf("Update failed: '%s'\n", apr_dbd_error(driver, handle, rv)); apr_dbd_transaction_end(driver, pool, trans); return rv; } printf("%d rows updated\n", nrows); statement = "INSERT INTO apr_dbd_test1 (col3) values (3)"; rv = apr_dbd_query(driver, handle, &nrows, statement); if (!rv) { printf("Oops, invalid op succeeded but shouldn't!\n"); } statement = "INSERT INTO apr_dbd_test values ('zzz', 'aaa', 3)"; rv = apr_dbd_query(driver, handle, &nrows, statement); printf("Valid insert returned %d. Should be nonzero (fail) because transaction is bad\n", rv) ; rv = apr_dbd_transaction_end(driver, pool, trans); if (rv) { printf("End transaction failed!\n%s\n", apr_dbd_error(driver, handle, rv)); return rv; } printf("Transaction ended (should be rollback) - viewing table\n" "A column of \"failed\" indicates transaction failed (no rollback)\n"); select_sequential(pool, handle, driver); /* trans 2 - complete successfully */ printf("Transaction 2\n"); rv = apr_dbd_transaction_start(driver, pool, handle, &trans); if (rv) { printf("Start transaction failed!\n%s\n", apr_dbd_error(driver, handle, rv)); return rv; } statement = "UPDATE apr_dbd_test SET col2 = 'success'"; rv = apr_dbd_query(driver, handle, &nrows, statement); if (rv) { printf("Update failed: '%s'\n", apr_dbd_error(driver, handle, rv)); apr_dbd_transaction_end(driver, pool, trans); return rv; } printf("%d rows updated\n", nrows); statement = "INSERT INTO apr_dbd_test values ('aaa', 'zzz', 3)"; rv = apr_dbd_query(driver, handle, &nrows, statement); printf("Valid insert returned %d. Should be zero (OK)\n", rv) ; rv = apr_dbd_transaction_end(driver, pool, trans); if (rv) { printf("End transaction failed!\n%s\n", apr_dbd_error(driver, handle, rv)); return rv; } printf("Transaction ended (should be commit) - viewing table\n"); select_sequential(pool, handle, driver); return rv; }