DapperLib / Dapper

Dapper - a simple object mapper for .Net
https://www.learndapper.com/
Other
17.44k stars 3.67k forks source link

”where in“ parameter splicing problem #1636

Open TeemoHQ opened 3 years ago

TeemoHQ commented 3 years ago

Parameter 'sqlParams111' has already been defined."

demo as follows:

//test_01 and test_11 only contain two fields id and addtime
try
{
    using (var conn = new MySqlConnection(connStr))
    {
        conn.Open();
        var ids1 = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
        var ids2 = new List<int> { 1, 20, 18, 17 };

        var sql = @"
                select addtime from test_01 where id in @sqlParams1
                union all 
                select addtime from test_11 where id in @sqlParams11";

        var res = conn.Query<DateTime>(sql, new { sqlParams1 = ids1, sqlParams11 = ids2 }).ToList();
        res.ForEach(s => Console.WriteLine(s));
        Console.ReadKey();
    }
}
catch (Exception ex)
{
    throw;
}

03

NickCraver commented 3 years ago

This can indeed happen if you conflict with the naming scheme. However, I don't think it's worth complicating query parsing or generating to handle this case (this is the first report I've seen on it). Advice would be: change how parameters are named a bit. I'd wager if parameter names are descriptive, you'd rarely ever see a conflict.

TeemoHQ commented 3 years ago

This can indeed happen if you conflict with the naming scheme. However, I don't think it's worth complicating query parsing or generating to handle this case (this is the first report I've seen on it). Advice would be: change how parameters are named a bit. I'd wager if parameter names are descriptive, you'd rarely ever see a conflict.

Yes, I can solve it myself, I just post the problem.