8. sqlite open (in graforge)
The sqlite_open
operation allows a sqlite handle to be
opened and managed inside of a graforge patch.
This is primarily intended to be used with SQLar, but since opening SQLar archives is just open a SQLite database, figured I might as well generalize this baby.
8.1. monolith_sqlite_open
The lowest level function for this is
monolith_sqlite_open
as a C function. Provided is the
graforge patch, as well the filename.
This will attempt to open and store a SQLite database
filehandle, and then return the sqlite3
pointer to the
instance. If something wrong happens, NULL
is returned.
The SQLite handle is appended as a graforge pointer, with a destroy function that will close the SQLite handle. Like all graforge pointers, this gets called when the patch is destroyed.
sqlite3 * monolith_sqlite_open_pw(gf_patch *pw,
const char *fname);
static void free_sqlite(gf_pointer *p)
{
sqlite3 *db;
db = gf_pointer_data(p);
sqlite3_close(db);
}
sqlite3 * monolith_sqlite_open_pw(gf_patch *pw,
const char *fname)
{
sqlite3 *db;
int rc;
rc = sqlite3_open(fname, &db);
if (rc) return NULL;
gf_patch_append_userdata(pw, free_sqlite, db);
return db;
}
8.2. sqlite_open in runt
the sqlite_open
runt word wraps monolith_sqlite_open
inside of a struct.
The loader for this is load_sqlite_open
.
int load_sqlite_open(runt_vm *vm, runt_ptr pw);
<<rproc_sqlite_open>>
int load_sqlite_open(runt_vm *vm, runt_ptr pw)
{
runt_cell *c;
runt_keyword_define(vm,
"sqlite_open", 11,
rproc_sqlite_open, &c);
runt_cell_data(vm, c, pw);
<<monsqlite_word_entry>>
return runt_is_alive(vm);
}
static runt_int rproc_sqlite_open(runt_vm *vm, runt_ptr p)
{
sqlite3 *db;
gf_patch *pw;
int rc;
runt_stacklet *s;
const char *filename;
rc = runt_ppop(vm, &s);
RUNT_ERROR_CHECK(rc);
filename = runt_to_string(s->p);
if (filename == NULL) {
runt_print(vm, "This probably wasn't a string\n");
return RUNT_NOT_OK;
}
pw = runt_to_cptr(p);
db = monolith_sqlite_open_pw(pw, filename);
if (db == NULL) {
runt_print(vm, "There was a problem opening the SQLite file\n");
return RUNT_NOT_OK;
}
rc = runt_ppush(vm, &s);
RUNT_ERROR_CHECK(rc);
rgf_stacklet_sqlite(vm, s, db);
return RUNT_OK;
}
prev | home | next