initial commit

This commit is contained in:
2025-12-13 03:35:34 +05:00
commit 16d44cf730
15 changed files with 766 additions and 0 deletions

109
src/statement.c Normal file
View File

@@ -0,0 +1,109 @@
#include "tsqlite.h"
Result(tsqlite_statement*) tsqlite_statement_prepare(sqlite3* conn, str sql_code){
sqlite3_stmt* st = NULL;
i32 flags = SQLITE_PREPARE_PERSISTENT;
try_sqlite3(sqlite3_prepare_v3(conn, sql_code.data, sql_code.len, flags, &st, NULL));
tsqlite_statement* self = malloc(sizeof(*self));
zeroStruct(self);
self->st = st;
return RESULT_VALUE(p, self);
}
void tsqlite_statement_free(tsqlite_statement* self){
if(!self)
return;
sqlite3_finalize(self->st);
free(self);
}
Result(void) tsqlite_statement_reset(tsqlite_statement* self){
try_sqlite3(sqlite3_reset(self->st));
self->bind_arg_pos = 0;
self->result_row = 0;
self->result_col = 0;
return RESULT_VOID;
}
Result(void) tsqlite_statement_bind_null(tsqlite_statement* self){
try_sqlite3(sqlite3_bind_null(self->st, self->bind_arg_pos));
self->bind_arg_pos++;
return RESULT_VOID;
}
Result(void) tsqlite_statement_bind_i32(tsqlite_statement* self, i32 v){
try_sqlite3(sqlite3_bind_int(self->st, self->bind_arg_pos, v));
self->bind_arg_pos++;
return RESULT_VOID;
}
Result(void) tsqlite_statement_bind_i64(tsqlite_statement* self, i64 v){
try_sqlite3(sqlite3_bind_int64(self->st, self->bind_arg_pos, v));
self->bind_arg_pos++;
return RESULT_VOID;
}
Result(void) tsqlite_statement_bind_f64(tsqlite_statement* self, f64 v){
try_sqlite3(sqlite3_bind_double(self->st, self->bind_arg_pos, v));
self->bind_arg_pos++;
return RESULT_VOID;
}
Result(void) tsqlite_statement_bind_str(tsqlite_statement* self, str v, NULLABLE(Destructor_t) d){
try_sqlite3(sqlite3_bind_text(self->st, self->bind_arg_pos, v.data, v.len, d));
self->bind_arg_pos++;
return RESULT_VOID;
}
Result(void) tsqlite_statement_bind_blob(tsqlite_statement* self, Array(u8) v, NULLABLE(Destructor_t) d){
try_sqlite3(sqlite3_bind_blob(self->st, self->bind_arg_pos, v.data, v.len, d));
self->bind_arg_pos++;
return RESULT_VOID;
}
Result(void) tsqlite_statement_bind_zeroblob(tsqlite_statement* self, i32 size){
try_sqlite3(sqlite3_bind_zeroblob(self->st, self->bind_arg_pos, size));
self->bind_arg_pos++;
return RESULT_VOID;
}
Result(bool) sqlite3_statement_moveNext(tsqlite_statement* self){
int r = sqlite3_step(self->st);
if(r == SQLITE_ROW){
return RESULT_VALUE(i, true);
}
if(r == SQLITE_DONE){
return RESULT_VALUE(i, false);
}
return RESULT_ERROR_SQLITE_CODE(r);
}
Result(i64) tsqlite_statement_getResult_i64(tsqlite_statement* self){
i64 r = sqlite3_column_int64(self->st, self->result_col);
// TODO: error checking in sqlite3_column
self->result_col++;
return RESULT_VALUE(i, r);
}
Result(i64) tsqlite_statement_getResult_f64(tsqlite_statement* self){
f64 r = sqlite3_column_double(self->st, self->result_col);
// TODO: error checking in sqlite3_column
self->result_col++;
return RESULT_VALUE(f, r);
}
Result(void) tsqlite_statement_getResult_str(tsqlite_statement* self, str* out_v){
(void)self;
(void)out_v;
// TODO: tsqlite_statement_getResult_str
return RESULT_VOID;
}
Result(void) tsqlite_statement_getResult_blob(tsqlite_statement* self, Array(u8)* out_v){
(void)self;
(void)out_v;
// TODO: tsqlite_statement_getResult_blob
return RESULT_VOID;
}