Skip to content

Commit

Permalink
WIP: Fix #544 Always convert int, float and bool values
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Aug 30, 2017
1 parent 233de01 commit 0dd12b9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ QueryBuilder.prototype.createRuleInput = function(rule) {
$valueContainer.show();

$inputs.on('change ' + (filter.input_event || ''), function() {
if (!this._updating_input) {
if (!rule._updating_input) {
rule._updating_value = true;
rule.value = self.getRuleInputValue(rule);
rule._updating_value = false;
Expand Down
23 changes: 16 additions & 7 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,20 @@ QueryBuilder.prototype.getRuleInputValue = function(rule) {
}
}

if (operator.multiple && filter.value_separator) {
value = value.map(function(val) {
return val.split(filter.value_separator);
});
}
value = value.map(function(val) {
if (operator.multiple && filter.value_separator && typeof val == 'string') {
val = val.split(filter.value_separator);
}

if ($.isArray(val)) {
return val.map(function(subval) {
return Utils.changeType(subval, filter.type);
});
}
else {
return Utils.changeType(val, filter.type);
}
});

if (operator.nb_inputs === 1) {
value = value[0];
Expand Down Expand Up @@ -435,7 +444,7 @@ QueryBuilder.prototype.setRuleInputValue = function(rule, value) {
return;
}

this._updating_input = true;
rule._updating_input = true;

if (filter.valueSetter) {
filter.valueSetter.call(this, rule, value);
Expand Down Expand Up @@ -476,7 +485,7 @@ QueryBuilder.prototype.setRuleInputValue = function(rule, value) {
}
}

this._updating_input = false;
rule._updating_input = false;
};

/**
Expand Down
13 changes: 6 additions & 7 deletions src/plugins/mongodb-support/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
getMongo: function(data) {
data = (data === undefined) ? this.getRules() : data;

if (!data) {
return null;
}

var self = this;

return (function parse(group) {
Expand All @@ -128,7 +132,6 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
else {
var mdb = self.settings.mongoOperators[rule.operator];
var ope = self.getOperatorByType(rule.operator);
var values = [];

if (mdb === undefined) {
Utils.error('UndefinedMongoOperator', 'Unknown MongoDB operation for operator "{0}"', rule.operator);
Expand All @@ -138,10 +141,6 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
if (!(rule.value instanceof Array)) {
rule.value = [rule.value];
}

rule.value.forEach(function(v) {
values.push(Utils.changeType(v, rule.type, false));
});
}

/**
Expand All @@ -155,7 +154,7 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
var field = self.change('getMongoDBField', rule.field, rule);

var ruleExpression = {};
ruleExpression[field] = mdb.call(self, values);
ruleExpression[field] = mdb.call(self, rule.value);

/**
* Modifies the MongoDB expression generated for a rul
Expand All @@ -167,7 +166,7 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
* @param {function} valueWrapper - function that takes the value and adds the operator
* @returns {object}
*/
parts.push(self.change('ruleToMongo', ruleExpression, rule, values, mdb));
parts.push(self.change('ruleToMongo', ruleExpression, rule, rule.value, mdb));
}
});

Expand Down
16 changes: 12 additions & 4 deletions src/plugins/sql-support/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,18 @@ QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ {
*/
getSQL: function(stmt, nl, data) {
data = (data === undefined) ? this.getRules() : data;

nl = !!nl ? '\n' : ' ';

if (!data) {
return null;
}

var boolean_as_integer = this.getPluginOptions('sql-support', 'boolean_as_integer');

if (stmt === true) stmt = 'question_mark';
if (stmt === true) {
stmt = 'question_mark';
}
if (typeof stmt == 'string') {
var config = getStmtConfig(stmt);
stmt = this.settings.sqlStatements[config[1]](config[2]);
Expand Down Expand Up @@ -296,10 +304,10 @@ QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ {
value += sql.sep;
}

if (rule.type == 'integer' || rule.type == 'double' || rule.type == 'boolean') {
v = Utils.changeType(v, rule.type, boolean_as_integer);
if (rule.type == 'boolean' && boolean_as_integer) {
v = v ? 1 : 0;
}
else if (!stmt) {
else if (!stmt && rule.type !== 'integer' && rule.type !== 'double' && rule.type !== 'boolean') {
v = Utils.escapeString(v);
}

Expand Down
11 changes: 6 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ Utils.error = function() {
* Changes the type of a value to int, float or bool
* @param {*} value
* @param {string} type - 'integer', 'double', 'boolean' or anything else (passthrough)
* @param {boolean} [boolAsInt=false] - return 0 or 1 for booleans
* @returns {*}
*/
Utils.changeType = function(value, type, boolAsInt) {
Utils.changeType = function(value, type) {
if (value === '') {
return undefined;
}

switch (type) {
// @formatter:off
case 'integer': return parseInt(value);
case 'double': return parseFloat(value);
case 'boolean':
var bool = value.trim().toLowerCase() === 'true' || value.trim() === '1' || value === 1;
return boolAsInt ? (bool ? 1 : 0) : bool;
case 'boolean': return value === true || value === 1 || value.trim().toLowerCase() === 'true' || value.trim() === '1';
default: return value;
// @formatter:on
}
Expand Down
2 changes: 1 addition & 1 deletion tests/data.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $(function() {

QUnit.module('data', {
afterEach: function() {
$b.queryBuilder('destroy');
//$b.queryBuilder('destroy');
}
});

Expand Down
5 changes: 0 additions & 5 deletions tests/utils.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ $(function () {
Utils.changeType('true', 'boolean') === true,
'"true" should be parsed as boolean'
);

assert.ok(
Utils.changeType('false', 'boolean', true) === 0,
'"false" should be parsed as integer'
);
});

/**
Expand Down

0 comments on commit 0dd12b9

Please sign in to comment.