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

Commit 8f4f2f12 by Sascha Depold Committed by Matt Broadstone

Add first steps for createTableQuery

1 parent 438cea4e
Showing with 38 additions and 0 deletions
......@@ -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';
},
......@@ -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() + ";";
}
};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!