Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cdadfa6

Browse files
authoredOct 19, 2023
whitespace for auto-SP detection: use unicode spec via regex (#1987)
* add fix and test for #1975 * wrong ticket number; is #1986
1 parent b446241 commit cdadfa6

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed
 

‎Dapper/CommandDefinition.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Data;
33
using System.Reflection;
44
using System.Reflection.Emit;
5+
using System.Text.RegularExpressions;
56
using System.Threading;
67

78
namespace Dapper
@@ -103,14 +104,14 @@ public CommandDefinition(string commandText, object? parameters = null, IDbTrans
103104

104105
static CommandType InferCommandType(string sql)
105106
{
106-
if (sql is null || sql.IndexOfAny(WhitespaceChars) >= 0) return System.Data.CommandType.Text;
107+
if (sql is null || WhitespaceChars.IsMatch(sql)) return System.Data.CommandType.Text;
107108
return System.Data.CommandType.StoredProcedure;
108109
}
109110
}
110111

111-
// if the sql contains any whitespace character (space/tab/cr/lf): interpret as ad-hoc; but "SomeName" should be treated as a stored-proc
112+
// 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
112113
// (note TableDirect would need to be specified explicitly, but in reality providers don't usually support TableDirect anyway)
113-
private static readonly char[] WhitespaceChars = new char[] { ' ', '\t', '\r', '\n' };
114+
private static readonly Regex WhitespaceChars = new(@"\s", RegexOptions.Compiled);
114115

115116
private CommandDefinition(object? parameters) : this()
116117
{

‎tests/Dapper.Tests/ProcedureTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,20 @@ select 1 as Num
302302

303303
Assert.Empty(result);
304304
}
305+
306+
[Theory]
307+
[InlineData(" ")]
308+
[InlineData("\u00A0")] // nbsp
309+
[InlineData("\u202F")] // narrow nbsp
310+
[InlineData("\u2000")] // n quad
311+
[InlineData("\t")]
312+
[InlineData("\r")]
313+
[InlineData("\n")]
314+
public async Task Issue1986_AutoProc_Whitespace(string space)
315+
{
316+
var sql = "select!42".Replace("!", space);
317+
var result = await connection.QuerySingleAsync<int>(sql);
318+
Assert.Equal(42, result);
319+
}
305320
}
306321
}

0 commit comments

Comments
 (0)
Please sign in to comment.