DapperLib / DapperLib/Dapper

IN clause is generated with syntax errors when generated variables conflict.

Open
#1,040 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

Dapper version: 1.50.5
Environment: .NET Framework 4.6.1

The syntax in use is MySQL but that shouldn't matter. I believe this issue will break with any DB driver.

Replication code:

var parameters = new Dictionary<string, object>
{
    {"values1", Enumerable.Range(1, 2)},
    {"values11", Enumerable.Range(1, 2)},
};

var expando = new ExpandoObject() as IDictionary<string, object>;
parameters.ToList().ForEach(kvp => expando[kvp.Key] = kvp.Value);

_connection.Execute("CREATE TABLE IF NOT EXISTS `X` (`Value` VARCHAR(255));");
_connection.Query("SELECT * FROM X WHERE Value IN @values1", expando);

The query received by the IDbCommand looks like this:

SELECT * FROM X WHERE Value IN ((@values111,@values112),@values12)

The issue appears to be due to the order of precedence that the IN clause replacement happens in. If values11 appears before values1, the replacement occurs fine. I believe that the correct strategy is to first mark all replacements and then perform the replacement, as opposed to replacing all occurrences of values1 and then all occurrences of values11.

I can fix this via PR if required in a couple of weeks but I thought I'd make sure I wasn't doing something silly or if a fix was already in the works that I couldn't find via Google.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied C# reproduction and inspect the parameter expansion that turns the IN clause into the shown IDbCommand SQL. Reproduce the collision with values1 and values11, then verify that the resulting query executes correctly and that similarly prefixed parameter names remain distinct.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, mysql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.