-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
SqlDetection.cs
256 lines (232 loc) · 9.69 KB
/
SqlDetection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Dapper.CodeAnalysis;
using System.Threading.Tasks;
using Xunit;
namespace Dapper.AOT.Test.Verifiers;
public class SqlDetection : Verifier<DapperAnalyzer>
{
[Fact]
public Task CSViaDapper() => CSVerifyAsync("""
using Dapper;
using System.Data.Common;
[DapperAot(true)]
class SomeCode
{
public void Foo(DbConnection conn)
{
conn.Execute("exec valid");
conn.Execute("{|#0:|}111 invalid");
}
}
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111.")]);
[Fact]
public Task CSVerifyQuestionMarkInQuery_LikePseudoPositional() => CSVerifyAsync("""
using Dapper;
using System.Data.Common;
[DapperAot(true)]
class SomeCode
{
public void Foo(DbConnection conn)
{
_ = conn.Query<int>("select {|#0:'this ?looks? like pseudo-positional'|}"); // line 9
_ = conn.Query<int>("select 'this ? does not look ? like pseudo-positional because of spaces'"); // line 10
_ = conn.Query<int>("select * from Orders where Id = ?id?", new Poco { Id = "1" }); // line 11
_ = conn.Query<int>("select * from Orders where Id = ?id? and Name = ?name?", new Poco { Id = "1", Name = "me" }); // line 12
_ = conn.Query<int>("select 'this ?' + 'does not look like ? pseudo-positional' + 'because only 1 question mark is in every string part ?'"); // line 13
_ = conn.Query<int>(@" // line 14
SELECT * // line 15
FROM Orders // line 16
WHERE Id = ?id? // line 17
AND Name = ?name?", // line 18
new Poco { Id = "1", Name = "me" }); // line 19
}
}
class Poco
{
public string Id { get; set; }
public string Name { get; set; }
}
""", DefaultConfig, [
Diagnostic(DapperAnalyzer.Diagnostics.PseudoPositionalParameter).WithLocation(0)
]);
[Fact]
public Task CSViaProperty() => CSVerifyAsync("""
using System.Data.Common;
using Dapper;
class SomeCode
{
public void Foo(DbCommand cmd)
{
cmd.CommandText = "exec valid";
cmd.CommandText = "{|#0:|}111 invalid";
CommandText = "222 invalid";
Bar = "333 invalid";
Blap = "{|#1:|}444 invalid";
}
public string CommandText {get;set;}
public string Bar {get;set;}
[Sql]
public string Blap {get;set;}
}
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1).WithArguments(46010, "Incorrect syntax near 444.")]);
[Fact]
public Task CSViaParam() => CSVerifyAsync("""
using System.Data.Common;
using Dapper;
class SomeCode
{
static void Foo(string sql) {}
static void Bar(string whatever) {}
static void Blap([Sql] string whatever) {}
public void Usage()
{
Foo("exec valid");
Foo("{|#0:|}111 invalid");
Bar("222 invalid");
Blap("{|#1:|}333 invalid");
}
}
""", DefaultConfig,
[Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0)
.WithArguments(46010, "Incorrect syntax near 111."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1)
.WithArguments(46010, "Incorrect syntax near 333.")]);
[Fact]
public Task VBViaDapper() => VBVerifyAsync("""
Imports Dapper
Imports System.Data.Common
<DapperAot(True)>
Class SomeCode
Public Sub Foo(conn As DbConnection)
conn.Execute("exec valid")
conn.Execute("{|#0:|}111 invalid")
End Sub
End Class
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111.")]);
[Fact]
public Task VBViaProperty() => VBVerifyAsync("""
Imports System.Data.Common
Imports Dapper
Class SomeCode
Public Sub Foo(cmd as DbCommand)
cmd.CommandText = "exec valid"
cmd.CommandText = "{|#0:|}111 invalid"
CommandText = "222 invalid"
Bar = "333 invalid"
Blap = "{|#1:|}444 invalid"
End Sub
Public Property CommandText As String
Public Property Bar As String
<Sql>
Public Property Blap As String
End Class
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1).WithArguments(46010, "Incorrect syntax near 444.")]);
[Fact]
public Task VBViaParam() => VBVerifyAsync("""
Imports System.Data.Common
Imports Dapper
Class SomeCode
Sub Foo(sql as String)
End Sub
Sub Bar(whatever as String)
End Sub
Sub Blap(<Sql> whatever As String)
End Sub
Public Sub Usage()
Foo("exec valid")
Foo("{|#0:|}111 invalid")
Bar("222 invalid")
Blap("{|#1:|}333 invalid")
End Sub
End Class
""", DefaultConfig,
[Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0)
.WithArguments(46010, "Incorrect syntax near 111."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1)
.WithArguments(46010, "Incorrect syntax near 333.")]);
[Fact]
public Task CSharpSmokeTestVanilla() => CSVerifyAsync(""""
using Microsoft.Data.SqlClient;
using Dapper;
static class Program
{
static void Main()
{
using var conn = new SqlConnection("my connection string here");
string name = "abc";
conn.{|#0:Execute|}("""
select Id, Name, Age
from Users
where Id = @id
and Name = @name
and Age = @age
and Something = {|#1:null|}
""", new
{
name,
id = 42,
age = 24,
});
using var cmd = new SqlCommand("should ' verify this too", conn);
cmd.CommandText = """
select Id, Name, Age
from Users
where Id = @id
and Name = @name
and Age = @age
and Something = {|#2:null|}
""";
cmd.ExecuteNonQuery();
}
}
"""", [], [
// (not enabled) Diagnostic(DapperAnalyzer.Diagnostics.DapperAotNotEnabled).WithLocation(0).WithArguments(1),
Diagnostic(DapperAnalyzer.Diagnostics.ExecuteCommandWithQuery).WithLocation(0),
Diagnostic(DapperAnalyzer.Diagnostics.NullLiteralComparison).WithLocation(1),
Diagnostic(DapperAnalyzer.Diagnostics.NullLiteralComparison).WithLocation(2),
], SqlSyntax.General, refDapperAot: false);
[Fact]
public Task VBSmokeTestVanilla() => VBVerifyAsync("""
Imports Dapper
Imports Microsoft.Data.SqlClient
Module Program
Sub Main()
Using conn As New SqlConnection("my connection string here")
Dim name As String = "name"
conn.{|#0:Execute|}("
select Id, Name, Age
from Users
where Id = @id
and Name = @name
and Age = @age
and Something = {|#1:null|}", New With {name, .id = 42, .age = 24 })
Using cmd As New SqlCommand("should ' verify this too", conn)
cmd.CommandText = "
select Id, Name, Age
from Users
where Id = @id
and Name = @name
and Age = @age
and Something = {|#2:null|}"
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
End Module
""", [], [
Diagnostic(DapperAnalyzer.Diagnostics.ExecuteCommandWithQuery).WithLocation(0),
Diagnostic(DapperAnalyzer.Diagnostics.NullLiteralComparison).WithLocation(1),
Diagnostic(DapperAnalyzer.Diagnostics.NullLiteralComparison).WithLocation(2)
], SqlSyntax.General, refDapperAot: false);
}