Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Auto adding placeholders to set, where #3

Closed
wants to merge 1 commit into from

2 participants

@fliespl

Added autoadding placeholders and converting null values.
Added support for set('x', '1'), set('x', 'IS', null), set('x', '>', a) for clarity
Added getParams() function which returns params to pass to sqlite database query.

Todo: add placeholder support for extensions

I used it for my personal project (chrome, firefox extensions which are using sqlite), maybe you find use for it.

Examples:

var query1 = squel.update().table('library').set('x', 'x').set('abc', null).set('cde', undefined).where('x = 1').where("y", "2").where("y", ">", "3");
var query2 = squel.select().from('library').where('x = 1').where("y", "2").where("y", ">", "3");
var query3 = squel.insert().into('library').set('x', '1').set("y", "2").set("z", "3");
var query4 = squel.delete().from('library').where('x = 1').where("y", "2").where('abc', '<>', 'x');

Results:

    console.log(query1.toString(), query1.getParams());
    console.log(query2.toString(), query2.getParams());
    console.log(query3.toString(), query3.getParams());
    console.log(query4.toString(), query4.getParams());

UPDATE library SET x = ?1, abc = NULL, cde = NULL WHERE (x = 1) AND (y = ?2) AND (y > ?3) ["x", "2", "3"]
SELECT * FROM library WHERE (x = 1) AND (y = ?1) AND (y > ?2) ["2", "3"]
INSERT INTO library (x, y, z) VALUES (?1, ?2, ?3) ["1", "2", "3"]
DELETE FROM library WHERE (x = 1) AND (y = ?1) ["2"]

@fliespl fliespl Added autoadding placeholders and converting null. Added support for
set('x', '1'), set('x', 'IS', null), set('x', '>', a)
0009139
@fliespl

There is a typo in todo:

Todo: add placeholder support for expressions (not extensions).

@hiddentao
Owner

I like the idea of the getParams() function to retrieve the parameters you passed in.

We'd need to modify the Coffeescript source and add tests before merging this into trunk.

@hiddentao
Owner

This pull request is no longer valid because master has changed. Squel builders can now be flexibly configured and extended.

Adding the improved set() method and the getParams() method can now be done by customizing the relevant building Block used in the query builder without modifying Squel internals. However, if enough people find these methods useful then they can be added to core.

Am closing this for now.

@hiddentao hiddentao closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 17, 2012
  1. @fliespl

    Added autoadding placeholders and converting null. Added support for

    fliespl authored
    set('x', '1'), set('x', 'IS', null), set('x', '>', a)
This page is out of date. Refresh to see the latest.
Showing with 75 additions and 7 deletions.
  1. +75 −7 squel.js
