-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add MSSQL support (requested changes) #33
base: master
Are you sure you want to change the base?
Changes from all commits
64fe70e
bbbb4f6
b01ed3a
20600ff
c62c500
2022daf
6bd2f6a
f12a5c6
99c711a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
var _ = require('underscore'); | ||
|
||
module.exports = function(dialect) { | ||
var parentValueBlock = dialect.blocks.get('value'); | ||
|
||
dialect.blocks.set('value', function(params) { | ||
var value = params.value; | ||
|
||
var result; | ||
if (_.isBoolean(value)) { | ||
result = String(Number(value)); | ||
} else { | ||
result = parentValueBlock(params); | ||
} | ||
|
||
return result; | ||
}); | ||
|
||
dialect.blocks.set('limit', function(params) { | ||
var result = ''; | ||
if (_.isUndefined(params.offset)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
result = 'top(' + dialect.builder._pushValue(params.limit) + ')'; | ||
} | ||
return result; | ||
}); | ||
|
||
dialect.blocks.set('offset', function(params) { | ||
var prefix = (!params.sort) ? 'order by 1 ' : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function was refactored to clean up the program flow. |
||
var offset = 'offset ' + dialect.builder._pushValue(params.offset) + ' '; | ||
var limit = ''; | ||
var suffix = 'rows'; | ||
|
||
if (params.limit) { | ||
limit = 'rows fetch next ' + dialect.builder._pushValue(params.limit) + ' '; | ||
suffix = 'rows only'; | ||
} | ||
|
||
return prefix + offset + limit + suffix; | ||
}); | ||
|
||
dialect.blocks.set('returning', function(params) { | ||
var result = dialect.buildBlock('fields', {fields: params.returning}); | ||
|
||
if (result) result = 'output ' + result; | ||
|
||
return result; | ||
}); | ||
|
||
dialect.blocks.set('insert:values', function(params) { | ||
var values = params.values; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function was refactored a little bit, as was the
#26 (comment) |
||
var fields = params.fields; | ||
var returning = params.returning; | ||
|
||
if (!_.isArray(values)) values = [values]; | ||
|
||
fields = fields || _(values) | ||
.chain() | ||
.map(function(row) { | ||
return _(row).keys(); | ||
}) | ||
.flatten() | ||
.uniq() | ||
.value(); | ||
|
||
values = _(values).map(function(row) { | ||
return _(fields).map(function(field) { | ||
return dialect.buildBlock('value', {value: row[field]}); | ||
}); | ||
}); | ||
|
||
return dialect.buildTemplate('insertValues', { | ||
values: values, | ||
fields: fields, | ||
returning: returning | ||
}); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
'use strict'; | ||
|
||
var templateChecks = require('../../utils/templateChecks'); | ||
var orRegExp = /^(rollback|abort|replace|fail|ignore)$/i; | ||
|
||
module.exports = function(dialect) { | ||
dialect.templates.set('select', { | ||
pattern: '{with} {withRecursive} select {limit} {distinct} {fields} ' + | ||
'from {from} {table} {query} {select} {expression} {alias} ' + | ||
'{join} {condition} {group} {having} {sort} {offset}', | ||
defaults: { | ||
fields: {} | ||
}, | ||
validate: function(type, params) { | ||
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']); | ||
templateChecks.propType(type, params, 'with', 'object'); | ||
templateChecks.propType(type, params, 'withRecursive', 'object'); | ||
|
||
templateChecks.propType(type, params, 'distinct', 'boolean'); | ||
|
||
templateChecks.propType(type, params, 'fields', ['array', 'object']); | ||
|
||
templateChecks.propType(type, params, 'from', ['string', 'array', 'object']); | ||
|
||
templateChecks.atLeastOneOfProps(type, params, ['table', 'query', 'select', 'expression']); | ||
templateChecks.onlyOneOfProps(type, params, ['table', 'query', 'select', 'expression']); | ||
|
||
templateChecks.propType(type, params, 'table', 'string'); | ||
templateChecks.propType(type, params, 'query', 'object'); | ||
templateChecks.propType(type, params, 'select', 'object'); | ||
templateChecks.propType(type, params, 'expression', ['string', 'object']); | ||
|
||
templateChecks.propType(type, params, 'alias', ['string', 'object']); | ||
|
||
templateChecks.propType(type, params, 'join', ['array', 'object']); | ||
|
||
templateChecks.propType(type, params, 'condition', ['array', 'object']); | ||
templateChecks.propType(type, params, 'having', ['array', 'object']); | ||
|
||
templateChecks.propType(type, params, 'group', ['string', 'array']); | ||
|
||
templateChecks.propType(type, params, 'sort', ['string', 'array', 'object']); | ||
|
||
templateChecks.propType(type, params, 'offset', ['number', 'string']); | ||
templateChecks.propType(type, params, 'limit', ['number', 'string']); | ||
} | ||
}); | ||
|
||
dialect.templates.set('insert', { | ||
pattern: '{with} {withRecursive} insert {or} into {table} {values} ' + | ||
'{condition}', | ||
validate: function(type, params) { | ||
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']); | ||
templateChecks.propType(type, params, 'with', 'object'); | ||
templateChecks.propType(type, params, 'withRecursive', 'object'); | ||
|
||
templateChecks.propType(type, params, 'or', 'string'); | ||
templateChecks.propMatch(type, params, 'or', orRegExp); | ||
|
||
templateChecks.requiredProp(type, params, 'table'); | ||
templateChecks.propType(type, params, 'table', 'string'); | ||
|
||
templateChecks.requiredProp(type, params, 'values'); | ||
templateChecks.propType(type, params, 'values', ['array', 'object']); | ||
|
||
templateChecks.propType(type, params, 'condition', ['array', 'object']); | ||
|
||
} | ||
}); | ||
|
||
dialect.templates.set('insertValues', { | ||
pattern: '({fields}) {returning} values {values}', | ||
validate: function(type, params) { | ||
templateChecks.requiredProp('values', params, 'fields'); | ||
templateChecks.propType('values', params, 'fields', 'array'); | ||
templateChecks.minPropLength('values', params, 'fields', 1); | ||
|
||
templateChecks.propType(type, params, 'returning', ['array', 'object']); | ||
|
||
templateChecks.requiredProp('values', params, 'values'); | ||
templateChecks.propType('values', params, 'values', 'array'); | ||
templateChecks.minPropLength('values', params, 'values', 1); | ||
} | ||
}); | ||
|
||
dialect.templates.set('update', { | ||
pattern: '{with} {withRecursive} update {or} {table} {alias} {modifier} {returning} ' + | ||
'{condition} ', | ||
validate: function(type, params) { | ||
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']); | ||
templateChecks.propType(type, params, 'with', 'object'); | ||
templateChecks.propType(type, params, 'withRecursive', 'object'); | ||
|
||
templateChecks.propType(type, params, 'or', 'string'); | ||
templateChecks.propMatch(type, params, 'or', orRegExp); | ||
|
||
templateChecks.requiredProp(type, params, 'table'); | ||
templateChecks.propType(type, params, 'table', 'string'); | ||
|
||
templateChecks.propType(type, params, 'returning', ['array', 'object']); | ||
|
||
templateChecks.propType(type, params, 'alias', 'string'); | ||
|
||
templateChecks.requiredProp(type, params, 'modifier'); | ||
templateChecks.propType(type, params, 'modifier', 'object'); | ||
|
||
templateChecks.propType(type, params, 'condition', ['array', 'object']); | ||
|
||
} | ||
}); | ||
|
||
dialect.templates.set('remove', { | ||
pattern: '{with} {withRecursive} delete from {table} {returning} {alias} {condition} ', | ||
validate: function(type, params) { | ||
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']); | ||
templateChecks.propType(type, params, 'with', 'object'); | ||
templateChecks.propType(type, params, 'withRecursive', 'object'); | ||
|
||
templateChecks.requiredProp(type, params, 'table'); | ||
templateChecks.propType(type, params, 'table', 'string'); | ||
|
||
templateChecks.propType(type, params, 'returning', ['array', 'object']); | ||
|
||
templateChecks.propType(type, params, 'alias', 'string'); | ||
|
||
templateChecks.propType(type, params, 'condition', ['array', 'object']); | ||
|
||
} | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#26 (comment)
Logic moved from
builder._pushValue
toblocks.js
.number
,string
, andnull
/undefined
cases are handled byparentValueBlock(params)
I belive.