java - FoundationDB SQL Parser to get WHERE clause -
java - FoundationDB SQL Parser to get WHERE clause -
i'm using foundationdb sql parser (https://github.com/foundationdb/sql-parser) parse query within java, i'm not familiar visitor design pattern, used parser consume query. send query parser this: "select a, b c d > 5" , result:
all fields names in select clause (accomplished) the table name from clause (accomplished) the columns names, operand , look where clausethats code i'm implementing:
@override public querydescription parse() throws standardexception { sqlparser parser = new sqlparser(); statementnode stmt = parser.parsestatement(sql); visitor v = new visitor() { @override public boolean visitchildrenfirst(visitable arg0) { // todo auto-generated method stub homecoming false; } @override public visitable visit(visitable arg0) throws standardexception { // temporary stores querydescription parameters statementenum se = null; string fromtable = null; string[] fields = null; if(arg0 instanceof cursornode) { cursornode cn = (cursornode) arg0; // print out statement been declared in sql query system.out.println("statement: " + cn.statementtostring()); // temporarly stores statement string statement = cn.statementtostring(); // creates right statementenum if(statement == "create table") { se = statementenum.create_table; } else if(statement == "insert") { se = statementenum.insert; } else if(statement == "select") { se = statementenum.select; } else if(statement == "drop table") { se = statementenum.drop_table; } } description = new querydescription(se, fromtable, fields); homecoming arg0; } @override public boolean stoptraversal() { homecoming false; } @override public boolean skipchildren(visitable arg0) throws standardexception { homecoming false; } }; stmt.accept(v); // todo remove, debug purpose stmt.treeprint(); homecoming description; }
and that's querydescription class code:
public class querydescription { /* fellow member variables: */ private querytypeenum querytype; private statementenum statement; private string fromtable; private string[] fields; /* constructors: */ /** * * @param statement * @param fromtable * @param fields */ public querydescription(statementenum statement, string fromtable, string[] fields) { this.statement = statement; this.fromtable = fromtable; this.fields = fields; } /* methods: */ /** * analyze type of query 1 passed parameter , assigns right querytypeenum */ public void assigntype() { switch(statement) { case create_table: break; case select: if(fields[0] == "allfields") querytype = querytypeenum.dump; else { // todo risolvere questione del querytype = querytypeenum.select_from; } break; case update: break; case insert: break; case drop_table: break; } } /* getters , setter: */ /** * * @return querytype */ public querytypeenum getquerytype() { homecoming querytype; } /** * * @return statement */ public statementenum getstatement() { homecoming statement; } /** * * @return table */ public string getfromtable() { homecoming fromtable; } /** * * @return fields */ public string[] getfields() { homecoming fields; } }
your code not show querydescription
class does, can guess.
in dealing clause, looking 3 types of nodes:
binarylogicaloperatornode
- has and, or, operators separate individual clauses in clause. binaryoperatornode
- has individual >, <, , other operations. the constants , column operators. in example, visit binaryoperatornode
, type of lt
, , 2 children constantnode
value of 5, , columnreference
node value of "d".
note: parser not connect column references tables in tables list. separate step in query processing. have columnreference
node, no link table column referencing. reason parser not have plenty info correctly link "d" column right table.
this plenty process simple query gave in example. queries can become much more complex.
the 1 node add together list of checks in inlistoperatornode
handles where d in (1,2,3,4)
.
edit add:
keep in mind vistor#visit()
method gets called each , every node in tree created query parser. visit method need check, , set variables properly, frombasetable
, constantnode
, columnreference
.
visitor v = new visitor() { list<string> fromtable = new arraylist<string>(); list<string> fields = new arraylist<string>(); // other visitor methods go here, not copied conciseness. @override public visitable visit(visitable arg0) throws standardexception { // other code visit() method goes here // if (arg0 instanceof frombasetable) { frombasetable table = (frombasetable)arg0; fromtable.append(table.gettablename()); } else if (arg0 instanceof columnreference) { columnreference column = (columnreference) arg0; fields.append(column.getcolumnname()) } // remove phone call create querydescription } public querydescription getquerydescription() { homecoming new querydescription(se, fromtable, fields) } }
then in main line of code call:
stmt.accept(v); querydescription description = v.getquerydescription();
now if have other parts of query interested in, need add together nodes visit method, , capture part of node (names, values, etc) interested in.
java sql eclipse parsing foundationdb
Comments
Post a Comment