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

Commit 9efcca74 by Matt Broadstone

clean up table create/drop queries

This is more of a stylistic change, as no functional behavior differs,
however it makes diagnostics much easier and is theoretically faster
than the previous select code.
1 parent 30933c9e
Showing with 6 additions and 19 deletions
...@@ -31,7 +31,7 @@ module.exports = (function() { ...@@ -31,7 +31,7 @@ module.exports = (function() {
}, },
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
var query = "CREATE TABLE <%= tableName %> (<%= attributes%>)"; var query = "IF OBJECT_ID('[<%= escapedTable %>]', 'U') IS NULL CREATE TABLE <%= table %> (<%= attributes%>)";
var attrStr = [] var attrStr = []
, self = this , self = this
, primaryKeys = Utils._.keys(Utils._.pick(attributes, function(dataType){ , primaryKeys = Utils._.keys(Utils._.pick(attributes, function(dataType){
...@@ -44,12 +44,11 @@ module.exports = (function() { ...@@ -44,12 +44,11 @@ module.exports = (function() {
} }
var values = { var values = {
unquotedTable: tableName, escapedTable: this.quoteTable(tableName).replace(/"/g, ''),
tableName: this.quoteIdentifier(tableName.toString()), table: this.quoteTable(tableName),
attributes: attrStr.join(", ") attributes: attrStr.join(", ")
}; };
query = addTableExistsWrapper(query);
return Utils._.template(query)(values).trim() + ";"; return Utils._.template(query)(values).trim() + ";";
}, },
...@@ -83,13 +82,12 @@ module.exports = (function() { ...@@ -83,13 +82,12 @@ module.exports = (function() {
}, },
dropTableQuery: function(tableName, options) { dropTableQuery: function(tableName, options) {
var query = "DROP TABLE <%= tableName %>"; var query = "IF OBJECT_ID('[<%= escapedTable %>]', 'U') IS NOT NULL DROP TABLE <%= table %>";
var values = { var values = {
unquotedTable: tableName, escapedTable: this.quoteTable(tableName).replace(/"/g, ''),
tableName: this.quoteIdentifier(tableName.toString()) table: this.quoteTable(tableName)
}; };
query = addTableExistsWrapper(query, true);
return Utils._.template(query)(values).trim() + ";"; return Utils._.template(query)(values).trim() + ";";
}, },
...@@ -539,17 +537,6 @@ module.exports = (function() { ...@@ -539,17 +537,6 @@ module.exports = (function() {
return Utils.addTicks(identifier, "'"); return Utils.addTicks(identifier, "'");
} }
function addTableExistsWrapper(query, exists){
return [
'IF (',
(exists ? '' : 'NOT '), 'EXISTS (',
'SELECT * FROM INFORMATION_SCHEMA.TABLES',
" WHERE TABLE_NAME='<%= unquotedTable %>')",
')',
' BEGIN ', query, ' END'
].join('');
}
function mssqlDataTypeMapping(tableName, attr, dataType) { function mssqlDataTypeMapping(tableName, attr, dataType) {
if (Utils._.includes(dataType, 'DATETIME')) { if (Utils._.includes(dataType, 'DATETIME')) {
dataType = dataType.replace(/DATETIME/, 'DATETIME2'); dataType = dataType.replace(/DATETIME/, 'DATETIME2');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!