Skip to content

Commit

Permalink
Supported Geospatial TSQL syntax with Parser Changes
Browse files Browse the repository at this point in the history
Signed-off-by: Anikait Agrawal <[email protected]>
  • Loading branch information
Anikait Agrawal committed Jan 18, 2024
1 parent 4cbf1e3 commit d521181
Show file tree
Hide file tree
Showing 7 changed files with 2,218 additions and 97 deletions.
847 changes: 827 additions & 20 deletions contrib/babelfishpg_tsql/src/tsqlIface.cpp

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions test/JDBC/expected/TestSpatialPoint-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,34 @@ DROP TABLE IF EXISTS TypeToGeog

DROP TABLE IF EXISTS SPATIALPOINT_dt

DROP PROCEDURE IF EXISTS GetPointsByXCoordinate

DROP PROCEDURE IF EXISTS GetPointsByXCoordinate1

DROP FUNCTION IF EXISTS dbo.GetXCoordinate

DROP FUNCTION IF EXISTS GetGeometry

DROP TRIGGER IF EXISTS trg_LogXCoordinateChange

DROP TABLE IF EXISTS XCoordinateChangeLog

DROP TRIGGER IF EXISTS trg_LogDistanceChange

DROP TABLE IF EXISTS DistanceChangeLog

DROP TABLE IF EXISTS YourTable

DROP TABLE IF EXISTS YourTable1

DROP TABLE IF EXISTS TableA

DROP TABLE IF EXISTS TableB

DROP PROCEDURE IF EXISTS GetPointsWithinDistance

DROP FUNCTION IF EXISTS dbo.CalculateDistance

DROP TABLE IF EXISTS YourTable2

Drop Table IF EXISTS babelfish_migration_mode_table
47 changes: 46 additions & 1 deletion test/JDBC/expected/TestSpatialPoint-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,51 @@ INSERT INTO SPATIALPOINTGEOM_dt (location) VALUES ( geometry::Point(47.65100, -2
~~ROW COUNT: 1~~


CREATE TABLE YourTable ( ID INT PRIMARY KEY, PointColumn geometry );

INSERT INTO YourTable (ID, PointColumn) VALUES (1, geometry::Point(3.0, 4.0, 4326)), (2, geometry::Point(5.0, 6.0, 4326));
~~ROW COUNT: 2~~


CREATE FUNCTION dbo.GetXCoordinate(@point geometry) RETURNS float AS BEGIN RETURN @point.STX; END;

CREATE TABLE TableA (ID INT PRIMARY KEY, PointA geometry);
CREATE TABLE TableB (ID INT PRIMARY KEY, PointB geometry);
INSERT INTO TableA (ID, PointA) VALUES (1, geometry::Point(1.0, 2.0, 4326));
~~ROW COUNT: 1~~

INSERT INTO TableB (ID, PointB) VALUES (1, geometry::Point(3.0, 4.0, 4326));
~~ROW COUNT: 1~~


CREATE PROCEDURE GetPointsByXCoordinate @XCoordinate FLOAT AS BEGIN SELECT * FROM YourTable WHERE PointColumn.STX = @XCoordinate; END;

CREATE PROCEDURE GetPointsByXCoordinate1 @XCoordinate FLOAT AS BEGIN DECLARE @Sql NVARCHAR(MAX); SET @Sql = N'SELECT ID, PointColumn.STX AS XCoordinate FROM YourTable WHERE PointColumn.STX = @ParamXCoordinate'; EXEC sp_executesql @Sql,N'@ParamXCoordinate FLOAT',@XCoordinate; END;

CREATE TABLE YourTable1 ( ID INT PRIMARY KEY, STX geometry );
INSERT INTO YourTable1 (ID, STX) VALUES (1, geometry::Point(3.0, 4.0, 4326)), (2, geometry::Point(5.0, 6.0, 4326));
~~ROW COUNT: 2~~


CREATE FUNCTION GetGeometry() RETURNS geometry AS BEGIN RETURN geometry::Point(1.0, 2.0, 4326); END;

CREATE TABLE XCoordinateChangeLog (PointID INT,OldXCoordinate FLOAT,NewXCoordinate FLOAT,ChangeDate DATETIME);

CREATE TRIGGER trg_LogXCoordinateChange ON YourTable AFTER UPDATE AS BEGIN INSERT INTO XCoordinateChangeLog (PointID, OldXCoordinate, NewXCoordinate, ChangeDate) SELECT i.ID, d.PointColumn.STX, i.PointColumn.STX, GETDATE() FROM inserted i JOIN deleted d ON i.ID = d.ID WHERE i.PointColumn.STX <> d.PointColumn.STX; END;

CREATE TABLE YourTable2 ( ID INT PRIMARY KEY, PointColumn1 geometry, PointColumn2 geometry );
INSERT INTO YourTable2 (ID, PointColumn1, PointColumn2) VALUES (1, geometry::Point(3.0, 4.0, 4326), geometry::Point(4.0, 5.0, 4326));
~~ROW COUNT: 1~~


CREATE FUNCTION dbo.CalculateDistance(@point1 geometry,@point2 geometry) RETURNS float AS BEGIN RETURN @point1.STDistance(@point2); END;

CREATE PROCEDURE GetPointsWithinDistance @referencePoint geometry, @maxDistance float AS BEGIN SELECT * FROM YourTable WHERE PointColumn.STDistance(@referencePoint) <= @maxDistance; END;

CREATE TABLE DistanceChangeLog (PointID INT,OldDistance FLOAT,NewDistance FLOAT,ChangeDate DATETIME);

CREATE TRIGGER trg_LogDistanceChange ON YourTable AFTER UPDATE AS BEGIN DECLARE @referencePoint geometry = geometry::Point(0.0, 0.0, 4326); INSERT INTO DistanceChangeLog (PointID, OldDistance, NewDistance, ChangeDate) SELECT i.ID, d.PointColumn.STDistance(@referencePoint), i.PointColumn.STDistance(@referencePoint), GETDATE() FROM inserted i JOIN deleted d ON i.ID = d.ID WHERE i.PointColumn.STDistance(@referencePoint) <> d.PointColumn.STDistance(@referencePoint); END;

#Tests for Geometry type Prepared Statements
prepst#!#INSERT INTO SPATIALPOINTGEOM_dt(location) values(?) #!#GEOMETRY|-|location|-|Point(47.65100 -22.34900):4326
~~ROW COUNT: 1~~
Expand Down Expand Up @@ -346,7 +391,7 @@ CREATE VIEW TextFromGeog AS SELECT STAsText(location) AS TextRepresentation FROM

CREATE VIEW BinaryFromGeog AS SELECT STAsBinary(location) AS BinaryRepresentation FROM SPATIALPOINTGEOG_dt;

CREATE VIEW CoordsFromGeog AS SELECT long(location), lat(location) AS Coordinates FROM SPATIALPOINTGEOG_dt;
CREATE VIEW CoordsFromGeog AS SELECT LONG(location), LAT(location) AS Coordinates FROM SPATIALPOINTGEOG_dt;

CREATE VIEW GeogView AS SELECT location.LONG, location.LAT AS Coordinates FROM SPATIALPOINTGEOG_dt;

Expand Down
Loading

0 comments on commit d521181

Please sign in to comment.