Skip to content

Commit

Permalink
fix: insert and update query builder to handle mssql geometry column …
Browse files Browse the repository at this point in the history
…correctly (#5947)

* [UPDATE] Update insert and update query builder to handle mssql geometry column with SRID properly

* [FIX] Fix indentation with spaces

* [FIX] Fix semicolon, and quota characters

* [FIX] Fix semicolon

Co-authored-by: Paul Kwok <wkkwok@uacs.hk>
  • Loading branch information
waikuen2010 and wkkwok committed May 16, 2020
1 parent 2fd0a8a commit 87cc6f4
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/query-builder/InsertQueryBuilder.ts
Expand Up @@ -500,6 +500,8 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
} else {
expression += `ST_GeomFromGeoJSON(${this.connection.driver.createParameter(paramName, parametersCount)})::${column.type}`;
}
} else if (this.connection.driver instanceof SqlServerDriver && this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
expression += column.type + "::STGeomFromText(" + this.connection.driver.createParameter(paramName, parametersCount) + ", " + (column.srid || "0") + ")";
} else {
expression += this.connection.driver.createParameter(paramName, parametersCount);
}
Expand Down
2 changes: 2 additions & 0 deletions src/query-builder/UpdateQueryBuilder.ts
Expand Up @@ -456,6 +456,8 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> implements
} else {
expression = `ST_GeomFromGeoJSON(${this.connection.driver.createParameter(paramName, parametersCount)})::${column.type}`;
}
} else if (this.connection.driver instanceof SqlServerDriver && this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
expression = column.type + "::STGeomFromText(" + this.connection.driver.createParameter(paramName, parametersCount) + ", " + (column.srid || "0") + ")";
} else {
expression = this.connection.driver.createParameter(paramName, parametersCount);
}
Expand Down
29 changes: 29 additions & 0 deletions test/core/column-kinds/geometry-column/entity/Feature.ts
@@ -0,0 +1,29 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../../src";

@Entity()
export class FeatureWithoutSRID {

@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@Column({ type: "geometry" })
shape: string;

}

@Entity()
export class FeatureWithSRID {

@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@Column({ type: "geometry", srid: 2326 })
shape: string;

}
108 changes: 108 additions & 0 deletions test/core/column-kinds/geometry-column/geometry-column.ts
@@ -0,0 +1,108 @@
import { expect } from "chai";
import "reflect-metadata";
import { Connection } from "../../../../src";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../../utils/test-utils";
import { FeatureWithoutSRID, FeatureWithSRID } from "./entity/Feature";

describe("column kinds > geometry column", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["mssql"]
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));



it("geometry column with SRID defined should be saved without error for valid WKT input", () => Promise.all(connections.map(async connection => {
const featureRepository = connection.getRepository(FeatureWithSRID);

// save a new feature
const feature = new FeatureWithSRID();
feature.name = "feature";
feature.shape = "POINT (828365.16700000037 823377.14699999988)";
await featureRepository.save(feature);

// load and check if createdAt was a value set by us
const loadedfeature = await featureRepository.findOne();
expect(loadedfeature).to.be.not.empty;
expect(loadedfeature!.name).to.be.eql("feature");
expect(loadedfeature!.shape).to.be.eql("POINT (828365.16700000037 823377.14699999988)");

})));

it("geometry column with SRID defined should be updated without error for valid WKT input", () => Promise.all(connections.map(async connection => {
const featureRepository = connection.getRepository(FeatureWithSRID);

// save a new feature
const feature = new FeatureWithSRID();
feature.name = "feature";
feature.shape = "POINT (828365.16700000037 823377.14699999988)";
await featureRepository.save(feature);

// load and check if createdAt was a value set by us
const loadedfeature = await featureRepository.findOne();
expect(loadedfeature).to.be.not.empty;
expect(loadedfeature!.name).to.be.eql("feature");
expect(loadedfeature!.shape).to.be.eql("POINT (828365.16700000037 823377.14699999988)");

feature.shape = "POINT (728365.16700000037 723377.14699999988)";
await featureRepository.save(feature);

// load and check if createdAt is a date (generated by db)
const updatedfeature = await featureRepository.findOne();
expect(updatedfeature).to.be.not.empty;
expect(updatedfeature!.name).to.be.eql("feature");
expect(updatedfeature!.shape).to.be.eql("POINT (728365.16700000037 723377.14699999988)");

})));

it("geometry column with no SRID should be saved without error for valid WKT input", () => Promise.all(connections.map(async connection => {
const featureRepository = connection.getRepository(FeatureWithoutSRID);

// save a new feature
const feature = new FeatureWithoutSRID();
feature.name = "feature";
feature.shape = "POINT (0 0)";
await featureRepository.save(feature);

// load and check if createdAt is a date (generated by db)
const loadedfeature = await featureRepository.findOne();
expect(loadedfeature).to.be.not.empty;
expect(loadedfeature!.name).to.be.eql("feature");
expect(loadedfeature!.shape).to.be.eql("POINT (0 0)");
})));

it("geometry column with no SRID should be updated without error for valid WKT input", () => Promise.all(connections.map(async connection => {
const featureRepository = connection.getRepository(FeatureWithoutSRID);

// save a new feature
const feature = new FeatureWithoutSRID();
feature.name = "feature";
feature.shape = "POINT (0 0)";
await featureRepository.save(feature);

// load and check if createdAt is a date (generated by db)
const loadedfeature = await featureRepository.findOne();
expect(loadedfeature).to.be.not.empty;
expect(loadedfeature!.name).to.be.eql("feature");
expect(loadedfeature!.shape).to.be.eql("POINT (0 0)");

feature.shape = "POINT (0.5 0.5)";
await featureRepository.save(feature);

// load and check if createdAt is a date (generated by db)
const updatedfeature = await featureRepository.findOne();
expect(updatedfeature).to.be.not.empty;
expect(updatedfeature!.name).to.be.eql("feature");
expect(updatedfeature!.shape).to.be.eql("POINT (0.5 0.5)");

})));

});

0 comments on commit 87cc6f4

Please sign in to comment.