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

Commit 9ebfb19f by Jan Aagaard Meier

Change hstore and range parse to use pg types arrayparser. Provides a better fix for #3383

1 parent ab8ca530
...@@ -11,6 +11,10 @@ var PostgresDialect = function(sequelize) { ...@@ -11,6 +11,10 @@ var PostgresDialect = function(sequelize) {
this.sequelize = sequelize; this.sequelize = sequelize;
this.connectionManager = new ConnectionManager(this, sequelize); this.connectionManager = new ConnectionManager(this, sequelize);
this.connectionManager.initPools(); this.connectionManager.initPools();
// parseDialectSpecificFields needs access to the pg lib in order to use its array parser. We cannot simply require pg in query.js since the user can specify another library path (such as pg-native etc)
Query.prototype.parseDialectSpecificFields.lib = this.connectionManager.lib;
this.QueryGenerator = _.extend({}, QueryGenerator, { this.QueryGenerator = _.extend({}, QueryGenerator, {
options: sequelize.options, options: sequelize.options,
_dialect: this, _dialect: this,
......
...@@ -18,28 +18,16 @@ parseDialectSpecificFields = { ...@@ -18,28 +18,16 @@ parseDialectSpecificFields = {
hstore: function (value, options) { hstore: function (value, options) {
if (value === null) return null; if (value === null) return null;
// hstore does not require you to escape newlines, but JSON.parse does!
return DataTypes.ARRAY.is(options.dataType, DataTypes.HSTORE) ? return DataTypes.ARRAY.is(options.dataType, DataTypes.HSTORE) ?
Utils._.map( this.lib.types.arrayParser.create(value, hstore.parse).parse() : hstore.parse(value);
JSON.parse('[' +
value.substring(1, value.length - 1)
.replace('\n', '\\n')
.replace('\t', '\\t')
.replace('\r', '\\r') +
']'), function (v) {
return hstore.parse(v);
}) : hstore.parse(value);
}, },
range: function (value, options) { range: function (value, options) {
if (value === null) return null; if (value === null) return null;
return DataTypes.ARRAY.is(options.dataType, DataTypes.RANGE) ? return DataTypes.ARRAY.is(options.dataType, DataTypes.RANGE) ?
Utils._.map( this.lib.types.arrayParser.create(value, function (v) {
JSON.parse('[' + value.substring(1, value.length - 1) + ']'),
function (v) {
return range.parse(v, options.dataType.type); return range.parse(v, options.dataType.type);
}) : }).parse() : range.parse(value, options.dataType);
range.parse(value, options.dataType);
} }
}; };
...@@ -93,6 +81,8 @@ module.exports = (function() { ...@@ -93,6 +81,8 @@ module.exports = (function() {
}; };
Utils.inherit(Query, AbstractQuery); Utils.inherit(Query, AbstractQuery);
Query.prototype.parseDialectSpecificFields = parseDialectSpecificFields;
Query.prototype.run = function(sql) { Query.prototype.run = function(sql) {
/* jshint -W027 */ /* jshint -W027 */
this.sql = sql; this.sql = sql;
...@@ -156,6 +146,7 @@ module.exports = (function() { ...@@ -156,6 +146,7 @@ module.exports = (function() {
results.forEach(function (result) { results.forEach(function (result) {
var attributes = /ON .*? (?:USING .*?\s)?\((.*)\)/gi.exec(result.definition)[1].split(',') var attributes = /ON .*? (?:USING .*?\s)?\((.*)\)/gi.exec(result.definition)[1].split(',')
, field , field
, attribute , attribute
, columns; , columns;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!