Skip to content
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

Enhancement: Explicit selection of the datatype for query parameters #1052

Closed
rafek1241 opened this issue Jul 14, 2022 · 2 comments
Closed
Assignees
Labels
breaking-changes A breaking changes (issue, request, enhancement, etc) enhancement New feature or request fixed The bug, issue, incident has been fixed. priority Top priority feature or things to do todo Things to be done in the future

Comments

@rafek1241
Copy link

Describe the enhancement

I would like to have better control over query parameters passed to execute against RAW SQL:

var parameter = new Parameter("@Id", "10045", DBType.AnsiString, false);
var fields = new[]{ new QueryField(parameter) };
var queryGroup = new QueryGroup(fields);

var result = connection.ExecuteQuery("<SOME SQL> WHERE Id = @Id", queryGroup);

/* Produces SQL:
EXEC sp_executesql '...' @id VARCHAR
*/

OR

var result = connection.ExecuteQuery("<SOME SQL> WHERE Id = @Id", new { Id = new Parameter("<value>",  DBType.AnsiString) });

/* Produces SQL:
EXEC sp_executesql '...' @id VARCHAR
*/

OR by using implementation from ADO.NET

var result = connection.ExecuteQuery("<SOME SQL> WHERE Id = @Id", new []{ new  Microsoft.Data.SqlClient.SqlParameter("Id", SqlDbType.VarChar) });

/* Produces SQL:
EXEC sp_executesql '...' @id VARCHAR
*/

Reference: https://repodb.net/class/parameter

Reference to the dapper's solution: https://www.learndapper.com/parameters

var parameters = new DynamicParameters();
var customerId = "ALFKI";
parameters.Add("@CustomerId", customerId, DbType.String, ParameterDirection.Input, customerId.Length);
var sql = "select * from customers where CustomerId = @CustomerId";
using (var connection = new SqlConnection(connString))
{
    var customer = connection.QuerySingle<Customer>(sql, parameters);
}

Context

In the company that I'm working I can use only raw SQLs and I would like to have better control of the passed parameters to the query statement to be able to select datatype. As @mikependon mentioned in previous issue that I made (#1051), RepoDB is probably leveraging the default behavior of ADO.NET for RAW SQL. Because the tables in the database uses VARCHAR instead of NVARCHAR, I've got a problem with slow performance of the queries. Things that should take 5-7 seconds, take 40-50s just because of different datatype..
I mitigate the problem by using:

var @params = new
            {
                Id = id
            };

            TypeMapper.Add<string>(DbType.AnsiString);
            var data = await connection.ExecuteQueryMultipleAsync(query, @params);
            TypeMapper.Remove<string>();

BUT when there will be two parameters of same CLR .NET type with different datatypes in sql - I will be ..... up.

@mikependon
Copy link
Owner

mikependon commented Aug 18, 2022

Referencing #1060

@mikependon mikependon added the fixed The bug, issue, incident has been fixed. label Aug 18, 2022
@mikependon mikependon unpinned this issue Aug 18, 2022
@mikependon
Copy link
Owner

The fixes to this will be available on the next version (> RepoDb v1.10.12).

@mikependon mikependon added the breaking-changes A breaking changes (issue, request, enhancement, etc) label Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-changes A breaking changes (issue, request, enhancement, etc) enhancement New feature or request fixed The bug, issue, incident has been fixed. priority Top priority feature or things to do todo Things to be done in the future
Projects
None yet
Development

No branches or pull requests

2 participants