Skip to content

Commit

Permalink
Database constructor with custom FS
Browse files Browse the repository at this point in the history
  • Loading branch information
phiresky committed Apr 16, 2021
1 parent f5d9f54 commit 837d25d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CFLAGS = \
-DSQLITE_DISABLE_LFS \
-DSQLITE_ENABLE_FTS3 \
-DSQLITE_ENABLE_FTS3_PARENTHESIS \
-DSQLITE_ENABLE_FTS5 \
-DSQLITE_ENABLE_JSON1 \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_ENABLE_NORMALIZE
Expand Down Expand Up @@ -54,8 +55,7 @@ EMFLAGS_WASM = \
EMFLAGS_OPTIMIZED= \
-s INLINING_LIMIT=50 \
-O3 \
-flto \
--closure 1
-flto

EMFLAGS_DEBUG = \
-s INLINING_LIMIT=10 \
Expand Down
2 changes: 1 addition & 1 deletion dist/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
*
# Except this file
!.gitignore
!.npmignore
!.npmignore
21 changes: 20 additions & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
var SQLITE_UTF8 = 1;
// var - cwrap function
var sqlite3_open = cwrap("sqlite3_open", "number", ["string", "number"]);
var sqlite3_open_v2 = cwrap("sqlite3_open_v2", "number", ["string", "number", "number", "string"]);
var sqlite3_close_v2 = cwrap("sqlite3_close_v2", "number", ["number"]);
var sqlite3_exec = cwrap(
"sqlite3_exec",
Expand Down Expand Up @@ -323,6 +324,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
}
return true;
};
Statement.prototype["bind_"] = Statement.prototype.bind; // work around https://github.com/GoogleChromeLabs/comlink/blob/4ba8162f6c28fb1bf53b491565ef9a3ae42b72d3/src/comlink.ts#L432

/** Execute the statement, fetching the the next line of result,
that can be retrieved with {@link Statement.get}.
Expand Down Expand Up @@ -790,8 +792,22 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
if (data != null) {
FS.createDataFile("/", this.filename, data, true, true);
}
this.handleError(sqlite3_open(this.filename, apiTemp));
const ret = sqlite3_open(this.filename, apiTemp);
this.db = getValue(apiTemp, "i32");
this.handleError(ret);
registerExtensionFunctions(this.db);
// A list of all prepared statements of the database
this.statements = {};
// A list of all user function of the database
// (created by create_function call)
this.functions = {};
}

function CustomDatabase(filename) {
this.filename = filename;
const ret = sqlite3_open(this.filename, apiTemp);
this.db = getValue(apiTemp, "i32");
this.handleError(ret);
registerExtensionFunctions(this.db);
// A list of all prepared statements of the database
this.statements = {};
Expand Down Expand Up @@ -1200,4 +1216,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {

// export Database to Module
Module.Database = Database;
Module["CustomDatabase"] = CustomDatabase;
Module["FS"] = FS;
CustomDatabase.prototype = Object.create(Database.prototype);
};

0 comments on commit 837d25d

Please sign in to comment.