-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtestComment.java
More file actions
81 lines (66 loc) · 3.04 KB
/
Copy pathtestComment.java
File metadata and controls
81 lines (66 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package test;
import gudusoft.gsqlparser.*;
import gudusoft.gsqlparser.nodes.TParseTreeVisitor;
import gudusoft.gsqlparser.nodes.TResultColumn;
import junit.framework.TestCase;
public class testComment extends TestCase {
public void test0(){
TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle);
sqlparser.sqltext = "SELECT last_name, -- select the name\n" +
" salary + NVL(commission_pct, 0),-- total compensation\n" +
" job_id, -- job\n" +
" e.department_id -- and department\n" +
" FROM employees e, -- of all employees\n" +
" departments d\n" +
" WHERE e.department_id = d.department_id\n" +
" AND salary + NVL(commission_pct, 0) > -- whose compensation \n" +
" -- is greater than\n" +
" (SELECT salary + NVL(commission_pct,0) -- the compensation\n" +
" FROM employees \n" +
" WHERE last_name = 'Pataballa') -- of Pataballa.";
assertTrue(sqlparser.parse() == 0);
// fetch all comments
for(int i=0;i<sqlparser.getSourcetokenlist().size();i++){
TSourceToken st = sqlparser.getSourcetokenlist().get(i);
if ((st.tokentype == ETokenType.ttsimplecomment)||(st.tokentype == ETokenType.ttbracketedcomment)){
// System.out.println(st.toString());
}
}
for(int i=0;i<sqlparser.sqlstatements.size();i++){
TCustomSqlStatement sqlStatement = sqlparser.sqlstatements.get(i);
analyzeStmt(sqlStatement);
}
}
void analyzeStmt( TCustomSqlStatement stmt ){
// System.out.println(stmt.sqlstatementtype);
selectItemVisitor itemVisitor = new selectItemVisitor();
stmt.acceptChildren(itemVisitor);
for ( int i = 0; i < stmt.getStatements( ).size( ); i++ )
{
analyzeStmt( stmt.getStatements( ).get( i ) );
}
}
}
class selectItemVisitor extends TParseTreeVisitor {
public void preVisit(TResultColumn node){
// System.out.println("--> select item: " + node.toString());
TSourceToken endToken = node.getEndToken();
TSourceToken commentToken = searchComment(endToken);
if (commentToken != null){
// System.out.println("comment: "+commentToken.toString());
}
}
TSourceToken searchComment(TSourceToken currentToken){
// check next solid token to see whether it is a comment
TSourceToken resultToken = null;
TSourceToken st = currentToken.nextSolidToken(true);
if (st != null){
if ((st.tokentype == ETokenType.ttsimplecomment)||(st.tokentype == ETokenType.ttbracketedcomment)){
resultToken = st;
}else if (st.tokencode == ','){
resultToken = searchComment(st);
}
}
return resultToken;
}
}