Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PagingHelper.SplitSQL does not work when query has comments #675

Open
MarcosLopezC opened this issue Nov 17, 2022 · 0 comments
Open

PagingHelper.SplitSQL does not work when query has comments #675

MarcosLopezC opened this issue Nov 17, 2022 · 0 comments

Comments

@MarcosLopezC
Copy link

PagingHelper.SplitSQL(string, out SQLParts) method doesn't handle queries with comments. Here is a test case to illustrate the issue:

[Test]
public void EnsureSplitSQLHandlesComments()
{
    var sql = @"/* comment */ select a, b, c from example order by a;";

    PagingHelper.SQLParts parts;
    PagingHelper.SplitSQL(sql, out parts);

    Assert.AreEqual("order by a", parts.sqlOrderBy);
    Assert.AreEqual("SELECT COUNT(*) FROM (select a, b, c from example ) npoco_tbl", parts.sqlCount);
}

The root of the problem seems to be the Regex PagingHelper.rxColumns. It doesn't take comments into account:

public static Regex rxColumns = new Regex(@"\A\s*SELECT\s+((?:\((?>\((?<depth>)|\)(?<-depth>)|.?)*(?(depth)(?!))\)|.)*?)(?<!,\s+)\bFROM\b", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled);

One possible solution could be to strip comments before the call to .Match():

var m = rxColumns.Match(sql);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant