Skip to content

Commit

Permalink
feat: support SRID in mysql/mariadb (#15835)
Browse files Browse the repository at this point in the history
  • Loading branch information
ephys committed Mar 24, 2023
1 parent 4bca19a commit d326d84
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
16 changes: 15 additions & 1 deletion packages/core/src/dialects/mariadb/data-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
export { BIGINT, DATE, BOOLEAN, DECIMAL, DOUBLE, FLOAT, ENUM, INTEGER, GEOMETRY, MEDIUMINT, SMALLINT, UUID, TINYINT, REAL } from '../mysql/data-types.js';
import { GEOMETRY as MySqlGeometry } from '../mysql/data-types.js';

export class GEOMETRY extends MySqlGeometry {
toSql() {
const sql = this.options.type?.toUpperCase() || 'GEOMETRY';

if (this.options.srid) {
return `${sql} REF_SYSTEM_ID=${this.options.srid}`;
}

return sql;
}
}

export { BIGINT, DATE, BOOLEAN, DECIMAL, DOUBLE, FLOAT, ENUM, INTEGER, MEDIUMINT, SMALLINT, UUID, TINYINT, REAL } from '../mysql/data-types.js';

// Unlike MySQL, MariaDB does not need to cast JSON values when comparing them to other values,
// so we do not need that override.
Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/dialects/mysql/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,32 @@ export class UUID extends BaseTypes.UUID {

export class GEOMETRY extends BaseTypes.GEOMETRY {
toBindableValue(value: GeoJson) {
const srid = this.options.srid ? `, ${this.options.srid}` : '';

return `ST_GeomFromText(${this._getDialect().escapeString(
wkx.Geometry.parseGeoJSON(value).toWkt(),
)})`;
)}${srid})`;
}

getBindParamSql(value: GeoJson, options: BindParamOptions) {
const srid = this.options.srid ? `, ${options.bindParam(this.options.srid)}` : '';

return `ST_GeomFromText(${options.bindParam(
wkx.Geometry.parseGeoJSON(value).toWkt(),
)})`;
)}${srid})`;
}

toSql() {
return this.options.type?.toUpperCase() || 'GEOMETRY';
const sql = this.options.type?.toUpperCase() || 'GEOMETRY';

if (this.options.srid) {
// According to the documentation examples the format is: POINT NOT NULL SRID 4326
// however in practise the order of NOT NULL and the SRID specification doesn't seem to matter.
// Using the /*!80003 ... */ syntax is for backwards compat with MySQL versions before 8.0: MySQL 5.7 doesn't support SRIDs on table columns.
return `${sql} /*!80003 SRID ${this.options.srid} */`;
}

return sql;
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/unit/data-types/geometry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('GEOMETRY', () => {
testDataTypeSql(`GEOMETRY('POINT',4326)`, DataTypes.GEOMETRY(GeoJsonType.Point, 4326), {
default: unsupportedError,
postgres: 'GEOMETRY(POINT,4326)',
'mysql mariadb': 'POINT',
mysql: 'POINT /*!80003 SRID 4326 */',
mariadb: 'POINT REF_SYSTEM_ID=4326',
});
});

0 comments on commit d326d84

Please sign in to comment.