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
--- --------------------------------------------------------------
--- This module contains datatype declarations, constructor functions
--- selectors and translation functions for complex select queries  
--- in particular for those selecting (1 to 5) single columns.
--- @author Julia Krone
---
--- @version 0.1
--- @category database
--- ----------------------------------------------------------------

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

---datatype for set operations
data SetOp = Union | Intersect | Except

--- datatype for joins
data  Join =  Cross
           | Inner Constraint


--- Constructorfunction for an inner join
---@param constraint
innerJoin :: Constraint -> Join
innerJoin constraint = Inner constraint

--- Constructorfunction for cross join
crossJoin :: Join
crossJoin = Cross

--- data structure to represent a table-clause (tables and joins)
--- in a way that at least one table has to be specified
data TableClause = TC Table Int (Maybe (Join,TableClause))

-- Data type to specify the result type of case expressions and its conversion.
type CaseVal a = (SQLType , (SQLValue -> a))

--- Datatype representing a single column in a select-clause. Can be just a 
--- column connected with an alias and an optional aggregation function(String)
--- or a Case-when-then-statement
data ColumnSingleCollection a = ResultColumnDescription (ColumnDescription a)
                                                        Int
                                                        String
                              | Case Condition (CValue, CValue) (CaseVal a)

--- Datatype to select two different columns which can be of different types 
--- and from different tables.
type ColumnTupleCollection a b = (ColumnSingleCollection a,
                                 ColumnSingleCollection b)

--- Datatype to select three different columns which can be of different types 
--- and from different tables.
type ColumnTripleCollection a b c = (ColumnSingleCollection a,
                                     ColumnSingleCollection b,
                                     ColumnSingleCollection c)

--- Datatype to select four different columns which can be of different types 
--- and from different tables.
type ColumnFourTupleCollection a b c d = (ColumnSingleCollection a,
                                          ColumnSingleCollection b,
                                          ColumnSingleCollection c,
                                          ColumnSingleCollection d)

--- Datatype to select five different columns which can be of different types 
--- and from different tables.
type ColumnFiveTupleCollection a b c d e= (ColumnSingleCollection a,
                                           ColumnSingleCollection b,
                                           ColumnSingleCollection c,
                                           ColumnSingleCollection d,
                                           ColumnSingleCollection e)

-- Data type to represent an aggregation function in a select-clause.                                           
type Fun a = (String , ColumnDescription a)

--- Constructor for aggregation function sum
--- in select-clauses.
--- A pseudo-ResultColumnDescription of type 
--- float is created for correct return type.
sum :: Specifier -> ColumnDescription _ -> Fun Float
sum spec (ColDesc name _ _ _) =
  ("Sum( "++ (trSpecifier spec) ,
  (ColDesc name SQLTypeFloat (\f -> (SQLFloat f)) getFloatValue))

--- Constructor for aggregation function avg 
--- in select-clauses.
--- A pseudo-ResultColumnDescription of type
--- float is created for correct return type.
avg :: Specifier -> ColumnDescription _ -> Fun Float
avg spec (ColDesc name _ _ _) =
  ("Avg( "++ (trSpecifier spec),
  (ColDesc name SQLTypeFloat (\f -> (SQLFloat f)) getFloatValue))

--- Constructor for aggregation function count 
--- in select-clauses.
--- A pseudo-ResultColumnDescription of type 
--- float is created for correct return type.
count :: Specifier -> ColumnDescription _ -> Fun Int
count spec (ColDesc name _ _ _) =
  ("Count( "++ (trSpecifier spec),
  (ColDesc name SQLTypeInt (\i -> (SQLInt i)) getIntValue))

--- Constructor for aggregation function min in select-clauses.
minV :: ColumnDescription a -> Fun a
minV cd = ("Min( ", cd)

--- Constructor for aggregation function max in select-clauses.
maxV :: ColumnDescription a -> Fun a
maxV cd = ("Max( ", cd)

--- Constructor function in case no aggregation function is specified.
none :: ColumnDescription a -> Fun a
none cd = ("(", cd)

---Constructor for CaseVal of type Int 
---expecting result of type Int in case-expression
caseResultInt :: CaseVal Int
caseResultInt = (SQLTypeInt, getIntValue)

---Constructor for CaseVal of type Float
---expecting result of type Float in case-expression
caseResultFloat :: CaseVal Float
caseResultFloat = (SQLTypeFloat, getFloatValue)

---Constructor for CaseVal of type String
---expecting result of type String in case-expression
caseResultString :: CaseVal String
caseResultString = (SQLTypeString, getStringValue)

