Using custom Dapper ITypeHandler for parsing data but cause conflict to handle SQL parameters
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
I use custom Dapper ITypeHandler to automap between C# string[] and mysql Json field. But it causes confict to handle SQL parameters
- table
CREATE TABLE if NOT EXISTS post (
id bigint NOT NULL AUTO_INCREMENT,
approver json NULL COMMENT 'approvers, null or array'
PRIMARY KEY (id)
}
-
mapping class
public class Post
{
public long Id { get; set; }
public string[] Approver { get; set; }
} -
custom Dapper ITypeHandler
` public class JsonTypeHandler : SqlMapper.TypeHandler
{
public override T? Parse(object value)
{
if (value is string json)
{
return JsonSerializer.Deserialize(json, Consts.Genernal.JsonSerializerOptions);
}return default; } public override void SetValue(IDbDataParameter parameter, T? value) { parameter.DbType = DbType.String; parameter.Value = JsonSerializer.Serialize(value, Consts.Genernal.JsonSerializerOptions); }}`
-
register custom typehandler
SqlMapper.AddTypeHandler(new JsonTypeHandler<int[]>()); -
confilct when using IN clause (from hangfire.mysql)
var connectionString = "your mysql connectionstring"; var queues = new string[] { "default"}; using var connection = new MySqlConnection(connectionString); int nUpdated = connection.Execute( $"updateJobQueue set FetchedAt = UTC_TIMESTAMP(), FetchToken = @fetchToken " + "where (FetchedAt is null or FetchedAt < DATE_ADD(UTC_TIMESTAMP(), INTERVAL @timeout SECOND)) " + " and Queue in @queues " + "LIMIT 1;", new { queues = queues, timeout = 60, fetchToken = Guid.NewGuid().ToString() });
the above code throws exception: MySqlConnector.MySqlException (0x80004005): 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 ''["default"]' LIMIT 1' at line 1
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the MySqlConnector Execute call with the registered JsonTypeHandler and the queues parameter, then inspect how Dapper expands the IN clause and applies the handler. Done means the query executes with a valid MySQL IN parameter representation while preserving JSON handling for the mapped Approver property.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, mysql
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100