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

Commit 64fb3354 by Mick Hansen

better schema quoting support

1 parent 9003aff1
...@@ -44,6 +44,7 @@ module.exports = (function() { ...@@ -44,6 +44,7 @@ module.exports = (function() {
var daos = {} var daos = {}
, sorter = new Toposort() , sorter = new Toposort()
, sorted , sorted
, dep
options = _.defaults(options || {}, { options = _.defaults(options || {}, {
reverse: true reverse: true
...@@ -57,7 +58,9 @@ module.exports = (function() { ...@@ -57,7 +58,9 @@ module.exports = (function() {
for (var attrName in dao.rawAttributes) { for (var attrName in dao.rawAttributes) {
if (dao.rawAttributes.hasOwnProperty(attrName)) { if (dao.rawAttributes.hasOwnProperty(attrName)) {
if (dao.rawAttributes[attrName].references) { if (dao.rawAttributes[attrName].references) {
deps.push(dao.rawAttributes[attrName].references) dep = dao.rawAttributes[attrName].references
dep = dep.tableName || dep
deps.push(dep)
} }
} }
} }
...@@ -65,6 +68,7 @@ module.exports = (function() { ...@@ -65,6 +68,7 @@ module.exports = (function() {
deps = deps.filter(function (dep) { deps = deps.filter(function (dep) {
return dao.tableName !== dep return dao.tableName !== dep
}) })
sorter.add(dao.tableName, deps) sorter.add(dao.tableName, deps)
}) })
......
...@@ -6,7 +6,8 @@ AbstractDialect.prototype.supports = { ...@@ -6,7 +6,8 @@ AbstractDialect.prototype.supports = {
'RETURNING': false, 'RETURNING': false,
'DEFAULT': true, 'DEFAULT': true,
'DEFAULT VALUES': false, 'DEFAULT VALUES': false,
'VALUES ()': false 'VALUES ()': false,
schemas: false
} }
module.exports = AbstractDialect module.exports = AbstractDialect
\ No newline at end of file
var Utils = require("../../utils") var Utils = require("../../utils")
, SqlString = require("../../sql-string") , SqlString = require("../../sql-string")
, daoFactory = require("../../dao-factory") , daoFactory = require("../../dao-factory")
, _ = require('lodash')
module.exports = (function() { module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
addSchema: function(opts) { addSchema: function(param) {
throwMethodUndefined('addSchema') var schema = (param.options && param.options.schema ? param.options.schema : undefined)
, schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
return {
tableName: param.tableName || param,
schema: schema,
delimiter: schemaDelimiter || '.'
}
}, },
/* /*
...@@ -27,7 +35,13 @@ module.exports = (function() { ...@@ -27,7 +35,13 @@ module.exports = (function() {
Returns a query for dropping a table. Returns a query for dropping a table.
*/ */
dropTableQuery: function(tableName, options) { dropTableQuery: function(tableName, options) {
throwMethodUndefined('dropTableQuery') options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %>;"
return Utils._.template(query)({
table: this.quoteTable(tableName)
})
}, },
/* /*
...@@ -329,6 +343,31 @@ module.exports = (function() { ...@@ -329,6 +343,31 @@ module.exports = (function() {
throwMethodUndefined('findAutoIncrementField') throwMethodUndefined('findAutoIncrementField')
}, },
quoteTable: function(param) {
if (_.isObject(param)) {
var table = '';
if (this._dialect.supports.schemas) {
if (param.schema) {
table += this.quoteIdentifier(param.schema) + param.delimiter
}
table += this.quoteIdentifier(param.tableName)
} else {
if (param.schema) {
table += param.schema + param.delimiter
}
table += param.tableName
table = this.quoteIdentifier(table)
}
return table
}
return this.quoteIdentifier(param)
},
/* /*
Quote an object based on its type. This is a more general version of quoteIdentifiers Quote an object based on its type. This is a more general version of quoteIdentifiers
Strings: should proxy to quoteIdentifiers Strings: should proxy to quoteIdentifiers
...@@ -531,8 +570,8 @@ module.exports = (function() { ...@@ -531,8 +570,8 @@ module.exports = (function() {
, subJoinQueries = [] , subJoinQueries = []
// Escape table // Escape table
options.table = table = !Array.isArray(tableName) ? this.quoteIdentifiers(tableName) : tableName.map(function(t) { options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) {
return this.quoteIdentifiers(t) return this.quoteTable(t)
}.bind(this)).join(", ") }.bind(this)).join(", ")
if (subQuery && mainAttributes) { if (subQuery && mainAttributes) {
...@@ -570,7 +609,7 @@ module.exports = (function() { ...@@ -570,7 +609,7 @@ module.exports = (function() {
} }
if (options.include && attr.indexOf('.') === -1 && addTable) { if (options.include && attr.indexOf('.') === -1 && addTable) {
attr = this.quoteIdentifier(options.table) + '.' + attr attr = this.quoteTable(options.table) + '.' + attr
} }
return attr return attr
......
...@@ -5,24 +5,6 @@ var Utils = require("../../utils") ...@@ -5,24 +5,6 @@ var Utils = require("../../utils")
module.exports = (function() { module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
dialect: 'mysql', dialect: 'mysql',
addSchema: function(opts) {
var tableName
var schema = (!!opts && !!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaDelimiter = (!!opts && !!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
if (!!opts && !!opts.tableName) {
tableName = opts.tableName
}
else if (typeof opts === "string") {
tableName = opts
}
if (!schema || schema.toString().trim() === "") {
return tableName
}
return this.quoteIdentifier(schema + (!schemaDelimiter ? '.' : schemaDelimiter) + tableName, false)
},
createSchema: function() { createSchema: function() {
var query = "SHOW TABLES" var query = "SHOW TABLES"
...@@ -103,16 +85,6 @@ module.exports = (function() { ...@@ -103,16 +85,6 @@ module.exports = (function() {
return Utils._.template(query)(values).trim() + ";" return Utils._.template(query)(values).trim() + ";"
}, },
dropTableQuery: function(tableName, options) {
options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %>;"
return Utils._.template(query)({
table: this.quoteIdentifier(tableName)
})
},
showTablesQuery: function() { showTablesQuery: function() {
return 'SHOW TABLES;' return 'SHOW TABLES;'
}, },
...@@ -424,10 +396,6 @@ module.exports = (function() { ...@@ -424,10 +396,6 @@ module.exports = (function() {
return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.') return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.')
}, },
quoteTable: function(table) {
return this.quoteIdentifier(table)
},
/** /**
* Generates an SQL query that returns all foreign keys of a table. * Generates an SQL query that returns all foreign keys of a table.
* *
......
...@@ -7,7 +7,8 @@ var PostgresDialect = function(sequelize) { ...@@ -7,7 +7,8 @@ var PostgresDialect = function(sequelize) {
PostgresDialect.prototype.supports = _.defaults({ PostgresDialect.prototype.supports = _.defaults({
'RETURNING': true, 'RETURNING': true,
'DEFAULT VALUES': true 'DEFAULT VALUES': true,
schemas: true
}, Abstract.prototype.supports) }, Abstract.prototype.supports)
module.exports = PostgresDialect module.exports = PostgresDialect
...@@ -10,23 +10,15 @@ module.exports = (function() { ...@@ -10,23 +10,15 @@ module.exports = (function() {
options: {}, options: {},
dialect: 'postgres', dialect: 'postgres',
addSchema: function(opts) { addSchema: function(param) {
var tableName = undefined var schema = (param.options && param.options.schema ? param.options.schema : undefined)
var schema = (!!opts.options && !!opts.options.schema ? opts.options.schema : undefined) , schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
var schemaDelimiter = (!!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
if (!!opts && !!opts.tableName) { return {
tableName = opts.tableName tableName: param.tableName || param,
schema: schema,
delimiter: schemaDelimiter || '.'
} }
else if (typeof opts === "string") {
tableName = opts
}
if (!schema || schema.toString().trim() === "") {
return tableName
}
return this.quoteIdentifiers((!!schema ? (schema + '.' + tableName) : tableName));
}, },
createSchema: function(schema) { createSchema: function(schema) {
...@@ -97,10 +89,9 @@ module.exports = (function() { ...@@ -97,10 +89,9 @@ module.exports = (function() {
dropTableQuery: function(tableName, options) { dropTableQuery: function(tableName, options) {
options = options || {} options = options || {}
var query = "DROP TABLE IF EXISTS <%= schema %><%= table %><%= cascade %>;" var query = "DROP TABLE IF EXISTS <%= table %><%= cascade %>;"
return Utils._.template(query)({ return Utils._.template(query)({
schema: options.schema ? this.quoteIdentifiers(options.schema) + '.' : '', table: this.quoteTable(tableName),
table: this.quoteIdentifiers(tableName),
cascade: options.cascade? " CASCADE" : "" cascade: options.cascade? " CASCADE" : ""
}) })
}, },
...@@ -818,10 +809,6 @@ module.exports = (function() { ...@@ -818,10 +809,6 @@ module.exports = (function() {
return identifiers.split('.').map(function(t) { return this.quoteIdentifier(t, force) }.bind(this)).join('.') return identifiers.split('.').map(function(t) { return this.quoteIdentifier(t, force) }.bind(this)).join('.')
}, },
quoteTable: function(table) {
return this.quoteIdentifiers(table)
},
/** /**
* Generates an SQL query that returns all foreign keys of a table. * Generates an SQL query that returns all foreign keys of a table.
* *
......
...@@ -15,23 +15,15 @@ module.exports = (function() { ...@@ -15,23 +15,15 @@ module.exports = (function() {
options: {}, options: {},
dialect: 'sqlite', dialect: 'sqlite',
addSchema: function(opts) { addSchema: function(param) {
var tableName = undefined var schema = (param.options && param.options.schema ? param.options.schema : undefined)
var schema = (!!opts && !!opts.options && !!opts.options.schema ? opts.options.schema : undefined) , schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
var schemaDelimiter = (!!opts && !!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
return {
if (!!opts && !!opts.tableName) { tableName: param.tableName || param,
tableName = opts.tableName schema: schema,
} delimiter: schemaDelimiter || '.'
else if (typeof opts === "string") {
tableName = opts
}
if (!schema || schema.toString().trim() === "") {
return tableName
} }
return this.quoteIdentifier(schema) + (!schemaDelimiter ? '.' : schemaDelimiter) + this.quoteIdentifier(tableName)
}, },
createSchema: function() { createSchema: function() {
...@@ -109,7 +101,7 @@ module.exports = (function() { ...@@ -109,7 +101,7 @@ module.exports = (function() {
} }
var values = { var values = {
table: this.quoteIdentifier(tableName), table: this.quoteTable(tableName),
attributes: attrStr.join(", "), attributes: attrStr.join(", "),
charset: (options.charset ? "DEFAULT CHARSET=" + options.charset : "") charset: (options.charset ? "DEFAULT CHARSET=" + options.charset : "")
} }
...@@ -133,16 +125,6 @@ module.exports = (function() { ...@@ -133,16 +125,6 @@ module.exports = (function() {
return !!value ? 1 : 0; return !!value ? 1 : 0;
}, },
dropTableQuery: function(tableName, options) {
options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %>;"
return Utils._.template(query)({
table: this.quoteIdentifier(tableName)
})
},
uniqueConstraintMapping: { uniqueConstraintMapping: {
code: 'SQLITE_CONSTRAINT', code: 'SQLITE_CONSTRAINT',
map: function(str) { map: function(str) {
...@@ -199,7 +181,7 @@ module.exports = (function() { ...@@ -199,7 +181,7 @@ module.exports = (function() {
var replacements = { var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' OR IGNORE' : '', ignoreDuplicates: options && options.ignoreDuplicates ? ' OR IGNORE' : '',
table: this.quoteIdentifier(tableName), table: this.quoteTable(tableName),
attributes: allAttributes.map(function(attr){ attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr) return this.quoteIdentifier(attr)
}.bind(this)).join(","), }.bind(this)).join(","),
...@@ -221,7 +203,7 @@ module.exports = (function() { ...@@ -221,7 +203,7 @@ module.exports = (function() {
} }
var replacements = { var replacements = {
table: this.quoteIdentifier(tableName), table: this.quoteTable(tableName),
values: values.join(","), values: values.join(","),
where: this.getWhereConditions(where) where: this.getWhereConditions(where)
} }
...@@ -234,7 +216,7 @@ module.exports = (function() { ...@@ -234,7 +216,7 @@ module.exports = (function() {
var query = "DELETE FROM <%= table %> WHERE <%= where %>" var query = "DELETE FROM <%= table %> WHERE <%= where %>"
var replacements = { var replacements = {
table: this.quoteIdentifier(tableName), table: this.quoteTable(tableName),
where: this.getWhereConditions(where) where: this.getWhereConditions(where)
} }
...@@ -285,7 +267,7 @@ module.exports = (function() { ...@@ -285,7 +267,7 @@ module.exports = (function() {
if(dataType.references) { if(dataType.references) {
template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)" template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements.referencesTable = this.quoteIdentifier(dataType.references) replacements.referencesTable = this.quoteTable(dataType.references)
if(dataType.referencesKey) { if(dataType.referencesKey) {
replacements.referencesKey = this.quoteIdentifier(dataType.referencesKey) replacements.referencesKey = this.quoteIdentifier(dataType.referencesKey)
...@@ -434,10 +416,6 @@ module.exports = (function() { ...@@ -434,10 +416,6 @@ module.exports = (function() {
return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.') return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.')
}, },
quoteTable: function(table) {
return this.quoteIdentifier(table)
},
/** /**
* Generates an SQL query that returns all foreign keys of a table. * Generates an SQL query that returns all foreign keys of a table.
* *
......
...@@ -76,7 +76,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -76,7 +76,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
it('supports schemas', function (done) { it.only('supports schemas', function (done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING }).schema('archive') var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING }).schema('archive')
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive') , Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive')
, self = this , self = this
...@@ -92,6 +92,8 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -92,6 +92,8 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
task.getUserXYZ().success(function(user) { task.getUserXYZ().success(function(user) {
expect(user).to.be.ok expect(user).to.be.ok
done() done()
}).on('sql', function (sql) {
console.log(sql)
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!