Skip to content

Commit

Permalink
[ new ] fromString for expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hoeck committed Nov 2, 2023
1 parent 0dd7d48 commit 740f851
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
1 change: 0 additions & 1 deletion sqlite3.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ modules = Sqlite3
, Sqlite3.Marshall
, Sqlite3.Parameter
, Sqlite3.Prim
, Sqlite3.Run
, Sqlite3.Table
, Sqlite3.Types

Expand Down
1 change: 0 additions & 1 deletion src/Sqlite3.idr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import public Sqlite3.Expr
import public Sqlite3.Marshall
import public Sqlite3.Parameter
import public Sqlite3.Prim
import public Sqlite3.Run
import public Sqlite3.Table
import public Sqlite3.Types
38 changes: 35 additions & 3 deletions src/Sqlite3/Expr.idr
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ export %inline
FromDouble (Expr s REAL) where
fromDouble = Lit REAL

-- export %inline
-- FromString (Expr s TEXT) where
-- fromString = Lit TEXT . Just
export %inline
fromString :
(col : String)
-> {auto 0 p : TableHasCol t col}
-> Expr [t] (ListColType t.cols p)
fromString = C

||| Convert a value of a marshallable type to a literal expression.
export
Expand All @@ -95,6 +98,22 @@ val x =
Nothing => NULL
Just v => Lit _ v

export %inline
text : String -> Expr s TEXT
text = val

export %inline
int : Int64 -> Expr s INTEGER
int = val

export %inline
blob : ByteString -> Expr s BLOB
blob = val

export %inline
real : Double -> Expr s REAL
real = val

--------------------------------------------------------------------------------
-- Encode
--------------------------------------------------------------------------------
Expand All @@ -120,12 +139,25 @@ hexChar _ = 'f'
%inline quote : Char
quote = '\''

||| Encodes a `ByteString` as an SQL literal.
|||
||| Every byte is encodec with two hexadecimal digits, and the
||| whole string is wrapped in single quotes prefixed with an "X".
|||
||| For instance, `encodeBytes (fromList [0xa1, 0x77])` yields the
||| string "X'a177'".
export
encodeBytes : ByteString -> String
encodeBytes = pack . (\x => 'X'::quote::x) . foldr acc [quote]
where
%inline acc : Bits8 -> List Char -> List Char
acc b cs = hexChar (b `shiftR` 4) :: hexChar (b .&. 0xf) :: cs

||| Encodes a `String` as an SQL literal.
|||
||| The whole string is wrapped in single quotes. Single quotes withing
||| the string are escaped by doubling them.
export
encodeText : String -> String
encodeText = go [<quote] . unpack
where
Expand Down
13 changes: 0 additions & 13 deletions src/Sqlite3/Run.idr

This file was deleted.

8 changes: 6 additions & 2 deletions test/src/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ insertFile : ByteString -> Cmd TInsert
insertFile ix = insert Files ["content"] [ix]

mol : Query [Bits32, String, Maybe String, Maybe Double]
mol =
SELECT_FROM Molecules [C "id", C "name", C "casnr", C "molweight"] (C "id" > 1)
mol = SELECT_FROM Molecules ["id", "name", "casnr", "molweight"] ("id" > 1)

fil : Query [ByteString]
fil = SELECT_FROM Files ["content"] ("id" == 1)

app : App Errs ()
app = withDB ":memory:" $ do
Expand All @@ -63,6 +65,8 @@ app = withDB ":memory:" $ do
]
ms <- query mol 1000
traverse_ printLn ms
fs <- query fil 1
traverse_ (\[bs] => putStrLn $ encodeBytes bs) fs

main : IO ()
main = runApp handlers app

0 comments on commit 740f851

Please sign in to comment.