Skip to content

Commit 574bcdc

Browse files
Implemented Table Management functions with tests (#4)
* Implemented Table Management functions with tests * fix typos
1 parent 4b0f7f3 commit 574bcdc

File tree

2 files changed

+380
-0
lines changed

2 files changed

+380
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
using System.Linq;
2+
3+
using LinqToDB;
4+
using NUnit.Framework;
5+
6+
namespace LinqToDBPostGisNetTopologySuite.Tests
7+
{
8+
[TestFixture]
9+
class TableManagementFunctionsTests : TestsBase
10+
{
11+
[SetUp]
12+
public void Setup()
13+
{
14+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
15+
{
16+
using (var cmd = db.CreateCommand())
17+
{
18+
cmd.CommandText = @"
19+
CREATE TABLE test_geometry_ddl(
20+
id integer primary key,
21+
geom geometry);";
22+
cmd.ExecuteNonQuery();
23+
}
24+
}
25+
}
26+
27+
[Test]
28+
public void TestAddGeometryColumn()
29+
{
30+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
31+
{
32+
var result = db.Select(() => TableManagementFunctions.AddGeometryColumn("test_geometry_ddl", "geom1", 4326, "POLYGON", 2, true));
33+
34+
Assert.AreEqual("public.test_geometry_ddl.geom1 SRID:4326 TYPE:POLYGON DIMS:2 ", result);
35+
}
36+
}
37+
38+
[Test]
39+
public void TestAddGeometryColumnWithSchema()
40+
{
41+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
42+
{
43+
var result = db.Select(() => TableManagementFunctions.AddGeometryColumn("public", "test_geometry_ddl", "geom2", 4326, "POLYGON", 2, true));
44+
45+
Assert.AreEqual("public.test_geometry_ddl.geom2 SRID:4326 TYPE:POLYGON DIMS:2 ", result);
46+
}
47+
}
48+
49+
[Test]
50+
public void TestAddGeometryColumnWithCatalog()
51+
{
52+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
53+
{
54+
var result = db.Select(() => TableManagementFunctions.AddGeometryColumn("postgistest", "public", "test_geometry_ddl", "geom3", 4326, "POLYGON", 2, true));
55+
56+
Assert.AreEqual("public.test_geometry_ddl.geom3 SRID:4326 TYPE:POLYGON DIMS:2 ", result);
57+
}
58+
}
59+
60+
[Test]
61+
public void TestDropGeometryColumn()
62+
{
63+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
64+
{
65+
var result = db.Select(() => TableManagementFunctions.DropGeometryColumn("test_geometry_ddl", "geom"));
66+
67+
using (var cmd = db.CreateCommand())
68+
{
69+
cmd.CommandText = "select count(1) from information_schema.columns WHERE table_schema = 'public' and table_name = 'test_geometry_ddl' and column_name = 'geom';";
70+
var countText = cmd.ExecuteScalar().ToString();
71+
72+
Assert.AreEqual("0", countText);
73+
}
74+
}
75+
}
76+
77+
[Test]
78+
public void TestDropGeometryColumnSchema()
79+
{
80+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
81+
{
82+
var result = db.Select(() => TableManagementFunctions.DropGeometryColumn("public", "test_geometry_ddl", "geom"));
83+
84+
using (var cmd = db.CreateCommand())
85+
{
86+
cmd.CommandText = "select count(1) from information_schema.columns WHERE table_schema = 'public' and table_name = 'test_geometry_ddl' and column_name = 'geom';";
87+
var countText = cmd.ExecuteScalar().ToString();
88+
89+
Assert.AreEqual("0", countText);
90+
}
91+
}
92+
}
93+
94+
[Test]
95+
public void TestDropGeometryColumnCatalog()
96+
{
97+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
98+
{
99+
var result = db.Select(() => TableManagementFunctions.DropGeometryColumn("postgis", "public", "test_geometry_ddl", "geom"));
100+
101+
using (var cmd = db.CreateCommand())
102+
{
103+
cmd.CommandText = "select count(1) from information_schema.columns WHERE table_schema = 'public' and table_name = 'test_geometry_ddl' and column_name = 'geom';";
104+
var countText = cmd.ExecuteScalar().ToString();
105+
106+
Assert.AreEqual("0", countText);
107+
}
108+
}
109+
}
110+
111+
[Test]
112+
public void TestDropGeometryTable()
113+
{
114+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
115+
{
116+
var result = db.SelectQuery(() => TableManagementFunctions.DropGeometryTable("test_geometry_ddl")).Single();
117+
118+
using (var cmd = db.CreateCommand())
119+
{
120+
cmd.CommandText = @"
121+
select count(*) from pg_class where relname = 'test_geometry_ddl';";
122+
var tableCount = (long)cmd.ExecuteScalar();
123+
124+
Assert.AreEqual(0, tableCount);
125+
}
126+
}
127+
}
128+
129+
[Test]
130+
public void TestDropGeometryTableSchema()
131+
{
132+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
133+
{
134+
var result = db.SelectQuery(() => TableManagementFunctions.DropGeometryTable("public", "test_geometry_ddl")).Single();
135+
136+
using (var cmd = db.CreateCommand())
137+
{
138+
cmd.CommandText = @"
139+
select count(*) from pg_class where relname = 'test_geometry_ddl';";
140+
var tableCount = (long)cmd.ExecuteScalar();
141+
142+
Assert.AreEqual(0, tableCount);
143+
}
144+
}
145+
}
146+
147+
[Test]
148+
public void TestDropGeometryTableCatalog()
149+
{
150+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
151+
{
152+
var result = db.SelectQuery(() => TableManagementFunctions.DropGeometryTable("postgistest", "public", "test_geometry_ddl")).Single();
153+
154+
using (var cmd = db.CreateCommand())
155+
{
156+
cmd.CommandText = @"
157+
select count(*) from pg_class where relname = 'test_geometry_ddl';";
158+
var tableCount = (long)cmd.ExecuteScalar();
159+
160+
Assert.AreEqual(0, tableCount);
161+
}
162+
}
163+
}
164+
165+
[Test]
166+
public void TestFindSrid()
167+
{
168+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
169+
{
170+
var srid = db.Select(() => TableManagementFunctions.FindSrid("public", "test_geometry_ddl", "geom"));
171+
172+
Assert.IsNotNull(srid);
173+
Assert.AreEqual(0, srid);
174+
}
175+
}
176+
177+
[TearDown]
178+
public void TearDown()
179+
{
180+
using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
181+
{
182+
using (var cmd = db.CreateCommand())
183+
{
184+
try
185+
{
186+
cmd.CommandText = "DROP TABLE test_geometry_ddl;";
187+
cmd.ExecuteNonQuery();
188+
}
189+
catch (System.Exception)
190+
{
191+
//ignore
192+
}
193+
}
194+
}
195+
}
196+
}
197+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
using System;
2+
using LinqToDB;
3+
4+
namespace LinqToDBPostGisNetTopologySuite
5+
{
6+
/// <summary>
7+
/// Table Management Functions
8+
/// </summary>
9+
/// <remarks>
10+
/// 8.2. Table Management Functions https://postgis.net/docs/manual-3.0/reference.html#Management_Functions
11+
/// </remarks>
12+
public static class TableManagementFunctions
13+
{
14+
/// <summary>
15+
/// Adds a geometry column to an existing table of attributes.If you require the old behavior of constraints use the default use_typmod, but set useTypMod to false
16+
/// </summary>
17+
/// <remarks>
18+
/// See https://postgis.net/docs/manual-3.0/AddGeometryColumn.html
19+
/// </remarks>
20+
/// <param name="tableName">Table name</param>
21+
/// <param name="columnName">Column name</param>
22+
/// <param name="srid">Spatial Reference Identifier</param>
23+
/// <param name="type">Geometry type</param>
24+
/// <param name="dimension">Dimension</param>
25+
/// <param name="useTypMod">Use typmod</param>
26+
/// <returns>Information about added column</returns>
27+
[Sql.Function("AddGeometryColumn", ServerSideOnly = true)]
28+
public static string AddGeometryColumn(string tableName, string columnName, int srid, string type, int dimension, bool useTypMod)
29+
{
30+
throw new InvalidOperationException();
31+
}
32+
33+
/// <summary>
34+
/// Adds a geometry column to an existing table of attributes.If you require the old behavior of constraints use the default use_typmod, but set useTypMod to false
35+
/// </summary>
36+
/// <remarks>
37+
/// See https://postgis.net/docs/manual-3.0/AddGeometryColumn.html
38+
/// </remarks>
39+
/// <param name="schemaName">Schema Name</param>
40+
/// <param name="tableName">Table name</param>
41+
/// <param name="columnName">Column name</param>
42+
/// <param name="srid">Spatial Reference Identifier</param>
43+
/// <param name="type">Geometry type</param>
44+
/// <param name="dimension">Dimension</param>
45+
/// <param name="useTypMod">Use typmod</param>
46+
/// <returns>Information about added column</returns>
47+
[Sql.Function("AddGeometryColumn", ServerSideOnly = true)]
48+
public static string AddGeometryColumn(string schemaName, string tableName, string columnName, int srid, string type, int dimension, bool useTypMod)
49+
{
50+
throw new InvalidOperationException();
51+
}
52+
53+
/// <summary>
54+
/// Adds a geometry column to an existing table of attributes.If you require the old behavior of constraints use the default use_typmod, but set useTypMod to false
55+
/// </summary>
56+
/// <remarks>
57+
/// See https://postgis.net/docs/manual-3.0/AddGeometryColumn.html
58+
/// </remarks>
59+
/// <param name="catalogName">Catalog name</param>
60+
/// <param name="schemaName">Schema name</param>
61+
/// <param name="tableName">Table name</param>
62+
/// <param name="columnName">Column name</param>
63+
/// <param name="srid">Spatial Reference Identifier</param>
64+
/// <param name="type">Geometry type</param>
65+
/// <param name="dimension">Dimension</param>
66+
/// <param name="useTypMod">Use typmod</param>
67+
/// <returns>Information about added column</returns>
68+
[Sql.Function("AddGeometryColumn", ServerSideOnly = true)]
69+
public static string AddGeometryColumn(string catalogName, string schemaName, string tableName, string columnName, int srid, string type, int dimension, bool useTypMod)
70+
{
71+
throw new InvalidOperationException();
72+
}
73+
74+
/// <summary>
75+
/// Removes a geometry column from a spatial table. Schema_name will need to match the f_table_schema field of the table's row in the geometry_columns table
76+
/// </summary>
77+
/// <remarks>
78+
/// See https://postgis.net/docs/manual-3.0/DropGeometryColumn.html
79+
/// </remarks>
80+
/// <param name="tableName">Table name</param>
81+
/// <param name="columnName">Column name</param>
82+
/// <returns>Information about dropped column</returns>
83+
[Sql.Function("DropGeometryColumn", ServerSideOnly = true)]
84+
public static string DropGeometryColumn(string tableName, string columnName)
85+
{
86+
throw new InvalidOperationException();
87+
}
88+
89+
/// <summary>
90+
/// Removes a geometry column from a spatial table. Schema_name will need to match the f_table_schema field of the table's row in the geometry_columns table
91+
/// </summary>
92+
/// <remarks>
93+
/// See https://postgis.net/docs/manual-3.0/DropGeometryColumn.html
94+
/// </remarks>
95+
/// <param name="schemaName">Schema name</param>
96+
/// <param name="tableName">Table name</param>
97+
/// <param name="columnName">Column name</param>
98+
/// <returns>Information about dropped column</returns>
99+
[Sql.Function("DropGeometryColumn", ServerSideOnly = true)]
100+
public static string DropGeometryColumn(string schemaName, string tableName, string columnName)
101+
{
102+
throw new InvalidOperationException();
103+
}
104+
105+
/// <summary>
106+
/// Removes a geometry column from a spatial table. Schema_name will need to match the f_table_schema field of the table's row in the geometry_columns table
107+
/// </summary>
108+
/// <remarks>
109+
/// See https://postgis.net/docs/manual-3.0/DropGeometryColumn.html
110+
/// </remarks>
111+
/// <param name="catalogName">Catalog name</param>
112+
/// <param name="schemaName">Schema name</param>
113+
/// <param name="tableName">Table name</param>
114+
/// <param name="columnName">Column name</param>
115+
/// <returns>Information about dropped column</returns>
116+
[Sql.Function("DropGeometryColumn", ServerSideOnly = true)]
117+
public static string DropGeometryColumn(string catalogName, string schemaName, string tableName, string columnName)
118+
{
119+
throw new InvalidOperationException();
120+
}
121+
122+
/// <summary>
123+
/// Drops a table and all its references in geometry_columns
124+
/// </summary>
125+
/// <remarks>
126+
/// See https://postgis.net/docs/manual-3.0/DropGeometryTable.html
127+
/// </remarks>
128+
/// <param name="tableName">Table name</param>
129+
/// <returns>Table Dropped</returns>
130+
[Sql.Function("DropGeometryTable", ServerSideOnly = true)]
131+
public static string DropGeometryTable(string tableName)//returning bool will get error
132+
{
133+
throw new InvalidOperationException();
134+
}
135+
136+
/// <summary>
137+
/// Drops a table and all its references in geometry_columns
138+
/// </summary>
139+
/// <remarks>
140+
/// See https://postgis.net/docs/manual-3.0/DropGeometryTable.html
141+
/// </remarks>
142+
/// <param name="schemaName">Schema name</param>
143+
/// <param name="tableName">Table name</param>
144+
/// <returns>Table Dropped</returns>
145+
[Sql.Function("DropGeometryTable", ServerSideOnly = true)]
146+
public static string DropGeometryTable(string schemaName, string tableName)//returning bool will get error
147+
{
148+
throw new InvalidOperationException();
149+
}
150+
151+
/// <summary>
152+
/// Drops a table and all its references in geometry_columns
153+
/// </summary>
154+
/// <remarks>
155+
/// See https://postgis.net/docs/manual-3.0/DropGeometryTable.html
156+
/// </remarks>
157+
/// <param name="catalogName">Catalog name</param>
158+
/// <param name="schemaName">Schema name</param>
159+
/// <param name="tableName">Table name</param>
160+
/// <returns>Table Dropped</returns>
161+
[Sql.Function("DropGeometryTable", ServerSideOnly = true)]
162+
public static string DropGeometryTable(string catalogName, string schemaName, string tableName)//returning bool will get error
163+
{
164+
throw new InvalidOperationException();
165+
}
166+
167+
/// <summary>
168+
/// Returns the integer SRID of the specified geometry column by searching through the GEOMETRY_COLUMNS table. If the geometry column has not been properly added (e.g. with the AddGeometryColumn function), this function will not work
169+
/// </summary>
170+
/// <remarks>
171+
/// See https://postgis.net/docs/manual-3.0/Find_SRID.html
172+
/// </remarks>
173+
/// <param name="schemaName">Schema name</param>
174+
/// <param name="tableName">Table name</param>
175+
/// <param name="geomFieldName">Geometry field name</param>
176+
/// <returns>The integer SRID of the specified geometry column</returns>
177+
[Sql.Function("Find_SRID", ServerSideOnly = true)]
178+
public static int? FindSrid(string schemaName, string tableName, string geomFieldName)
179+
{
180+
throw new InvalidOperationException();
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)