Skip to content

Commit 84da2b4

Browse files
committed
Merge branch 'master' into extension_functions
2 parents e9f7bc9 + e41ef2e commit 84da2b4

File tree

10 files changed

+75232
-92434
lines changed

10 files changed

+75232
-92434
lines changed

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Ophir LOJKINE <[email protected]> (https://github.com/lovasoa)
22
@kripken
33
@hankinsoft
44
@firien
5-
5+
@dinedal

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ EMCC=$(EMSCRIPTEN)/emcc
66

77
CFLAGS=-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DLONGDOUBLE_TYPE=double -DSQLITE_INT64_TYPE="long long int" -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS
88

9-
all: js/sql.js
9+
all: js/sql.js js/sql-debug.js js/sql-worker.js
1010

1111
# RESERVED_FUNCTION_POINTERS setting is used for registering custom functions
1212
debug: EMFLAGS= -O1 -g -s INLINING_LIMIT=10 -s RESERVED_FUNCTION_POINTERS=64
@@ -25,12 +25,12 @@ js/sql%-raw.js: c/sqlite3.bc js/api.js exported_functions c/extension-functions.
2525
$(EMCC) $(EMFLAGS) -s EXPORTED_FUNCTIONS=@exported_functions c/extension-functions.bc c/sqlite3.bc --post-js js/api.js -o $@
2626

2727
js/api.js: coffee/api.coffee coffee/exports.coffee coffee/api-data.coffee
28-
coffee --bare --compile --join $@ --compile $^
28+
cat $^ | coffee --bare --compile --stdio > $@
2929

3030
# Web worker API
3131
worker: js/worker.sql.js
3232
js/worker.js: coffee/worker.coffee
33-
coffee --bare --compile --join $@ --compile $^
33+
cat $^ | coffee --bare --compile --stdio > $@
3434

3535
js/worker.sql.js: js/sql.js js/worker.js
3636
cat $^ > $@

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
4848

4949
// You can also use javascript functions inside your SQL code
5050
// Create the js function you need
51-
// (Custom function can only return null, a string, or a number)
5251
function add(a, b) {return a+b;}
5352
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
54-
db.create_function("add_js", 2, add);
53+
db.create_function("add_js", add);
5554
// Run a query in which the function is used
56-
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));");
55+
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
5756

5857
// free the memory used by the statement
5958
stmt.free();

coffee/api.coffee

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,16 +428,15 @@ class Database
428428
throw new Error(errmsg)
429429

430430
### Register a custom function with SQLite
431+
@example Register a simple function
432+
db.create_function("addOne", function(x) {return x+1;})
433+
db.exec("SELECT addOne(1)") // = 2
434+
431435
@param name [String] the name of the function as referenced in SQL statements.
432-
@param numParams [Number] the number of parameters the function expects.
433436
@param func [Function] the actual function to be executed.
434-
@param dbg_func [Function, optional] useful for breakpointing and testing the wrapper function.
435437
###
436-
'create_function': (name, numParams, func, dbg_func=(->)) ->
438+
'create_function': (name, func) ->
437439
wrapped_func = (cx, argc, argv) ->
438-
# Debug function, useful for breakpointing if you need to debug this
439-
dbg_func()
440-
441440
# Parse the args from sqlite into JS objects
442441
args = []
443442
for i in [0..argc]
@@ -471,5 +470,5 @@ class Database
471470

472471
# Generate a pointer to the wrapped, user defined function, and register with SQLite.
473472
func_ptr = Runtime.addFunction(wrapped_func)
474-
@handleError sqlite3_create_function_v2 @db, name, numParams, SQLite.UTF8, 0, func_ptr, 0, 0, 0
473+
@handleError sqlite3_create_function_v2 @db, name, func.length, SQLite.UTF8, 0, func_ptr, 0, 0, 0
475474
return @

coffee/worker.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ if typeof importScripts is 'function' # Detect webworker context
4040
try
4141
postMessage result, [result]
4242
catch err # Some browsers fail when trying to use transferable objects
43-
console.log(err)
4443
postMessage result
4544
when 'close'
4645
db?.close()

js/sql-debug.js

Lines changed: 74626 additions & 92309 deletions
Large diffs are not rendered by default.

js/sql.js

Lines changed: 56 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/worker.sql.js

Lines changed: 481 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sql.js",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "SQLite library with support for opening and writing databases, prepared statements, and more. This SQLite library is in pure javascript (compiled with emscripten).",
55
"keywords": ["sql", "sqlite", "stand-alone", "relational", "database", "RDBMS", "data", "query", "statement", "emscripten", "asm", "asm.js"],
66
"license": "MIT",

