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

Commit 64fb3354 by Mick Hansen

better schema quoting support

1 parent 9003aff1
......@@ -44,6 +44,7 @@ module.exports = (function() {
var daos = {}
, sorter = new Toposort()
, sorted
, dep
options = _.defaults(options || {}, {
reverse: true
......@@ -57,7 +58,9 @@ module.exports = (function() {
for (var attrName in dao.rawAttributes) {
if (dao.rawAttributes.hasOwnProperty(attrName)) {
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() {
deps = deps.filter(function (dep) {
return dao.tableName !== dep
})
sorter.add(dao.tableName, deps)
})
......
......@@ -6,7 +6,8 @@ AbstractDialect.prototype.supports = {
'RETURNING': false,
'DEFAULT': true,
'DEFAULT VALUES': false,
'VALUES ()': false
'VALUES ()': false,
schemas: false
}
module.exports = AbstractDialect
\ No newline at end of file
var Utils = require("../../utils")
, SqlString = require("../../sql-string")
, daoFactory = require("../../dao-factory")
, _ = require('lodash')
module.exports = (function() {
var QueryGenerator = {
addSchema: function(opts) {
throwMethodUndefined('addSchema')
addSchema: function(param) {
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() {
Returns a query for dropping a table.
*/
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() {
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
Strings: should proxy to quoteIdentifiers
......@@ -531,8 +570,8 @@ module.exports = (function() {
, subJoinQueries = []
// Escape table
options.table = table = !Array.isArray(tableName) ? this.quoteIdentifiers(tableName) : tableName.map(function(t) {
return this.quoteIdentifiers(t)
options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) {
return this.quoteTable(t)
}.bind(this)).join(", ")
if (subQuery && mainAttributes) {
......@@ -570,7 +609,7 @@ module.exports = (function() {
}
if (options.include && attr.indexOf('.') === -1 && addTable) {
attr = this.quoteIdentifier(options.table) + '.' + attr
attr = this.quoteTable(options.table) + '.' + attr
}
return attr
......
......@@ -5,24 +5,6 @@ var Utils = require("../../utils")
module.exports = (function() {
var QueryGenerator = {
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() {
var query = "SHOW TABLES"
......@@ -103,16 +85,6 @@ module.exports = (function() {
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() {
return 'SHOW TABLES;'
},
......@@ -424,10 +396,6 @@ module.exports = (function() {
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.
*
......
......@@ -7,7 +7,8 @@ var PostgresDialect = function(sequelize) {
PostgresDialect.prototype.supports = _.defaults({
'RETURNING': true,
'DEFAULT VALUES': true
'DEFAULT VALUES': true,
schemas: true
}, Abstract.prototype.supports)
module.exports = PostgresDialect
......@@ -10,23 +10,15 @@ module.exports = (function() {
options: {},
dialect: 'postgres',
addSchema: function(opts) {
var tableName = undefined
var schema = (!!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaDelimiter = (!!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
addSchema: function(param) {
var schema = (param.options && param.options.schema ? param.options.schema : undefined)
, schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
if (!!opts && !!opts.tableName) {
tableName = opts.tableName
return {
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) {
......@@ -97,10 +89,9 @@ module.exports = (function() {
dropTableQuery: function(tableName, options) {
options = options || {}
var query = "DROP TABLE IF EXISTS <%= schema %><%= table %><%= cascade %>;"
var query = "DROP TABLE IF EXISTS <%= table %><%= cascade %>;"
return Utils._.template(query)({
schema: options.schema ? this.quoteIdentifiers(options.schema) + '.' : '',
table: this.quoteIdentifiers(tableName),
table: this.quoteTable(tableName),
cascade: options.cascade? " CASCADE" : ""
})
},
......@@ -818,10 +809,6 @@ module.exports = (function() {
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.
*
......
......@@ -15,23 +15,15 @@ module.exports = (function() {
options: {},
dialect: 'sqlite',
addSchema: function(opts) {
var tableName = undefined
var schema = (!!opts && !!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaDelimiter = (!!opts && !!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
addSchema: function(param) {
var schema = (param.options && param.options.schema ? param.options.schema : undefined)
, schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
if (!!opts && !!opts.tableName) {
tableName = opts.tableName
return {
tableName: param.tableName || param,
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() {
......@@ -109,7 +101,7 @@ module.exports = (function() {
}
var values = {
table: this.quoteIdentifier(tableName),
table: this.quoteTable(tableName),
attributes: attrStr.join(", "),
charset: (options.charset ? "DEFAULT CHARSET=" + options.charset : "")
}
......@@ -133,16 +125,6 @@ module.exports = (function() {
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: {
code: 'SQLITE_CONSTRAINT',
map: function(str) {
......@@ -199,7 +181,7 @@ module.exports = (function() {
var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' OR IGNORE' : '',
table: this.quoteIdentifier(tableName),
table: this.quoteTable(tableName),
attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
}.bind(this)).join(","),
......@@ -221,7 +203,7 @@ module.exports = (function() {
}
var replacements = {
table: this.quoteIdentifier(tableName),
table: this.quoteTable(tableName),
values: values.join(","),
where: this.getWhereConditions(where)
}
......@@ -234,7 +216,7 @@ module.exports = (function() {
var query = "DELETE FROM <%= table %> WHERE <%= where %>"
var replacements = {
table: this.quoteIdentifier(tableName),
table: this.quoteTable(tableName),
where: this.getWhereConditions(where)
}
......@@ -285,7 +267,7 @@ module.exports = (function() {
if(dataType.references) {
template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements.referencesTable = this.quoteIdentifier(dataType.references)
replacements.referencesTable = this.quoteTable(dataType.references)
if(dataType.referencesKey) {
replacements.referencesKey = this.quoteIdentifier(dataType.referencesKey)
......@@ -434,10 +416,6 @@ module.exports = (function() {
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.
*
......
......@@ -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')
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive')
, self = this
......@@ -92,6 +92,8 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
task.getUserXYZ().success(function(user) {
expect(user).to.be.ok
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!