Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added autoadding placeholders and converting null. Added support for

set('x', '1'), set('x', 'IS', null), set('x', '>', a)
  • Loading branch information...
commit 0009139c3909b37e3130151bd889d5a9984f2c4f 1 parent 58ceb91
fliespl authored

Showing 1 changed file with 75 additions and 7 deletions. Show diff stats Hide diff stats

  1. +75 7 squel.js
82 squel.js
@@ -150,7 +150,7 @@ OTHER DEALINGS IN THE SOFTWARE.
150 150 })();
151 151
152 152 DefaultInsertBuilderOptions = DefaultUpdateBuilderOptions = {
153   - usingValuePlaceholders: false
  153 + usingValuePlaceholders: true
154 154 };
155 155
156 156 getObjectClassName = function(obj) {
@@ -201,13 +201,14 @@ OTHER DEALINGS IN THE SOFTWARE.
201 201 sanitizeValue = function(item) {
202 202 var t;
203 203 t = typeof item;
  204 + if(item == undefined) item = null;
204 205 if (null !== item && "string" !== t && "number" !== t && "boolean" !== t) {
205 206 throw new Error("field value must be a string, number, boolean or null");
206 207 }
207 208 return item;
208 209 };
209 210
210   - formatValue = function(value, options) {
  211 + formatValue = function(value, options, context) {
211 212 if (null === value) {
212 213 value = "NULL";
213 214 } else if ("boolean" === typeof value) {
@@ -215,6 +216,7 @@ OTHER DEALINGS IN THE SOFTWARE.
215 216 } else if ("number" !== typeof value) {
216 217 if (false === options.usingValuePlaceholders) value = "\"" + value + "\"";
217 218 }
  219 +
218 220 return value;
219 221 };
220 222
@@ -237,9 +239,27 @@ OTHER DEALINGS IN THE SOFTWARE.
237 239 }
238 240
239 241 WhereOrderLimit.prototype.where = function(condition) {
240   - condition = sanitizeCondition(condition);
241   - if ("" !== condition) this.wheres.push(condition);
242   - return this;
  242 + if(arguments.length > 1) {
  243 + var glue = '=';
  244 + var value = arguments[1];
  245 + if(arguments.length == 3) {
  246 + glue = arguments[1];
  247 + value = arguments[2];
  248 + }
  249 +
  250 + if(value === undefined || value === null || value === '') {
  251 + value = "NULL";
  252 + } else {
  253 + this.placeholdersCount++;
  254 + this.values.push(value);
  255 + value = '?'+this.placeholdersCount;
  256 + }
  257 + condition = arguments[0] + ' ' + glue + ' ' + value;
  258 + }
  259 +
  260 + condition = sanitizeCondition(condition);
  261 + if ("" !== condition) this.wheres.push(condition);
  262 + return this;
243 263 };
244 264
245 265 WhereOrderLimit.prototype.order = function(field, asc) {
@@ -327,6 +347,14 @@ OTHER DEALINGS IN THE SOFTWARE.
327 347 this.fields = [];
328 348 this.joins = [];
329 349 this.groups = [];
  350 +
  351 +
  352 + this.getParams = __bind(this.getParams, this);
  353 + this.placeholdersCount = 0;
  354 + this.values = [];
  355 +
  356 +
  357 +
330 358 this._join = function(type, table, alias, condition) {
331 359 table = sanitizeTable(table);
332 360 if (alias) alias = sanitizeAlias(alias);
@@ -340,6 +368,10 @@ OTHER DEALINGS IN THE SOFTWARE.
340 368 return _this;
341 369 };
342 370 }
  371 +
  372 + Select.prototype.getParams = function() {
  373 + return this.values;
  374 + }
343 375
344 376 Select.prototype.distinct = function() {
345 377 this.useDistinct = true;
@@ -469,10 +501,13 @@ OTHER DEALINGS IN THE SOFTWARE.
469 501
470 502 function Update(options) {
471 503 this.toString = __bind(this.toString, this);
  504 + this.getParams = __bind(this.getParams, this);
472 505 this.set = __bind(this.set, this);
473 506 this.table = __bind(this.table, this); Update.__super__.constructor.apply(this, arguments);
474 507 this.tables = [];
  508 + this.placeholdersCount = 0;
475 509 this.fields = {};
  510 + this.values = [];
476 511 this.options = _extend({}, DefaultUpdateBuilderOptions, options);
477 512 }
478 513
@@ -490,11 +525,23 @@ OTHER DEALINGS IN THE SOFTWARE.
490 525 Update.prototype.set = function(field, value) {
491 526 field = sanitizeField(field);
492 527 value = sanitizeValue(value);
  528 + if(value === undefined || value === null || value === '') {
  529 + value = "NULL";
  530 + } else {
  531 + this.placeholdersCount++;
  532 + this.values.push(value);
  533 + value = '?'+this.placeholdersCount;
  534 + }
493 535 this.fields[field] = value;
494 536 return this;
495 537 };
  538 +
  539 + Update.prototype.getParams = function() {
  540 + return this.values;
  541 + }
496 542
497 543 Update.prototype.toString = function() {
  544 +
498 545 var field, fieldNames, fields, ret, table, tables, _i, _j, _len, _len2, _ref;
499 546 if (0 >= this.tables.length) throw new Error("table() needs to be called");
500 547 fieldNames = (function() {
@@ -522,7 +569,7 @@ OTHER DEALINGS IN THE SOFTWARE.
522 569 for (_j = 0, _len2 = fieldNames.length; _j < _len2; _j++) {
523 570 field = fieldNames[_j];
524 571 if ("" !== fields) fields += ", ";
525   - fields += "" + field + " = " + (formatValue(this.fields[field], this.options));
  572 + fields += "" + field + " = " + (formatValue(this.fields[field], this.options, this));
526 573 }
527 574 ret += " SET " + fields;
528 575 ret += this.whereString();
@@ -542,6 +589,8 @@ OTHER DEALINGS IN THE SOFTWARE.
542 589 function Delete() {
543 590 this.toString = __bind(this.toString, this);
544 591 this.from = __bind(this.from, this);
  592 + this.placeholdersCount = 0;
  593 + this.values = [];
545 594 Delete.__super__.constructor.apply(this, arguments);
546 595 }
547 596
@@ -552,6 +601,10 @@ OTHER DEALINGS IN THE SOFTWARE.
552 601 this.table = table;
553 602 return this;
554 603 };
  604 +
  605 + Delete.prototype.getParams = function() {
  606 + return this.values;
  607 + };
555 608
556 609 Delete.prototype.toString = function() {
557 610 var ret;
@@ -577,10 +630,18 @@ OTHER DEALINGS IN THE SOFTWARE.
577 630
578 631 function Insert(options) {
579 632 this.toString = __bind(this.toString, this);
  633 +
580 634 this.set = __bind(this.set, this);
581 635 this.into = __bind(this.into, this); this.fields = {};
  636 + this.values = [];
  637 + this.placeholdersCount = 0;
  638 +
582 639 this.options = _extend({}, DefaultInsertBuilderOptions, options);
583 640 }
  641 +
  642 + Insert.prototype.getParams = function() {
  643 + return this.values;
  644 + }
584 645
585 646 Insert.prototype.into = function(table) {
586 647 table = sanitizeTable(table);
@@ -591,6 +652,13 @@ OTHER DEALINGS IN THE SOFTWARE.
591 652 Insert.prototype.set = function(field, value) {
592 653 field = sanitizeField(field);
593 654 value = sanitizeValue(value);
  655 + if(value === undefined || value === null || value === '') {
  656 + value = "NULL";
  657 + } else {
  658 + this.placeholdersCount++;
  659 + this.values.push(value);
  660 + value = '?'+this.placeholdersCount;
  661 + }
594 662 this.fields[field] = value;
595 663 return this;
596 664 };
@@ -616,7 +684,7 @@ OTHER DEALINGS IN THE SOFTWARE.
616 684 if ("" !== fields) fields += ", ";
617 685 fields += field;
618 686 if ("" !== values) values += ", ";
619   - values += formatValue(this.fields[field], this.options);
  687 + values += formatValue(this.fields[field], this.options, this);
620 688 }
621 689 return "INSERT INTO " + this.table + " (" + fields + ") VALUES (" + values + ")";
622 690 };

0 comments on commit 0009139

Please sign in to comment.
Something went wrong with that request. Please try again.