View
82 squel.js
@@ -150,7 +150,7 @@ OTHER DEALINGS IN THE SOFTWARE.
})();
DefaultInsertBuilderOptions = DefaultUpdateBuilderOptions = {
- usingValuePlaceholders: false
+ usingValuePlaceholders: true
};
getObjectClassName = function(obj) {
@@ -201,13 +201,14 @@ OTHER DEALINGS IN THE SOFTWARE.
sanitizeValue = function(item) {
var t;
t = typeof item;
+ if(item == undefined) item = null;
if (null !== item && "string" !== t && "number" !== t && "boolean" !== t) {
throw new Error("field value must be a string, number, boolean or null");
}
return item;
};
- formatValue = function(value, options) {
+ formatValue = function(value, options, context) {
if (null === value) {
value = "NULL";
} else if ("boolean" === typeof value) {
@@ -215,6 +216,7 @@ OTHER DEALINGS IN THE SOFTWARE.
} else if ("number" !== typeof value) {
if (false === options.usingValuePlaceholders) value = "\"" + value + "\"";
}
+
return value;
};
@@ -237,9 +239,27 @@ OTHER DEALINGS IN THE SOFTWARE.
}
WhereOrderLimit.prototype.where = function(condition) {
- condition = sanitizeCondition(condition);
- if ("" !== condition) this.wheres.push(condition);
- return this;
+ if(arguments.length > 1) {
+ var glue = '=';
+ var value = arguments[1];
+ if(arguments.length == 3) {
+ glue = arguments[1];
+ value = arguments[2];
+ }
+
+ if(value === undefined || value === null || value === '') {
+ value = "NULL";
+ } else {
+ this.placeholdersCount++;
+ this.values.push(value);
+ value = '?'+this.placeholdersCount;
+ }
+ condition = arguments[0] + ' ' + glue + ' ' + value;
+ }
+
+ condition = sanitizeCondition(condition);
+ if ("" !== condition) this.wheres.push(condition);
+ return this;
};
WhereOrderLimit.prototype.order = function(field, asc) {
@@ -327,6 +347,14 @@ OTHER DEALINGS IN THE SOFTWARE.
this.fields = [];
this.joins = [];
this.groups = [];
+
+
+ this.getParams = __bind(this.getParams, this);
+ this.placeholdersCount = 0;
+ this.values = [];
+
+
+
this._join = function(type, table, alias, condition) {
table = sanitizeTable(table);
if (alias) alias = sanitizeAlias(alias);
@@ -340,6 +368,10 @@ OTHER DEALINGS IN THE SOFTWARE.
return _this;
};
}
+
+ Select.prototype.getParams = function() {
+ return this.values;
+ }
Select.prototype.distinct = function() {
this.useDistinct = true;
@@ -469,10 +501,13 @@ OTHER DEALINGS IN THE SOFTWARE.
function Update(options) {
this.toString = __bind(this.toString, this);
+ this.getParams = __bind(this.getParams, this);
this.set = __bind(this.set, this);
this.table = __bind(this.table, this); Update.__super__.constructor.apply(this, arguments);
this.tables = [];
+ this.placeholdersCount = 0;
this.fields = {};
+ this.values = [];
this.options = _extend({}, DefaultUpdateBuilderOptions, options);
}
@@ -490,11 +525,23 @@ OTHER DEALINGS IN THE SOFTWARE.
Update.prototype.set = function(field, value) {
field = sanitizeField(field);
value = sanitizeValue(value);
+ if(value === undefined || value === null || value === '') {
+ value = "NULL";
+ } else {
+ this.placeholdersCount++;
+ this.values.push(value);
+ value = '?'+this.placeholdersCount;
+ }
this.fields[field] = value;
return this;
};
+
+ Update.prototype.getParams = function() {
+ return this.values;
+ }
Update.prototype.toString = function() {
+
var field, fieldNames, fields, ret, table, tables, _i, _j, _len, _len2, _ref;
if (0 >= this.tables.length) throw new Error("table() needs to be called");
fieldNames = (function() {
@@ -522,7 +569,7 @@ OTHER DEALINGS IN THE SOFTWARE.
for (_j = 0, _len2 = fieldNames.length; _j < _len2; _j++) {
field = fieldNames[_j];
if ("" !== fields) fields += ", ";
- fields += "" + field + " = " + (formatValue(this.fields[field], this.options));
+ fields += "" + field + " = " + (formatValue(this.fields[field], this.options, this));
}
ret += " SET " + fields;
ret += this.whereString();
@@ -542,6 +589,8 @@ OTHER DEALINGS IN THE SOFTWARE.
function Delete() {
this.toString = __bind(this.toString, this);
this.from = __bind(this.from, this);
+ this.placeholdersCount = 0;
+ this.values = [];
Delete.__super__.constructor.apply(this, arguments);
}
@@ -552,6 +601,10 @@ OTHER DEALINGS IN THE SOFTWARE.
this.table = table;
return this;
};
+
+ Delete.prototype.getParams = function() {
+ return this.values;
+ };
Delete.prototype.toString = function() {
var ret;
@@ -577,10 +630,18 @@ OTHER DEALINGS IN THE SOFTWARE.
function Insert(options) {
this.toString = __bind(this.toString, this);
+
this.set = __bind(this.set, this);
this.into = __bind(this.into, this); this.fields = {};
+ this.values = [];
+ this.placeholdersCount = 0;
+
this.options = _extend({}, DefaultInsertBuilderOptions, options);
}
+
+ Insert.prototype.getParams = function() {
+ return this.values;
+ }
Insert.prototype.into = function(table) {
table = sanitizeTable(table);
@@ -591,6 +652,13 @@ OTHER DEALINGS IN THE SOFTWARE.
Insert.prototype.set = function(field, value) {
field = sanitizeField(field);
value = sanitizeValue(value);
+ if(value === undefined || value === null || value === '') {
+ value = "NULL";
+ } else {
+ this.placeholdersCount++;
+ this.values.push(value);
+ value = '?'+this.placeholdersCount;
+ }
this.fields[field] = value;
return this;
};
@@ -616,7 +684,7 @@ OTHER DEALINGS IN THE SOFTWARE.
if ("" !== fields) fields += ", ";
fields += field;
if ("" !== values) values += ", ";
- values += formatValue(this.fields[field], this.options);
+ values += formatValue(this.fields[field], this.options, this);
}
return "INSERT INTO " + this.table + " (" + fields + ") VALUES (" + values + ")";
};
Something went wrong with that request. Please try again.