static int set_schema_version(pool *p, const char *schema_name, unsigned int schema_version) { int res, xerrno = 0; const char *stmt, *errstr = NULL; array_header *results; /* CREATE TABLE $schema_name.schema_version ( * schema TEXT NOT NULL PRIMARY KEY, * version INTEGER NOT NULL * ); */ stmt = pstrcat(p, "CREATE TABLE IF NOT EXISTS ", schema_name, ".schema_version (schema TEXT NOT NULL PRIMARY KEY, version INTEGER NOT NULL);", NULL); res = proxy_db_exec_stmt(p, stmt, &errstr); if (res < 0) { (void) pr_log_debug(DEBUG3, MOD_PROXY_VERSION ": error executing statement '%s': %s", stmt, errstr); errno = EPERM; return -1; } stmt = pstrcat(p, "INSERT INTO ", schema_name, ".schema_version (schema, version) VALUES (?, ?);", NULL); res = proxy_db_prepare_stmt(p, stmt); if (res < 0) { xerrno = errno; (void) pr_log_debug(DEBUG3, MOD_PROXY_VERSION ": error preparing statement '%s': %s", stmt, strerror(xerrno)); errno = xerrno; return -1; } res = proxy_db_bind_stmt(p, stmt, 1, PROXY_DB_BIND_TYPE_TEXT, (void *) schema_name); if (res < 0) { return -1; } res = proxy_db_bind_stmt(p, stmt, 2, PROXY_DB_BIND_TYPE_INT, (void *) &schema_version); if (res < 0) { return -1; } results = proxy_db_exec_prepared_stmt(p, stmt, &errstr); if (results == NULL) { (void) pr_log_debug(DEBUG3, MOD_PROXY_VERSION ": error executing statement '%s': %s", stmt, errstr ? errstr : strerror(errno)); errno = EPERM; return -1; } return 0; }
END_TEST START_TEST (db_exec_stmt_test) { int res; const char *table_path, *schema_name, *stmt, *errstr; res = proxy_db_exec_stmt(NULL, NULL, NULL); fail_unless(res < 0, "Failed to handle null arguments"); fail_unless(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL, strerror(errno), errno); res = proxy_db_exec_stmt(p, NULL, NULL); fail_unless(res < 0, "Failed to handle null statement"); fail_unless(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL, strerror(errno), errno); stmt = "SELECT COUNT(*) FROM foo;"; errstr = NULL; res = proxy_db_exec_stmt(p, stmt, &errstr); fail_unless(res < 0, "Failed to handle missing database handle"); fail_unless(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM, strerror(errno), errno); (void) unlink(db_test_table); table_path = db_test_table; schema_name = "proxy_test"; res = proxy_db_open(p, table_path, schema_name); fail_unless(res == 0, "Failed to open table '%s', schema '%s': %s", table_path, schema_name, strerror(errno)); res = proxy_db_exec_stmt(p, stmt, &errstr); fail_unless(res < 0, "Failed to execute statement '%s'", stmt); fail_unless(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL, strerror(errno), errno); res = proxy_db_close(p, NULL); fail_unless(res == 0, "Failed to close database: %s", strerror(errno)); (void) unlink(db_test_table); }
END_TEST static int create_table(pool *stmt_pool, const char *schema_name, const char *table_name) { int res; const char *stmt, *errstr = NULL; stmt = pstrcat(stmt_pool, "CREATE TABLE ", schema_name, ".", table_name, " (id INTEGER, name TEXT);", NULL); res = proxy_db_exec_stmt(stmt_pool, stmt, &errstr); return res; }