Skip to content

Commit

Permalink
Fix #469 [sql-support] add boolean_as_integer global option
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Apr 17, 2017
1 parent bcd4fde commit 845017c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 34 deletions.
33 changes: 0 additions & 33 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 62 additions & 0 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
10 changes: 9 additions & 1 deletion src/plugins/sql-support/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions tests/plugins.sql-support.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\' ) '
Expand Down

0 comments on commit 845017c

Please sign in to comment.