---Constructor for CaseVal of type Date 
---expecting result of type Date in case-expression
caseResultDate :: CaseVal Time.ClockTime
caseResultDate = (SQLTypeDate, getDateValue)

---Constructor for CaseVal of type Bool 
---expecting result of type Bool in case-expression
caseResultBool :: CaseVal Bool
caseResultBool = (SQLTypeBool, getBoolValue)

---Constructor for CaseVal of type Char 
---expecting result of type Char in case-expression
caseResultChar :: CaseVal Char
caseResultChar = (SQLTypeChar, getCharValue)

--- Constructor function for representation of statement: 
--- CASE WHEN condition THEN val1 ELSE val2 END.
--- It does only work for the same type in then and
--- else branch.
---@param con - the condition
---@param val1 - value for then-branch
---@param val2 - value for else-branch
---@param cv - data providing SQLType and conversion function                           
caseThen :: Condition -> Value a -> Value a -> (CaseVal a)
                        -> ColumnSingleCollection a
caseThen con val1 val2 cv =
  Case con ((toCValue val1), (toCValue val2)) cv

--- Constructorfunction for ColumnSingleCollection.
---@param coldecs - ColumnDescription of column to select
---@param alias - alias of the table
---@param f - aggregation function (constructor)
singleCol :: ColumnDescription a -> Int -> (ColumnDescription a -> Fun b)
                                        -> ColumnSingleCollection b
singleCol colDesc alias f = ResultColumnDescription convColDesc alias fun
  where (fun, convColDesc) = f colDesc

---Constructor function for ColumnTupleCollection.
---@param col1 - first ColumnSingleCollection
---@param col2 - second ColumnSingleCollection
tupleCol :: ColumnSingleCollection a -> ColumnSingleCollection b
                                     -> ColumnTupleCollection a b
tupleCol col1 col2 = (col1,col2)

---Constructor function for ColumnTripleCollection.
tripleCol :: ColumnSingleCollection a
          -> ColumnSingleCollection b
          -> ColumnSingleCollection c
          -> ColumnTripleCollection a b c
tripleCol col1 col2 col3 = (col1, col2, col3)

---Constructor function for ColumnFourTupleCollection.
fourCol :: ColumnSingleCollection a
        -> ColumnSingleCollection b
        -> ColumnSingleCollection c
        -> ColumnSingleCollection d
        -> ColumnFourTupleCollection a b c d
fourCol col1 col2 col3 col4 = (col1, col2, col3, col4)

---Constructor function for ColumnFiveTupleCollection.
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)

--- Datatype to describe all parts of a select-query without Setoperators 
--- order-by and limit (selecthead) for a single column.   
data SingleColumnSelect a  = SingleCS Specifier
                                      (ColumnSingleCollection a)
                                      TableClause
                                      Criteria

--- Datatype to describe all parts of a select-query without Setoperators 
--- order-by and limit (selecthead) for two columns.   
data TupleColumnSelect a b = TupleCS  Specifier
                                      (ColumnTupleCollection a b)
                                      TableClause
                                      Criteria

--- Datatype to describe all parts of a select-query without Setoperators 
--- order-by and limit (selecthead) for three columns. 
data TripleColumnSelect a b c = TripleCS Specifier
                                         (ColumnTripleCollection a b c)
                                         TableClause
                                         Criteria

--- Datatype to describe all parts of a select-query without Setoperators 
--- order-by and limit (selecthead) for four columns. 
data FourColumnSelect a b c d = FourCS Specifier
                                       (ColumnFourTupleCollection a b c d)
                                       TableClause
                                       Criteria

--- Datatype to describe all parts of a select-query without Setoperators 
--- order-by and limit (selecthead) for five columns. 
data FiveColumnSelect a b c d e = FiveCS Specifier
                                         (ColumnFiveTupleCollection a b c d e)
                                         TableClause
                                         Criteria

--selector of the SQLType encasulated in a ColumnSingleCollection                                         
getColumnType :: ColumnSingleCollection _ -> [SQLType]
getColumnType col =
  case col of
       (ResultColumnDescription (ColDesc _ typ _ _) _ _) -> [typ]
       (Case _ (_ , _) (typ,_))                          -> [typ]

-- selector of the conversion function encapsulated in a 
-- ColumnSingleCollection
getColumnValFunc :: ColumnSingleCollection a -> (SQLValue -> a)
getColumnValFunc col =
  case col of
       (ResultColumnDescription (ColDesc _ _ _ f) _ _)   -> f
       (Case _ (_, _) (_,f))                             -> f

