Skip to content

Commit 845017c

Browse files
committed
Fix #469 [sql-support] add boolean_as_integer global option
1 parent bcd4fde commit 845017c

4 files changed

Lines changed: 109 additions & 34 deletions

File tree

src/core.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,3 @@
1-
/**
2-
* Initializes plugins for an instance
3-
* @throws ConfigError
4-
* @private
5-
*/
6-
QueryBuilder.prototype.initPlugins = function() {
7-
if (!this.plugins) {
8-
return;
9-
}
10-
11-
if ($.isArray(this.plugins)) {
12-
var tmp = {};
13-
this.plugins.forEach(function(plugin) {
14-
tmp[plugin] = null;
15-
});
16-
this.plugins = tmp;
17-
}
18-
19-
Object.keys(this.plugins).forEach(function(plugin) {
20-
if (plugin in QueryBuilder.plugins) {
21-
this.plugins[plugin] = $.extend(true, {},
22-
QueryBuilder.plugins[plugin].def,
23-
this.plugins[plugin] || {}
24-
);
25-
26-
QueryBuilder.plugins[plugin].fct.call(this, this.plugins[plugin]);
27-
}
28-
else {
29-
Utils.error('Config', 'Unable to find plugin "{0}"', plugin);
30-
}
31-
}, this);
32-
};
33-
341
/**
352
* Checks the configuration of each filter
363
* @param {QueryBuilder.Filter[]} filters

src/plugins.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,65 @@ QueryBuilder.define = function(name, fct, def) {
5050
QueryBuilder.extend = function(methods) {
5151
$.extend(QueryBuilder.prototype, methods);
5252
};
53+
54+
/**
55+
* Initializes plugins for an instance
56+
* @throws ConfigError
57+
* @private
58+
*/
59+
QueryBuilder.prototype.initPlugins = function() {
60+
if (!this.plugins) {
61+
return;
62+
}
63+
64+
if ($.isArray(this.plugins)) {
65+
var tmp = {};
66+
this.plugins.forEach(function(plugin) {
67+
tmp[plugin] = null;
68+
});
69+
this.plugins = tmp;
70+
}
71+
72+
Object.keys(this.plugins).forEach(function(plugin) {
73+
if (plugin in QueryBuilder.plugins) {
74+
this.plugins[plugin] = $.extend(true, {},
75+
QueryBuilder.plugins[plugin].def,
76+
this.plugins[plugin] || {}
77+
);
78+
79+
QueryBuilder.plugins[plugin].fct.call(this, this.plugins[plugin]);
80+
}
81+
else {
82+
Utils.error('Config', 'Unable to find plugin "{0}"', plugin);
83+
}
84+
}, this);
85+
};
86+
87+
/**
88+
* Returns the config of a plugin, if the plugin is not loaded, returns the default config.
89+
* @param {string} name
90+
* @param {string} [property]
91+
* @throws ConfigError
92+
* @returns {*}
93+
*/
94+
QueryBuilder.prototype.getPluginOptions = function(name, property) {
95+
var plugin;
96+
if (this.plugins && this.plugins[name]) {
97+
plugin = this.plugins[name];
98+
}
99+
else if (QueryBuilder.plugins[name]) {
100+
plugin = QueryBuilder.plugins[name].def;
101+
}
102+
103+
if (plugin) {
104+
if (property) {
105+
return plugin[property];
106+
}
107+
else {
108+
return plugin;
109+
}
110+
}
111+
else {
112+
Utils.error('Config', 'Unable to find plugin "{0}"', name);
113+
}
114+
};

src/plugins/sql-support/plugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
* @class SqlSupport
33
* @memberof module:plugins
44
* @description Allows to export rules as a SQL WHERE statement as well as populating the builder from an SQL query.
5+
* @param {object} [options]
6+
* @param {boolean} [options.boolean_as_integer=true] - `true` to convert boolean values to integer in the SQL output
57
*/
8+
QueryBuilder.define('sql-support', function(options) {
9+
10+
}, {
11+
boolean_as_integer: true
12+
});
613

714
QueryBuilder.defaults({
815
// operators for internal -> SQL conversion
@@ -242,6 +249,7 @@ QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ {
242249
getSQL: function(stmt, nl, data) {
243250
data = (data === undefined) ? this.getRules() : data;
244251
nl = !!nl ? '\n' : ' ';
252+
var boolean_as_integer = this.getPluginOptions('sql-support', 'boolean_as_integer');
245253

246254
if (stmt === true) stmt = 'question_mark';
247255
if (typeof stmt == 'string') {
@@ -289,7 +297,7 @@ QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ {
289297
}
290298

291299
if (rule.type == 'integer' || rule.type == 'double' || rule.type == 'boolean') {
292-
v = Utils.changeType(v, rule.type, true);
300+
v = Utils.changeType(v, rule.type, boolean_as_integer);
293301
}
294302
else if (!stmt) {
295303
v = Utils.escapeString(v);

tests/plugins.sql-support.module.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,44 @@ $(function() {
307307
);
308308
});
309309

310+
QUnit.test('Cast booleans', function(assert) {
311+
$b.queryBuilder({
312+
plugins: {
313+
'sql-support': {
314+
boolean_as_integer: true
315+
}
316+
},
317+
filters: [
318+
{
319+
id: 'done',
320+
type: 'boolean'
321+
}
322+
],
323+
rules: [
324+
{
325+
id: 'done',
326+
operator: 'equal',
327+
value: true
328+
}
329+
]
330+
});
331+
332+
assert.rulesMatch(
333+
$b.queryBuilder('getSQL'),
334+
'done = 1',
335+
'Should convert boolean value to integer'
336+
);
337+
338+
// don't do that in real life !
339+
$b[0].queryBuilder.plugins['sql-support'].boolean_as_integer = false;
340+
341+
assert.rulesMatch(
342+
$b.queryBuilder('getSQL'),
343+
'done = true',
344+
'Should not convert boolean value to integer'
345+
);
346+
});
347+
310348

311349
var basic_rules_sql_raw = {
312350
sql: 'price < 10.25 AND name IS NULL AND ( category IN(\'mo\', \'mu\') OR id != \'1234-azer-5678\' ) '

0 commit comments

Comments
 (0)