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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
module Database.CDBI.QueryTypes(
SetOp(..),Join(..),innerJoin, crossJoin,
ColumnSingleCollection(..), ColumnTupleCollection,
ColumnTripleCollection, ColumnFourTupleCollection,
ColumnFiveTupleCollection,
sum, avg, count, minV, maxV, none,
caseThen, singleCol, tupleCol, tripleCol, fourCol, fiveCol,
SingleColumnSelect(..), TupleColumnSelect(..),
TripleColumnSelect(..), FourColumnSelect(..),
FiveColumnSelect(..), TableClause(..),
getSingleType, getTupleTypes, getTripleTypes, getFourTupleTypes,
getFiveTupleTypes, getSingleValFunc, getTupleValFuncs, getTripleValFuncs,
getFourTupleValFuncs, getFiveTupleValFuncs,
trLimit, trSpecifier, trSetOp, trSingleSelectQuery, asTable,
trTupleSelectQuery, trJoinPart1, trJoinPart2, trTripleSelectQuery,
trFourTupleSelectQuery, trFiveTupleSelectQuery,
caseResultInt, caseResultFloat, caseResultString, caseResultChar,
caseResultBool)
where
import List(intercalate)
import Time(ClockTime)
import Database.CDBI.Connection
import Database.CDBI.Criteria
import Database.CDBI.Description
data SetOp = Union | Intersect | Except
data Join = Cross
| Inner Constraint
innerJoin :: Constraint -> Join
innerJoin constraint = Inner constraint
crossJoin :: Join
crossJoin = Cross
data TableClause = TC Table Int (Maybe (Join,TableClause))
type CaseVal a = (SQLType , (SQLValue -> a))
data ColumnSingleCollection a = ResultColumnDescription (ColumnDescription a)
Int
String
| Case Condition (CValue, CValue) (CaseVal a)
type ColumnTupleCollection a b = (ColumnSingleCollection a,
ColumnSingleCollection b)
type ColumnTripleCollection a b c = (ColumnSingleCollection a,
ColumnSingleCollection b,
ColumnSingleCollection c)
type ColumnFourTupleCollection a b c d = (ColumnSingleCollection a,
ColumnSingleCollection b,
ColumnSingleCollection c,
ColumnSingleCollection d)
type ColumnFiveTupleCollection a b c d e= (ColumnSingleCollection a,
ColumnSingleCollection b,
ColumnSingleCollection c,
ColumnSingleCollection d,
ColumnSingleCollection e)
type Fun a = (String , ColumnDescription a)
sum :: Specifier -> ColumnDescription _ -> Fun Float
sum spec (ColDesc name _ _ _) =
("Sum( "++ (trSpecifier spec) ,
(ColDesc name SQLTypeFloat (\f -> (SQLFloat f)) getFloatValue))
avg :: Specifier -> ColumnDescription _ -> Fun Float
avg spec (ColDesc name _ _ _) =
("Avg( "++ (trSpecifier spec),
(ColDesc name SQLTypeFloat (\f -> (SQLFloat f)) getFloatValue))
count :: Specifier -> ColumnDescription _ -> Fun Int
count spec (ColDesc name _ _ _) =
("Count( "++ (trSpecifier spec),
(ColDesc name SQLTypeInt (\i -> (SQLInt i)) getIntValue))
minV :: ColumnDescription a -> Fun a
minV cd = ("Min( ", cd)
maxV :: ColumnDescription a -> Fun a
maxV cd = ("Max( ", cd)
none :: ColumnDescription a -> Fun a
none cd = ("(", cd)
caseResultInt :: CaseVal Int
caseResultInt = (SQLTypeInt, getIntValue)
caseResultFloat :: CaseVal Float
caseResultFloat = (SQLTypeFloat, getFloatValue)
caseResultString :: CaseVal String
caseResultString = (SQLTypeString, getStringValue)
caseResultDate :: CaseVal Time.ClockTime
caseResultDate = (SQLTypeDate, getDateValue)
caseResultBool :: CaseVal Bool
caseResultBool = (SQLTypeBool, getBoolValue)
caseResultChar :: CaseVal Char
caseResultChar = (SQLTypeChar, getCharValue)
caseThen :: Condition -> Value a -> Value a -> (CaseVal a)
-> ColumnSingleCollection a
caseThen con val1 val2 cv =
Case con ((toCValue val1), (toCValue val2)) cv
singleCol :: ColumnDescription a -> Int -> (ColumnDescription a -> Fun b)
-> ColumnSingleCollection b
singleCol colDesc alias f = ResultColumnDescription convColDesc alias fun
where (fun, convColDesc) = f colDesc
tupleCol :: ColumnSingleCollection a -> ColumnSingleCollection b
-> ColumnTupleCollection a b
tupleCol col1 col2 = (col1,col2)
tripleCol :: ColumnSingleCollection a
-> ColumnSingleCollection b
-> ColumnSingleCollection c
-> ColumnTripleCollection a b c
tripleCol col1 col2 col3 = (col1, col2, col3)
fourCol :: ColumnSingleCollection a
-> ColumnSingleCollection b
-> ColumnSingleCollection c
-> ColumnSingleCollection d
-> ColumnFourTupleCollection a b c d
fourCol col1 col2 col3 col4 = (col1, col2, col3, col4)
fiveCol :: ColumnSingleCollection a
-> ColumnSingleCollection b
-> ColumnSingleCollection c
-> ColumnSingleCollection d
-> ColumnSingleCollection e
-> ColumnFiveTupleCollection a b c d e
fiveCol col1 col2 col3 col4 col5 = (col1, col2, col3, col4, col5)
data SingleColumnSelect a = SingleCS Specifier
(ColumnSingleCollection a)
TableClause
Criteria
data TupleColumnSelect a b = TupleCS Specifier
(ColumnTupleCollection a b)
TableClause
Criteria
data TripleColumnSelect a b c = TripleCS Specifier
(ColumnTripleCollection a b c)
TableClause
Criteria
data FourColumnSelect a b c d = FourCS Specifier
(ColumnFourTupleCollection a b c d)
TableClause
Criteria
data FiveColumnSelect a b c d e = FiveCS Specifier
(ColumnFiveTupleCollection a b c d e)
TableClause
Criteria
getColumnType :: ColumnSingleCollection _ -> [SQLType]
getColumnType col =
case col of
(ResultColumnDescription (ColDesc _ typ _ _) _ _) -> [typ]
(Case _ (_ , _) (typ,_)) -> [typ]
getColumnValFunc :: ColumnSingleCollection a -> (SQLValue -> a)
getColumnValFunc col =
case col of
(ResultColumnDescription (ColDesc _ _ _ f) _ _) -> f
(Case _ (_, _) (_,f)) -> f
getSingleType :: SingleColumnSelect _ -> [SQLType]
getSingleType (SingleCS _ col _ _) = getColumnType col
getSingleValFunc :: SingleColumnSelect a -> (SQLValue -> a)
getSingleValFunc (SingleCS _ col _ _) = getColumnValFunc col
getTupleTypes :: TupleColumnSelect _ _ -> [SQLType]
getTupleTypes (TupleCS _ (col1,col2) _ _) = getColumnType col1
++ getColumnType col2
getTupleValFuncs :: TupleColumnSelect a b -> ((SQLValue -> a), (SQLValue -> b))
getTupleValFuncs (TupleCS _ (col1,col2) _ _) = (getColumnValFunc col1,
getColumnValFunc col2)
getTripleTypes :: TripleColumnSelect _ _ _ -> [SQLType]
getTripleTypes (TripleCS _ (col1, col2, col3) _ _) =
getColumnType col1 ++ getColumnType col2 ++ getColumnType col3
getTripleValFuncs :: TripleColumnSelect a b c ->
((SQLValue -> a), (SQLValue -> b), (SQLValue -> c))
getTripleValFuncs (TripleCS _ (col1, col2, col3) _ _) =
(getColumnValFunc col1, getColumnValFunc col2, getColumnValFunc col3)
getFourTupleTypes :: FourColumnSelect _ _ _ _ -> [SQLType]
getFourTupleTypes (FourCS _ (col1, col2, col3, col4) _ _) =
getColumnType col1 ++ getColumnType col2 ++
getColumnType col3 ++ getColumnType col4
getFourTupleValFuncs :: FourColumnSelect a b c d ->
((SQLValue -> a), (SQLValue -> b),
(SQLValue -> c), (SQLValue -> d))
getFourTupleValFuncs (FourCS _ (col1, col2, col3, col4) _ _) =
(getColumnValFunc col1, getColumnValFunc col2,
getColumnValFunc col3, getColumnValFunc col4)
getFiveTupleTypes :: FiveColumnSelect a b c d e -> [SQLType]
getFiveTupleTypes (FiveCS _ (col1, col2, col3, col4, col5) _ _) =
getColumnType col1 ++ getColumnType col2 ++
getColumnType col3 ++ getColumnType col4 ++ getColumnType col5
getFiveTupleValFuncs :: FiveColumnSelect a b c d e -> ((SQLValue -> a),
(SQLValue -> b), (SQLValue -> c), (SQLValue -> d), (SQLValue -> e))
getFiveTupleValFuncs (FiveCS _ (col1, col2, col3, col4, col5) _ _) =
(getColumnValFunc col1, getColumnValFunc col2, getColumnValFunc col3,
getColumnValFunc col4, getColumnValFunc col5)
trSingleSelectQuery :: SingleColumnSelect _ -> String
trSingleSelectQuery (SingleCS sp col tabs crit) =
("select "++ trSpecifier sp ++ getResultColumnString col ++" from " ++
(getTableString tabs "") ++ " " ++ trCriteria crit)
trTupleSelectQuery :: TupleColumnSelect _ _ -> String
trTupleSelectQuery (TupleCS sp (col1, col2) tabs crit) =
("select " ++trSpecifier sp ++ getResultColumnString col1 ++ ", "++
getResultColumnString col2 ++" from " ++ (getTableString tabs "") ++
trCriteria crit )
trTripleSelectQuery :: TripleColumnSelect _ _ _ -> String
trTripleSelectQuery (TripleCS sp (col1, col2, col3) tabs crit) =
("select " ++trSpecifier sp ++ getResultColumnString col1 ++ ", "++
getResultColumnString col2 ++ ", "++ getResultColumnString col3 ++
" from " ++ (getTableString tabs "") ++ trCriteria crit )
trFourTupleSelectQuery :: FourColumnSelect _ _ _ _ -> String
trFourTupleSelectQuery (FourCS sp (col1, col2, col3, col4) tabs crit) =
("select " ++trSpecifier sp ++ getResultColumnString col1 ++ ", "++
getResultColumnString col2 ++ ", "++ getResultColumnString col3
++ ", "++ getResultColumnString col4 ++ " from " ++
(getTableString tabs "") ++ trCriteria crit )
trFiveTupleSelectQuery :: FiveColumnSelect _ _ _ _ _ -> String
trFiveTupleSelectQuery (FiveCS sp (col1, col2, col3, col4, col5) tabs crit) =
("select " ++trSpecifier sp ++ getResultColumnString col1 ++ ", "++
getResultColumnString col2 ++", "++ getResultColumnString col3
++", "++ getResultColumnString col4 ++ ", "++ getResultColumnString col5
++" from " ++ (getTableString tabs "") ++ trCriteria crit )
trSetOp :: SetOp -> String
trSetOp Union = " union "
trSetOp Intersect = " intersect "
trSetOp Except = " except "
trLimit :: Maybe Int -> String
trLimit limit = case limit of
Nothing -> ""
Just n -> " Limit "++(show n)
asTable :: Table -> Int -> Table
asTable table n = case n of
0 -> ""
m -> " as '" ++ (show m) ++ table ++ "'"
trJoinPart1 :: Join -> String
trJoinPart1 Cross = " cross join"
trJoinPart1 (Inner _) = " inner join"
trJoinPart2 :: Join -> String
trJoinPart2 Cross = ""
trJoinPart2 (Inner constraint) =
" ON (" ++ (trConstraint constraint)++")"
getResultColumnString:: ColumnSingleCollection a -> String
getResultColumnString (ResultColumnDescription (ColDesc name _ _ _) alias aggr)
= aggr ++ (trColumn name alias) ++ ")"
getResultColumnString (Case con (val1, val2) _ )
= "( case when " ++ trCondition con ++ " then " ++ trValue val1
++ " else " ++ trValue val2 ++ " end)"
getTableString :: TableClause -> String -> String
getTableString (TC tab alias Nothing) join2 =
(" '" ++tab ++ "'" ++ (asTable tab alias)++" "++join2)
getTableString (TC tab alias (Just (join, tc))) join2 =
(" '" ++tab ++ "'" ++ (asTable tab alias)++join2
++(trJoinPart1 join) ++ (getTableString tc (trJoinPart2 join)))
getCharValue :: SQLValue -> Char
getCharValue (SQLChar char) = char
getDateValue :: SQLValue -> Time.ClockTime
getDateValue (SQLDate date) = date
getFloatValue :: SQLValue -> Float
getFloatValue (SQLFloat float) = float
getIntValue :: SQLValue -> Int
getIntValue (SQLInt int) = int
getStringValue :: SQLValue -> String
getStringValue (SQLString str) = str
getBoolValue :: SQLValue -> Bool
getBoolValue (SQLBool bool) = bool
|