sqlite3 api add bind_text and bind_blob

This commit is contained in:
danawan
2025-08-10 18:20:52 +07:00
parent efd6fde389
commit f450524ddb
4 changed files with 592 additions and 22 deletions

View File

@@ -13,6 +13,11 @@ void test_sqlite3_bind_parameter_name();
void test_sqlite3_bind_parameter_count();
void test_sqlite3_column_name();
void test_sqlite3_last_insert_rowid();
void test_sqlite3_bind_text();
void test_sqlite3_bind_text2();
void test_sqlite3_bind_blob();
int allocated = 0;
int main(void)
{
@@ -21,8 +26,12 @@ int main(void)
test_sqlite3_bind_double();
test_sqlite3_bind_parameter_name();
test_sqlite3_bind_parameter_count();
test_sqlite3_column_name();
//test_sqlite3_column_name();
test_sqlite3_last_insert_rowid();
test_sqlite3_bind_text();
test_sqlite3_bind_text2();
test_sqlite3_bind_blob();
return 0;
}
@@ -257,7 +266,8 @@ void test_sqlite3_column_name() {
assert(strcmp(col0, "id") == 0);
assert(strcmp(col1, "full_name") == 0);
assert(strcmp(col2, "age") == 0);
//will cause panic because get_column_name uses expect()
const char *invalid_col = sqlite3_column_name(stmt, 99);
assert(invalid_col == NULL);
@@ -309,3 +319,166 @@ void test_sqlite3_last_insert_rowid() {
sqlite3_close(db);
}
static void custom_destructor(void *ptr)
{
free(ptr);
allocated--;
}
void test_sqlite3_bind_text()
{
sqlite3 *db;
sqlite3_stmt *stmt;
int rc;
rc = sqlite3_open(":memory:", &db);
assert(rc == SQLITE_OK);
rc = sqlite3_exec(db, "CREATE TABLE bind_text(x TEXT)", 0, 0, 0);
assert(rc == SQLITE_OK);
rc = sqlite3_prepare_v2(db, "INSERT INTO bind_text VALUES (?1)", -1, &stmt, 0);
assert(rc == SQLITE_OK);
char *data = malloc(10);
snprintf(data, 10, "leaktest");
allocated++;
rc = sqlite3_bind_text(stmt, 1, data, -1, custom_destructor);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_DONE);
printf("Before final allocated = %d\n", allocated);
sqlite3_finalize(stmt);
printf("After final allocated = %d\n", allocated);
assert(allocated == 0);
rc = sqlite3_prepare_v2(db, "SELECT x FROM bind_text", -1, &stmt, 0);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_ROW);
const unsigned char *text = sqlite3_column_text(stmt, 0);
int len = sqlite3_column_bytes(stmt, 0);
assert(text != NULL);
assert(strcmp((const char *)text, "leaktest") == 0);
printf("Read text: %s (len=%d)\n", text, len);
assert(len == 8);
sqlite3_finalize(stmt);
sqlite3_close(db);
printf("Test passed: no leaks detected and column text read correctly!\n");
}
void test_sqlite3_bind_text2() {
sqlite3 *db;
sqlite3_stmt *stmt;
sqlite3_stmt *check_stmt;
int rc;
rc = sqlite3_open(":memory:", &db);
assert(rc == SQLITE_OK);
rc = sqlite3_exec(db, "CREATE TABLE bind_text(x TEXT)", 0, 0, 0);
assert(rc == SQLITE_OK);
rc = sqlite3_prepare_v2(db, "INSERT INTO bind_text VALUES (?1)", -1, &stmt, 0);
assert(rc == SQLITE_OK);
rc = sqlite3_bind_text(stmt, 1, "hello", -1, SQLITE_TRANSIENT);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_DONE);
sqlite3_reset(stmt);
const char *long_str = "this_is_a_long_test_string_for_sqlite_bind_text_function";
rc = sqlite3_bind_text(stmt, 1, long_str, -1, SQLITE_TRANSIENT);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_DONE);
sqlite3_reset(stmt);
const char weird_str[] = {'a','b','c','\0','x','y','z'};
//bind text will terminate \0
rc = sqlite3_bind_text(stmt, 1, weird_str, sizeof(weird_str), SQLITE_TRANSIENT);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_DONE);
sqlite3_finalize(stmt);
rc = sqlite3_prepare_v2(db, "SELECT x FROM bind_text", -1, &check_stmt, 0);
assert(rc == SQLITE_OK);
int row = 0;
while ((rc = sqlite3_step(check_stmt)) == SQLITE_ROW) {
const unsigned char *val = sqlite3_column_text(check_stmt, 0);
int len = sqlite3_column_bytes(check_stmt, 0);
printf("Row %d: \"%.*s\" (len=%d)\n", row, len, val, len);
row++;
}
assert(rc == SQLITE_DONE);
sqlite3_finalize(check_stmt);
sqlite3_close(db);
printf("Test passed: bind_text handled multiple cases correctly!\n");
}
void test_sqlite3_bind_blob()
{
sqlite3 *db;
sqlite3_stmt *stmt;
const char *sql = "INSERT INTO test_blob (data) VALUES (?);";
int rc;
rc = sqlite3_open(":memory:", &db);
assert(rc == SQLITE_OK);
rc = sqlite3_exec(db, "CREATE TABLE test_blob (data BLOB);", NULL, NULL, NULL);
assert(rc == SQLITE_OK);
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
assert(rc == SQLITE_OK);
unsigned char blob_data[] = {0x61, 0x62, 0x00, 0x63, 0x64}; // "ab\0cd"
int blob_size = sizeof(blob_data);
rc = sqlite3_bind_blob(stmt, 1, blob_data, blob_size, SQLITE_STATIC);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_DONE);
sqlite3_finalize(stmt);
rc = sqlite3_prepare_v2(db, "SELECT data FROM test_blob;", -1, &stmt, NULL);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_ROW);
const void *retrieved_blob = sqlite3_column_blob(stmt, 0);
int retrieved_size = sqlite3_column_bytes(stmt, 0);
assert(retrieved_size == blob_size);
assert(memcmp(blob_data, retrieved_blob, blob_size) == 0);
printf("Test passed: BLOB inserted and retrieved correctly (size=%d)\n", retrieved_size);
sqlite3_finalize(stmt);
sqlite3_close(db);
}