Querying for empty list fails with MySQL Server 5.6 #565
Description
MySQL Server 5.6.29
Dapper 1.50.1
MySql.Data 6.9.9
Execute the following code (query adapted from https://github.com/StackExchange/dapper-dot-net#list-support):
var csb = new MySqlConnectionStringBuilder { Server = "localhost", UserID = "root", Password = "..." };
using (var connection = new MySqlConnection(csb.ConnectionString))
{
connection.Open();
connection.Query<int>("select * from (select 1 as Id) as X where Id in @Ids", new { Ids = new int[0] }).ToList();
}
It throws a MySqlException: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 1 = 0)' at line 1".
For this statement, Dapper sets the CommandText to select * from (select 1 as Id) as X where Id in (SELECT @Ids WHERE 1 = 0)
The MySql.Data connector interpolates the command parameters and rewrites this as select * from (select 1 as Id) as X where Id in (SELECT NULL WHERE 1 = 0)
before sending it to MySQL Server.
MySQL Server 5.6 rejects this as invalid SQL syntax.
Upgrading to MySQL Server 5.7.11 fixes the exception, but may not always be practical. For MySQL Server 5.6, the following query would be valid (and selects 0 rows): EDIT: deleted this suggested workaround because—although it selects zero rows—the inverse query select * from (select 1 as Id) as X where Id in (SELECT NULL)
.WHERE Id NOT IN (SELECT NULL)
also selects zero rows when the intent is to select all rows.