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

Commit 202fde0d by Jan Aagaard Meier

Fixed show schemas and desribe table for pg

1 parent 8e516f1a
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#### Backwards compatability changes #### Backwards compatability changes
- The default query type for `sequelize.query` is now `RAW` - this means that two arguments (results and metadata) will be returned by default and you should use `.spread` - The default query type for `sequelize.query` is now `RAW` - this means that two arguments (results and metadata) will be returned by default and you should use `.spread`
- The 4th argument to `sequelize.query` has been deprecated in favor of `options.replacements` - The 4th argument to `sequelize.query` has been deprecated in favor of `options.replacements`
>>>>>>> Clean up test and added docs for rewamped raw query handling
# 2.0.0-rc7 # 2.0.0-rc7
- [FEATURE] Throw an error if no where clause is given to `Model.destroy()`. - [FEATURE] Throw an error if no where clause is given to `Model.destroy()`.
......
...@@ -141,6 +141,10 @@ module.exports = (function() { ...@@ -141,6 +141,10 @@ module.exports = (function() {
return this.options.type === QueryTypes.SHOWINDEXES; return this.options.type === QueryTypes.SHOWINDEXES;
}; };
AbstractQuery.prototype.isDescribeQuery = function () {
return this.options.type === QueryTypes.DESCRIBE;
};
AbstractQuery.prototype.isSelectQuery = function() { AbstractQuery.prototype.isSelectQuery = function() {
return this.options.type === QueryTypes.SELECT; return this.options.type === QueryTypes.SELECT;
}; };
......
...@@ -129,23 +129,20 @@ module.exports = (function() { ...@@ -129,23 +129,20 @@ module.exports = (function() {
if (this.isShowTablesQuery()) { if (this.isShowTablesQuery()) {
result = this.handleShowTablesQuery(data); result = this.handleShowTablesQuery(data);
} else if (this.isShowOrDescribeQuery()) { } else if (this.isDescribeQuery()) {
result = data; result = {};
if (this.sql.toLowerCase().indexOf("select c.column_name as 'name', c.data_type as 'type', c.is_nullable as 'isnull'") === 0) { data.forEach(function(_result) {
result = {}; if (_result.Default)
data.forEach(function(_result) { _result.Default = _result.Default.replace('(\'','').replace('\')','').replace(/'/g,'');
if (_result.Default)
_result.Default = _result.Default.replace('(\'','').replace('\')','').replace(/'/g,''); result[_result.Name] = {
type: _result.Type.toUpperCase(),
result[_result.Name] = { allowNull: (_result.IsNull === 'YES' ? true : false),
type: _result.Type.toUpperCase(), defaultValue: _result.Default
allowNull: (_result.IsNull === 'YES' ? true : false), };
defaultValue: _result.Default });
}; } else if (this.isShowIndexesQuery()) {
}); result = this.handleShowIndexesQuery(data);
} else if (this.isShowIndexesQuery()) {
result = this.handleShowIndexesQuery(data);
}
} else if (this.isSelectQuery()) { } else if (this.isSelectQuery()) {
result = this.handleSelectQuery(data); result = this.handleSelectQuery(data);
} else if (this.isCallQuery()) { } else if (this.isCallQuery()) {
......
...@@ -76,22 +76,19 @@ module.exports = (function() { ...@@ -76,22 +76,19 @@ module.exports = (function() {
result = this.handleSelectQuery(data); result = this.handleSelectQuery(data);
} else if (this.isShowTablesQuery()) { } else if (this.isShowTablesQuery()) {
result = this.handleShowTablesQuery(data); result = this.handleShowTablesQuery(data);
} else if (this.isShowOrDescribeQuery()) { } else if (this.isDescribeQuery()) {
result = data; result = {};
if (this.sql.toLowerCase().indexOf('describe') === 0) { data.forEach(function(_result) {
result = {}; result[_result.Field] = {
type: _result.Type.toUpperCase(),
data.forEach(function(_result) { allowNull: (_result.Null === 'YES'),
result[_result.Field] = { defaultValue: _result.Default
type: _result.Type.toUpperCase(), };
allowNull: (_result.Null === 'YES'), });
defaultValue: _result.Default } else if (this.isShowIndexesQuery()) {
}; result = this.handleShowIndexesQuery(data);
});
} else if (this.isShowIndexesQuery()) {
result = this.handleShowIndexesQuery(data);
}
} else if (this.isCallQuery()) { } else if (this.isCallQuery()) {
result = data[0]; result = data[0];
} else if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery() || this.isUpsertQuery()) { } else if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery() || this.isUpsertQuery()) {
......
...@@ -147,62 +147,60 @@ module.exports = (function() { ...@@ -147,62 +147,60 @@ module.exports = (function() {
}); });
return result; return result;
} else if (self.isSelectQuery()) { } else if (self.isSelectQuery()) {
if (self.sql.toLowerCase().indexOf('select c.column_name') === 0) { // Postgres will treat tables as case-insensitive, so fix the case
result = {}; // of the returned values to match attributes
if (self.options.raw === false && self.sequelize.options.quoteIdentifiers === false) {
rows.forEach(function(_result) { var attrsMap = Utils._.reduce(self.callee.attributes, function(m, v, k) { m[k.toLowerCase()] = k; return m; }, {});
result[_result.Field] = { rows.forEach(function(row) {
type: _result.Type.toUpperCase(), Utils._.keys(row).forEach(function(key) {
allowNull: (_result.Null === 'YES'), var targetAttr = attrsMap[key];
defaultValue: _result.Default, if (targetAttr !== key) {
special: (!!_result.special ? self.sequelize.queryInterface.QueryGenerator.fromArray(_result.special) : []) row[targetAttr] = row[key];
}; delete row[key];
}
});
});
}
if (result[_result.Field].type === 'BOOLEAN') { if (!!self.callee && !!self.callee._hasHstoreAttributes) {
result[_result.Field].defaultValue = { 'false': false, 'true': true }[result[_result.Field].defaultValue]; rows.forEach(function(row) {
parseHstoreFields(self.callee, row);
});
}
if (result[_result.Field].defaultValue === undefined) { return self.handleSelectQuery(rows);
result[_result.Field].defaultValue = null; } else if (QueryTypes.DESCRIBE === self.options.type) {
} result = {};
rows.forEach(function(_result) {
result[_result.Field] = {
type: _result.Type.toUpperCase(),
allowNull: (_result.Null === 'YES'),
defaultValue: _result.Default,
special: (!!_result.special ? self.sequelize.queryInterface.QueryGenerator.fromArray(_result.special) : [])
};
if (result[_result.Field].type === 'BOOLEAN') {
result[_result.Field].defaultValue = { 'false': false, 'true': true }[result[_result.Field].defaultValue];
if (result[_result.Field].defaultValue === undefined) {
result[_result.Field].defaultValue = null;
} }
}
if (typeof result[_result.Field].defaultValue === 'string') { if (typeof result[_result.Field].defaultValue === 'string') {
result[_result.Field].defaultValue = result[_result.Field].defaultValue.replace(/'/g, ''); result[_result.Field].defaultValue = result[_result.Field].defaultValue.replace(/'/g, '');
if (result[_result.Field].defaultValue.indexOf('::') > -1) { if (result[_result.Field].defaultValue.indexOf('::') > -1) {
var split = result[_result.Field].defaultValue.split('::'); var split = result[_result.Field].defaultValue.split('::');
if (split[1].toLowerCase() !== 'regclass)') { if (split[1].toLowerCase() !== 'regclass)') {
result[_result.Field].defaultValue = split[0]; result[_result.Field].defaultValue = split[0];
}
} }
} }
});
return result;
} else {
// Postgres will treat tables as case-insensitive, so fix the case
// of the returned values to match attributes
if (self.options.raw === false && self.sequelize.options.quoteIdentifiers === false) {
var attrsMap = Utils._.reduce(self.callee.attributes, function(m, v, k) { m[k.toLowerCase()] = k; return m; }, {});
rows.forEach(function(row) {
Utils._.keys(row).forEach(function(key) {
var targetAttr = attrsMap[key];
if (targetAttr !== key) {
row[targetAttr] = row[key];
delete row[key];
}
});
});
}
if (!!self.callee && !!self.callee._hasHstoreAttributes) {
rows.forEach(function(row) {
parseHstoreFields(self.callee, row);
});
} }
});
return self.handleSelectQuery(rows); return result;
}
} else if (self.isShowOrDescribeQuery()) { } else if (self.isShowOrDescribeQuery()) {
return results; return results;
} else if (QueryTypes.BULKUPDATE === self.options.type) { } else if (QueryTypes.BULKUPDATE === self.options.type) {
......
...@@ -46,12 +46,13 @@ module.exports = (function() { ...@@ -46,12 +46,13 @@ module.exports = (function() {
var self = this; var self = this;
options = Utils._.extend({ options = Utils._.extend({
raw: true raw: true,
type: this.sequelize.QueryTypes.SELECT
}, options || {}); }, options || {});
var showSchemasSql = self.QueryGenerator.showSchemasQuery(); var showSchemasSql = self.QueryGenerator.showSchemasQuery();
return this.sequelize.query(showSchemasSql, null, options).then(function(schemaNames) { return this.sequelize.query(showSchemasSql, options).then(function(schemaNames) {
return Utils._.flatten( return Utils._.flatten(
Utils._.map(schemaNames, function(value) { Utils._.map(schemaNames, function(value) {
return (!!value.schema_name ? value.schema_name : value); return (!!value.schema_name ? value.schema_name : value);
...@@ -317,7 +318,7 @@ module.exports = (function() { ...@@ -317,7 +318,7 @@ module.exports = (function() {
var sql = this.QueryGenerator.describeTableQuery(tableName, schema, schemaDelimiter); var sql = this.QueryGenerator.describeTableQuery(tableName, schema, schemaDelimiter);
return this.sequelize.query(sql, null, { raw: true }).then(function(data) { return this.sequelize.query(sql, { type: QueryTypes.DESCRIBE }).then(function(data) {
// If no data is returned from the query, then the table name may be wrong. // If no data is returned from the query, then the table name may be wrong.
// Query generators that use information_schema for retrieving table info will just return an empty result set, // Query generators that use information_schema for retrieving table info will just return an empty result set,
// it will not throw an error like built-ins do (e.g. DESCRIBE on MySql). // it will not throw an error like built-ins do (e.g. DESCRIBE on MySql).
......
...@@ -10,5 +10,6 @@ module.exports = { ...@@ -10,5 +10,6 @@ module.exports = {
VERSION: 'VERSION', VERSION: 'VERSION',
SHOWTABLES: 'SHOWTABLES', SHOWTABLES: 'SHOWTABLES',
SHOWINDEXES: 'SHOWINDEXES', SHOWINDEXES: 'SHOWINDEXES',
DESCRIBE: 'DESCRIBE',
RAW: 'RAW', RAW: 'RAW',
}; };
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!