Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ addons:
before_script:
- "mysql -e 'create database sequelize_test;'"
- "psql -c 'create database sequelize_test;' -U postgres"
- "psql sequelize_test -c 'create extension postgis;' -U postgres"

script:
- "make test"
Expand Down
24 changes: 23 additions & 1 deletion lib/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var util = require('util')
, _ = require('lodash')
, Wkt = require('wellknown')
, warnings = {};

/**
Expand Down Expand Up @@ -594,6 +595,26 @@ var helpers = {
SCALE: [DECIMAL]
};

var GEOMETRY = function(type, srid) {
var options = _.isPlainObject(type) ? type : {
type: type,
srid: srid
};

if (!(this instanceof GEOMETRY)) return new GEOMETRY(options);

this.options = options;
this.type = options.type;
this.srid = options.srid;
};
util.inherits(GEOMETRY, ABSTRACT);

GEOMETRY.prototype.key = GEOMETRY.key = 'GEOMETRY';

GEOMETRY.prototype.parse = function(value) {
return Wkt.parse(value);
};

Object.keys(helpers).forEach(function (helper) {
helpers[helper].forEach(function (DataType) {
if (!DataType[helper]) {
Expand Down Expand Up @@ -640,5 +661,6 @@ module.exports = {
RANGE: RANGE,
REAL: REAL,
DOUBLE: DOUBLE,
'DOUBLE PRECISION': DOUBLE
'DOUBLE PRECISION': DOUBLE,
GEOMETRY: GEOMETRY
};
9 changes: 8 additions & 1 deletion lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,10 @@ var QueryGenerator = {
attr[0] = self.handleSequelizeMethod(attr[0]);
addTable = false;
} else {
if (attr[0].indexOf('(') === -1 && attr[0].indexOf(')') === -1) {
if (mainModel && mainModel._isGeometryAttribute(attr[1])) {
attr[0] = this.geometrySelect(attr[0]);
}
else if (attr[0].indexOf('(') === -1 && attr[0].indexOf(')') === -1) {
attr[0] = self.quoteIdentifier(attr[0]);
}
}
Expand Down Expand Up @@ -2143,6 +2146,10 @@ var QueryGenerator = {

booleanValue: function(value) {
return value;
},

geometrySelect: function(column) {
return this.quoteIdentifiers(column);
}
};

Expand Down
36 changes: 33 additions & 3 deletions lib/dialects/postgres/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

var BaseTypes = require('../../data-types')
, util = require('util')
, _ = require('lodash');
, _ = require('lodash')
, wkx = require('wkx')
, wkt = require('wellknown');

BaseTypes.ABSTRACT.prototype.dialectTypes = 'http://www.postgresql.org/docs/9.4/static/datatype.html';

Expand Down Expand Up @@ -153,6 +155,33 @@ BaseTypes.BLOB.prototype.toSql = function() {
return 'BYTEA';
};

var GEOMETRY = function() {
if (!(this instanceof GEOMETRY)) return new GEOMETRY();
BaseTypes.GEOMETRY.apply(this, arguments);
};
util.inherits(GEOMETRY, BaseTypes.GEOMETRY);

GEOMETRY.prototype.toSql = function() {
var result = this.key;

if (this.type){
result += '(' + this.type;

if (this.srid){
result += ',' + this.srid;
}

result += ')';
}

return result;
};

GEOMETRY.prototype.parse = function(value) {
var b = new Buffer(value, 'hex');
return wkt.parse(wkx.Geometry.parse(b).toWkt());
};

module.exports = {
STRING: STRING,
CHAR: CHAR,
Expand All @@ -162,7 +191,8 @@ module.exports = {
BIGINT: BIGINT,
REAL: REAL,
'DOUBLE PRECISION': DOUBLE,
FLOAT: FLOAT
FLOAT: FLOAT,
GEOMETRY: GEOMETRY
};

_.forIn(module.exports, function (DataType, key) {
Expand All @@ -172,4 +202,4 @@ _.forIn(module.exports, function (DataType, key) {
return new DataType(oldType.options);
};
}
});
});
9 changes: 8 additions & 1 deletion lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var QueryGenerator = {
},

showTablesQuery: function() {
return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';";
return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type LIKE '%TABLE' AND table_name != 'spatial_ref_sys';";
},

describeTableQuery: function(tableName, schema) {
Expand Down Expand Up @@ -897,6 +897,13 @@ var QueryGenerator = {
return 'ARRAY[' + value.map(function (v) {
return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field);
}, this).join(',') + ']::' + jsonType.key + '[]';
} else if (value && field && field.type instanceof DataTypes.GEOMETRY) {
var s = "ST_GeomFromGeoJSON('" + JSON.stringify(value) + "'";
if (field.type.srid) {
s += + ", " + field.type.srid;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @mikeringrose, I think you are either missing another term in this statement, or need to remove the leading "+". I tried creating geometry with DataTypes.GEOMETRY("Point", 4326) and ended up having bad queries generated due to this line.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up. I'll be submitting a fix for this issue tomorrow
to address the following bug -
#4054. If you need things to
work for now, you can leave off the SRID. Thanks again.

Mike Ringrose

On Tue, Jul 7, 2015 at 9:22 PM, Kris Rasmussen [email protected]
wrote:

In lib/dialects/postgres/query-generator.js
#4013 (comment):

@@ -897,6 +897,13 @@ var QueryGenerator = {
return 'ARRAY[' + value.map(function (v) {
return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field);
}, this).join(',') + ']::' + jsonType.key + '[]';

  • } else if (value && field && field.type instanceof DataTypes.GEOMETRY) {
  •  var s = "ST_GeomFromGeoJSON('" + JSON.stringify(value) + "'";
    
  •  if (field.type.srid) {
    
  •    s += + ", " + field.type.srid;
    

hey @mikeringrose https://github.com/mikeringrose, I think you are
either missing another term in this statement, or need to remove the
leading "+". I tried creating geometry with DataTypes.GEOMETRY("Point",
4326) and ended up having bad queries generated due to this line.

—
Reply to this email directly or view it on GitHub
https://github.com/sequelize/sequelize/pull/4013/files#r34108922.

}
s += ")";
return s;
}

return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
Expand Down
4 changes: 4 additions & 0 deletions lib/dialects/postgres/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ parseDialectSpecificFields = {
this.lib.types.arrayParser.create(value, function (v) {
return range.parse(v, options.dataType.type);
}).parse() : range.parse(value, options.dataType);
},
geometry: function (value, options) {
if (value === null) return null;
return options.dataType.parse(value);
}
};

Expand Down
7 changes: 6 additions & 1 deletion lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ Instance.prototype.set = function(key, value, options) {
value = (value === 1) ? true : (value === 0) ? false : value;
}
}

if (this.Model._hasGeometryAttributes && this.Model._isGeometryAttribute(key) && _.isString(value)) {
value = this.dialect.DatatTypes.GEOMETRY.parse(value);
}

if (!options.raw && ((primitives.indexOf(typeof value) === -1 && value !== null) || value !== originalValue)) {
this._previousDataValues[key] = originalValue;
this.changed(key, true);
Expand Down Expand Up @@ -978,7 +983,7 @@ Instance.prototype.decrement = function(fields, options) {
*/
Instance.prototype.equals = function(other) {
var result = true;

if (!other || !other.dataValues) {
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ Model.prototype.refreshAttributes = function() {
this._hstoreAttributes = [];
this._rangeAttributes = [];
this._jsonAttributes = [];
this._geometryAttributes = [];
this._virtualAttributes = [];
this._defaultValues = {};
this.Instance.prototype.validators = {};
Expand Down Expand Up @@ -767,6 +768,8 @@ Model.prototype.refreshAttributes = function() {
self._jsonAttributes.push(name);
} else if (definition.type instanceof DataTypes.VIRTUAL) {
self._virtualAttributes.push(name);
} else if (definition.type instanceof DataTypes.GEOMETRY) {
self._geometryAttributes.push(name);
}

if (definition.hasOwnProperty('defaultValue')) {
Expand Down Expand Up @@ -825,6 +828,11 @@ Model.prototype.refreshAttributes = function() {
return self._virtualAttributes.indexOf(key) !== -1;
});

this._hasGeometryAttributes = !!this._geometryAttributes.length;
this._isGeometryAttribute = Utils._.memoize(function(key) {
return self._geometryAttributes.indexOf(key) !== -1;
});

this._hasDefaultValues = !Utils._.isEmpty(this._defaultValues);

this.attributes = this.rawAttributes;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"node-uuid": "~1.4.1",
"shimmer": "1.0.0",
"toposort-class": "~0.3.0",
"validator": "^3.34.0"
"validator": "^3.34.0",
"wellknown": "^0.3.1",
"wkx": "0.0.7"
},
"devDependencies": {
"chai": "^2.1.2",
Expand Down
32 changes: 31 additions & 1 deletion test/integration/dialects/postgres/dao.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ if (dialect.match(/^postgres/)) {
course_period: DataTypes.RANGE(DataTypes.DATE),
acceptable_marks: { type: DataTypes.RANGE(DataTypes.DECIMAL), defaultValue: [0.65, 1] },
available_amount: DataTypes.RANGE,
holidays: DataTypes.ARRAY(DataTypes.RANGE(DataTypes.DATE))
holidays: DataTypes.ARRAY(DataTypes.RANGE(DataTypes.DATE)),
location: DataTypes.GEOMETRY()
});
return this.User.sync({ force: true });
});
Expand Down Expand Up @@ -875,6 +876,35 @@ if (dialect.match(/^postgres/)) {
});
});

