不要怂,就是干,撸起袖子干!

Commit 7d8eee95 by Sascha Depold

use arrays for aliasing in select queries

1 parent f15dcc21
...@@ -56,7 +56,12 @@ var QueryGenerator = module.exports = { ...@@ -56,7 +56,12 @@ var QueryGenerator = module.exports = {
selectQuery: function(tableName, options) { selectQuery: function(tableName, options) {
options = options || {} options = options || {}
options.table = Utils.addTicks(tableName) options.table = Utils.addTicks(tableName)
options.attributes = options.attributes && options.attributes.map(function(attr){return Utils.addTicks(attr)}).join(", ") options.attributes = options.attributes && options.attributes.map(function(attr){
if(Array.isArray(attr) && attr.length == 2)
return [attr[0], Utils.addTicks(attr[1])].join(' as ')
else
return Utils.addTicks(attr)
}).join(", ")
options.attributes = options.attributes || '*' options.attributes = options.attributes || '*'
var query = "SELECT <%= attributes %> FROM <%= table %>" var query = "SELECT <%= attributes %> FROM <%= table %>"
......
...@@ -17,4 +17,14 @@ describe('QueryGenerator', function() { ...@@ -17,4 +17,14 @@ describe('QueryGenerator', function() {
) )
}) })
}) })
describe('selectQuery', function() {
it("should correctly convert arrays into aliases", function() {
expect(
QueryGenerator.selectQuery('foo', { attributes: [['count(*)', 'count']] })
).toEqual(
'SELECT count(*) as `count` FROM `foo`;'
)
})
})
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!