Permalink
Browse files

_sanitizeField() will now quote field names if autoQuoteFieldNames is…

… set to true.

_sanitizeTable() will now quote table names if autoQuoteTableNames is set to true.
  • Loading branch information...
1 parent 749d133 commit e362659988ce567b86c32a8cbf4d660771d3566f @hiddentao committed Jan 4, 2013
Showing with 60 additions and 19 deletions.
  1. +22 −9 src/squel.coffee
  2. +38 −10 test/baseclasses.test.coffee
View
@@ -204,9 +204,24 @@ class QueryBuilder extends Cloneable
throw new Error "#{type} must be a string"
value
- _sanitizeField: (item) -> @_sanitizeName item, "field name"
- _sanitizeTable: (item) -> @_sanitizeName item, "table name"
- _sanitizeAlias: (item) -> @_sanitizeName item, "alias"
+ _sanitizeField: (item) ->
+ sanitized = @_sanitizeName item, "field name"
+
+ if @options.autoQuoteFieldNames
+ "#{@options.nameQuoteCharacter}#{sanitized}#{@options.nameQuoteCharacter}"
+ else
+ sanitized
+
+ _sanitizeTable: (item) ->
+ sanitized = @_sanitizeName item, "table name"
+
+ if @options.autoQuoteTableNames
+ "#{@options.nameQuoteCharacter}#{sanitized}#{@options.nameQuoteCharacter}"
+ else
+ sanitized
+
+ _sanitizeAlias: (item) ->
+ @_sanitizeName item, "alias"
# Sanitize the given limit/offset value.
_sanitizeLimitOffset: (value) ->
@@ -223,15 +238,13 @@ class QueryBuilder extends Cloneable
item
# Format the given field value for inclusion into the query string
- #
- # options: see DefaultBuilderOptions
- _formatValue: (value, options) ->
+ _formatValue: (value) ->
if null is value
value = "NULL"
else if "boolean" is typeof value
value = if value then "TRUE" else "FALSE"
else if "number" isnt typeof value
- if not options or false is options.usingValuePlaceholders
+ if false is @options.usingValuePlaceholders
value = "'#{value}'"
value
@@ -543,7 +556,7 @@ class Update extends WhereOrderLimit
fields = ""
for field in fieldNames
fields += ", " if "" isnt fields
- fields += "#{field} = #{@_formatValue(@fields[field], @options)}"
+ fields += "#{field} = #{@_formatValue(@fields[field])}"
ret += " SET #{fields}"
# where
@@ -645,7 +658,7 @@ class Insert extends QueryBuilder
fields += ", " if "" isnt fields
fields += field
values += ", " if "" isnt values
- values += @_formatValue(@fields[field], @options)
+ values += @_formatValue(@fields[field])
"INSERT INTO #{@table} (#{fields}) VALUES (#{values})"
@@ -162,20 +162,44 @@ test['QueryBuilder base class'] =
assert.throws (=> @inst._sanitizeName(undefined, 'no')), 'no must be a string'
- '_sanitizeField': ->
- test.mocker.spy @inst, '_sanitizeName'
+ '_sanitizeField':
+ 'default': ->
+ test.mocker.spy @inst, '_sanitizeName'
- assert.same 'abc', @inst._sanitizeField('abc')
+ assert.same 'abc', @inst._sanitizeField('abc')
- assert.ok @inst._sanitizeName.calledWithExactly 'abc', 'field name'
+ assert.ok @inst._sanitizeName.calledWithExactly 'abc', 'field name'
+ 'auto quote names':
+ beforeEach: ->
+ @inst.options.autoQuoteFieldNames = true
- '_sanitizeTable': ->
- test.mocker.spy @inst, '_sanitizeName'
+ 'default quote character': ->
+ assert.same '`abc`', @inst._sanitizeField('abc')
- assert.same 'abc', @inst._sanitizeTable('abc')
+ 'custom quote character': ->
+ @inst.options.nameQuoteCharacter = '|'
+ assert.same '|abc|', @inst._sanitizeField('abc')
- assert.ok @inst._sanitizeName.calledWithExactly 'abc', 'table name'
+
+ '_sanitizeTable':
+ 'default': ->
+ test.mocker.spy @inst, '_sanitizeName'
+
+ assert.same 'abc', @inst._sanitizeTable('abc')
+
+ assert.ok @inst._sanitizeName.calledWithExactly 'abc', 'table name'
+
+ 'auto quote names':
+ beforeEach: ->
+ @inst.options.autoQuoteTableNames = true
+
+ 'default quote character': ->
+ assert.same '`abc`', @inst._sanitizeTable('abc')
+
+ 'custom quote character': ->
+ @inst.options.nameQuoteCharacter = '|'
+ assert.same '|abc|', @inst._sanitizeTable('abc')
'_sanitizeAlias': ->
@@ -264,8 +288,12 @@ test['QueryBuilder base class'] =
'string': ->
assert.same "'test'", @inst._formatValue('test')
- assert.same "'test'", @inst._formatValue('test', { usingValuePlaceholders: false })
- assert.same "test", @inst._formatValue('test', { usingValuePlaceholders: true })
+
+ @inst.options.usingValuePlaceholders = false
+ assert.same "'test'", @inst._formatValue('test')
+
+ @inst.options.usingValuePlaceholders = true
+ assert.same "test", @inst._formatValue('test')

0 comments on commit e362659

Please sign in to comment.