Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix insert and update query builder to handle mssql geometry column correctly #5947

Merged
merged 4 commits into from May 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)");

})));

});