Skip to content

Commit

Permalink
add custom_extensions function in a new module
Browse files Browse the repository at this point in the history
for custom (non-standard) SQLite3 extension functions that may be
added in the future

with some additional explanatory comments added to the CoffeeScript
  • Loading branch information
Christopher J. Brody committed Jan 9, 2020
1 parent e810a2c commit e316007
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ EMFLAGS_DEBUG = \
-s ASSERTIONS=1 \
-O1

BITCODE_FILES = out/sqlite3.bc out/extension-functions.bc
BITCODE_FILES = out/sqlite3.bc out/extension-functions.bc out/custom_extensions.bc

OUTPUT_WRAPPER_FILES = src/shell-pre.js src/shell-post.js

all: optimized debug worker
Expand Down Expand Up @@ -145,6 +146,10 @@ out/extension-functions.bc: sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTI
mkdir -p out
$(EMCC) $(CFLAGS) -s LINKABLE=1 sqlite-src/$(SQLITE_AMALGAMATION)/extension-functions.c -o $@

out/custom_extensions.bc: src/custom_extensions/custom_extensions.c
mkdir -p out
$(EMCC) $(CFLAGS) -Isqlite-src/$(SQLITE_AMALGAMATION) -s LINKABLE=1 $^ -o $@

# TODO: This target appears to be unused. If we re-instatate it, we'll need to add more files inside of the JS folder
# module.tar.gz: test package.json AUTHORS README.md dist/sql-asm.js
# tar --create --gzip $^ > $@
Expand Down
6 changes: 6 additions & 0 deletions src/api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,13 @@ class Database
constructor: (data) ->
@filename = 'dbfile_' + (0xffffffff*Math.random()>>>0)
if data? then FS.createDataFile '/', @filename, data, true, true
# open the database and register extension functions
@handleError sqlite3_open @filename, apiTemp
@db = getValue(apiTemp, 'i32')
# register built-in extension functions:
RegisterExtensionFunctions(@db)
# register any custom (non-standard) extension functions:
custom_extensions(@db)
@statements = {} # A list of all prepared statements of the database
@functions = {} # A list of all user function of the database (created by create_function call)

Expand Down Expand Up @@ -404,6 +408,8 @@ class Database
@functions={}
@handleError sqlite3_close_v2 @db
binaryDb = FS.readFile @filename, encoding:'binary'
# just open the database
# (no need to register extension functions)
@handleError sqlite3_open @filename, apiTemp
@db = getValue apiTemp, 'i32'
binaryDb
Expand Down
8 changes: 8 additions & 0 deletions src/custom_extensions/custom_extensions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "sqlite3.h"

/** FUTURE TBD Build this module with any custom extension module(s) included */

int custom_extensions(sqlite3 * db)
{
return SQLITE_OK;
}
1 change: 1 addition & 0 deletions src/exported_functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
"_sqlite3_result_int",
"_sqlite3_result_int64",
"_sqlite3_result_error",
"_custom_extensions",
"_RegisterExtensionFunctions"
]
3 changes: 3 additions & 0 deletions src/exports.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ sqlite3_result_int64 = Module['cwrap'] 'sqlite3_result_int64', '', ['number', 'n
sqlite3_result_error = Module['cwrap'] 'sqlite3_result_error', '', ['number', 'string', 'number']
RegisterExtensionFunctions = Module['cwrap'] 'RegisterExtensionFunctions', 'number', ['number']

## Support custom db extensions
custom_extensions = Module['cwrap'] 'custom_extensions', 'number', ['number']

# Export the API
this['SQL'] = {'Database':Database}
Module[i] = this['SQL'][i] for i of this['SQL']

0 comments on commit e316007

Please sign in to comment.