--selector: returns the SQLType of the ColumnSingleCollection
-- inside of SingleColumnSelect
getSingleType :: SingleColumnSelect _ -> [SQLType]
getSingleType (SingleCS _ col _ _) = getColumnType col

--selector: returns the function to convert the SQLValue to the resulttype
getSingleValFunc :: SingleColumnSelect a -> (SQLValue -> a)
getSingleValFunc (SingleCS _ col _ _) = getColumnValFunc col

--selector: returns the list of SQLTypes used
-- inside the TupleColumnSelect
getTupleTypes :: TupleColumnSelect _ _ -> [SQLType]
getTupleTypes (TupleCS _ (col1,col2) _ _) = getColumnType col1
                                              ++ getColumnType col2

--selector: returns the tuple of functions to convert the SQLValue 
--to the resulttype
getTupleValFuncs :: TupleColumnSelect a b -> ((SQLValue -> a), (SQLValue -> b))
getTupleValFuncs (TupleCS _ (col1,col2) _ _) = (getColumnValFunc col1,
                                                  getColumnValFunc col2)

--selector: returns the list of SQLTypes used
-- inside the TripleColumnSelect
getTripleTypes :: TripleColumnSelect _ _ _ -> [SQLType]
getTripleTypes (TripleCS _ (col1, col2, col3) _ _) =
  getColumnType col1 ++ getColumnType col2 ++ getColumnType col3

--selector: returns the triple of functions to convert the SQLValue 
--to the resulttype
getTripleValFuncs :: TripleColumnSelect a b c ->
                            ((SQLValue -> a), (SQLValue -> b), (SQLValue -> c))
getTripleValFuncs (TripleCS _ (col1, col2, col3) _ _) =
  (getColumnValFunc col1, getColumnValFunc col2, getColumnValFunc col3)

--selector: returns the list of SQLTypes used
-- inside the FourColumnSelect
getFourTupleTypes :: FourColumnSelect _ _ _ _ -> [SQLType]
getFourTupleTypes (FourCS _ (col1, col2, col3, col4) _ _) =
  getColumnType col1 ++ getColumnType col2 ++
    getColumnType col3 ++ getColumnType col4

--selector: returns the fourtuple of functions to convert the 
--SQLValue to the resulttype
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)


--selector: returns the list of SQLTypes used
-- inside the FiveColumnSelect
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

--selector: returns the fivetuple of functions to convert the 
--SQLValue to the resulttype
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)

-- ------------------------------------------------------------------------------
-- translation functions
-- ------------------------------------------------------------------------------

-- Transform a SingleColumnSelect to its string representation.
trSingleSelectQuery :: SingleColumnSelect _ -> String
trSingleSelectQuery (SingleCS sp col tabs crit) =
  ("select "++ trSpecifier sp ++ getResultColumnString col ++" from " ++
   (getTableString tabs "") ++ " " ++ trCriteria crit)

-- Transform a TupleColumnSelect to its string representation.
trTupleSelectQuery :: TupleColumnSelect _ _ -> String
trTupleSelectQuery (TupleCS sp (col1, col2) tabs crit) =
  ("select " ++trSpecifier sp ++ getResultColumnString col1 ++ ", "++
    getResultColumnString col2 ++" from " ++ (getTableString tabs "") ++
      trCriteria crit )

-- Transform a TripleColumnSelect to its string representation.
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 )

-- Transform a FourTupleColumnSelect to its string representation.
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 )

-- Transform a FiveTupleColumnSelect to its string representation.
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 )

-- translate set operations        
trSetOp :: SetOp -> String
trSetOp Union = " union "
trSetOp Intersect = " intersect "
trSetOp Except = " except "

-- translate limit clause
trLimit :: Maybe Int -> String
trLimit limit = case limit of
                     Nothing -> ""
                     Just n -> " Limit "++(show n)

-- Create the "as tablename" string
asTable :: Table -> Int -> Table
asTable table n = case n of
                      0 -> ""
                      m -> " as '" ++ (show m) ++ table ++ "'"

-- translate joins                      
trJoinPart1 :: Join -> String
trJoinPart1 Cross = " cross join"
trJoinPart1 (Inner _) = " inner join"

trJoinPart2 :: Join -> String
trJoinPart2 Cross = ""
trJoinPart2 (Inner constraint) =
                          " ON (" ++ (trConstraint constraint)++")"

-- translate a ColumnSingleCollection                          
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)"

-- translate a table-clause                                        
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