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

Commit da12e432 by Matt Broadstone

refactor MssqlDialect to inherit AbstractDialect

  - major deletion of code duplication
  - cleaned up code (removed duplicate documentation)
  - moved a lot of SqlGenerator sql back into QueryGenerator
1 parent 7b0b7ec4
......@@ -4,9 +4,9 @@ var Utils = require('../../utils')
, SqlString = require('../../sql-string')
, DataTypes = require('./data-types')
, _ = require('lodash')
, _options
, _dialect = 'mssql'
, _sequelize;
, _sequelize
, _options;
/*
Escape a value (e.g. a string, number or date)
......@@ -34,6 +34,7 @@ function escape(value, field) {
}
}
function quoteIdentifier(identifier, force) {
if (identifier === '*') return identifier;
return Utils.addTicks(identifier, '"');
......@@ -154,17 +155,6 @@ function loadColumn(attributes){
return attrStr;
}
function addTableExistsWrapper(query, exists){
return [
'IF (',
(exists ? '' : 'NOT '), 'EXISTS (',
'SELECT * FROM INFORMATION_SCHEMA.TABLES',
" WHERE TABLE_NAME='<%= unquotedTable %>')",
')',
' BEGIN ', query, ' END'
].join('');
}
//select stuff
function loadFields(attributes){
var attrStr = [];
......@@ -288,47 +278,6 @@ module.exports = {
].join(' ');
},
getCreateSchemaSql: function(schema){
return [
'IF NOT EXISTS (SELECT schema_name',
'FROM information_schema.schemata',
'WHERE schema_name = ', wrapSingleQuote(schema), ')',
'BEGIN',
'EXEC sp_executesql N\'CREATE SCHEMA', quoteIdentifier(schema),';\'',
"END;"].join(' ');
},
dataTypeMapping: function(tableName, attr, dataType) {
if (Utils._.includes(dataType, 'DATETIME')) {
dataType = dataType.replace(/DATETIME/, 'DATETIME2');
}
return dataType;
},
getCreateTableSql: function(tableName, attributes, options) {
var query = "CREATE TABLE <%= tableName %> (<%= attributes%>)";
var attrStr = []
, self = this
, primaryKeys = Utils._.keys(Utils._.pick(attributes, function(dataType){
return dataType.indexOf('PRIMARY KEY') >= 0;
}));
for (var attr in attributes) {
var dataType = this.dataTypeMapping(tableName, attr, attributes[attr]);
attrStr.push(quoteIdentifier(attr) + " " + dataType);
}
var values = {
unquotedTable: tableName,
tableName: quoteIdentifier(tableName.toString()),
attributes: attrStr.join(", ")
};
query = addTableExistsWrapper(query);
return Utils._.template(query)(values).trim() + ";";
},
alterTableSql: function(tableName){
var query = 'ALTER TABLE <%= tableName %>';
var value = {
......@@ -337,19 +286,7 @@ module.exports = {
return Utils._.template(query)(value);
},
dropTableSql: function(tableName, options){
var query = "DROP TABLE <%= tableName %>";
var values ={};
values = {
unquotedTable: tableName,
tableName: quoteIdentifier(tableName.toString())
};
query = addTableExistsWrapper(query, true);
return Utils._.template(query)(values).trim() + ";";
},
insertSql: function(tableName, valueHash, modelAttributeMap) {
insertSql: function(tableName, valueHash, modelAttributeMap, options) {
var query
, valueQuery = 'INSERT INTO <%= tableName %> (<%= attributes %>)'
, emptyQuery = 'INSERT INTO <%= tableName %>';
......@@ -360,7 +297,7 @@ module.exports = {
}
emptyQuery += ' DEFAULT VALUES';
valueHash = Utils.removeNullValuesFromHash(valueHash, _options.omitNull);
valueHash = Utils.removeNullValuesFromHash(valueHash, this.options.omitNull);
var selFields = [];
var insertKey = false;
......@@ -486,20 +423,6 @@ module.exports = {
return Utils._.template(query)(values);
},
removeIndexSql: function(tableName, indexNameOrAttributes){
var sql = 'DROP INDEX <%= indexName %> ON <%= tableName %>'
, indexName = indexNameOrAttributes;
if (typeof indexName !== 'string') {
indexName = Utils.inflection.underscore(tableName + '_' + indexNameOrAttributes.join('_'));
}
var values = {
tableName: quoteIdentifiers(tableName),
indexName: indexName
};
return Utils._.template(sql)(values);
},
attributeToSQL: function(attribute, options) {
if (!Utils._.isPlainObject(attribute)) {
attribute = {
......@@ -591,35 +514,6 @@ module.exports = {
return template.join(' ');
},
describeTableSql: function(tableName, schema, schemaDelimiter){
var table = tableName;
if(schema){
table = schema + '.' + tableName;
}
var qry = [
"SELECT c.COLUMN_NAME AS 'Name', c.DATA_TYPE AS 'Type',",
"c.IS_NULLABLE as 'IsNull' , COLUMN_DEFAULT AS 'Default'",
"FROM INFORMATION_SCHEMA.TABLES t ",
"INNER JOIN INFORMATION_SCHEMA.COLUMNS c ON t.TABLE_NAME = c.TABLE_NAME",
"where t.TABLE_NAME =",
wrapSingleQuote(table),
";"
].join(" ");
return qry;
},
getForeignKeysSql: function(tableName){
return [
"SELECT",
"constraint_name = C.CONSTRAINT_NAME",
"FROM",
"INFORMATION_SCHEMA.TABLE_CONSTRAINTS C",
"WHERE C.CONSTRAINT_TYPE != 'PRIMARY KEY'",
"AND C.TABLE_NAME = ", wrapSingleQuote(tableName)
].join(" ");
},
dropSql: function(val){
return [
'DROP',
......
......@@ -1882,9 +1882,14 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it("should be able to list schemas", function(done){
this.sequelize.showAllSchemas().then(function(schemas) {
expect(schemas).to.be.instanceof(Array)
// FIXME: reenable when schema support is properly added
if (dialect !== 'mssql') {
// sqlite & MySQL doesn't actually create schemas unless Model.sync() is called
// Postgres supports schemas natively
expect(schemas).to.have.length((dialect === "postgres" ? 2 : 1))
}
done()
})
})
......@@ -2011,7 +2016,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
expect(UserSpecial.indexOf('INSERT INTO `special.UserSpecials`')).to.be.above(-1)
expect(UserPublic.indexOf('INSERT INTO `UserPublics`')).to.be.above(-1)
} else if (dialect === 'mssql'){
expect(self.UserSpecialSync.getTableName().toString()).to.equal('special.UserSpecials');
expect(self.UserSpecialSync.getTableName().toString()).to.equal('"special.UserSpecials"');
expect(UserSpecial.indexOf('INSERT INTO "special.UserSpecials"')).to.be.above(-1)
expect(UserPublic.indexOf('INSERT INTO "UserPublics"')).to.be.above(-1)
} else {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!