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

Commit c12a8ea5 by Jan Aagaard Meier

Merge pull request #647 from durango/pgsqldes

Added special field for describing tables with Postgres, this will give us ...
2 parents a9bb613a 2f98c968
...@@ -98,7 +98,7 @@ module.exports = (function() { ...@@ -98,7 +98,7 @@ module.exports = (function() {
}, },
describeTableQuery: function(tableName) { describeTableQuery: function(tableName) {
var query = 'SELECT column_name as "Field", column_default as "Default", is_nullable as "Null", data_type as "Type" FROM information_schema.columns WHERE table_name = <%= table %>;' var query = 'SELECT c.column_name as "Field", c.column_default as "Default", c.is_nullable as "Null", c.data_type as "Type", (SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS special FROM information_schema.columns c WHERE table_name = <%= table %>;'
return Utils._.template(query)({ return Utils._.template(query)({
table: QueryGenerator.addQuotes(tableName, "'") table: QueryGenerator.addQuotes(tableName, "'")
}) })
...@@ -686,6 +686,21 @@ module.exports = (function() { ...@@ -686,6 +686,21 @@ module.exports = (function() {
return "DROP TYPE IF EXISTS " + enumName + "; CREATE TYPE " + enumName + " AS " + dataType.match(/^ENUM\(.+\)/)[0] + "; " return "DROP TYPE IF EXISTS " + enumName + "; CREATE TYPE " + enumName + " AS " + dataType.match(/^ENUM\(.+\)/)[0] + "; "
}, },
fromArray: function(text) {
text = text.replace(/^{/, '').replace(/}$/, '')
var matches = text.match(/("(?:\\.|[^"\\\\])*"|[^,]*)(?:\s*,\s*|\s*$)/ig)
if (matches.length < 1) {
return []
}
matches = matches.map(function(m){
return m.replace(/",$/, '').replace(/,$/, '').replace(/(^"|"$)/, '')
})
return matches.slice(0, -1)
},
toHstore: function(text) { toHstore: function(text) {
var obj = {} var obj = {}
, pattern = '("\\\\.|[^"\\\\]*"\s*=|[^=]*)\s*=\s*>\s*("(?:\\.|[^"\\\\])*"|[^,]*)(?:\s*,\s*|$)' , pattern = '("\\\\.|[^"\\\\]*"\s*=|[^=]*)\s*=\s*>\s*("(?:\\.|[^"\\\\])*"|[^,]*)(?:\s*,\s*|$)'
......
...@@ -56,6 +56,7 @@ module.exports = (function() { ...@@ -56,6 +56,7 @@ module.exports = (function() {
var onSuccess = function(rows) { var onSuccess = function(rows) {
var results = [] var results = []
, self = this
, isTableNameQuery = (this.sql.indexOf('SELECT table_name FROM information_schema.tables') === 0) , isTableNameQuery = (this.sql.indexOf('SELECT table_name FROM information_schema.tables') === 0)
, isRelNameQuery = (this.sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') === 0) , isRelNameQuery = (this.sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') === 0)
...@@ -74,14 +75,15 @@ module.exports = (function() { ...@@ -74,14 +75,15 @@ module.exports = (function() {
} }
if (this.send('isSelectQuery')) { if (this.send('isSelectQuery')) {
if (this.sql.toLowerCase().indexOf('select column_name') === 0) { if (this.sql.toLowerCase().indexOf('select c.column_name') === 0) {
var result = {} var result = {}
rows.forEach(function(_result) { rows.forEach(function(_result) {
result[_result.Field] = { result[_result.Field] = {
type: _result.Type.toUpperCase(), type: _result.Type.toUpperCase(),
allowNull: (_result.Null === 'YES'), allowNull: (_result.Null === 'YES'),
defaultValue: _result.Default defaultValue: _result.Default,
special: (!!_result.special ? self.sequelize.queryInterface.QueryGenerator.fromArray(_result.special) : [])
} }
if (result[_result.Field].type === 'BOOLEAN') { if (result[_result.Field].type === 'BOOLEAN') {
......
...@@ -93,7 +93,8 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() { ...@@ -93,7 +93,8 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
before(function(done) { before(function(done) {
this.interface.createTable('User', { this.interface.createTable('User', {
username: Helpers.Sequelize.STRING, username: Helpers.Sequelize.STRING,
isAdmin: Helpers.Sequelize.BOOLEAN isAdmin: Helpers.Sequelize.BOOLEAN,
enumVals: Helpers.Sequelize.ENUM('hello', 'world')
}).success(done) }).success(done)
}) })
...@@ -103,6 +104,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() { ...@@ -103,6 +104,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
var username = metadata.username var username = metadata.username
var isAdmin = metadata.isAdmin var isAdmin = metadata.isAdmin
var enumVals = metadata.enumVals
expect(username.type).toEqual(dialect === 'postgres' ? 'CHARACTER VARYING' : 'VARCHAR(255)') expect(username.type).toEqual(dialect === 'postgres' ? 'CHARACTER VARYING' : 'VARCHAR(255)')
expect(username.allowNull).toBeTrue() expect(username.allowNull).toBeTrue()
...@@ -112,6 +114,11 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() { ...@@ -112,6 +114,11 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
expect(isAdmin.allowNull).toBeTrue() expect(isAdmin.allowNull).toBeTrue()
expect(isAdmin.defaultValue).toBeNull() expect(isAdmin.defaultValue).toBeNull()
if (dialect === 'postgres') {
expect(enumVals.special).toBeArray();
expect(enumVals.special.length).toEqual(2);
}
done() done()
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!