3 | import Data.Buffer.Indexed
4 | import Data.ByteString
7 | import Sqlite3.Marshall
14 | data Numeric : SqliteType -> Type where
15 | N_INTEGER : Numeric INTEGER
16 | N_REAL : Numeric REAL
20 | data Expr : Schema -> SqliteType -> Type where
22 | Lit : (t : SqliteType) -> (v : IdrisType t) -> Expr s t
36 | Raw : String -> Expr s t
43 | -> {auto 0 p : IsJust (FindSchemaCol col s)}
44 | -> Expr s (SchemaColType col s)
50 | (>) : Expr s t -> Expr s t -> Expr s BOOL
56 | (<) : Expr s t -> Expr s t -> Expr s BOOL
62 | (>=) : Expr s t -> Expr s t -> Expr s BOOL
68 | (<=) : Expr s t -> Expr s t -> Expr s BOOL
74 | (==) : Expr s t -> Expr s t -> Expr s BOOL
83 | (/=) : Expr s t -> Expr s t -> Expr s BOOL
89 | IS : Expr s t -> Expr s t -> Expr s BOOL
95 | IS_NOT : Expr s t -> Expr s t -> Expr s BOOL
101 | (&&) : Expr s BOOL -> Expr s BOOL -> Expr s BOOL
107 | (||) : Expr s BOOL -> Expr s BOOL -> Expr s BOOL
113 | NOT : Expr s BOOL -> Expr s BOOL
118 | (.&.) : Expr s INTEGER -> Expr s INTEGER -> Expr s INTEGER
123 | (.|.) : Expr s INTEGER -> Expr s INTEGER -> Expr s INTEGER
128 | ShiftL : Expr s INTEGER -> Expr s INTEGER -> Expr s INTEGER
133 | ShiftR : Expr s INTEGER -> Expr s INTEGER -> Expr s INTEGER
140 | Add : (0 prf : Numeric t) => Expr s t -> Expr s t -> Expr s t
147 | Mult : (0 prf : Numeric t) => Expr s t -> Expr s t -> Expr s t
154 | Sub : (0 prf : Numeric t) => Expr s t -> Expr s t -> Expr s t
159 | Abs : (0 prf : Numeric t) => Expr s t -> Expr s t
162 | Neg : (0 prf : Numeric t) => Expr s t -> Expr s t
169 | Div : (0 prf : Numeric t) => Expr s t -> Expr s t -> Expr s t
174 | Mod : Expr s INTEGER -> Expr s INTEGER -> Expr s INTEGER
179 | (++) : Expr s TEXT -> Expr s TEXT -> Expr s TEXT
182 | CURRENT_TIME : Expr s TEXT
185 | CURRENT_DATE : Expr s TEXT
188 | CURRENT_TIMESTAMP : Expr s TEXT
192 | LIKE : Expr s TEXT -> (pattern : Expr s TEXT) -> Expr s BOOL
196 | GLOB : Expr s TEXT -> (pattern : Expr s TEXT) -> Expr s BOOL
199 | IN : Expr s t -> List (Expr s t) -> Expr s BOOL
202 | COALESCE : List (Expr s t) -> Expr s t
207 | COUNT : Expr s t -> Expr s INTEGER
212 | AVG : (0 prf : Numeric t) => Expr s t -> Expr s REAL
217 | SUM : (0 prf : Numeric t) => Expr s t -> Expr s t
222 | MIN : Expr s t -> Expr s t
227 | MAX : Expr s t -> Expr s t
232 | GROUP_CONCAT : Expr s TEXT -> (sep : String) -> Expr s TEXT
235 | Num (Expr s INTEGER) where
236 | fromInteger = Lit INTEGER . fromInteger
241 | Neg (Expr s INTEGER) where
246 | Integral (Expr s INTEGER) where
251 | Num (Expr s REAL) where
252 | fromInteger = Lit REAL . fromInteger
257 | Neg (Expr s REAL) where
262 | Fractional (Expr s REAL) where
266 | FromDouble (Expr s REAL) where
267 | fromDouble = Lit REAL
273 | -> {auto 0 p : IsJust (FindSchemaCol col s)}
274 | -> Expr s (SchemaColType col s)
279 | val : ToCell a => a -> Expr s (ToCellType a)
286 | text : String -> Expr s TEXT
290 | int : Int64 -> Expr s INTEGER
294 | blob : ByteString -> Expr s BLOB
298 | real : Double -> Expr s REAL
308 | commaSep : (a -> String) -> List a -> String
309 | commaSep f = concat . intersperse ", " . map f
316 | encodeText : String -> String
317 | encodeText = go [<quote] . unpack
319 | go : SnocList Char -> List Char -> String
320 | go sc [] = pack $
sc <>> [quote]
321 | go sc ('\'' :: xs) = go (sc :< quote :< quote) xs
322 | go sc (x :: xs) = go (sc :< x) xs
326 | encodeLit : (t : SqliteType) -> IdrisType t -> String
327 | encodeLit BLOB x = encodeBytes x
328 | encodeLit TEXT x = encodeText x
329 | encodeLit INTEGER x = show x
330 | encodeLit REAL x = show x
332 | encOp : String -> Expr s t -> Expr s t -> String
334 | encPrefix : String -> Expr s t -> String
336 | encExprs : SnocList String -> List (Expr s t) -> String
338 | encFun1 : String -> Expr s t -> String
340 | encFun : String -> List (Expr s t) -> String
350 | encodeExpr : Expr s t -> String
351 | encodeExpr (Lit t v) = encodeLit t v
352 | encodeExpr (Add x y) = encOp "+" x y
353 | encodeExpr (Mult x y) = encOp "*" x y
354 | encodeExpr (Sub x y) = encOp "-" x y
355 | encodeExpr (Div x y) = encOp "/" x y
356 | encodeExpr (Mod x y) = encOp "%" x y
357 | encodeExpr (Abs y) = encFun1 "abs" y
358 | encodeExpr (x < y) = encOp "<" x y
359 | encodeExpr (x > y) = encOp ">" x y
360 | encodeExpr (x <= y) = encOp "<=" x y
361 | encodeExpr (x >= y) = encOp ">=" x y
362 | encodeExpr (x == y) = encOp "==" x y
363 | encodeExpr (IS x y) = encOp "IS" x y
364 | encodeExpr (IS_NOT x y) = encOp "IS NOT" x y
365 | encodeExpr (x /= y) = encOp "!=" x y
366 | encodeExpr (x && y) = encOp "AND" x y
367 | encodeExpr (x || y) = encOp "OR" x y
368 | encodeExpr (x ++ y) = encOp "||" x y
369 | encodeExpr (x .&. y) = encOp "&" x y
370 | encodeExpr (x .|. y) = encOp "|" x y
371 | encodeExpr (ShiftR x y) = encOp ">>" x y
372 | encodeExpr (ShiftL x y) = encOp "<<" x y
373 | encodeExpr (NOT x) = encPrefix "NOT" x
374 | encodeExpr (Neg x) = encPrefix "-" x
375 | encodeExpr (Raw s) = s
376 | encodeExpr (Col c) = c
377 | encodeExpr NULL = "NULL"
378 | encodeExpr TRUE = "1"
379 | encodeExpr FALSE = "0"
380 | encodeExpr CURRENT_TIME = "CURRENT_TIME"
381 | encodeExpr CURRENT_DATE = "CURRENT_DATE"
382 | encodeExpr CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP"
383 | encodeExpr (LIKE x y) = encOp "LIKE" x y
384 | encodeExpr (GLOB x y) = encOp "GLOB" x y
385 | encodeExpr (IN x xs) = "\{encodeExpr x} IN (\{encExprs [<] xs})"
386 | encodeExpr (COALESCE xs) = encFun "coalesce" xs
387 | encodeExpr (COUNT x) = encFun1 "count" x
388 | encodeExpr (AVG x) = encFun1 "avg" x
389 | encodeExpr (SUM x) = encFun1 "sum" x
390 | encodeExpr (MIN x) = encFun1 "min" x
391 | encodeExpr (MAX x) = encFun1 "max" x
392 | encodeExpr (GROUP_CONCAT x s) = "group_concat(\{encodeExpr x}, \{s})"
395 | let sx := encodeExpr x
397 | in "(\{sx} \{s} \{sy})"
399 | encPrefix s x = "\{s}(\{encodeExpr x})"
401 | encExprs sc [] = commaSep id (sc <>> [])
402 | encExprs sc (x::xs) = encExprs (sc :< encodeExpr x) xs
404 | encFun f xs = "\{f}(\{encExprs [<] xs})"
406 | encFun1 f x = "\{f}(\{encodeExpr x})"