2018-05-06 03:51:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-05-06 17:28:09 +00:00
|
|
|
#include "sqlite3.c"
|
|
|
|
|
2018-05-06 03:51:29 +00:00
|
|
|
#include "sqlite3.h"
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
|
|
|
|
|
|
|
/* Called to garbage collect a sqlite3 connection */
|
|
|
|
static int gcsqlite(void *p, size_t s) {
|
|
|
|
(void) s;
|
|
|
|
sqlite3 *conn = *((sqlite3 **)p);
|
|
|
|
sqlite3_close_v2(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const DstAbstractType sql_conn_type = {
|
2018-05-08 23:40:28 +00:00
|
|
|
":sqlite3.connection",
|
2018-05-06 03:51:29 +00:00
|
|
|
gcsqlite,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Open a new database connection */
|
|
|
|
static int sql_open(DstArgs args) {
|
|
|
|
sqlite3 **conn;
|
|
|
|
const uint8_t *filename;
|
|
|
|
int status;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_STRING(filename, args, 0);
|
2018-05-06 03:51:29 +00:00
|
|
|
conn = (sqlite3 **) dst_abstract(&sql_conn_type, sizeof(sqlite3 *));
|
|
|
|
status = sqlite3_open((const char *)filename, conn);
|
|
|
|
if (status == SQLITE_OK) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_ABSTRACT(args, conn);
|
2018-05-06 03:51:29 +00:00
|
|
|
} else {
|
|
|
|
const char *err = sqlite3_errmsg(*conn);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, err);
|
2018-05-06 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sql_close(DstArgs args) {
|
|
|
|
sqlite3 **conn;
|
|
|
|
int status;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &sql_conn_type);
|
2018-05-06 03:51:29 +00:00
|
|
|
conn = (sqlite3 **)dst_unwrap_abstract(args.v[0]);
|
|
|
|
status = sqlite3_close_v2(*conn);
|
|
|
|
if (status == SQLITE_OK) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_NIL(args);
|
2018-05-06 03:51:29 +00:00
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "unable to close the sqlite3 connection");
|
2018-05-06 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sql_execute_callback(void *rowsp, int n, char **vals, char **colnames) {
|
|
|
|
int i;
|
|
|
|
DstArray *rows = (DstArray *)rowsp;
|
2018-05-08 23:40:28 +00:00
|
|
|
DstKV *row = dst_struct_begin(n);
|
2018-05-06 03:51:29 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2018-05-08 23:40:28 +00:00
|
|
|
dst_struct_put(row, dst_cstringv(colnames[i]), dst_cstringv(vals[i]));
|
2018-05-06 03:51:29 +00:00
|
|
|
}
|
2018-05-08 23:40:28 +00:00
|
|
|
dst_array_push(rows, dst_wrap_struct(dst_struct_end(row)));
|
2018-05-06 03:51:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:40:28 +00:00
|
|
|
static int sql_sql(DstArgs args) {
|
2018-05-06 03:51:29 +00:00
|
|
|
int status;
|
|
|
|
char *errmsg = "connection closed";
|
|
|
|
const uint8_t *str;
|
|
|
|
DstArray *rows;
|
|
|
|
sqlite3 **conn;
|
2018-05-17 17:34:11 +00:00
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &sql_conn_type);
|
2018-05-06 03:51:29 +00:00
|
|
|
conn = (sqlite3 **)dst_unwrap_abstract(args.v[0]);
|
2018-05-17 17:34:11 +00:00
|
|
|
DST_ARG_STRING(str, args, 1);
|
2018-05-06 03:51:29 +00:00
|
|
|
rows = dst_array(10);
|
|
|
|
status = sqlite3_exec(
|
|
|
|
*conn,
|
|
|
|
(const char *)str,
|
|
|
|
sql_execute_callback,
|
|
|
|
rows,
|
|
|
|
&errmsg);
|
|
|
|
if (status == SQLITE_OK) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_ARRAY(args, rows);
|
2018-05-06 03:51:29 +00:00
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, errmsg);
|
2018-05-06 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
static int sql_tables(DstArgs args) {}
|
|
|
|
static int sql_changes(DstArgs args) {}
|
|
|
|
static int sql_timeout(DstArgs args) {}
|
|
|
|
static int sql_rowid(DstArgs args) {}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static const DstReg cfuns[] = {
|
|
|
|
{"open", sql_open},
|
|
|
|
{"close", sql_close},
|
2018-05-08 23:40:28 +00:00
|
|
|
{"sql", sql_sql},
|
2018-05-06 03:51:29 +00:00
|
|
|
/*{"tables", sql_tables},*/
|
|
|
|
/*{"changes", sql_changes},*/
|
|
|
|
/*{"timeout", sql_timeout},*/
|
|
|
|
/*{"rowid", sql_rowid},*/
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
int _dst_init(DstArgs args) {
|
|
|
|
DstTable *env = dst_env_arg(args);
|
|
|
|
dst_env_cfuns(env, cfuns);
|
|
|
|
return 0;
|
|
|
|
}
|