Skip to content

Instantly share code, notes, and snippets.

@rifatx
Last active October 20, 2021 10:41
Show Gist options
  • Save rifatx/8e0d0dc2e2e1b6ebbbf154773ca15e84 to your computer and use it in GitHub Desktop.
Save rifatx/8e0d0dc2e2e1b6ebbbf154773ca15e84 to your computer and use it in GitHub Desktop.
ANTLR PlSqlParser VisitAssignment_statement override
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