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
--- This module defines the Token needed to scan an embedded
--- SQL Statement. In Addition it defines a 'ToString'-method
--- for all token.
--- The supported datatypes, functions, operator etc. are based on
--- the CDBI-Interface.
---@author Julia Krone
---@version 0.1
-- -----------------------------------------------------------

module SQLToken where

import Data.Time (CalendarTime)


data Token
  --identifier
    = Constant ConstVal
    | Ident String
    | EmbedExp String
  --Keywords
    | KW_Select
    | KW_From
    | KW_Where
    | KW_Order
    | KW_Group
    | KW_By
    | KW_Having
    | KW_Insert
    | KW_Into
    | KW_Values
    | KW_Update
    | KW_Set
    | KW_Delete
    | KW_Transaction
    | KW_Inner
    | KW_Cross
    | KW_Join
    | KW_On
    | KW_Satisfies
    | KW_As
    | KW_Distinct
    | KW_All
    | KW_Case
    | KW_When
    | KW_Then
    | KW_Else
    | KW_End
    | KW_Asc
    | KW_Desc
    | KW_Limit
    | KW_Exists
    | KW_Not
    | KW_Commit
    | KW_Rollback
    | KW_Begin
    | KW_Table
  --Operators
    | Fun Func
    | BinOp BinOperator
    | LogOp LogOperator
    | SetOp SetOperator
    | Is
    | Between
    | In
  --Punctuation
    | LParen
    | RParen
    | Comma
    | Semi
    | Stop
    | Asterix
   -- unsupported Char
    | Unexpected Char
 deriving Eq

data ConstVal = NumInt Int
              | NumFloat Float
              | Boolean Bool
              | Date CalendarTime
              | VarStr String
              | VarChar Char
              | Null
 deriving Eq

data Func = Sum | Avg | Min | Max | Count
 deriving Eq

data BinOperator = Lth | Gth | Lte | Gte | Equal | Uneq | Like
 deriving Eq

data LogOperator = And | Or
 deriving Eq

data SetOperator = Union | Intersect | Except
 deriving Eq

--auxiliary function to print token for error messages
tokentoString :: Token -> String
tokentoString t = case t of
                       Constant (NumInt int)     -> "interger " ++ show int
                       Constant (NumFloat float) -> "float " ++ show float
                       Constant (Date date)      -> "date " ++ show date
                       Constant (Boolean bool)   -> "boolean " ++ show bool
                       Constant (VarChar char)   -> "char "++ show char
                       Ident str                 -> "identifier " ++ str
                       Constant (VarStr str)     -> "string " ++ str
                       EmbedExp exp              -> ("embedded curry  "++
                                                       "expression" ++ exp)
                       Constant Null             -> "null"
                       KW_Select                 -> "Select"
                       KW_From                   -> "From"
                       KW_Where                  -> "Where"
                       KW_Order                  -> "Order"
                       KW_Group                  -> "Group"
                       KW_By                     -> "By"
                       KW_Having                 -> "Having"
                       KW_Insert                 -> "Insert"
                       KW_Into                   -> "Into"
                       KW_Values                 -> "Values"
                       KW_Update                 -> "Update"
                       KW_Set                    -> "Set"
                       KW_Delete                 -> "Delete"
                       KW_Transaction            -> "Transaction"
                       KW_Begin                  -> "Begin"
                       KW_Inner                  -> "Inner"
                       KW_Cross                  -> "Cross"
                       KW_Join                   -> "Join"
                       KW_On                     -> "On"
                       KW_Satisfies              -> "Satisfies"
                       KW_As                     -> "As"
                       KW_Distinct               -> "Distinct"
                       KW_All                    -> "All"
                       KW_Case                   -> "Case"
                       KW_When                   -> "When"
                       KW_Then                   -> "Then"
                       KW_Else                   -> "Else"
                       KW_End                    -> "End"
                       KW_Commit                 -> "Commit"
                       KW_Rollback               -> "Rollback"
                       KW_Table                  -> "keyword table"
                       Fun Count                 -> "Count"
                       Fun Avg                   -> "Avg"
                       Fun Min                   -> "Min"
                       Fun Max                   -> "Max"
                       Fun Sum                   -> "Sum"
                       KW_Asc                    -> "Asc"
                       KW_Desc                   -> "Desc"
                       KW_Limit                  -> "Limit"
                       KW_Exists                 -> "Exists"
                       KW_Not                    -> "Not"
                       BinOp Lth                 -> "<"
                       BinOp Lte                 -> "<="
                       BinOp Gth                 -> ">"
                       BinOp Gte                 -> ">="
                       BinOp Equal               -> "="
                       BinOp Uneq                -> "!="
                       BinOp Like                -> "like"
                       LogOp And                 -> "And"
                       LogOp Or                  -> "Or"
                       SetOp Union               -> "Union"
                       SetOp Intersect           -> "Intersect"
                       SetOp Except              -> "Except"
                       Between                   -> "between"
                       In                        -> "In"
                       Is                        -> "Is"
                       LParen                    -> "("
                       RParen                    -> ")"
                       Semi                      -> ";"
                       Comma                     -> ","
                       Stop                      -> "."
                       Asterix                   -> "*"
                       Unexpected c              -> ("Unsupported Character: "
                                                                      ++(c:""))