-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Named Variables? #3
Comments
@ryburn52 C# compiler doesn't provide the names of interpolated variables, so you'd have to provide them as You'd have to make this change in I'm not sure if someone is already passing anonymous objects to the library (since Dapper accepts anonymous objects), in this case it could break something. I don't think this is a priority for now, but I'll leave this ticket open for future TODO. |
Thanks, Something like this
It might seem pointless but i'm having to generate the SQL template to use in the QueryBuilder. So i'm probably not using the builder to it's full potential. I guess what it would look like is something like this
|
Not sure if I understood what you want, but have you checked the You should be able to do something like: |
DapperQueryBuilder is being deprecated and this repo will be archived soon. The replacement library is InterpolatedSql.Dapper. I'll move this issue to that new repo. |
InterpolatedSql.Dapper provides better extensibility support. using InterpolatedSql.Dapper;
// ...
public class MyCustomSqlParameterMapper : SqlParameterMapper
{
public override string CalculateAutoParameterName(InterpolatedSqlParameter parameter, int pos, InterpolatedSql.SqlBuilders.InterpolatedSqlBuilderOptions options)
{
// Dapper requires unique parameter names for each element of the array.
// This is the default implementation:
//return options.AutoGeneratedParameterPrefix +
// (IsEnumerable(parameter.Argument) ? options.ParameterArrayNameSuffix : "") +
// pos.ToString();
// Instead of using "Prefix + Pos" we can just use the format (specified after the interpolated variable)
return parameter.Format! + (base.IsEnumerable(parameter.Argument) ? options.ParameterArrayNameSuffix : "");
}
}
//...
InterpolatedSqlDapperOptions.InterpolatedSqlParameterParser = new MyCustomSqlParameterMapper();
var cn = new SqlConnection(connectionString);
DateTime asOfDate = DateTime.Now;
var query = cn.QueryBuilder($"Select * from Users Where ModifiedDate >= {asOfDate:asOfDate}");
Assert.AreEqual("Select * from Users Where ModifiedDate >= @asOfDate", query.Build().Sql); |
Curious...does the example above basically disable parameter hints like |
Haven't tested, but on a very quick look I think Feel free to test and fix if anything is wrong. |
Tested and added PR. Think it is there if you following pattern of |
Is there any way to name your SQL variables the same as the C# variable name instead of @p0, @p01?
I have a script I'm building up and even though i'm reusing the same variables in multiple parts of the script they appear in the outpoint as different prams, which is hard to verify later. i'm using approval tests to versify the script output.
The text was updated successfully, but these errors were encountered: