File tree 2 files changed +9
-1
lines changed
2 files changed +9
-1
lines changed Original file line number Diff line number Diff line change 13
13
import com .google .common .collect .Sets ;
14
14
import org .antlr .v4 .runtime .CharStreams ;
15
15
import org .antlr .v4 .runtime .CommonTokenStream ;
16
+ import org .slf4j .Logger ;
17
+ import org .slf4j .LoggerFactory ;
16
18
17
19
import java .util .Set ;
18
20
19
21
public class SQLExtractor {
20
22
23
+ private static final Logger log = LoggerFactory .getLogger (SQLExtractor .class );
24
+
21
25
public static Set <String > extractTables (String sql ) throws SQLParserException {
22
26
try {
23
27
Set <String > tables = Sets .newLinkedHashSet ();
@@ -67,9 +71,12 @@ public Void visitUpdate_stmt(SQLParser.Update_stmtContext ctx) {
67
71
68
72
@ Override
69
73
public Void visitTable_name (SQLParser .Table_nameContext ctx ) {
70
- tables .add (ctx .getText ());
74
+ log .info ("table {} from context {}" , ctx .getText (), ctx .getParent ().getClass ().getSimpleName ());
75
+ if (ctx .getParent () instanceof SQLParser .Table_or_subqueryContext )
76
+ tables .add (ctx .getText ());
71
77
return super .visitTable_name (ctx );
72
78
}
79
+
73
80
});
74
81
75
82
return tables ;
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ public class SQLExtractorTest {
21
21
@ Test
22
22
public void testExtractTables () throws SQLParserException {
23
23
doExtractTables ("select * from CNSIM1" , "CNSIM1" );
24
+ doExtractTables ("select tab.LAB_GLUC from CNSIM1 as tab" , "CNSIM1" );
24
25
doExtractTables ("select * from CNSIM1 union all select * from CNSIM2" , "CNSIM1 | CNSIM2" );
25
26
doExtractTables ("select count(LAB_HDL) as hdl_count, avg(LAB_HDL) as hdl_avg, GENDER as gender " +
26
27
"from (select * from CNSIM1 union all select * from CNSIM2) " +
You can’t perform that action at this time.
0 commit comments