quaiton89 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

Empty IEnumerable Parameter Breaks on Oracle #51

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This is a very minor issue, but I thought I would report it just in case 
someone else had the same problem. When using an Oracle command, if an 
IEnumerable parameter count is zero, the query fails.

Example Query: SELECT * FROM [Table Name] WHERE [Column Name] IN :ParamName

In the PackListParameters method, if the param list is empty, the following 
line of code is used allow the statement to execute as expected:

command.CommandText = Regex.Replace(command.CommandText, @"[?@:]" + 
Regex.Escape(namePrefix), "(SELECT NULL WHERE 1 = 0)");

"(SELECT NULL WHERE 1 = 0)" does not work on Oracle. My fix was to instead use 
"(SELECT NULL FROM dual WHERE 1 = 0)" for Oracle commands.

Temp Fix:
string temp = "(SELECT NULL WHERE 1 = 0)";

if (command.GetType() == typeof(OracleCommand))
{
    temp = "(SELECT NULL FROM dual WHERE 1 = 0)";
}

command.CommandText = Regex.Replace(command.CommandText, @"[?@:]" + 
Regex.Escape(namePrefix), temp);

Thanks for the great work!

Original issue reported on code.google.com by bran...@brandonzeider.me on 21 Jul 2011 at 8:34

GoogleCodeExporter commented 8 years ago
One reason this is tricky is that we want dapper to work with wrapped commands 
(in particular, MVC Mini Profiler ;p). So checking the command type is not 
reliable. There may be other ways to do this, perhaps....? I wonder if we can 
find something that works in all variants.

Original comment by marc.gravell on 21 Jul 2011 at 8:48

GoogleCodeExporter commented 8 years ago
Gotcha - well there should be some way to figure out the underlying command 
type at runtime...I considered checking the command text format, as Oracle uses 
the semicolon instead of the ampersand for parameters, but that seemed like an 
unreliable solution.

Original comment by bran...@brandonzeider.me on 21 Jul 2011 at 9:46