From f9d48583833f742b190e7114302f6210971b208e Mon Sep 17 00:00:00 2001 From: LtWorf Date: Thu, 20 Aug 2009 16:10:55 +0000 Subject: [PATCH] added code for selection into sql.py git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@212 014f5005-505e-4b48-8d0a-63407b615a7c --- CHANGELOG | 1 + relational/sql.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index fd6e6a3..fcf1444 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -92,4 +92,5 @@ 0.11 - Font is set only on windows (Rev 206) - Improved futile_union_intersection_subtraction in case of A-A, when A is a sub-query (Rev 208) +- Improved futile_union_intersection_subtraction, handles when a branch of subtracion has a selection (Rev 209) - Can load relations specified in command line (Rev 210) \ No newline at end of file diff --git a/relational/sql.py b/relational/sql.py index f5fd31a..8e9c1b8 100644 --- a/relational/sql.py +++ b/relational/sql.py @@ -22,5 +22,59 @@ def stub(): "NATURAL JOIN" "CROSS JOIN" , + +def sql2relational(query): + query=query.replace('\n',' ').replace(',',' , ') - \ No newline at end of file + t=query.split(' ') + escape=False + for i in range(len(t)): + tok=t[i].lower().strip() + if tok=='from' and not escape: + break + + if tok in ('select','as',','): + escape=True + else: + escape=False + + return extract_select(t[1:i]) + +def extract_select(query,internal=''): + + + #Handling select * + if len(query)==1 and query[0]=='*': + return '' + #Create dictionary for projection and rename . + #Keys are fields to project. Value is none if no rename is needed + pr_dic={} + key=None + for i in query: + if i.lower() ==',': + key=None + elif key==None: + pr_dic[i]=None + key=i + else: + pr_dic[key]=i + + #Preparing string for projection and rename + ren='' + proj='' + for i in pr_dic.iterkeys(): + proj+=',%s'%i + if pr_dic[i]!=None: + ren+=',%s➡%s'%(i,pr_dic[i]) + #Removes starting commas + ren=ren[1:] + proj=proj[1:] + + result='π %s (%s)' % (proj,internal) + if len(ren)!=0: + result='ρ %s (%s)' % (ren,result) + return result +def extract_from(query): + return query +if __name__=="__main__": + print sql2relational('SELECT a,c AS q FROM from;') \ No newline at end of file