Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Bug fixes to toParam()
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao committed Oct 28, 2014
1 parent 385cbfd commit 1b69614
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 20 deletions.
12 changes: 6 additions & 6 deletions squel-basic.js
Expand Up @@ -431,12 +431,12 @@ OTHER DEALINGS IN THE SOFTWARE.
child = _ref[_i];
if (child.expr != null) {
nodeStr = child.expr;
if (child.para != null) {
if (void 0 !== child.para) {
if (!paramMode) {
nodeStr = nodeStr.replace('?', this._formatValue(child.para));
} else {
cv = this._formatValueAsParam(child.para);
if ((cv.text != null)) {
if (((cv != null ? cv.text : void 0) != null)) {
params = params.concat(cv.values);
nodeStr = nodeStr.replace('?', "(" + cv.text + ")");
} else {
Expand Down Expand Up @@ -983,7 +983,7 @@ OTHER DEALINGS IN THE SOFTWARE.
str += field;
} else {
p = this._formatValueAsParam(value);
if (p.text != null) {
if ((p != null ? p.text : void 0) != null) {
str += "" + field + " = (" + p.text + ")";
_ref5 = p.values;
for (_j = 0, _len = _ref5.length; _j < _len; _j++) {
Expand Down Expand Up @@ -1052,7 +1052,7 @@ OTHER DEALINGS IN THE SOFTWARE.
for (i = _i = 0, _ref5 = this.values.length; 0 <= _ref5 ? _i < _ref5 : _i > _ref5; i = 0 <= _ref5 ? ++_i : --_i) {
for (j = _j = 0, _ref6 = this.values[i].length; 0 <= _ref6 ? _j < _ref6 : _j > _ref6; j = 0 <= _ref6 ? ++_j : --_j) {
p = this._formatValueAsParam(this.values[i][j]);
if (p.text != null) {
if ((p != null ? p.text : void 0) != null) {
str = p.text;
_ref7 = p.values;
for (_k = 0, _len = _ref7.length; _k < _len; _k++) {
Expand Down Expand Up @@ -1295,11 +1295,11 @@ OTHER DEALINGS IN THE SOFTWARE.
_ref6 = where.values;
for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) {
v = _ref6[_j];
p = this._formatValueAsParam(v);
if (str[i] != null) {
whereStr += "" + str[i];
}
if ((p.text != null)) {
p = this._formatValueAsParam(v);
if (((p != null ? p.text : void 0) != null)) {
whereStr += "(" + p.text + ")";
_ref7 = p.values;
for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) {
Expand Down
2 changes: 1 addition & 1 deletion squel-basic.min.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions squel.js
Expand Up @@ -432,12 +432,12 @@ OTHER DEALINGS IN THE SOFTWARE.
child = _ref[_i];
if (child.expr != null) {
nodeStr = child.expr;
if (child.para != null) {
if (void 0 !== child.para) {
if (!paramMode) {
nodeStr = nodeStr.replace('?', this._formatValue(child.para));
} else {
cv = this._formatValueAsParam(child.para);
if ((cv.text != null)) {
if (((cv != null ? cv.text : void 0) != null)) {
params = params.concat(cv.values);
nodeStr = nodeStr.replace('?', "(" + cv.text + ")");
} else {
Expand Down Expand Up @@ -984,7 +984,7 @@ OTHER DEALINGS IN THE SOFTWARE.
str += field;
} else {
p = this._formatValueAsParam(value);
if (p.text != null) {
if ((p != null ? p.text : void 0) != null) {
str += "" + field + " = (" + p.text + ")";
_ref5 = p.values;
for (_j = 0, _len = _ref5.length; _j < _len; _j++) {
Expand Down Expand Up @@ -1053,7 +1053,7 @@ OTHER DEALINGS IN THE SOFTWARE.
for (i = _i = 0, _ref5 = this.values.length; 0 <= _ref5 ? _i < _ref5 : _i > _ref5; i = 0 <= _ref5 ? ++_i : --_i) {
for (j = _j = 0, _ref6 = this.values[i].length; 0 <= _ref6 ? _j < _ref6 : _j > _ref6; j = 0 <= _ref6 ? ++_j : --_j) {
p = this._formatValueAsParam(this.values[i][j]);
if (p.text != null) {
if ((p != null ? p.text : void 0) != null) {
str = p.text;
_ref7 = p.values;
for (_k = 0, _len = _ref7.length; _k < _len; _k++) {
Expand Down Expand Up @@ -1296,11 +1296,11 @@ OTHER DEALINGS IN THE SOFTWARE.
_ref6 = where.values;
for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) {
v = _ref6[_j];
p = this._formatValueAsParam(v);
if (str[i] != null) {
whereStr += "" + str[i];
}
if ((p.text != null)) {
p = this._formatValueAsParam(v);
if (((p != null ? p.text : void 0) != null)) {
whereStr += "(" + p.text + ")";
_ref7 = p.values;
for (_k = 0, _len2 = _ref7.length; _k < _len2; _k++) {
Expand Down
2 changes: 1 addition & 1 deletion squel.min.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/squel.coffee
Expand Up @@ -407,12 +407,12 @@ class cls.Expression extends cls.BaseBuilder
if child.expr?
nodeStr = child.expr
# have param?
if child.para?
if undefined isnt child.para
if not paramMode
nodeStr = nodeStr.replace '?', @_formatValue(child.para)
else
cv = @_formatValueAsParam child.para
if (cv.text?)
if (cv?.text?)
params = params.concat(cv.values)
nodeStr = nodeStr.replace '?', "(#{cv.text})"
else
Expand Down Expand Up @@ -831,7 +831,7 @@ class cls.SetFieldBlock extends cls.AbstractSetFieldBlock
str += field
else
p = @_formatValueAsParam( value )
if p.text?
if p?.text?
str += "#{field} = (#{p.text})"
for v in p.values
vals.push v
Expand Down Expand Up @@ -872,7 +872,7 @@ class cls.InsertFieldValueBlock extends cls.AbstractSetFieldBlock
for i in [0...@values.length]
for j in [0...@values[i].length]
p = @_formatValueAsParam( @values[i][j] )
if p.text?
if p?.text?
str = p.text
for v in p.values
params.push v
Expand Down Expand Up @@ -1043,9 +1043,9 @@ class cls.WhereBlock extends cls.Block
str = where.text.split('?')
i = 0
for v in where.values
p = @_formatValueAsParam(v)
whereStr += "#{str[i]}" if str[i]?
if (p.text?)
p = @_formatValueAsParam(v)
if (p?.text?)
whereStr += "(#{p.text})"
for qv in p.values
ret.values.push( qv )
Expand Down
9 changes: 9 additions & 0 deletions test/baseclasses.test.coffee
Expand Up @@ -630,6 +630,15 @@ test['Builder base class'] =
u = squel.update().table('table').set('f', 'val')
assert.same '(UPDATE table SET f = \'val\')', @inst._formatValue(u)

'Expression': ->
s = squel.expr()
.and("s.name <> 'Fred'")
.or_begin()
.or("s.id = 5")
.or("s.id = 6")
.end()
assert.same "(s.name <> 'Fred' OR (s.id = 5 OR s.id = 6))", @inst._formatValue(s)

'checks to see if it is custom value type first': ->
test.mocker.stub @inst, '_formatCustomValue', -> 'abc'
assert.same "'abc'", @inst._formatValue(123)
Expand Down
13 changes: 13 additions & 0 deletions test/expressions.test.coffee
Expand Up @@ -151,6 +151,19 @@ test['Expression builder base class'] =
assert.same @inst.toString(), "test = 3 AND flight = '4' OR dummy IN (1,2,3)"


'and("test = ?", null)':
beforeEach: ->
@inst.and("test = ?", null)

'>> toString()': ->
assert.same @inst.toString(), 'test = NULL'

'>> toParam()': ->
assert.same @inst.toParam(), {
text: 'test = ?'
values: [null]
}

'and("test = ?", 3)':
beforeEach: ->
@inst.and("test = ?", 3)
Expand Down
7 changes: 7 additions & 0 deletions test/insert.test.coffee
Expand Up @@ -65,6 +65,13 @@ test['INSERT builder'] =
@inst.into('table')
assert.throws (=> @inst.toString()), 'set() needs to be called'

'>> into(table).set(field, null)':
beforeEach: -> @inst.into('table').set('field', null)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field) VALUES (NULL)'
toParam: ->
assert.same @inst.toParam(), { text: 'INSERT INTO table (field) VALUES (?)', values: [null] }

'>> into(table).set(field, 1)':
beforeEach: -> @inst.into('table').set('field', 1)
toString: ->
Expand Down
10 changes: 10 additions & 0 deletions test/select.test.coffee
Expand Up @@ -122,6 +122,16 @@ test['SELECT builder'] =
values: [10, 2, 3]
}

'>> where(a = ?, null)':
beforeEach: -> @inst.where('a = ?', null)
toString: ->
assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = NULL) GROUP BY field, field2'
toParam: ->
assert.same @inst.toParam(), {
text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = ?) GROUP BY field, field2'
values: [null]
}

'>> where(a = ?, 1)':
beforeEach: -> @inst.where('a = ?', 1)
toString: ->
Expand Down
7 changes: 7 additions & 0 deletions test/update.test.coffee
Expand Up @@ -80,6 +80,13 @@ test['UPDATE builder'] =
toString: ->
assert.same @inst.toString(), 'UPDATE table `t1` SET field = 1, field2 = TRUE'

'>> set(field2, null)':
beforeEach: -> @inst.set('field2', null)
toString: ->
assert.same @inst.toString(), 'UPDATE table `t1` SET field = 1, field2 = NULL'
toParam: ->
assert.same @inst.toParam(), { text: 'UPDATE table `t1` SET field = 1, field2 = ?', values: [null] }

'>> set(field2, "str")':
beforeEach: -> @inst.set('field2', 'str')
toString: ->
Expand Down

0 comments on commit 1b69614

Please sign in to comment.