How to parse an SQL query in Java? -


if have text file called file.txt contains data. instance:

55 90  10 45 33 23 10 500 5  2 

where first column called column c1 , second c2.

and have file called input.txt 2 sql queries:

select * file  c2 > 60;   select c1  file; 

what 1 way parse file , produce input looks real dbms?

i've tried far:

// 1. read file.   main obj = new main(); url url = obj.getclass().getresource("file.txt"); file file = new file(url.touri()); filereader filereader = new filereader(file); bufferedreader bufferreader = new bufferedreader(filereader); stringbuffer stringbuffer = new stringbuffer(); string line; while ((line = bufferreader.readline()) != null) {     stringbuffer.append(line);     stringbuffer.append("\n"); } filereader.close(); string data = stringbuffer.tostring(); //this contains data file.text string[] list = data.split(" "); //this stores list  // 2. read input file.  main input = new main(); url urlinput = input.getclass().getresource("input.txt"); file inputfile = new file(urlinput.touri()); filereader filereaderinput = new filereader(inputfile); bufferedreader bufferedreaderinput = new bufferedreader(filereaderinput); stringbuffer stringbufferinput = new stringbuffer(); string lineinput; while ((lineinput = bufferedreaderinput.readline()) != null) {     stringbufferinput.append(lineinput);     stringbufferinput.append("\n"); }  

but lost here... don't know how parse query. program manages read both files, when comes processing query in input file, can't seem figure out logic it.

first suggest representing data collection of rows. that's how dbms treats data , make other logic easier. can create own object type store values of c1 , c2. loop through data file , create collection of rows (maybe list<row>)

now "parse" sql. you'll need tokenize sql actual pieces you'll use logic later. use built in java string splitting functions actual clauses of query.

i think of getting specific rows (as determined where clauses) first. can worry actual data each row return select.

i'm assuming from clause won't change since have 1 data file. if did you'd use clause did choose actual data source (file name maybe?)

for sql without where clause, of rows valid , can return entire collection of rows. otherwise you'll need figure out how turn text after clause java interpretable predicate (you might want search part separately since it's entirely separate problem , outside scope of answer). loop through data rows , return each row passes predicate.

the select statement determines column(s) include. use logic string.contains check column names included. * should select columns. since have collection of valid rows loop through them , data need each row. example concatenate valid data (as determined string.contains) long string terminated new line character.

that should work requirement. sorry not include actual code outline should help.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -