-
Notifications
You must be signed in to change notification settings - Fork 41
/
QConv.hs
157 lines (145 loc) · 4 KB
/
QConv.hs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
module Main where
import SQLCompiler ---- Converter
import Algebra
import OptimizeAlgebra
import Design (file2ER)
import Fundep (prRelationInfo,pRelation,prNormalizations)
import Relation2XML (env2document)
import AbsXML
import XPath (execXPath)
import ValidateXML
import MinSQL
import ErrM
import Viewer
import System.IO ( stdout, hFlush) -- , stdin, hGetContents
--import System.Environment ( getArgs, getProgName )
--import System.Exit ( exitFailure, exitSuccess )
import System.Process
--import Data.Char
main = do
putStrLn helpMsg
writeFile "qconv-history.tmp" ""
env <- loop initQEnv
return ()
type QEnv = (SEnv, [AbsXML.Document])
initQEnv = (initSEnv,[])
loop env@(senv, xmls) = do
putStr "qconv=# "
hFlush stdout
s <- getLine
appendFile "qconv-history.tmp" s
case words s of
"q":[] -> return env
"i":file:_ -> do
s <- readFile file
senv' <- runSQLScript senv s
loop (senv', xmls)
"d":file:_ -> do
file2ER False file
loop env
"dw":file:_ -> do
file2ER True file
loop env
"f":file:_ -> do
rs <- readFile file >>= return . lines
case pRelation rs of
Left rel -> putStrLn $ prRelationInfo rel
Right msg -> putStrLn $ msg
loop env
"n":file:_ -> do
rs <- readFile file >>= return . lines
case pRelation rs of
Left (rel@(_,(_,mvds))) -> do
sequence [do
putStrLn hdr
putStrLn output
| (hdr,output) <- prNormalizations rel]
return ()
Right msg -> putStrLn msg
loop env
"h":[] -> do
putStrLn helpMsg
loop env
"a":ws -> do
alg2latex senv (takeWhile (/=';') (unwords ws))
loop env
"xp":ws@(_:_) -> do
execXPath (unwords ws) xmls
loop env
"x":_ -> do
let xml = env2document "QConvData" senv
putStrLn $ printXML xml
loop (senv,xml:xmls)
"ix":file:_ -> do
xml <- getXML file
loop (senv, xml : xmls)
_ -> do
senv' <- runSQLScript senv s
loop (senv',xmls)
runSQLScript :: SEnv -> String -> IO Env
runSQLScript env s = case parseScript s of
Ok c -> transScript env c
Bad s -> putStrLn s >> return env
alg2latex :: SEnv -> String -> IO ()
alg2latex env s = case parseQuery s of
Bad s -> putStrLn s
Ok c -> do
let rel = transQuery c
let orel = pushSelect env rel
let cs = printSQL c
---- let ks = prKeywordRel rel
---- let kos = prKeywordRel orel
let s = prRelLatex rel
let os = prRelLatex orel
---- putStrLn ks
---- if rel /= orel then putStrLn kos else return ()
writeFile mintex $ unlines [
"\\batchmode",
"\\documentclass[12pt]{article}",
"\\begin{document}",
"",
"\\noindent",
"Source:",
"",
"\\begin{verbatim}",
cs,
"\\end{verbatim}",
"",
"",
"\\noindent",
"Original query:",
"",
"\\noindent",
"\\begin{multline}",
s,
"\\end{multline}",
"",
"\\noindent",
"Optimized query:",
"",
"\\noindent",
"\\begin{multline}",
os,
"\\end{multline}",
"",
"\\end{document}"
]
system "pdflatex qconv-latex-tmp.tex > //dev//null"
system $ viewer ++ " qconv-latex-tmp.pdf"
return ()
mintex = "qconv-latex-tmp.tex"
helpMsg = unlines $ [
"Query converter v0.1 (A. Ranta 2015). Commands:",
" <SQL> = run SQL command ",
" a <SQL> = show algebra for sql query",
" i <File> = read SQL, run commands",
" d <File> = read design, show E-R, schema, English",
" dw <File> = read design, show E-R, schema, English, GF lexicon",
" f <File> = read relation, analyse dependencies and keys",
" n <File> = read relation, normalize to BCNF and 4NF",
" ix <File> = read XML file to an XML document",
" x = convert current SQLdatabase to an xml document",
" xp <XPath> = run xpath query, using current XML documents",
" h = help",
" q = quit"
]