69 lines
1.9 KiB
C
69 lines
1.9 KiB
C
#include "tsqlite.h"
|
|
|
|
#define _create_statement(SQL, TMP_VAR){\
|
|
try(tsqlite_statement* TMP_VAR, p, tsqlite_statement_compile(conn, STR(SQL)));\
|
|
Defer(tsqlite_statement_free(TMP_VAR));\
|
|
st = TMP_VAR;\
|
|
}
|
|
#define create_statement(SQL) _create_statement(SQL, CAT2(_st_, __LINE__))
|
|
|
|
#define exec_statement_no_result() \
|
|
printf("executing SQL statement:\n%s\n", sqlite3_sql(st->st));\
|
|
try_void(tsqlite_statement_step(st));\
|
|
|
|
Result(void) test_connection(){
|
|
Deferral(8);
|
|
|
|
try(tsqlite_connection* conn, p, tsqlite_connection_open("db.sqlite"));
|
|
Defer(tsqlite_connection_close(conn));
|
|
|
|
tsqlite_statement* st;
|
|
create_statement("DROP TABLE IF EXISTS Test;");
|
|
exec_statement_no_result();
|
|
|
|
create_statement(
|
|
"CREATE TABLE Test (\n"
|
|
" id INTEGER PRIMARY KEY,\n"
|
|
" a TEXT\n"
|
|
");");
|
|
exec_statement_no_result();
|
|
|
|
create_statement(
|
|
"INSERT INTO Test(a) VALUES\n"
|
|
" ('ooeeoo'),\n"
|
|
" ('wiwiwi');");
|
|
exec_statement_no_result();
|
|
|
|
create_statement("SELECT * FROM Test WHERE id IS NOT $nul;");
|
|
try_void(tsqlite_statement_bind_null(st, "$nul"));
|
|
printf("executing SQL statement:\n%s\n", sqlite3_sql(st->st));
|
|
while(true) {
|
|
try(bool has_result, i, tsqlite_statement_step(st));
|
|
if(!has_result)
|
|
break;
|
|
i32 column_count = sqlite3_column_count(st->st);
|
|
str cell_str;
|
|
printf("ROW(%i):", st->result_row);
|
|
for(i32 i = 0; i < column_count; i++){
|
|
try_void(tsqlite_statement_getResult_str(st, &cell_str));
|
|
printf(" [%i]='"FMT_str"'", st->result_col, str_unwrap(cell_str));
|
|
}
|
|
printf("\n");
|
|
};
|
|
|
|
Return RESULT_VOID;
|
|
}
|
|
|
|
int main(){
|
|
Deferral(32);
|
|
|
|
try_fatal_void(tlibc_init());
|
|
try_fatal_void(tsqlite_init());
|
|
Defer(tlibc_deinit());
|
|
Defer(tsqlite_deinit());
|
|
|
|
try_fatal_void(test_connection());
|
|
|
|
Return 0;
|
|
}
|