mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-17 05:12:13 +08:00
whitespace for auto-SP detection: use unicode spec via regex (#1987)
* add fix and test for #1975 * wrong ticket number; is #1986
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
|
||||
namespace Dapper
|
||||
@@ -103,14 +104,14 @@ namespace Dapper
|
||||
|
||||
static CommandType InferCommandType(string sql)
|
||||
{
|
||||
if (sql is null || sql.IndexOfAny(WhitespaceChars) >= 0) return System.Data.CommandType.Text;
|
||||
if (sql is null || WhitespaceChars.IsMatch(sql)) return System.Data.CommandType.Text;
|
||||
return System.Data.CommandType.StoredProcedure;
|
||||
}
|
||||
}
|
||||
|
||||
// if the sql contains any whitespace character (space/tab/cr/lf): interpret as ad-hoc; but "SomeName" should be treated as a stored-proc
|
||||
// if the sql contains any whitespace character (space/tab/cr/lf/etc - via unicode): interpret as ad-hoc; but "SomeName" should be treated as a stored-proc
|
||||
// (note TableDirect would need to be specified explicitly, but in reality providers don't usually support TableDirect anyway)
|
||||
private static readonly char[] WhitespaceChars = new char[] { ' ', '\t', '\r', '\n' };
|
||||
private static readonly Regex WhitespaceChars = new(@"\s", RegexOptions.Compiled);
|
||||
|
||||
private CommandDefinition(object? parameters) : this()
|
||||
{
|
||||
|
||||
@@ -302,5 +302,20 @@ namespace Dapper.Tests
|
||||
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(" ")]
|
||||
[InlineData("\u00A0")] // nbsp
|
||||
[InlineData("\u202F")] // narrow nbsp
|
||||
[InlineData("\u2000")] // n quad
|
||||
[InlineData("\t")]
|
||||
[InlineData("\r")]
|
||||
[InlineData("\n")]
|
||||
public async Task Issue1986_AutoProc_Whitespace(string space)
|
||||
{
|
||||
var sql = "select!42".Replace("!", space);
|
||||
var result = await connection.QuerySingleAsync<int>(sql);
|
||||
Assert.Equal(42, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user