MySqlDialectProviderBase.DoesColumnExist() fails for special table names

Hello!

There is an issue with the method MySqlDialectProviderBase.DoesColumnExist() (as well as the async version)

    public override bool DoesColumnExist(IDbConnection db, string columnName, string tableName, string schema = null)
    {
        tableName = GetTableName(tableName, schema);
        var sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS"
                  + " WHERE TABLE_NAME = @tableName AND COLUMN_NAME = @columnName AND TABLE_SCHEMA = @schema"
	                  .SqlFmt(GetTableName(tableName, schema).StripDbQuotes(), columnName);
        
        var result = db.SqlScalar<long>(sql, new { tableName, columnName, schema = db.Database });

        return result > 0;
    }

The method GetTableName() in the first line will escape any special MySql names with the quotes and this quoted name will be applied into the SqlScalar() parameter which is wrong, as the table and column names stored in the INFORMATION_SCHEMA.COLUMNS are not escaped.

In our case, we have a signal table, which gets escaped into 'signal'. Because of that, check on the ColumnExists() fails and looks more-less like: TABLE_NAME = "'signal'" but should: TABLE_NAME = "signal"

Did you create your table with OrmLite? What’s the POCO or CREATE TABLE Definition?

Yes, we create all the tables using the OrmLite extensions

public class Signal
{
    [AutoIncrement]
    public int Id { get; set; }
    public string Uid { get; set; }
    public string Barcode { get; set; }
    public string BarId { get; set; }
    public short Code { get; set; }
    public string EndCode { get; set; }
    public bool Ignored { get; set; }
    [ForeignKey(typeof(Machine), OnDelete = "CASCADE", ForeignKeyName = "FK_Signal_Machine_MachineId")]
    public int MachineId { get; set; }
    public string OrderId { get; set; }
    public long OriginalId { get; set; }
    public string Param1 { get; set; }
    public string Param2 { get; set; }
    public string Param3 { get; set; }
    public string Param4 { get; set; }
    public string Param5 { get; set; }
    public string PartId { get; set; }
    public string ProfileId { get; set; }
    public string ProgramId { get; set; }
    public string SpindleId { get; set; }
    public string StationId { get; set; }
    public DateTime Timestamp { get; set; }
    public string ToolId { get; set; }
    public int? IgnoredReason { get; set; }
    public long? ProcessingTime { get; set; }
}

// Then we just call
db.CreateTableIfNotExists<Signal>();

Should be resolved from this commit. This change is available from the latest v5.10.5 that’s now available on MyGet.

Awesome! :+1:

Thanks :heart:

1 Like