Skip to content

Commit

Permalink
Add first steps for createTableQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
sdepold authored and Matt Broadstone committed Dec 9, 2014
1 parent 438cea4 commit 8f4f2f1
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/dialects/mssql/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ module.exports = (function() {
var QueryGenerator = {
dialect: 'mssql',

quoteIdentifier: function(identifier, force) {
if (identifier === '*') return identifier;
return Utils.addTicks(identifier, '"');
},

showTablesQuery: function () {
return 'SELECT * FROM sys.Tables';
},
Expand All @@ -18,6 +23,39 @@ module.exports = (function() {
return Utils._.template(query)({
tableName: tableName
});
},

createTableQuery: function(tableName, attributes, options) {
var query = "IF OBJECT_ID('<%= unquotedTable %>', N'U') IS NULL CREATE TABLE <%= table %> (<%= attributes%>)"
, attrStr = []
, self = this
, primaryKeys = Utils._.keys(Utils._.pick(attributes, function(dataType){
return dataType.indexOf('PRIMARY KEY') >= 0;
}));

for (var attr in attributes) {
if (attributes.hasOwnProperty(attr)) {
var dataType = attributes[attr];
if (primaryKeys.length > 1){
dataType = dataType.replace(/ PRIMARY KEY/, '');
}
attrStr.push(self.quote(attr) + " " + dataType);
}
}

if (primaryKeys.length > 1) {
attrStr.push('PRIMARY KEY(' + primaryKeys.map(function(column){
return self.quote(column);
}).join(', ') + ')');
}

var values = {
unquotedTable: tableName,
table: self.quote(tableName),
attributes: attrStr.join(", ")
};

return Utils._.template(query)(values).trim() + ";";
}
};

Expand Down

0 comments on commit 8f4f2f1

Please sign in to comment.