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

Commit

Permalink
added stringFormatter option
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao committed May 18, 2016
1 parent 8b7df73 commit c4d54ab
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -1,6 +1,7 @@
# Changelog for [squel](https://github.com/hiddentao/squel)

## 18 May 2016 (5.1.1)
## 18 May 2016 (5.2.0)
* Fix for #109 - custom string formatting function enabled
* Fix for #235 - fix a regression

## 14 May 2016 (5.1.0)
Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "squel",
"version": "5.0.4",
"version": "5.2.0",
"main": "squel.js",
"ignore": [
"**/.*",
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "squel",
"description": "SQL query string builder",
"version": "5.1.0",
"version": "5.2.0",
"author": "Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)",
"contributors": [
"Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)",
Expand Down
11 changes: 9 additions & 2 deletions squel-basic.js
Expand Up @@ -275,7 +275,9 @@ function _buildSquel() {
// The string to replace single quotes with in query strings
singleQuoteReplacement: '\'\'',
// String used to join individual blocks in a query when it's stringified
separator: ' '
separator: ' ',
// Function for formatting string values prior to insertion into query string
stringFormatter: null
};

// Global custom value handlers for all instances of builder
Expand Down Expand Up @@ -612,6 +614,11 @@ function _buildSquel() {
} else if (value instanceof cls.BaseBuilder) {
value = this._applyNestingFormatting(value.toString());
} else if (typeofValue !== "number") {
// if it's a string and we have custom string formatting turned on then use that
if ('string' === typeofValue && this.options.stringFormatter) {
return this.options.stringFormatter(value);
}

if (formattingOptions.dontQuote) {
value = '' + value;
} else {
Expand Down Expand Up @@ -2935,7 +2942,7 @@ function _buildSquel() {
}(cls.QueryBuilder);

var _squel = {
VERSION: '5.1.0',
VERSION: '5.2.0',
flavour: flavour,
expr: function expr(options) {
return new cls.Expression(options);
Expand Down
4 changes: 2 additions & 2 deletions squel-basic.min.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions squel.js
Expand Up @@ -275,7 +275,9 @@ function _buildSquel() {
// The string to replace single quotes with in query strings
singleQuoteReplacement: '\'\'',
// String used to join individual blocks in a query when it's stringified
separator: ' '
separator: ' ',
// Function for formatting string values prior to insertion into query string
stringFormatter: null
};

// Global custom value handlers for all instances of builder
Expand Down Expand Up @@ -612,6 +614,11 @@ function _buildSquel() {
} else if (value instanceof cls.BaseBuilder) {
value = this._applyNestingFormatting(value.toString());
} else if (typeofValue !== "number") {
// if it's a string and we have custom string formatting turned on then use that
if ('string' === typeofValue && this.options.stringFormatter) {
return this.options.stringFormatter(value);
}

if (formattingOptions.dontQuote) {
value = '' + value;
} else {
Expand Down Expand Up @@ -2935,7 +2942,7 @@ function _buildSquel() {
}(cls.QueryBuilder);

var _squel = {
VERSION: '5.1.0',
VERSION: '5.2.0',
flavour: flavour,
expr: function expr(options) {
return new cls.Expression(options);
Expand Down
4 changes: 2 additions & 2 deletions squel.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/core.js
Expand Up @@ -160,6 +160,8 @@ function _buildSquel(flavour = null) {
singleQuoteReplacement: '\'\'',
// String used to join individual blocks in a query when it's stringified
separator: ' ',
// Function for formatting string values prior to insertion into query string
stringFormatter: null,
};

// Global custom value handlers for all instances of builder
Expand Down Expand Up @@ -470,6 +472,11 @@ function _buildSquel(flavour = null) {
value = this._applyNestingFormatting(value.toString());
}
else if (typeofValue !== "number") {
// if it's a string and we have custom string formatting turned on then use that
if ('string' === typeofValue && this.options.stringFormatter) {
return this.options.stringFormatter(value);
}

if (formattingOptions.dontQuote) {
value = `${value}`;
} else {
Expand Down
33 changes: 20 additions & 13 deletions test/baseclasses.test.coffee
Expand Up @@ -99,6 +99,7 @@ test['Default query builder options'] =
replaceSingleQuotes: false
singleQuoteReplacement: '\'\''
separator: ' '
stringFormatter: null
}, squel.cls.DefaultQueryBuilderOptions


Expand Down Expand Up @@ -646,24 +647,30 @@ test['Builder base class'] =
'float': ->
assert.same 1.2, @inst._formatValueForQueryString(1.2)

'string': ->
escapedValue = undefined
test.mocker.stub @inst, '_escapeValue', (str) -> escapedValue or str
'string':
'have string formatter function': ->
@inst.options.stringFormatter = (str) -> "N(#{str})"

assert.same "N(test)", @inst._formatValueForQueryString('test')

'default': ->
escapedValue = undefined
test.mocker.stub @inst, '_escapeValue', (str) -> escapedValue or str

assert.same "'test'", @inst._formatValueForQueryString('test')
assert.same "'test'", @inst._formatValueForQueryString('test')

assert.same "'test'", @inst._formatValueForQueryString('test')
assert.ok @inst._escapeValue.calledWithExactly('test')
escapedValue = 'blah'
assert.same "'blah'", @inst._formatValueForQueryString('test')
assert.same "'test'", @inst._formatValueForQueryString('test')
assert.ok @inst._escapeValue.calledWithExactly('test')
escapedValue = 'blah'
assert.same "'blah'", @inst._formatValueForQueryString('test')

'string - dont quote': ->
escapedValue = undefined
test.mocker.stub @inst, '_escapeValue', (str) -> escapedValue or str
'dont quote': ->
escapedValue = undefined
test.mocker.stub @inst, '_escapeValue', (str) -> escapedValue or str

assert.same "test", @inst._formatValueForQueryString('test', dontQuote: true )
assert.same "test", @inst._formatValueForQueryString('test', dontQuote: true )

assert.ok @inst._escapeValue.notCalled
assert.ok @inst._escapeValue.notCalled

'Array - recursively calls itself on each element': ->
spy = test.mocker.spy @inst, '_formatValueForQueryString'
Expand Down
1 change: 1 addition & 0 deletions test/mssql.test.coffee
Expand Up @@ -155,6 +155,7 @@ test['MSSQL flavour'] =
replaceSingleQuotes: true
singleQuoteReplacement: '\'\''
separator: ' '
stringFormatter: null
}, squel.cls.DefaultQueryBuilderOptions

module?.exports[require('path').basename(__filename)] = test
1 change: 1 addition & 0 deletions test/postgres.test.coffee
Expand Up @@ -160,6 +160,7 @@ test['Postgres flavour'] =
numberedParametersPrefix: '$'
numberedParametersStartAt: 1
separator: ' '
stringFormatter: null
}, squel.cls.DefaultQueryBuilderOptions


Expand Down

0 comments on commit c4d54ab

Please sign in to comment.