Last active
October 20, 2021 10:41
-
-
Save rifatx/8e0d0dc2e2e1b6ebbbf154773ca15e84 to your computer and use it in GitHub Desktop.
ANTLR PlSqlParser VisitAssignment_statement override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static readonly Regex _reFrom = new Regex(@"[\n\r\s]+FROM[\n\r\s]+"); | |
public override object VisitAssignment_statement(PlSqlParser.Assignment_statementContext context) | |
{ | |
if (context.general_element().GetText() is var varName && !_assignments.ContainsKey(varName)) | |
{ | |
var selectQuery = context.expression().GetText().Trim('\'').Trim(); | |
if (selectQuery.StartsWith("SELECT", StringComparison.InvariantCultureIgnoreCase) || | |
selectQuery.StartsWith("WITH", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
if (selectQuery.LastIndexOf("WHERE", StringComparison.InvariantCultureIgnoreCase) is var i and >0) | |
{ | |
selectQuery = selectQuery.Substring(0, i) | |
.Replace("''", "'"); | |
} | |
if (!_reFrom.IsMatch(selectQuery)) | |
{ | |
selectQuery = $"{selectQuery} FROM X"; | |
} | |
_assignments.Add(varName, selectQuery); | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment