Skip to content

Commit 1adfc37

Browse files
author
Paul Bergeron
committed
Remove the dbg_func and numParams options from Database.create_function, adds better docs for function creation.
Closes sql-js#139
1 parent c555301 commit 1adfc37

File tree

6 files changed

+575
-516
lines changed

6 files changed

+575
-516
lines changed

coffee/api.coffee

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

429429
### Register a custom function with SQLite
430+
@example Register a simple function
431+
db.create_function("addOne", (x) -> x + 1)
432+
db.exec("SELECT addOne(1)") # 2
433+
430434
@param name [String] the name of the function as referenced in SQL statements.
431-
@param numParams [Number] the number of parameters the function expects.
432435
@param func [Function] the actual function to be executed.
433-
@param dbg_func [Function, optional] useful for breakpointing and testing the wrapper function.
434436
###
435-
'create_function': (name, numParams, func, dbg_func=(->)) ->
437+
'create_function': (name, func) ->
436438
wrapped_func = (cx, argc, argv) ->
437-
# Debug function, useful for breakpointing if you need to debug this
438-
dbg_func()
439-
440439
# Parse the args from sqlite into JS objects
441440
args = []
442441
for i in [0..argc]
@@ -470,5 +469,5 @@ class Database
470469

471470
# Generate a pointer to the wrapped, user defined function, and register with SQLite.
472471
func_ptr = Runtime.addFunction(wrapped_func)
473-
@handleError sqlite3_create_function_v2 @db, name, numParams, SQLite.UTF8, 0, func_ptr, 0, 0, 0
472+
@handleError sqlite3_create_function_v2 @db, name, func.length, SQLite.UTF8, 0, func_ptr, 0, 0, 0
474473
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: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275353,20 +275353,18 @@ Database = (function() {
275353275353

275354275354

275355275355
/* Register a custom function with SQLite
275356+
@example Register a simple function
275357+
db.create_function("addOne", (x) -> x + 1)
275358+
db.exec("SELECT addOne(1)") # 2
275359+
275356275360
@param name [String] the name of the function as referenced in SQL statements.
275357-
@param numParams [Number] the number of parameters the function expects.
275358275361
@param func [Function] the actual function to be executed.
275359-
@param dbg_func [Function, optional] useful for breakpointing and testing the wrapper function.
275360275362
*/
275361275363

275362-
Database.prototype['create_function'] = function(name, numParams, func, dbg_func) {
275364+
Database.prototype['create_function'] = function(name, func) {
275363275365
var func_ptr, wrapped_func;
275364-
if (dbg_func == null) {
275365-
dbg_func = (function() {});
275366-
}
275367275366
wrapped_func = function(cx, argc, argv) {
275368275367
var arg, args, data_func, i, k, ref, result, value_ptr, value_type;
275369-
dbg_func();
275370275368
args = [];
275371275369
for (i = k = 0, ref = argc; 0 <= ref ? k <= ref : k >= ref; i = 0 <= ref ? ++k : --k) {
275372275370
value_ptr = getValue(argv + (4 * i), 'i32');
@@ -275412,7 +275410,7 @@ Database = (function() {
275412275410
}
275413275411
};
275414275412
func_ptr = Runtime.addFunction(wrapped_func);
275415-
this.handleError(sqlite3_create_function_v2(this.db, name, numParams, SQLite.UTF8, 0, func_ptr, 0, 0, 0));
275413+
this.handleError(sqlite3_create_function_v2(this.db, name, func.length, SQLite.UTF8, 0, func_ptr, 0, 0, 0));
275416275414
return this;
275417275415
};
275418275416

0 commit comments

Comments
 (0)