0 | module Sqlite3.Marshall
3 | import Data.Buffer.Indexed
4 | import Data.ByteString
8 | import public Data.List.Quantifiers.Extra
13 | 0 LAll : (0 f : k -> Type) -> List k -> Type
14 | LAll = Data.List.Quantifiers.All.All
22 | interface FromCell a where
23 | constructor MkFromCell
24 | fromCellType : SqliteType
25 | fromCell : Maybe (IdrisType fromCellType) -> Either SqlError a
28 | public export %inline
29 | FromCellType : (0 a : Type) -> FromCell a => SqliteType
30 | FromCellType a = fromCellType {a}
40 | -> (t -> Either SqlError a)
42 | -> Either SqlError a
43 | decodeJust str f Nothing = Left (NullPointer str)
44 | decodeJust str f (Just v) = f v
46 | public export %inline
47 | FromCell a => FromCell (Maybe a) where
48 | fromCellType = FromCellType a
49 | fromCell Nothing = Right Nothing
50 | fromCell v = Just <$> fromCell v
53 | FromCell Integer where
54 | fromCellType = INTEGER
55 | fromCell = decodeJust "Integer" (Right . cast)
59 | fromCellType = INTEGER
60 | fromCell = decodeJust "Nat" (Right . cast)
63 | FromCell Int64 where
64 | fromCellType = INTEGER
65 | fromCell = decodeJust "Int64" Right
68 | FromCell Int32 where
69 | fromCellType = INTEGER
70 | fromCell = decodeJust "Int32" (Right . cast)
73 | FromCell Int16 where
74 | fromCellType = INTEGER
75 | fromCell = decodeJust "Int16" (Right . cast)
79 | fromCellType = INTEGER
80 | fromCell = decodeJust "Int8" (Right . cast)
83 | FromCell Bits64 where
84 | fromCellType = INTEGER
85 | fromCell = decodeJust "Bits64" (Right . cast)
88 | FromCell Bits32 where
89 | fromCellType = INTEGER
90 | fromCell = decodeJust "Bits32" (Right . cast)
93 | FromCell Bits16 where
94 | fromCellType = INTEGER
95 | fromCell = decodeJust "Bits16" (Right . cast)
98 | FromCell Bits8 where
99 | fromCellType = INTEGER
100 | fromCell = decodeJust "Bits8" (Right . cast)
103 | FromCell String where
104 | fromCellType = TEXT
105 | fromCell = decodeJust "String" Right
108 | FromCell ByteString where
109 | fromCellType = BLOB
110 | fromCell = decodeJust "ByteString" Right
113 | FromCell Double where
114 | fromCellType = REAL
115 | fromCell = decodeJust "Double" Right
118 | FromCell Bool where
119 | fromCellType = INTEGER
120 | fromCell = decodeJust "Bool" (\case 0 => Right False;
_ => Right True)
128 | interface ToCell a where
129 | constructor MkToCell
130 | toCellType : SqliteType
131 | toCell : a -> Maybe (IdrisType toCellType)
134 | public export %inline
135 | ToCellType : (0 a : Type) -> ToCell a => SqliteType
136 | ToCellType a = toCellType {a}
138 | public export %inline
139 | ToCell a => ToCell (Maybe a) where
140 | toCellType = ToCellType a
141 | toCell m = m >>= toCell
145 | toCellType = INTEGER
150 | toCellType = INTEGER
151 | toCell = Just . cast
155 | toCellType = INTEGER
156 | toCell = Just . cast
160 | toCellType = INTEGER
161 | toCell = Just . cast
164 | ToCell Bits64 where
165 | toCellType = INTEGER
166 | toCell = Just . cast
169 | ToCell Bits32 where
170 | toCellType = INTEGER
171 | toCell = Just . cast
174 | ToCell Bits16 where
175 | toCellType = INTEGER
176 | toCell = Just . cast
180 | toCellType = INTEGER
181 | toCell = Just . cast
184 | ToCell String where
189 | ToCell ByteString where
194 | ToCell Double where
200 | toCellType = INTEGER
201 | toCell True = Just 1
202 | toCell False = Just 0
210 | interface FromRow a where
211 | constructor MkFromRow
212 | fromRowTypes : List SqliteType
213 | fromRow : LAll (Maybe . IdrisType) fromRowTypes -> Either SqlError a
216 | FromCell a => FromRow a where
217 | fromRowTypes = [FromCellType a]
218 | fromRow [v] = fromCell v
221 | public export %inline
222 | FromRowTypes : (0 a : Type) -> FromRow a => List SqliteType
223 | FromRowTypes a = fromRowTypes {a}
228 | FromRowsTypes : LAll (FromRow . f) ts -> List SqliteType
229 | FromRowsTypes [] = []
230 | FromRowsTypes (x :: xs) = fromRowTypes @{x} ++ FromRowsTypes xs
233 | (ps : LAll (FromRow . f) ts)
234 | -> LAll (Maybe . IdrisType) (FromRowsTypes ps)
235 | -> Either SqlError (LAll f ts)
236 | fromRowImpl [] [] = Right []
237 | fromRowImpl (p :: ps) xs =
238 | let (qs,rs) := splitAt (fromRowTypes @{p}) xs
239 | Right v := fromRow @{p} qs | Left err => Left err
240 | Right vs := fromRowImpl ps rs | Left err => Left err
244 | {0 f : k -> Type} -> (ps : LAll (FromRow . f) ts) => FromRow (LAll f ts) where
245 | fromRowTypes = FromRowsTypes ps
246 | fromRow = fromRowImpl ps
252 | FromRowsTypesN : Nat -> FromRow a -> List SqliteType
253 | FromRowsTypesN 0 p = []
254 | FromRowsTypesN (S k) p = fromRowTypes @{p} ++ FromRowsTypesN k p
259 | -> LAll (Maybe . IdrisType) (FromRowsTypesN n p)
260 | -> Either SqlError (Vect n a)
261 | fromRowVectImpl 0 p [] = Right []
262 | fromRowVectImpl (S k) p xs =
263 | let (qs,rs) := splitAt (fromRowTypes @{p}) xs
264 | Right v := fromRow @{p} qs | Left err => Left err
265 | Right vs := fromRowVectImpl k p rs | Left err => Left err
269 | {n : _} -> (p : FromRow a) => FromRow (Vect n a) where
270 | fromRowTypes = FromRowsTypesN n p
271 | fromRow = fromRowVectImpl n p
279 | interface ToRow a where
280 | constructor MkToRow
281 | toRowTypes : List SqliteType
282 | toRow : a -> LAll (Maybe . IdrisType) toRowTypes
285 | ToCell a => ToRow a where
286 | toRowTypes = [ToCellType a]
287 | toRow v = [toCell v]
290 | public export %inline
291 | ToRowTypes : (0 a : Type) -> ToRow a => List SqliteType
292 | ToRowTypes a = toRowTypes {a}
297 | ToRowsTypes : LAll (ToRow . f) ts -> List SqliteType
298 | ToRowsTypes [] = []
299 | ToRowsTypes (p :: ps) = toRowTypes @{p} ++ ToRowsTypes ps
302 | (ps : LAll (ToRow . f) ts)
304 | -> LAll (Maybe . IdrisType) (ToRowsTypes ps)
305 | toRowImpl [] [] = []
306 | toRowImpl (p::ps) (v::vs) = toRow v ++ toRowImpl ps vs
309 | {0 f : k -> Type} -> (ps : LAll (ToRow . f) ts) => ToRow (LAll f ts) where
310 | toRowTypes = ToRowsTypes ps
311 | toRow = toRowImpl ps
317 | ToRowsTypesN : Nat -> ToRow a -> List SqliteType
318 | ToRowsTypesN 0 p = []
319 | ToRowsTypesN (S k) p = toRowTypes @{p} ++ ToRowsTypesN k p
325 | -> LAll (Maybe . IdrisType) (ToRowsTypesN n p)
326 | toRowVectImpl 0 p [] = []
327 | toRowVectImpl (S k) p (v::vs) = toRow @{p} v ++ toRowVectImpl k p vs
330 | {n : _} -> (p : ToRow a) => ToRow (Vect n a) where
331 | toRowTypes = ToRowsTypesN n p
332 | toRow = toRowVectImpl n p
336 | Concat : List (List a) -> List a
338 | Concat (x :: xs) = x ++ Concat xs
343 | splitAll : (v : List (List a)) -> LAll f (Concat v) -> LAll (LAll f) v
344 | splitAll [] [] = []
345 | splitAll (xs :: xss) ys =
346 | let (zs,r) := splitAt xs ys
347 | in zs :: splitAll xss r