Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 87563f7

Browse files
committed
use exact match when table/column/schema is reserved word
1 parent 159c88e commit 87563f7

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/ServiceStack.OrmLite.PostgreSQL.Tests/NormalizeTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public void Can_create_and_populate_tables_without_quotes()
5656

5757
var dbCustomer = db.SingleById<Customer>(customer.Id);
5858
Assert.That(dbCustomer.Name, Is.EqualTo(customer.Name));
59+
dbCustomer = db.SqlList<Customer>("select * from Customer where Id = @Id", new { customer.Id })[0];
60+
Assert.That(dbCustomer.Name, Is.EqualTo(customer.Name));
5961

6062
var address = db.Single<CustomerAddress>(x => x.CustomerId == customer.Id && x.Id == customer.PrimaryAddress.Id);
6163
Assert.That(address.Country, Is.EqualTo("Australia"));

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public bool Normalize
6868
get => normalize;
6969
set
7070
{
71-
normalize = !value;
72-
NamingStrategy = value
71+
normalize = value;
72+
NamingStrategy = normalize
7373
? new OrmLiteNamingStrategyBase()
7474
: new PostgreSqlNamingStrategy();
7575
}
@@ -260,7 +260,7 @@ public override IDbDataParameter CreateParam()
260260

261261
public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string schema = null)
262262
{
263-
var sql = !Normalize
263+
var sql = !Normalize || ReservedWords.Contains(tableName)
264264
? "SELECT COUNT(*) FROM pg_class WHERE relname = {0}".SqlFmt(tableName)
265265
: "SELECT COUNT(*) FROM pg_class WHERE lower(relname) = {0}".SqlFmt(tableName.ToLower());
266266

@@ -274,7 +274,7 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string s
274274
// If a search path (schema) is specified, and there is only one, then assume the CREATE TABLE directive should apply to that schema.
275275
if (!string.IsNullOrEmpty(schema) && !schema.Contains(","))
276276
{
277-
sql = !Normalize
277+
sql = !Normalize || ReservedWords.Contains(schema)
278278
? "SELECT COUNT(*) FROM pg_class JOIN pg_catalog.pg_namespace n ON n.oid = pg_class.relnamespace WHERE relname = {0} AND nspname = {1}".SqlFmt(tableName, schema)
279279
: "SELECT COUNT(*) FROM pg_class JOIN pg_catalog.pg_namespace n ON n.oid = pg_class.relnamespace WHERE lower(relname) = {0} AND lower(nspname) = {1}".SqlFmt(tableName.ToLower(), schema.ToLower());
280280
}
@@ -287,13 +287,17 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName, string s
287287

288288
public override bool DoesColumnExist(IDbConnection db, string columnName, string tableName, string schema = null)
289289
{
290-
var sql = !Normalize
291-
? "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName AND COLUMN_NAME = @columnName".SqlFmt(tableName, columnName)
292-
: "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE lower(TABLE_NAME) = @tableName AND lower(COLUMN_NAME) = @columnName".SqlFmt(tableName.ToLower(), columnName.ToLower());
290+
var sql = !Normalize || ReservedWords.Contains(tableName)
291+
? "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName".SqlFmt(tableName)
292+
: "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE lower(TABLE_NAME) = @tableName".SqlFmt(tableName.ToLower());
293+
294+
sql += !Normalize || ReservedWords.Contains(columnName)
295+
? " AND COLUMN_NAME = @columnName".SqlFmt(columnName)
296+
: " AND lower(COLUMN_NAME) = @columnName".SqlFmt(columnName.ToLower());
293297

294298
if (schema != null)
295299
{
296-
sql += !Normalize
300+
sql += !Normalize || ReservedWords.Contains(schema)
297301
? " AND TABLE_SCHEMA = @schema"
298302
: " AND lower(TABLE_SCHEMA) = @schema";
299303

0 commit comments

Comments
 (0)