it('should save geometry correctly', function() {
var point = { type: 'Point', coordinates: [39.807222,-76.984722] };
return this.User.create({ username: 'user', email: ['[email protected]'], location: point}).then(function(newUser) {
expect(newUser.location).to.deep.eql(point);
});
});

it('should update geometry correctly', function() {
var User = this.User;
var point1 = { type: 'Point', coordinates: [39.807222,-76.984722] }
, point2 = { type: 'Point', coordinates: [39.828333,-77.232222] };
return User.create({ username: 'user', email: ['[email protected]'], location: point1}).then(function(oldUser) {
return User.update({ location: point2 }, { where: { username: oldUser.username }, returning: true }).spread(function(count, updatedUsers) {
expect(updatedUsers[0].location).to.deep.eql(point2);
});
});
});

it('should read geometry correctly', function() {
var User = this.User;
var point = { type: 'Point', coordinates: [39.807222,-76.984722] };

return User.create({ username: 'user', email: ['[email protected]'], location: point}).then(function(user) {
return User.find({ where: { username: user.username }});
}).then(function(user) {
expect(user.location).to.deep.eql(point);
});
});

describe('[POSTGRES] Unquoted identifiers', function() {
it('can insert and select', function() {
var self = this;
Expand Down
26 changes: 25 additions & 1 deletion test/unit/sql/data-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,5 +666,29 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
}
});
}

if (current.dialect.supports.ARRAY) {
suite('GEOMETRY', function () {
testsql('GEOMETRY', DataTypes.GEOMETRY, {
postgres: 'GEOMETRY'
});

testsql('GEOMETRY(\'POINT\')', DataTypes.GEOMETRY('POINT'), {
postgres: 'GEOMETRY(POINT)'
});

testsql('GEOMETRY(\'LINESTRING\')', DataTypes.GEOMETRY('LINESTRING'), {
postgres: 'GEOMETRY(LINESTRING)',
});

testsql('GEOMETRY(\'POLYGON\')', DataTypes.GEOMETRY('POLYGON'), {
postgres: 'GEOMETRY(POLYGON)'
});

testsql('GEOMETRY(\'POINT\',4326)', DataTypes.GEOMETRY('POINT', 4326), {
postgres: 'GEOMETRY(POINT,4326)'
});
});
}
});
});
});