Skip to content

Commit 69d173b

Browse files
Added Table Management functions with tests
1 parent 8cc35c1 commit 69d173b

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

LinqToDBPostGisNetTopologySuite.Tests/TableManagementFunctionsTests.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22

33
using LinqToDB;
4+
using Npgsql;
45
using NUnit.Framework;
56

67
namespace LinqToDBPostGisNetTopologySuite.Tests
@@ -174,6 +175,113 @@ public void TestFindSrid()
174175
}
175176
}
176177

178+
[Test]
179+
public void TestPopulateGeometryColumns()
180+
{
181+
var expectedConstrainNames = new[] { "enforce_dims_geom", "enforce_geotype_geom", "enforce_srid_geom", };
182+
var expectedCheckClause = new[] { "((st_ndims(geom) = 3))", "((geometrytype(geom) = 'LINESTRING'::text))", "((st_srid(geom) = 4326))" };
183+
184+
//insert geometry data for populating then get table oid
185+
int oid = 0;
186+
using (var conn = new NpgsqlConnection(TestDatabaseConnectionString))
187+
{
188+
conn.Open();
189+
using (var cmd = conn.CreateCommand())
190+
{
191+
cmd.CommandText = "INSERT INTO test_geometry_ddl(id,geom) VALUES(1,ST_GeomFromText('LINESTRING(1 2 1, 3 4 1)',4326) );";
192+
cmd.ExecuteNonQuery();
193+
194+
cmd.CommandText = "select oid::int4 from pg_class where oid = 'test_geometry_ddl'::regclass";
195+
var reader = cmd.ExecuteReader();
196+
reader.Read();
197+
oid = reader.GetInt32(0);
198+
}
199+
}
200+
201+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
202+
{
203+
db.Select(() => TableManagementFunctions.PopulateGeometryColumns(oid, false));
204+
using (var cmd = db.CreateCommand())
205+
{
206+
cmd.CommandText = @"SELECT
207+
tc.table_schema,
208+
tc.TABLE_NAME,
209+
string_agg ( col.COLUMN_NAME, ', ' ) AS COLUMNS,
210+
tc.CONSTRAINT_NAME,
211+
cc.check_clause
212+
FROM
213+
information_schema.table_constraints tc
214+
JOIN information_schema.check_constraints cc ON tc.CONSTRAINT_SCHEMA = cc.CONSTRAINT_SCHEMA
215+
AND tc.CONSTRAINT_NAME = cc.
216+
CONSTRAINT_NAME JOIN pg_namespace nsp ON nsp.nspname = cc.
217+
CONSTRAINT_SCHEMA JOIN pg_constraint pgc ON pgc.conname = cc.CONSTRAINT_NAME
218+
AND pgc.connamespace = nsp.oid
219+
AND pgc.contype = 'c'
220+
JOIN information_schema.COLUMNS col ON col.table_schema = tc.table_schema
221+
AND col.TABLE_NAME = tc.TABLE_NAME
222+
AND col.ordinal_position = ANY ( pgc.conkey )
223+
WHERE
224+
tc.CONSTRAINT_SCHEMA NOT IN ( 'pg_catalog', 'information_schema' ) and tc.TABLE_NAME = 'test_geometry_ddl'
225+
GROUP BY
226+
tc.table_schema,
227+
tc.TABLE_NAME,
228+
tc.CONSTRAINT_NAME,
229+
cc.check_clause
230+
ORDER BY
231+
tc.table_schema,
232+
tc.TABLE_NAME";
233+
var reader = cmd.ExecuteReader();
234+
int constraintCount = 0;
235+
while (reader.Read())
236+
{
237+
var constraintName = reader.GetString(3);
238+
var check = reader.GetString(4);
239+
240+
Assert.True(expectedConstrainNames.Contains(constraintName));
241+
Assert.True(expectedCheckClause.Contains(check));
242+
constraintCount++;
243+
}
244+
Assert.AreEqual(3, constraintCount);
245+
}
246+
}
247+
}
248+
249+
[Test]
250+
public void TestUpdateGeometrySRID()
251+
{
252+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
253+
{
254+
var updateGeomText = db.Select(() => TableManagementFunctions.UpdateGeometrySRID("test_geometry_ddl", "geom", 4326));
255+
256+
var srid = db.Select(() => TableManagementFunctions.FindSrid("public", "test_geometry_ddl", "geom"));
257+
Assert.AreEqual(4326, srid.GetValueOrDefault());
258+
}
259+
}
260+
261+
[Test]
262+
public void TestUpdateGeometrySRIDSchema()
263+
{
264+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
265+
{
266+
var updateGeomText = db.Select(() => TableManagementFunctions.UpdateGeometrySRID("public", "test_geometry_ddl", "geom", 4326));
267+
var srid = db.Select(() => TableManagementFunctions.FindSrid("public", "test_geometry_ddl", "geom"));
268+
269+
Assert.AreEqual(4326, srid.GetValueOrDefault());
270+
}
271+
}
272+
273+
[Test]
274+
public void TestUpdateGeometrySRIDCatalog()
275+
{
276+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
277+
{
278+
var updateGeomText = db.Select(() => TableManagementFunctions.UpdateGeometrySRID("postgistest", "public", "test_geometry_ddl", "geom", 4326));
279+
var srid = db.Select(() => TableManagementFunctions.FindSrid("public", "test_geometry_ddl", "geom"));
280+
281+
Assert.AreEqual(4326, srid.GetValueOrDefault());
282+
}
283+
}
284+
177285
[TearDown]
178286
public void TearDown()
179287
{

LinqToDBPostGisNetTopologySuite/TableManagementFunctions.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,85 @@ public static string DropGeometryTable(string catalogName, string schemaName, st
179179
{
180180
throw new InvalidOperationException();
181181
}
182+
183+
/// <summary>
184+
/// Ensures geometry columns have appropriate type modifiers or spatial constraints to ensure they are registered correctly in the geometry_columns view. By default will convert all geometry columns with no type modifier to ones with type modifiers
185+
/// </summary>
186+
/// <remarks>
187+
/// See https://postgis.net/docs/manual-3.0/Populate_Geometry_Columns.html
188+
/// </remarks>
189+
/// <param name="useTypeMod">Use type modifiers</param>
190+
/// <returns>Populate result</returns>
191+
[Sql.Function("Populate_Geometry_Columns", ServerSideOnly = true)]
192+
public static string PopulateGeometryColumns(bool useTypeMod)
193+
{
194+
throw new InvalidOperationException();
195+
}
196+
197+
/// <summary>
198+
/// Ensures geometry columns have appropriate type modifiers or spatial constraints to ensure they are registered correctly in the geometry_columns view. By default will convert all geometry columns with no type modifier to ones with type modifiers
199+
/// </summary>
200+
/// <remarks>
201+
/// See https://postgis.net/docs/manual-3.0/Populate_Geometry_Columns.html
202+
/// </remarks>
203+
/// <param name="oId">OId</param>
204+
/// <param name="useTypeMod">Use type modifiers</param>
205+
/// <returns>Populate result</returns>
206+
[Sql.Function("Populate_Geometry_Columns", ServerSideOnly = true)]
207+
public static int PopulateGeometryColumns(int oId, bool useTypeMod)
208+
{
209+
throw new InvalidOperationException();
210+
}
211+
212+
/// <summary>
213+
/// Updates the SRID of all features in a geometry column, updating constraints and reference in geometry_columns. If the column was enforced by a type definition, the type definition will be changed
214+
/// </summary>
215+
/// <remarks>
216+
/// See https://postgis.net/docs/manual-3.0/UpdateGeometrySRID.html
217+
/// </remarks>
218+
/// <param name="tableName"></param>
219+
/// <param name="columnName"></param>
220+
/// <param name="srid"></param>
221+
/// <returns>SRID update information</returns>
222+
[Sql.Function("UpdateGeometrySRID", ServerSideOnly = true)]
223+
public static string UpdateGeometrySRID(string tableName, string columnName, int srid)
224+
{
225+
throw new InvalidOperationException();
226+
}
227+
228+
/// <summary>
229+
/// Updates the SRID of all features in a geometry column, updating constraints and reference in geometry_columns. If the column was enforced by a type definition, the type definition will be changed
230+
/// </summary>
231+
/// <remarks>
232+
/// See https://postgis.net/docs/manual-3.0/UpdateGeometrySRID.html
233+
/// </remarks>
234+
/// <param name="schemaName">Schema name</param>
235+
/// <param name="tableName"></param>
236+
/// <param name="columnName"></param>
237+
/// <param name="srid"></param>
238+
/// <returns>SRID update information</returns>
239+
[Sql.Function("UpdateGeometrySRID", ServerSideOnly = true)]
240+
public static string UpdateGeometrySRID(string schemaName, string tableName, string columnName, int srid)
241+
{
242+
throw new InvalidOperationException();
243+
}
244+
245+
/// <summary>
246+
/// Updates the SRID of all features in a geometry column, updating constraints and reference in geometry_columns. If the column was enforced by a type definition, the type definition will be changed
247+
/// </summary>
248+
/// <remarks>
249+
/// See https://postgis.net/docs/manual-3.0/UpdateGeometrySRID.html
250+
/// </remarks>
251+
/// <param name="catalogName">Catalog name</param>
252+
/// <param name="schemaName">Schema name</param>
253+
/// <param name="tableName">Table name</param>
254+
/// <param name="columnName">Column name</param>
255+
/// <param name="srid">Spatial Reference System Identifier</param>
256+
/// <returns>SRID update information</returns>
257+
[Sql.Function("UpdateGeometrySRID", ServerSideOnly = true)]
258+
public static string UpdateGeometrySRID(string catalogName, string schemaName, string tableName, string columnName, int srid)
259+
{
260+
throw new InvalidOperationException();
261+
}
182262
}
183263
}

0 commit comments

Comments
 (0)