test/test_functions.js

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,61 @@
11
exports.test = function(SQL, assert){
2-
var db = new SQL.Database();
3-
db.exec("CREATE TABLE test (data); INSERT INTO test VALUES ('Hello World');");
4-
5-
// Simple function, appends extra text on a string.
6-
function test_function(string_arg) {
7-
return "Function called with: " + string_arg;
8-
};
9-
10-
// Register with SQLite.
11-
db.create_function("TestFunction", 1, test_function);
12-
13-
// Use in a query, check expected result.
14-
var result = db.exec("SELECT TestFunction(data) FROM test;");
15-
var result_str = result[0]["values"][0][0];
16-
assert.equal(result_str, "Function called with: Hello World");
17-
18-
// 2 arg function, adds two ints together.
19-
db.exec("CREATE TABLE test2 (int1, int2); INSERT INTO test2 VALUES (456, 789);");
20-
21-
function test_add(int1, int2) {
22-
return int1 + int2;
23-
};
24-
25-
db.create_function("TestAdd", 2, test_add);
26-
result = db.exec("SELECT TestAdd(int1, int2) FROM test2;");
27-
result_int = result[0]["values"][0][0];
28-
assert.equal(result_int, 1245);
29-
30-
// Binary data function, tests which byte in a column is set to 0
31-
db.exec("CREATE TABLE test3 (data); INSERT INTO test3 VALUES (x'6100ff'), (x'ffffff00ffff');");
32-
33-
function test_zero_byte_index(data) {
34-
// Data is a Uint8Array
35-
for (var i=0; i<data.length; i++) {
36-
if (data[i] === 0) {
37-
return i;
38-
}
39-
}
40-
return -1;
41-
};
42-
43-
db.create_function("TestZeroByteIndex", 1, test_zero_byte_index);
44-
result = db.exec("SELECT TestZeroByteIndex(data) FROM test3;");
45-
result_int0 = result[0]["values"][0][0];
46-
result_int1 = result[0]["values"][1][0];
47-
assert.equal(result_int0, 1);
48-
assert.equal(result_int1, 3);
49-
50-
db.close();
2+
var db = new SQL.Database();
3+
db.exec("CREATE TABLE test (data); INSERT INTO test VALUES ('Hello World');");
4+
5+
// Simple function, appends extra text on a string.
6+
function test_function(string_arg) {
7+
return "Function called with: " + string_arg;
8+
};
9+
10+
// Register with SQLite.
11+
db.create_function("TestFunction", test_function);
12+
13+
// Use in a query, check expected result.
14+
var result = db.exec("SELECT TestFunction(data) FROM test;");
15+
var result_str = result[0]["values"][0][0];
16+
assert.equal(result_str, "Function called with: Hello World", "Named functions can be registered");
17+
18+
// 2 arg function, adds two ints together.
19+
db.exec("CREATE TABLE test2 (int1, int2); INSERT INTO test2 VALUES (456, 789);");
20+
21+
function test_add(int1, int2) {
22+
return int1 + int2;
23+
};
24+
25+
db.create_function("TestAdd", test_add);
26+
result = db.exec("SELECT TestAdd(int1, int2) FROM test2;");
27+
result_int = result[0]["values"][0][0];
28+
assert.equal(result_int, 1245, "Multiple argument functions can be registered");
29+
30+
// Binary data function, tests which byte in a column is set to 0
31+
db.exec("CREATE TABLE test3 (data); INSERT INTO test3 VALUES (x'6100ff'), (x'ffffff00ffff');");
32+
33+
function test_zero_byte_index(data) {
34+
// Data is a Uint8Array
35+
for (var i=0; i<data.length; i++) {
36+
if (data[i] === 0) {
37+
return i;
38+
}
39+
}
40+
return -1;
41+
};
42+
43+
db.create_function("TestZeroByteIndex", test_zero_byte_index);
44+
result = db.exec("SELECT TestZeroByteIndex(data) FROM test3;");
45+
result_int0 = result[0]["values"][0][0];
46+
result_int1 = result[0]["values"][1][0];
47+
assert.equal(result_int0, 1, "Binary data works inside functions");
48+
assert.equal(result_int1, 3, "Binary data works inside functions");
49+
50+
db.create_function("addOne", function (x) { return x + 1;} );
51+
result = db.exec("SELECT addOne(1);");
52+
assert.equal(result[0]["values"][0][0], 2, "Accepts anonymous functions");
53+
54+
db.close();
5155
};
5256

5357
if (module == require.main) {
54-
var sql = require('../js/sql.js');
55-
var assert = require("assert");
56-
exports.test(sql, assert);
58+
var sql = require('../js/sql.js');
59+
var assert = require("assert");
60+
exports.test(sql, assert);
5761
}

0 commit comments

Comments
 (0)