From 26768f9b5e9abe06225644e56e9a4ed13a6ac615 Mon Sep 17 00:00:00 2001 From: Jon Morehouse Date: Tue, 10 Jun 2014 17:34:49 -0700 Subject: [PATCH 1/3] Implement internal variable for holding custom functions. --- lib/index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/index.js b/lib/index.js index 75d1c2e7..a6285356 100644 --- a/lib/index.js +++ b/lib/index.js @@ -60,6 +60,14 @@ Sql.prototype.setDialect = function(dialect) { return this; }; +Sql.prototype.registerFunctions = function(functions) { + if ( ! typeof functions == "array") { + functions = [functions]; + } + this.customFunctions = functions + return this +} + // back compat shim for the Sql class constructor var create = function(dialect) { return new Sql(dialect); From 60dee15d6bb44c567ff553631200a20c07ee4903 Mon Sep 17 00:00:00 2001 From: Jon Morehouse Date: Tue, 10 Jun 2014 17:59:19 -0700 Subject: [PATCH 2/3] Build out custom functions feature. You can now register functions for your database dialect with sql.registerFunctions --- lib/functions.js | 15 ++++++++++++--- lib/index.js | 9 +++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/functions.js b/lib/functions.js index 8186161a..e906ac38 100644 --- a/lib/functions.js +++ b/lib/functions.js @@ -14,7 +14,7 @@ var getFunctionCallCreator = function(name, sql) { }; // creates a hash of functions for a sql instance -var getFunctions = function(functionNames, sql) { +var _getFunctions = function(functionNames, sql) { var functions = _.reduce(functionNames, function(reducer, name) { reducer[name] = getFunctionCallCreator(name, sql); return reducer; @@ -52,9 +52,18 @@ var hstoreFunction = 'HSTORE'; var standardFunctionNames = aggregateFunctions.concat(scalarFunctions).concat(hstoreFunction); -// creates a hash of standard functions for a sql instance var getStandardFunctions = function(sql) { - return getFunctions(standardFunctionNames, sql); + return _getFunctions(standardFunctionNames, sql); +} + +var getFunctions = function(sql, functionNames) { + var functions = _getFunctions(standardFunctionNames, sql); + if (functionNames !== undefined) { + var customFunctions = _getFunctions(functionNames, sql); + return _.merge(functions, customFunctions); + } + return functions; }; +module.exports.getFunctions = getFunctions; module.exports.getStandardFunctions = getStandardFunctions; diff --git a/lib/index.js b/lib/index.js index a6285356..bfd918ff 100644 --- a/lib/index.js +++ b/lib/index.js @@ -60,11 +60,12 @@ Sql.prototype.setDialect = function(dialect) { return this; }; -Sql.prototype.registerFunctions = function(functions) { - if ( ! typeof functions == "array") { - functions = [functions]; +Sql.prototype.registerFunctions = function(_functions) { + if ( typeof _functions !== "array") { + _functions = [_functions]; } - this.customFunctions = functions + // attach the standard SQL functions to this instance + this.functions = functions.getFunctions(this, _functions); return this } From 162df361b871063868d1c3e456b791f82d252513 Mon Sep 17 00:00:00 2001 From: Jon Morehouse Date: Tue, 10 Jun 2014 21:00:10 -0700 Subject: [PATCH 3/3] Check registerFunctions input against instanceof Array instead of typeof. --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index bfd918ff..adf90011 100644 --- a/lib/index.js +++ b/lib/index.js @@ -61,7 +61,7 @@ Sql.prototype.setDialect = function(dialect) { }; Sql.prototype.registerFunctions = function(_functions) { - if ( typeof _functions !== "array") { + if ( ! _functions instanceof Array) { _functions = [_functions]; } // attach the standard SQL functions to this instance