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

Commit fd0d5059 by Matt Broadstone

refactor bulk insert and clean up logs

Bulk insertions were refactored to prefer row construction for bulk
inserts that are not empty. The actual sql generation was moved back
into the query-generator, and removed from sql-generator. Also a number
of console.log and console.warn's were moved over to using sequelize's
internal logging mechanism
1 parent 0968f43b
...@@ -7,8 +7,6 @@ var Utils = require('../../utils') ...@@ -7,8 +7,6 @@ var Utils = require('../../utils')
, _ = require('lodash') , _ = require('lodash')
, util = require('util'); , util = require('util');
module.exports = (function() { module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
get options(){ get options(){
...@@ -156,60 +154,64 @@ module.exports = (function() { ...@@ -156,60 +154,64 @@ module.exports = (function() {
Returns an insert into command for multiple values. Returns an insert into command for multiple values.
Parameters: table name + list of hashes of attribute-value-pairs. Parameters: table name + list of hashes of attribute-value-pairs.
*/ */
/* istanbul ignore next */ bulkInsertQuery: function(tableName, attrValueHashes, options, attributes) {
bulkInsertQuery: function(tableName, attrValueHashes,options, attributes) { var query = 'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;'
var query = '', , emptyQuery = 'INSERT INTO <%= table %> DEFAULT VALUES'
allAttributes = [], , tuples = []
insertKey = false, , allAttributes = []
isEmpty = true, , needIdentityInsertWrapper = false
ignoreKeys = []; , allQueries = [];
for(var key in attributes){
var aliasKey = attributes[key].field || key;
if(ignoreKeys.indexOf(aliasKey) < 0){
ignoreKeys.push(aliasKey);
}
if(attributes[key].autoIncrement){
for(var i = 0; i < attrValueHashes.length; i++){
if(aliasKey in attrValueHashes[i]){
delete attrValueHashes[i][aliasKey];
}
}
}
}
Utils._.forEach(attrValueHashes, function(attrValueHash, i) { Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forOwn(attrValueHash, function(value, key, hash) { // special case for empty objects with primary keys
if (allAttributes.indexOf(key) === -1) allAttributes.push(key); var fields = Object.keys(attrValueHash);
var aliasKey = attributes[key].field || key; if (fields.length === 1 && attributes[fields[0]].autoIncrement && attrValueHash[fields[0]] === null) {
if (value !== null && ignoreKeys.indexOf(key) > -1){ allQueries.push(emptyQuery);
return;
}
ignoreKeys.splice(ignoreKeys.indexOf(key),1); // normal case
}else if(value !== null && attributes[key].autoIncrement){ Utils._.forOwn(attrValueHash, function(value, key, hash) {
insertKey = true; if (value !== null && attributes[key].autoIncrement) {
needIdentityInsertWrapper = true;
} }
if(value !== null){
isEmpty = false; if (allAttributes.indexOf(key) === -1) {
if (value === null && attributes[key].autoIncrement)
return;
allAttributes.push(key);
} }
}); });
}); });
if(!isEmpty){ if (allAttributes.length > 0) {
for(var j = 0; j < ignoreKeys.length; j++){ Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
if(allAttributes.indexOf(ignoreKeys[j]) > -1){ tuples.push('(' +
allAttributes.splice(allAttributes.indexOf(ignoreKeys[j]), 1); allAttributes.map(function(key) {
} return this.escape(attrValueHash[key]);
} }.bind(this)).join(',') +
query = SqlGenerator.bulkInsertSql(tableName, allAttributes, attrValueHashes,options); ')');
if(insertKey){ }.bind(this));
query = SqlGenerator.identityInsertWrapper(query, tableName);
} allQueries.push(query);
}else{
for(var k = 0; k < attrValueHashes.length; k++){
query += SqlGenerator.insertSql(tableName);
}
} }
return query;
var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' IGNORE' : '',
table: this.quoteTable(tableName),
attributes: allAttributes.map(function(attr) {
return this.quoteIdentifier(attr);
}.bind(this)).join(','),
tuples: tuples
};
var generatedQuery = Utils._.template(allQueries.join(';'))(replacements);
if (needIdentityInsertWrapper)
return SqlGenerator.identityInsertWrapper(generatedQuery, tableName);
return generatedQuery;
}, },
/* /*
Returns an update query. Returns an update query.
Parameters: Parameters:
...@@ -226,7 +228,7 @@ module.exports = (function() { ...@@ -226,7 +228,7 @@ module.exports = (function() {
//very unique case for cascades, i generally don't approve //very unique case for cascades, i generally don't approve
if(Object.keys(attrValueHash).length === 1 && attributes[Object.keys(attrValueHash)[0]].primaryKey){ if(Object.keys(attrValueHash).length === 1 && attributes[Object.keys(attrValueHash)[0]].primaryKey){
console.warn('Updating a Primary Key is not supported in MSSQL, please restructure your query'); this.sequelize.log('Updating a Primary Key is not supported in MSSQL, please restructure your query');
}else{ }else{
for(var key in attributes){ for(var key in attributes){
var aliasKey = attributes[key].field || key; var aliasKey = attributes[key].field || key;
...@@ -250,6 +252,7 @@ module.exports = (function() { ...@@ -250,6 +252,7 @@ module.exports = (function() {
return query; return query;
}, },
/* /*
Returns a deletion query. Returns a deletion query.
Parameters: Parameters:
...@@ -351,7 +354,7 @@ module.exports = (function() { ...@@ -351,7 +354,7 @@ module.exports = (function() {
if(attribute.onDelete || attribute.onUpdate){ if(attribute.onDelete || attribute.onUpdate){
//handles self referencial keys, first it doesnt make sense, second, what? //handles self referencial keys, first it doesnt make sense, second, what?
if(attribute.Model && attribute.Model.tableName === attribute.references){ if(attribute.Model && attribute.Model.tableName === attribute.references){
console.warn('MSSQL does not support self referencial constraints, ' this.sequelize.log('MSSQL does not support self referencial constraints, '
+ 'we will remove it but we recommend restructuring your query'); + 'we will remove it but we recommend restructuring your query');
attribute.onDelete = ''; attribute.onDelete = '';
attribute.onUpdate = ''; attribute.onUpdate = '';
...@@ -365,7 +368,7 @@ module.exports = (function() { ...@@ -365,7 +368,7 @@ module.exports = (function() {
for(var j = i+1; j < cascadeCheck.length; j++){ for(var j = i+1; j < cascadeCheck.length; j++){
var casKey2 = cascadeCheck[j]; var casKey2 = cascadeCheck[j];
if(attributes[casKey].referencesKey === attributes[casKey2].referencesKey){ if(attributes[casKey].referencesKey === attributes[casKey2].referencesKey){
console.warn('MSSQL does not support multiple cascade keys on the same reference, ' this.sequelize.log('MSSQL does not support multiple cascade keys on the same reference, '
+ 'we will remove them to make this work but we recommend restructuring your query.'); + 'we will remove them to make this work but we recommend restructuring your query.');
attributes[casKey].onDelete = ''; attributes[casKey].onDelete = '';
attributes[casKey].onUpdate = ''; attributes[casKey].onUpdate = '';
...@@ -402,6 +405,7 @@ module.exports = (function() { ...@@ -402,6 +405,7 @@ module.exports = (function() {
} }
return fields; return fields;
}, },
quoteTable: function(param, as) { quoteTable: function(param, as) {
return SqlGenerator.quoteTable(param, as); return SqlGenerator.quoteTable(param, as);
}, },
......
...@@ -44,7 +44,7 @@ module.exports = (function() { ...@@ -44,7 +44,7 @@ module.exports = (function() {
var trans = new self.connection.lib.Transaction(self.connection.context); var trans = new self.connection.lib.Transaction(self.connection.context);
trans.begin(function(err){ trans.begin(function(err){
if (err) { if (err) {
console.log(err.message); this.sequelize.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
}else{ }else{
self.connection.lib._transaction = trans; self.connection.lib._transaction = trans;
...@@ -53,7 +53,7 @@ module.exports = (function() { ...@@ -53,7 +53,7 @@ module.exports = (function() {
}); });
} else{ } else{
var request, transCommand; var request, transCommand;
if (self.connection.lib._transaction if (self.connection.lib._transaction
&& self.connection.uuid){ && self.connection.uuid){
request = new self.connection.lib.Request(self.connection.lib._transaction); request = new self.connection.lib.Request(self.connection.lib._transaction);
...@@ -68,7 +68,7 @@ module.exports = (function() { ...@@ -68,7 +68,7 @@ module.exports = (function() {
self.sql === 'ROLLBACK TRANSACTION;') { self.sql === 'ROLLBACK TRANSACTION;') {
self.connection.lib._transaction[transCommand](function (err, result) { self.connection.lib._transaction[transCommand](function (err, result) {
if (err) { if (err) {
console.log(err.message); self.sequelize.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
} else { } else {
resolve(self.formatResults(result)); resolve(self.formatResults(result));
...@@ -85,7 +85,7 @@ module.exports = (function() { ...@@ -85,7 +85,7 @@ module.exports = (function() {
promise.emit('sql', self.sql, self.connection.uuid); promise.emit('sql', self.sql, self.connection.uuid);
} }
if(err){ if(err){
console.log(err.message); self.sequelize.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
} else { } else {
resolve(self.formatResults(recordset)); resolve(self.formatResults(recordset));
...@@ -141,7 +141,7 @@ module.exports = (function() { ...@@ -141,7 +141,7 @@ module.exports = (function() {
} else if (this.isCallQuery()) { } else if (this.isCallQuery()) {
result = data[0]; result = data[0];
} else if (this.isBulkUpdateQuery()) { } else if (this.isBulkUpdateQuery()) {
result = data.length; result = data.length;
} else if (this.isBulkDeleteQuery()){ } else if (this.isBulkDeleteQuery()){
result = data[0].AFFECTEDROWS; result = data[0].AFFECTEDROWS;
} else if (result && result.dataValues){} } else if (result && result.dataValues){}
......
...@@ -25,6 +25,7 @@ var attributeMap = { ...@@ -25,6 +25,7 @@ var attributeMap = {
onUpdate:"ON UPDATE", onUpdate:"ON UPDATE",
default:"DEFAULT" default:"DEFAULT"
}; };
function escape(value, field) { function escape(value, field) {
if (value && value._isSequelizeMethod) { if (value && value._isSequelizeMethod) {
return value.toString(); return value.toString();
...@@ -32,10 +33,12 @@ function escape(value, field) { ...@@ -32,10 +33,12 @@ function escape(value, field) {
return SqlString.escape(value, false, _options.timezone, _dialect, field); return SqlString.escape(value, false, _options.timezone, _dialect, field);
} }
} }
function quoteIdentifier(identifier, force) { function quoteIdentifier(identifier, force) {
if (identifier === '*') return identifier; if (identifier === '*') return identifier;
return Utils.addTicks(identifier, '"'); return Utils.addTicks(identifier, '"');
} }
/* /*
Split an identifier into .-separated tokens and quote each part Split an identifier into .-separated tokens and quote each part
*/ */
...@@ -48,9 +51,11 @@ function quoteIdentifiers(identifiers, force) { ...@@ -48,9 +51,11 @@ function quoteIdentifiers(identifiers, force) {
return quoteIdentifier(identifiers); return quoteIdentifier(identifiers);
} }
} }
function wrapSingleQuote(identifier){ function wrapSingleQuote(identifier){
return Utils.addTicks(identifier, "'"); return Utils.addTicks(identifier, "'");
} }
function nameIndexes(indexes, rawTablename) { function nameIndexes(indexes, rawTablename) {
return Utils._.map(indexes, function (index) { return Utils._.map(indexes, function (index) {
if (!index.hasOwnProperty('name')) { if (!index.hasOwnProperty('name')) {
...@@ -120,6 +125,7 @@ function processValue(val, modelAttribute){ ...@@ -120,6 +125,7 @@ function processValue(val, modelAttribute){
return escape(val, modelAttribute); return escape(val, modelAttribute);
} }
} }
function valuesToSql(fields, modelAttributeMap, isUpdate){ function valuesToSql(fields, modelAttributeMap, isUpdate){
var values = []; var values = [];
for (var key in fields) { for (var key in fields) {
...@@ -138,6 +144,7 @@ function valuesToSql(fields, modelAttributeMap, isUpdate){ ...@@ -138,6 +144,7 @@ function valuesToSql(fields, modelAttributeMap, isUpdate){
} }
return ''; return '';
} }
function loadColumn(attributes){ function loadColumn(attributes){
var attrStr = []; var attrStr = [];
for (var attr in attributes) { for (var attr in attributes) {
...@@ -167,6 +174,7 @@ function loadFields(attributes){ ...@@ -167,6 +174,7 @@ function loadFields(attributes){
} }
return attrStr; return attrStr;
} }
function processField(attribute, tableName){ function processField(attribute, tableName){
var sql = ''; var sql = '';
if(tableName){ if(tableName){
...@@ -178,13 +186,14 @@ function processField(attribute, tableName){ ...@@ -178,13 +186,14 @@ function processField(attribute, tableName){
return sql + quoteIdentifier(attribute[0]); return sql + quoteIdentifier(attribute[0]);
} }
} }
function loadFieldsWithName(attributes, tableName){ function loadFieldsWithName(attributes, tableName){
var attrStr = []; var attrStr = [];
if(Array.isArray(attributes[0])){ if(Array.isArray(attributes[0])){
for (var i = 0; i < attributes.length; i++) { for (var i = 0; i < attributes.length; i++) {
attrStr.push(processField(attributes[i], tableName)); attrStr.push(processField(attributes[i], tableName));
} }
}else{ }else{
for (var attr in attributes) { for (var attr in attributes) {
if(tableName){ if(tableName){
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr)); attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
...@@ -195,6 +204,7 @@ function loadFieldsWithName(attributes, tableName){ ...@@ -195,6 +204,7 @@ function loadFieldsWithName(attributes, tableName){
} }
return attrStr.join(','); return attrStr.join(',');
} }
function joinFields(attributes, tableName){ function joinFields(attributes, tableName){
var attrStr = []; var attrStr = [];
if(tableName){ if(tableName){
...@@ -208,41 +218,51 @@ function joinFields(attributes, tableName){ ...@@ -208,41 +218,51 @@ function joinFields(attributes, tableName){
return attrStr.join(','); return attrStr.join(',');
} }
module.exports = { module.exports = {
get options(){ get options(){
return _options; return _options;
}, },
set options(opt) { set options(opt) {
_options = opt; _options = opt;
}, },
get dialect(){ get dialect(){
return _dialect; return _dialect;
}, },
set dialect(dial) { set dialect(dial) {
_dialect = dial; _dialect = dial;
}, },
get sequelize(){ get sequelize(){
return _sequelize; return _sequelize;
}, },
set sequelize(seq) { set sequelize(seq) {
_sequelize = seq; _sequelize = seq;
}, },
quoteIdentifier: function(val){ quoteIdentifier: function(val){
return quoteIdentifier(val); return quoteIdentifier(val);
}, },
quoteIdentifiers: function(val, force){ quoteIdentifiers: function(val, force){
return quoteIdentifiers(val, force); return quoteIdentifiers(val, force);
}, },
escape: function(value, field) { escape: function(value, field) {
return escape(value,field); return escape(value,field);
}, },
showTableSql: function(){ showTableSql: function(){
return 'SELECT name FROM sys.Tables;'; return 'SELECT name FROM sys.Tables;';
}, },
processValue: function(val, modelAttribute){ processValue: function(val, modelAttribute){
return processValue(val, modelAttribute); return processValue(val, modelAttribute);
}, },
quoteTable: function(param, as) { quoteTable: function(param, as) {
var table = ''; var table = '';
if (as === true) { if (as === true) {
...@@ -264,6 +284,7 @@ module.exports = { ...@@ -264,6 +284,7 @@ module.exports = {
} }
return table; return table;
}, },
identityInsertWrapper: function(query, table){ identityInsertWrapper: function(query, table){
return[ return[
'SET IDENTITY_INSERT', quoteIdentifier(table), 'ON;', 'SET IDENTITY_INSERT', quoteIdentifier(table), 'ON;',
...@@ -271,6 +292,7 @@ module.exports = { ...@@ -271,6 +292,7 @@ module.exports = {
'SET IDENTITY_INSERT', quoteIdentifier(table), 'OFF;', 'SET IDENTITY_INSERT', quoteIdentifier(table), 'OFF;',
].join(' '); ].join(' ');
}, },
getCreateSchemaSql: function(schema){ getCreateSchemaSql: function(schema){
return [ return [
'IF NOT EXISTS (SELECT schema_name', 'IF NOT EXISTS (SELECT schema_name',
...@@ -280,6 +302,7 @@ module.exports = { ...@@ -280,6 +302,7 @@ module.exports = {
'EXEC sp_executesql N\'CREATE SCHEMA', quoteIdentifier(schema),';\'', 'EXEC sp_executesql N\'CREATE SCHEMA', quoteIdentifier(schema),';\'',
"END;"].join(' '); "END;"].join(' ');
}, },
getCreateTableSql: function(tableName, attributes, options) { getCreateTableSql: function(tableName, attributes, options) {
var query = "CREATE TABLE <%= tableName %> (<%= attributes%>)"; var query = "CREATE TABLE <%= tableName %> (<%= attributes%>)";
var attrStr = [] var attrStr = []
...@@ -300,6 +323,7 @@ module.exports = { ...@@ -300,6 +323,7 @@ module.exports = {
query = addTableExistsWrapper(query); query = addTableExistsWrapper(query);
return Utils._.template(query)(values).trim() + ";"; return Utils._.template(query)(values).trim() + ";";
}, },
alterTableSql: function(tableName){ alterTableSql: function(tableName){
var query = 'ALTER TABLE <%= tableName %>'; var query = 'ALTER TABLE <%= tableName %>';
var value = { var value = {
...@@ -307,44 +331,19 @@ module.exports = { ...@@ -307,44 +331,19 @@ module.exports = {
}; };
return Utils._.template(query)(value); return Utils._.template(query)(value);
}, },
dropTableSql: function(tableName, options){ dropTableSql: function(tableName, options){
var query = "DROP TABLE <%= tableName %>"; var query = "DROP TABLE <%= tableName %>";
var values ={}; var values ={};
values = { values = {
unquotedTable: tableName, unquotedTable: tableName,
tableName: quoteIdentifier(tableName.toString()) tableName: quoteIdentifier(tableName.toString())
}; };
query = addTableExistsWrapper(query, true); query = addTableExistsWrapper(query, true);
return Utils._.template(query)(values).trim() + ";"; return Utils._.template(query)(values).trim() + ";";
}, },
bulkInsertSql: function(tableName, attributeKeys, attributes,options) {
var query = 'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;'
, emptyQuery = 'INSERT INTO <%= table %> DEFAULT VALUES'
, tuples = [];
Utils._.forEach(attributes, function(attrValueHash, i) {
tuples.push('(' +
attributeKeys.map(function(key) {
if(typeof attrValueHash[key] === 'boolean'){
return "'" + attrValueHash[key] + "'";
}
return escape(attrValueHash[key]);
}.bind(this)).join(',') +
')');
}.bind(this));
var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' IGNORE' : '',
table: quoteIdentifier(tableName.toString()),
attributes: attributeKeys.map(function(attr) {
return quoteIdentifier(attr);
}.bind(this)).join(','),
tuples: tuples
};
return Utils._.template(query)(replacements);
},
insertSql: function(tableName, valueHash, modelAttributeMap) { insertSql: function(tableName, valueHash, modelAttributeMap) {
var query var query
, valueQuery = 'INSERT INTO <%= tableName %> (<%= attributes %>)' , valueQuery = 'INSERT INTO <%= tableName %> (<%= attributes %>)'
...@@ -366,7 +365,7 @@ module.exports = { ...@@ -366,7 +365,7 @@ module.exports = {
&& (valueHash[key] === 'DEFAULT' && (valueHash[key] === 'DEFAULT'
|| valueHash[key] === null)){ || valueHash[key] === null)){
delete valueHash[key]; delete valueHash[key];
}else if(valueHash[key] }else if(valueHash[key]
&& modelAttributeMap[key].autoIncrement){ && modelAttributeMap[key].autoIncrement){
insertKey = true; insertKey = true;
} }
...@@ -386,6 +385,7 @@ module.exports = { ...@@ -386,6 +385,7 @@ module.exports = {
} }
return Utils._.template(query)(replacements); return Utils._.template(query)(replacements);
}, },
updateSql: function(tableName, valueHash, where, options, attributes){ updateSql: function(tableName, valueHash, where, options, attributes){
options = options || {}; options = options || {};
...@@ -411,6 +411,7 @@ module.exports = { ...@@ -411,6 +411,7 @@ module.exports = {
return Utils._.template(query)(replacements); return Utils._.template(query)(replacements);
}, },
incrementSql: function(tableName, attrValueHash, options) { incrementSql: function(tableName, attrValueHash, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull); attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull);
...@@ -424,7 +425,7 @@ module.exports = { ...@@ -424,7 +425,7 @@ module.exports = {
for (key in attrValueHash) { for (key in attrValueHash) {
value = attrValueHash[key]; value = attrValueHash[key];
values.push(quoteIdentifier(key) + '=' + quoteIdentifier(key) + ' + ' + escape(value)); values.push(quoteIdentifier(key) + '=' + quoteIdentifier(key) + ' + ' + escape(value));
selFields.push('INSERTED.' + quoteIdentifier(key)); selFields.push('INSERTED.' + quoteIdentifier(key));
} }
...@@ -442,6 +443,7 @@ module.exports = { ...@@ -442,6 +443,7 @@ module.exports = {
return Utils._.template(query)(replacements); return Utils._.template(query)(replacements);
}, },
deleteSql: function(tableName) { deleteSql: function(tableName) {
var query = "DELETE FROM <%= table %>"; var query = "DELETE FROM <%= table %>";
var replacements = { var replacements = {
...@@ -459,10 +461,12 @@ module.exports = { ...@@ -459,10 +461,12 @@ module.exports = {
}) })
}); });
return 'ADD ' + attribute; return 'ADD ' + attribute;
}, },
alterColumnSql: function(){ alterColumnSql: function(){
return 'ALTER COLUMN'; return 'ALTER COLUMN';
}, },
renameColumnSql: function(tableName, attrBefore, newColumnName){ renameColumnSql: function(tableName, attrBefore, newColumnName){
var query = 'EXEC SP_RENAME \'<%= tableName %>.<%= before %>\', \'<%= after %>\';'; var query = 'EXEC SP_RENAME \'<%= tableName %>.<%= before %>\', \'<%= after %>\';';
var attrString = []; var attrString = [];
...@@ -497,6 +501,7 @@ module.exports = { ...@@ -497,6 +501,7 @@ module.exports = {
options: (options || {}).database ? ' FROM \'' + options.database + '\'' : '' options: (options || {}).database ? ' FROM \'' + options.database + '\'' : ''
}); });
}, },
addIndexSql: function(tableName, attributes, options, rawTablename){ addIndexSql: function(tableName, attributes, options, rawTablename){
if (!options.name) { if (!options.name) {
// Mostly for cases where addIndex is called directly by the user without an options object (for example in migrations) // Mostly for cases where addIndex is called directly by the user without an options object (for example in migrations)
...@@ -521,6 +526,7 @@ module.exports = { ...@@ -521,6 +526,7 @@ module.exports = {
'(' + attrStr.join(', ') + ')' '(' + attrStr.join(', ') + ')'
]).join(' '); ]).join(' ');
}, },
removeIndexSql: function(tableName, indexNameOrAttributes){ removeIndexSql: function(tableName, indexNameOrAttributes){
var sql = 'DROP INDEX <%= indexName %> ON <%= tableName %>' var sql = 'DROP INDEX <%= indexName %> ON <%= tableName %>'
, indexName = indexNameOrAttributes; , indexName = indexNameOrAttributes;
...@@ -600,8 +606,6 @@ module.exports = { ...@@ -600,8 +606,6 @@ module.exports = {
template.push(attributeMap.unique); template.push(attributeMap.unique);
} }
if (attribute.references) { if (attribute.references) {
template.push(attributeMap.references); template.push(attributeMap.references);
template.push(this.quoteTable(attribute.references)); template.push(this.quoteTable(attribute.references));
...@@ -656,12 +660,14 @@ module.exports = { ...@@ -656,12 +660,14 @@ module.exports = {
"AND C.TABLE_NAME = ", wrapSingleQuote(tableName) "AND C.TABLE_NAME = ", wrapSingleQuote(tableName)
].join(" "); ].join(" ");
}, },
dropSql: function(val){ dropSql: function(val){
return [ return [
'DROP', 'DROP',
quoteIdentifier(val) quoteIdentifier(val)
].join(' '); ].join(' ');
}, },
alterAttributesSql: function(attributes){ alterAttributesSql: function(attributes){
var attrString = []; var attrString = [];
for (var attrName in attributes) { for (var attrName in attributes) {
...@@ -674,9 +680,11 @@ module.exports = { ...@@ -674,9 +680,11 @@ module.exports = {
} }
return attrString.join(', '); return attrString.join(', ');
}, },
getTopClause: function(limit){ getTopClause: function(limit){
return "TOP(" + limit + ")"; return "TOP(" + limit + ")";
}, },
getCountClause: function(alias, columnName){ getCountClause: function(alias, columnName){
return [ return [
"SELECT COUNT(", "SELECT COUNT(",
...@@ -684,6 +692,7 @@ module.exports = { ...@@ -684,6 +692,7 @@ module.exports = {
") AS", quoteIdentifier(alias) ") AS", quoteIdentifier(alias)
].join(' '); ].join(' ');
}, },
getSelectorClause: function(model, options){ getSelectorClause: function(model, options){
var query = ['SELECT']; var query = ['SELECT'];
//we have joins //we have joins
...@@ -705,6 +714,7 @@ module.exports = { ...@@ -705,6 +714,7 @@ module.exports = {
} }
return query.join(' '); return query.join(' ');
}, },
getFromClause: function(tableName, asValue){ getFromClause: function(tableName, asValue){
var query = ["FROM", var query = ["FROM",
quoteIdentifier(tableName)]; quoteIdentifier(tableName)];
...@@ -714,6 +724,7 @@ module.exports = { ...@@ -714,6 +724,7 @@ module.exports = {
} }
return query.join(' '); return query.join(' ');
}, },
getJoinClause: function(model, include){ getJoinClause: function(model, include){
var query = []; var query = [];
var primaryKey = quoteIdentifier(model.primaryKeyAttribute); var primaryKey = quoteIdentifier(model.primaryKeyAttribute);
...@@ -774,6 +785,7 @@ module.exports = { ...@@ -774,6 +785,7 @@ module.exports = {
} }
return query.join(' '); return query.join(' ');
}, },
formatWhereCondition: function(where, tableName){ formatWhereCondition: function(where, tableName){
var query = []; var query = [];
for(var key in where){ for(var key in where){
...@@ -799,22 +811,10 @@ module.exports = { ...@@ -799,22 +811,10 @@ module.exports = {
} }
return query.join(' '); return query.join(' ');
}, },
getWhereClause: function(where, tableName){ getWhereClause: function(where, tableName){
return [ return [
'WHERE', 'WHERE',
this.formatWhereCondition(where, tableName) this.formatWhereCondition(where, tableName)
].join(' '); ].join(' ');
}}; }};
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!