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

Commit 701b9724 by sdepold

created index: done

1 parent 0027dfae
var Utils = require("../../utils") var Utils = require("../../utils")
, util = require("util")
var QueryGenerator = module.exports = { var QueryGenerator = module.exports = {
/* /*
...@@ -185,17 +186,42 @@ var QueryGenerator = module.exports = { ...@@ -185,17 +186,42 @@ var QueryGenerator = module.exports = {
}, },
addIndexQuery: function(tableName, attributes, options) { addIndexQuery: function(tableName, attributes, options) {
var transformedAttributes = attributes.map(function(attribute) {
if(typeof attribute == 'string')
return attribute
else {
var result = ""
if(!attribute.attribute)
throw new Error('The following index attribute has no attribute: ' + util.inspect(attribute))
result += attribute.attribute
if(attribute.length)
result += '(' + attribute.length + ')'
if(attribute.order)
result += ' ' + attribute.order
return result
}
})
var onlyAttributeNames = attributes.map(function(attribute) {
return (typeof attribute == 'string') ? attribute : attribute.attribute
})
options = Utils._.extend({ options = Utils._.extend({
indicesType: null, indicesType: null,
indexName: Utils._.underscored(tableName + '_' + attributes.join('_')), indexName: Utils._.underscored(tableName + '_' + onlyAttributeNames.join('_')),
parser: null parser: null
}, options || {}) }, options || {})
return Utils._.compact([ return Utils._.compact([
"CREATE", options.indicesType || null, "INDEX", options.indexName, "CREATE", options.indicesType, "INDEX", options.indexName,
options.indexType ? 'USING ' + options.indexType : undefined, (options.indexType ? ('USING ' + options.indexType) : undefined),
"ON", tableName, attributes.join(', '), "ON", tableName, '(' + transformedAttributes.join(', ') + ')',
options.parser ? "WITH PARSER " + options.parser : undefined (options.parser ? "WITH PARSER " + options.parser : undefined)
]).join(' ') ]).join(' ')
......
...@@ -127,7 +127,20 @@ describe('QueryGenerator', function() { ...@@ -127,7 +127,20 @@ describe('QueryGenerator', function() {
'addIndexQuery': [ 'addIndexQuery': [
{ {
arguments: ['User', ['username', 'isAdmin']], arguments: ['User', ['username', 'isAdmin']],
expectation: 'CREATE INDEX user_username_is_admin ON User username, isAdmin' expectation: 'CREATE INDEX user_username_is_admin ON User (username, isAdmin)'
}, {
arguments: [
'User', [
{ attribute: 'username', length: 10, order: 'ASC'},
'isAdmin'
]
],
expectation: "CREATE INDEX user_username_is_admin ON User (username(10) ASC, isAdmin)"
}, {
arguments: [
'User', ['username', 'isAdmin'], { parser: 'foo', indicesType: 'FULLTEXT', indexName: 'bar'}
],
expectation: "CREATE FULLTEXT INDEX bar ON User (username, isAdmin) WITH PARSER foo"
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!