Last active
October 20, 2021 11:00
-
-
Save rifatx/bf1151ab51f5a3e5d6c7995bd7c59dfd to your computer and use it in GitHub Desktop.
ANTLR PlSqlParser GetColumns
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 IEnumerable<string> GetColumns(PlSqlParser.Select_statementContext ssc) | |
{ | |
var sl = ssc | |
.select_only_statement() | |
.subquery() | |
.subquery_basic_elements() | |
.query_block() | |
.selected_list(); | |
var slel = sl.select_list_elements(); | |
if (!slel.Any()) | |
{ | |
yield return sl.GetChild<TerminalNodeImpl>(0).GetText(); // SELECT * | |
} | |
foreach (var sle in slel) | |
{ | |
if (sle.column_alias() is { } ca) | |
{ | |
yield return ca.identifier().GetText(); // XXX AS COL_NAME | |
continue; | |
} | |
var ue = sle.expression() | |
.logical_expression() | |
.unary_logical_expression() | |
.multiset_expression() | |
.relational_expression() | |
.compound_expression() | |
.concatenation(0) | |
.model_expression() | |
.unary_expression(); | |
if (ue.atom() is { } a) | |
{ | |
yield return a.expressions() | |
?.expression(0) | |
?.GetText() // DISTINCT(XX) | |
?? a.constant() | |
.quoted_string() | |
.Select(qs => qs.variable_name() | |
.id_expression() | |
.Last() | |
.GetText()).FirstOrDefault(); // CONCAT(X,Y) COL_NAME | |
} | |
else if (ue.case_statement() | |
?.simple_case_statement() | |
?.label_name(0) is { } ln) | |
{ | |
yield return ln.GetText(); // CASE ... WHEN ... THEN ... END COL_NAME | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment