Permalink
Browse files

WIP: Fix #544 Always convert int, float and bool values

  • Loading branch information...
mistic100 committed Aug 11, 2017
1 parent 233de01 commit 0dd12b9735d533cecfa830602087b546fe7d4041
Showing with 42 additions and 30 deletions.
  1. +1 −1 src/core.js
  2. +16 −7 src/data.js
  3. +6 −7 src/plugins/mongodb-support/plugin.js
  4. +12 −4 src/plugins/sql-support/plugin.js
  5. +6 −5 src/utils.js
  6. +1 −1 tests/data.module.js
  7. +0 −5 tests/utils.module.js
View
@@ -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;
View
@@ -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];
@@ -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);
@@ -476,7 +485,7 @@ QueryBuilder.prototype.setRuleInputValue = function(rule, value) {
}
}
this._updating_input = false;
rule._updating_input = false;
};
/**
@@ -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) {
@@ -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);
@@ -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));
});
}
/**
@@ -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
@@ -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));
}
});
@@ -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]);
@@ -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);
}
View
@@ -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
}
View
@@ -3,7 +3,7 @@ $(function() {
QUnit.module('data', {
afterEach: function() {
$b.queryBuilder('destroy');
//$b.queryBuilder('destroy');
}
});
View
@@ -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'
);
});
/**

0 comments on commit 0dd12b9

Please sign in to comment.