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

Commit 92488c4

Browse files
committed
Update MySql DoesColumnExist to handle quoted tables
1 parent 8eda13b commit 92488c4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/ServiceStack.OrmLite.MySql/MySqlDialectProviderBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public override async Task<bool> DoesTableExistAsync(IDbCommand dbCmd, string ta
437437

438438
public override bool DoesColumnExist(IDbConnection db, string columnName, string tableName, string schema = null)
439439
{
440-
tableName = GetTableName(tableName, schema);
440+
tableName = GetTableName(tableName, schema).StripQuotes();
441441
var sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS"
442442
+ " WHERE TABLE_NAME = @tableName AND COLUMN_NAME = @columnName AND TABLE_SCHEMA = @schema"
443443
.SqlFmt(GetTableName(tableName, schema).StripDbQuotes(), columnName);
@@ -449,7 +449,7 @@ public override bool DoesColumnExist(IDbConnection db, string columnName, string
449449

450450
public override async Task<bool> DoesColumnExistAsync(IDbConnection db, string columnName, string tableName, string schema = null, CancellationToken token=default)
451451
{
452-
tableName = GetTableName(tableName, schema);
452+
tableName = GetTableName(tableName, schema).StripQuotes();
453453
var sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS"
454454
+ " WHERE TABLE_NAME = @tableName AND COLUMN_NAME = @columnName AND TABLE_SCHEMA = @schema"
455455
.SqlFmt(GetTableName(tableName, schema).StripDbQuotes(), columnName);

tests/ServiceStack.OrmLite.MySql.Tests/OrmLiteCreateTableTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,25 @@ public void Can_create_ModelWithIdAndName_table_with_specified_DefaultStringLeng
140140
Console.WriteLine("createTableSql: " + createTableSql);
141141
Assert.That(createTableSql.Contains("VARCHAR(255)"), Is.True);
142142
}
143+
144+
public class Signal
145+
{
146+
[AutoIncrement]
147+
public int Id { get; set; }
148+
public short Code { get; set; }
149+
}
150+
151+
[Test]
152+
public void Does_DoesColumnExist()
153+
{
154+
using var db = OpenDbConnection();
155+
db.DropTable<Signal>();
156+
var exists = db.ColumnExists<Signal>(x => x.Code);
157+
Assert.That(exists, Is.False);
158+
159+
db.CreateTable<Signal>();
160+
exists = db.ColumnExists<Signal>(x => x.Code);
161+
Assert.That(exists);
162+
}
143163
}
144164
}

0 commit comments

Comments
 (0)