mirror of
https://github.com/janet-lang/janet
synced 2024-12-26 00:10:27 +00:00
Add sqlite3 native module to repo.
This commit is contained in:
parent
06b80e56eb
commit
181a38f412
17
natives/sqlite3/.gitignore
vendored
Normal file
17
natives/sqlite3/.gitignore
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
# Created by https://www.gitignore.io/api/cmake
|
||||||
|
|
||||||
|
### CMake ###
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Testing
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
CTestTestfile.cmake
|
||||||
|
build
|
||||||
|
|
||||||
|
|
||||||
|
# End of https://www.gitignore.io/api/cmake
|
34
natives/sqlite3/CMakeLists.txt
Normal file
34
natives/sqlite3/CMakeLists.txt
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.7)
|
||||||
|
project(dstsqlite3)
|
||||||
|
|
||||||
|
# Set Some Variables
|
||||||
|
set(TARGET_NAME "dstsqlite3")
|
||||||
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
|
||||||
|
|
||||||
|
set(SOURCES main.c sqlite3.c sqlite3.h)
|
||||||
|
|
||||||
|
include_directories(../../src/include)
|
||||||
|
|
||||||
|
# Build the module
|
||||||
|
add_library(${TARGET_NAME} SHARED ${SOURCES})
|
132
natives/sqlite3/main.c
Normal file
132
natives/sqlite3/main.c
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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 = {
|
||||||
|
":sqlite3.conn",
|
||||||
|
gcsqlite,
|
||||||
|
NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Open a new database connection */
|
||||||
|
static int sql_open(DstArgs args) {
|
||||||
|
sqlite3 **conn;
|
||||||
|
const uint8_t *filename;
|
||||||
|
int status;
|
||||||
|
dst_fixarity(args, 1);
|
||||||
|
dst_arg_string(filename, args, 0);
|
||||||
|
conn = (sqlite3 **) dst_abstract(&sql_conn_type, sizeof(sqlite3 *));
|
||||||
|
status = sqlite3_open((const char *)filename, conn);
|
||||||
|
if (status == SQLITE_OK) {
|
||||||
|
return dst_return(args, dst_wrap_abstract(conn));
|
||||||
|
} else {
|
||||||
|
const char *err = sqlite3_errmsg(*conn);
|
||||||
|
return dst_throw(args, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sql_close(DstArgs args) {
|
||||||
|
sqlite3 **conn;
|
||||||
|
int status;
|
||||||
|
dst_fixarity(args, 1);
|
||||||
|
dst_checkabstract(args, 0, &sql_conn_type);
|
||||||
|
conn = (sqlite3 **)dst_unwrap_abstract(args.v[0]);
|
||||||
|
status = sqlite3_close_v2(*conn);
|
||||||
|
if (status == SQLITE_OK) {
|
||||||
|
return dst_return(args, dst_wrap_nil());
|
||||||
|
} else {
|
||||||
|
return dst_throw(args, "unable to close the sqlite3 connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sql_execute_callback(void *rowsp, int n, char **vals, char **colnames) {
|
||||||
|
int i;
|
||||||
|
DstArray *rows = (DstArray *)rowsp;
|
||||||
|
DstTable *row = dst_table(n);
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
dst_table_put(row, dst_cstringv(colnames[i]), dst_cstringv(vals[i]));
|
||||||
|
}
|
||||||
|
dst_array_push(rows, dst_wrap_table(row));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sql_execute(DstArgs args) {
|
||||||
|
int status;
|
||||||
|
char *errmsg = "connection closed";
|
||||||
|
const uint8_t *str;
|
||||||
|
DstArray *rows;
|
||||||
|
sqlite3 **conn;
|
||||||
|
dst_fixarity(args, 2);
|
||||||
|
dst_checkabstract(args, 0, &sql_conn_type);
|
||||||
|
conn = (sqlite3 **)dst_unwrap_abstract(args.v[0]);
|
||||||
|
dst_arg_string(str, args, 1);
|
||||||
|
rows = dst_array(10);
|
||||||
|
status = sqlite3_exec(
|
||||||
|
*conn,
|
||||||
|
(const char *)str,
|
||||||
|
sql_execute_callback,
|
||||||
|
rows,
|
||||||
|
&errmsg);
|
||||||
|
if (status == SQLITE_OK) {
|
||||||
|
return dst_return(args, dst_wrap_array(rows));
|
||||||
|
} else {
|
||||||
|
return dst_throw(args, errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
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},
|
||||||
|
{"execute", sql_execute},
|
||||||
|
/*{"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;
|
||||||
|
}
|
209941
natives/sqlite3/sqlite3.c
Normal file
209941
natives/sqlite3/sqlite3.c
Normal file
File diff suppressed because it is too large
Load Diff
11177
natives/sqlite3/sqlite3.h
Normal file
11177
natives/sqlite3/sqlite3.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -237,7 +237,7 @@ static void dst_deinit_block(DstGCMemoryHeader *block) {
|
|||||||
case DST_MEMORY_ABSTRACT:
|
case DST_MEMORY_ABSTRACT:
|
||||||
if (h->type->gc) {
|
if (h->type->gc) {
|
||||||
if (h->type->gc((void *)(h + 1), h->size)) {
|
if (h->type->gc((void *)(h + 1), h->size)) {
|
||||||
/* finalizer failed. try again later? */
|
/* finalizer failed. try again later? Panic? For now do nothing. */
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -257,9 +257,9 @@ int dst_arity_err(DstArgs args, int32_t n, const char *prefix) {
|
|||||||
prefix, n, n == 1 ? "" : "s", args.n)));
|
prefix, n, n == 1 ? "" : "s", args.n)));
|
||||||
}
|
}
|
||||||
|
|
||||||
int dst_typeabstract_err(DstArgs args, int32_t n, DstAbstractType *at) {
|
int dst_typeabstract_err(DstArgs args, int32_t n, const DstAbstractType *at) {
|
||||||
return dst_throwv(args,
|
return dst_throwv(args,
|
||||||
dst_wrap_string(dst_formatc(
|
dst_wrap_string(dst_formatc(
|
||||||
"bad argument #%d, expected %t, got %s",
|
"bad argument #%d, expected %s, got %s",
|
||||||
n, at->name, typestr(args, n))));
|
n, at->name, typestr(args, n))));
|
||||||
}
|
}
|
||||||
|
@ -201,7 +201,7 @@ DstTable *dst_stl_env(void);
|
|||||||
int dst_arity_err(DstArgs args, int32_t n, const char *prefix);
|
int dst_arity_err(DstArgs args, int32_t n, const char *prefix);
|
||||||
int dst_type_err(DstArgs args, int32_t n, DstType expected);
|
int dst_type_err(DstArgs args, int32_t n, DstType expected);
|
||||||
int dst_typemany_err(DstArgs args, int32_t n, int expected);
|
int dst_typemany_err(DstArgs args, int32_t n, int expected);
|
||||||
int dst_typeabstract_err(DstArgs args, int32_t n, DstAbstractType *at);
|
int dst_typeabstract_err(DstArgs args, int32_t n, const DstAbstractType *at);
|
||||||
#define dst_throw(a, e) (*((a).ret) = dst_cstringv(e), 1)
|
#define dst_throw(a, e) (*((a).ret) = dst_cstringv(e), 1)
|
||||||
#define dst_throwv(a, v) (*((a).ret) = (v), 1)
|
#define dst_throwv(a, v) (*((a).ret) = (v), 1)
|
||||||
#define dst_return(a, v) (*((a).ret) = (v), 0)
|
#define dst_return(a, v) (*((a).ret) = (v), 0)
|
||||||
@ -230,7 +230,7 @@ int dst_typeabstract_err(DstArgs args, int32_t n, DstAbstractType *at);
|
|||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define dst_checkabstract(A, N, AT) do {\
|
#define dst_checkabstract(A, N, AT) do {\
|
||||||
if ((A).n <= (N) || !dst_checktype() {\
|
if ((A).n > (N)) {\
|
||||||
Dst x = (A).v[(N)];\
|
Dst x = (A).v[(N)];\
|
||||||
if (!dst_checktype(x, DST_ABSTRACT) ||\
|
if (!dst_checktype(x, DST_ABSTRACT) ||\
|
||||||
dst_abstract_type(dst_unwrap_abstract(x)) != (AT))\
|
dst_abstract_type(dst_unwrap_abstract(x)) != (AT))\
|
||||||
|
Loading…
Reference in New Issue
Block a user