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

Now custom value handler receives as a second argument hash of options a... #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions squel-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,14 @@ OTHER DEALINGS IN THE SOFTWARE.
return value.replace(/\'/g, this.options.singleQuoteReplacement);
};

BaseBuilder.prototype._formatCustomValue = function(value) {
BaseBuilder.prototype._formatCustomValue = function(value, formattingOptions) {
var customHandler;
if (formattingOptions == null) {
formattingOptions = {};
}
customHandler = getValueHandler(value, this.options.valueHandlers, cls.globalValueHandlers);
if (customHandler) {
value = customHandler(value);
value = customHandler(value, formattingOptions);
}
return value;
};
Expand Down Expand Up @@ -310,7 +313,7 @@ OTHER DEALINGS IN THE SOFTWARE.
if (formattingOptions == null) {
formattingOptions = {};
}
value = this._formatCustomValue(value);
value = this._formatCustomValue(value, formattingOptions);
if (Array.isArray(value)) {
value = value.map(function(v) {
return _this._formatValue(v);
Expand Down
2 changes: 1 addition & 1 deletion squel-basic.min.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions squel.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,14 @@ OTHER DEALINGS IN THE SOFTWARE.
return value.replace(/\'/g, this.options.singleQuoteReplacement);
};

BaseBuilder.prototype._formatCustomValue = function(value) {
BaseBuilder.prototype._formatCustomValue = function(value, formattingOptions) {
var customHandler;
if (formattingOptions == null) {
formattingOptions = {};
}
customHandler = getValueHandler(value, this.options.valueHandlers, cls.globalValueHandlers);
if (customHandler) {
value = customHandler(value);
value = customHandler(value, formattingOptions);
}
return value;
};
Expand Down Expand Up @@ -311,7 +314,7 @@ OTHER DEALINGS IN THE SOFTWARE.
if (formattingOptions == null) {
formattingOptions = {};
}
value = this._formatCustomValue(value);
value = this._formatCustomValue(value, formattingOptions);
if (Array.isArray(value)) {
value = value.map(function(v) {
return _this._formatValue(v);
Expand Down
2 changes: 1 addition & 1 deletion squel.min.js

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions src/squel.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ class cls.BaseBuilder extends cls.Cloneable
value.replace /\'/g, @options.singleQuoteReplacement

# Format the given custom value
_formatCustomValue: (value) ->
_formatCustomValue: (value, formattingOptions = {}) ->
# user defined custom handlers takes precedence
customHandler = getValueHandler(value, @options.valueHandlers, cls.globalValueHandlers)
if customHandler
# use the custom handler if available
value = customHandler(value)
value = customHandler(value, formattingOptions)

value

Expand All @@ -275,7 +275,7 @@ class cls.BaseBuilder extends cls.Cloneable

# Format the given field value for inclusion into the query string
_formatValue: (value, formattingOptions = {}) ->
value = @_formatCustomValue(value)
value = @_formatCustomValue(value, formattingOptions)

# if it's an array then format each element separately
if Array.isArray(value)
Expand Down Expand Up @@ -1569,5 +1569,3 @@ squel.useFlavour = (flavour) ->
else
throw new Error "Flavour not available: #{flavour}"
squel


7 changes: 7 additions & 0 deletions test/baseclasses.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,13 @@ test['Builder base class'] =
test.mocker.stub @inst, '_formatCustomValue', -> 'abc'
assert.same "'abc'", @inst._formatValue(123)

'Custom value - dont quote option': ->
class SqlDefault
@inst.registerValueHandler SqlDefault, (v, formattingOptions) ->
formattingOptions.dontQuote = true
'DEFAULT'
assert.same "DEFAULT", @inst._formatValue(new SqlDefault())



test['QueryBuilder base class'] =
Expand Down