From 845017c85bf73125a1dea5f657099fdb139cb63e Mon Sep 17 00:00:00 2001 From: mistic100 Date: Mon, 17 Apr 2017 10:30:21 +0200 Subject: [PATCH] Fix #469 [sql-support] add boolean_as_integer global option --- src/core.js | 33 --------------- src/plugins.js | 62 +++++++++++++++++++++++++++++ src/plugins/sql-support/plugin.js | 10 ++++- tests/plugins.sql-support.module.js | 38 ++++++++++++++++++ 4 files changed, 109 insertions(+), 34 deletions(-) diff --git a/src/core.js b/src/core.js index d9f1a85a..b3e2d2ec 100644 --- a/src/core.js +++ b/src/core.js @@ -1,36 +1,3 @@ -/** - * Initializes plugins for an instance - * @throws ConfigError - * @private - */ -QueryBuilder.prototype.initPlugins = function() { - if (!this.plugins) { - return; - } - - if ($.isArray(this.plugins)) { - var tmp = {}; - this.plugins.forEach(function(plugin) { - tmp[plugin] = null; - }); - this.plugins = tmp; - } - - Object.keys(this.plugins).forEach(function(plugin) { - if (plugin in QueryBuilder.plugins) { - this.plugins[plugin] = $.extend(true, {}, - QueryBuilder.plugins[plugin].def, - this.plugins[plugin] || {} - ); - - QueryBuilder.plugins[plugin].fct.call(this, this.plugins[plugin]); - } - else { - Utils.error('Config', 'Unable to find plugin "{0}"', plugin); - } - }, this); -}; - /** * Checks the configuration of each filter * @param {QueryBuilder.Filter[]} filters diff --git a/src/plugins.js b/src/plugins.js index 35108e3e..036f9cc4 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -50,3 +50,65 @@ QueryBuilder.define = function(name, fct, def) { QueryBuilder.extend = function(methods) { $.extend(QueryBuilder.prototype, methods); }; + +/** + * Initializes plugins for an instance + * @throws ConfigError + * @private + */ +QueryBuilder.prototype.initPlugins = function() { + if (!this.plugins) { + return; + } + + if ($.isArray(this.plugins)) { + var tmp = {}; + this.plugins.forEach(function(plugin) { + tmp[plugin] = null; + }); + this.plugins = tmp; + } + + Object.keys(this.plugins).forEach(function(plugin) { + if (plugin in QueryBuilder.plugins) { + this.plugins[plugin] = $.extend(true, {}, + QueryBuilder.plugins[plugin].def, + this.plugins[plugin] || {} + ); + + QueryBuilder.plugins[plugin].fct.call(this, this.plugins[plugin]); + } + else { + Utils.error('Config', 'Unable to find plugin "{0}"', plugin); + } + }, this); +}; + +/** + * Returns the config of a plugin, if the plugin is not loaded, returns the default config. + * @param {string} name + * @param {string} [property] + * @throws ConfigError + * @returns {*} + */ +QueryBuilder.prototype.getPluginOptions = function(name, property) { + var plugin; + if (this.plugins && this.plugins[name]) { + plugin = this.plugins[name]; + } + else if (QueryBuilder.plugins[name]) { + plugin = QueryBuilder.plugins[name].def; + } + + if (plugin) { + if (property) { + return plugin[property]; + } + else { + return plugin; + } + } + else { + Utils.error('Config', 'Unable to find plugin "{0}"', name); + } +}; diff --git a/src/plugins/sql-support/plugin.js b/src/plugins/sql-support/plugin.js index e3676825..6abc101a 100644 --- a/src/plugins/sql-support/plugin.js +++ b/src/plugins/sql-support/plugin.js @@ -2,7 +2,14 @@ * @class SqlSupport * @memberof module:plugins * @description Allows to export rules as a SQL WHERE statement as well as populating the builder from an SQL query. + * @param {object} [options] + * @param {boolean} [options.boolean_as_integer=true] - `true` to convert boolean values to integer in the SQL output */ +QueryBuilder.define('sql-support', function(options) { + +}, { + boolean_as_integer: true +}); QueryBuilder.defaults({ // operators for internal -> SQL conversion @@ -242,6 +249,7 @@ QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ { getSQL: function(stmt, nl, data) { data = (data === undefined) ? this.getRules() : data; nl = !!nl ? '\n' : ' '; + var boolean_as_integer = this.getPluginOptions('sql-support', 'boolean_as_integer'); if (stmt === true) stmt = 'question_mark'; if (typeof stmt == 'string') { @@ -289,7 +297,7 @@ QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ { } if (rule.type == 'integer' || rule.type == 'double' || rule.type == 'boolean') { - v = Utils.changeType(v, rule.type, true); + v = Utils.changeType(v, rule.type, boolean_as_integer); } else if (!stmt) { v = Utils.escapeString(v); diff --git a/tests/plugins.sql-support.module.js b/tests/plugins.sql-support.module.js index a64ecf4a..7d2da34e 100644 --- a/tests/plugins.sql-support.module.js +++ b/tests/plugins.sql-support.module.js @@ -307,6 +307,44 @@ $(function() { ); }); + QUnit.test('Cast booleans', function(assert) { + $b.queryBuilder({ + plugins: { + 'sql-support': { + boolean_as_integer: true + } + }, + filters: [ + { + id: 'done', + type: 'boolean' + } + ], + rules: [ + { + id: 'done', + operator: 'equal', + value: true + } + ] + }); + + assert.rulesMatch( + $b.queryBuilder('getSQL'), + 'done = 1', + 'Should convert boolean value to integer' + ); + + // don't do that in real life ! + $b[0].queryBuilder.plugins['sql-support'].boolean_as_integer = false; + + assert.rulesMatch( + $b.queryBuilder('getSQL'), + 'done = true', + 'Should not convert boolean value to integer' + ); + }); + var basic_rules_sql_raw = { sql: 'price < 10.25 AND name IS NULL AND ( category IN(\'mo\', \'mu\') OR id != \'1234-azer-5678\' ) '