Skip to content

Commit

Permalink
final touch
Browse files Browse the repository at this point in the history
  • Loading branch information
DeagleGross committed Jun 9, 2024
1 parent 56e8e2a commit 6529ca7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@

namespace Dapper.SqlAnalysis;

internal readonly struct TSqlSpecialScenariosProcessingContext
internal readonly struct SqlProcessingContext
{
private readonly TSqlProcessor.VariableTrackingVisitor _variableVisitor;
private readonly IReadOnlyDictionary<int, string> _pseudoPositionalArgumentsOffsetNames;

private readonly IDictionary<int, string> _pseudoPositionalArgumentsOffsetNames;

public TSqlSpecialScenariosProcessingContext(TSqlProcessor.VariableTrackingVisitor variableVisitor, string sql)
public SqlProcessingContext(TSqlProcessor.VariableTrackingVisitor variableVisitor, string sql)
{
_variableVisitor = variableVisitor;

var regexMatch = CompiledRegex.PseudoPositional.Match(sql);
if (!regexMatch!.Success)
{
_pseudoPositionalArgumentsOffsetNames = new Dictionary<int, string>();
}

_pseudoPositionalArgumentsOffsetNames = new Dictionary<int, string>();
foreach (var match in regexMatch.Groups.OfType<Match>())
_pseudoPositionalArgumentsOffsetNames = BuildPseudoPositionalArgsOffsetNamesMap();

IReadOnlyDictionary<int, string> BuildPseudoPositionalArgsOffsetNamesMap()
{
_pseudoPositionalArgumentsOffsetNames.Add(match.Index, match.Value.Trim('?'));
var offsetNamesMap = new Dictionary<int, string>();
var regexMatch = CompiledRegex.PseudoPositional.Match(sql);
while (regexMatch.Success)
{
foreach (var match in regexMatch.Groups.OfType<Match>())
{
offsetNamesMap.Add(match.Index, match.Value.Trim('?'));
}

regexMatch = regexMatch.NextMatch();
}

return offsetNamesMap;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/Dapper.AOT.Analyzers/SqlAnalysis/TSqlProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ public virtual bool Execute(string sql, ImmutableArray<SqlParameter> members = d
{
Flags |= SqlParseOutputFlags.SqlAdjustedForDapperSyntax;
}
var sqlSpecialScenariosProcessingContext = new TSqlSpecialScenariosProcessingContext(_visitor, fixedSql);

var sqlProcessingContext = new SqlProcessingContext(_visitor, fixedSql);

var memberNamesSet = new HashSet<string>(members.Select(x => x.Name), CaseSensitive ? StringComparer.InvariantCulture : StringComparer.InvariantCultureIgnoreCase);
sqlSpecialScenariosProcessingContext.MarkPseudoPositionalVariablesUsed(memberNamesSet);
sqlProcessingContext.MarkPseudoPositionalVariablesUsed(memberNamesSet);

var parser = new TSql160Parser(true, SqlEngineType.All);
TSqlFragment tree;
Expand All @@ -98,7 +99,7 @@ public virtual bool Execute(string sql, ImmutableArray<SqlParameter> members = d
tree = parser.Parse(reader, out var errors);
if (errors is not null && errors.Count != 0)
{
foreach (var error in sqlSpecialScenariosProcessingContext.GetErrorsToReport(errors))
foreach (var error in sqlProcessingContext.GetErrorsToReport(errors))
{
Flags |= SqlParseOutputFlags.SyntaxError;
OnParseError(error, new Location(error.Line, error.Column, error.Offset, 0));
Expand Down
20 changes: 10 additions & 10 deletions test/Dapper.AOT.Test/Verifiers/SqlDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ 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 {|#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
_ = 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
}
}

Expand Down

0 comments on commit 6529ca7

Please sign in to comment.