Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Database.Sqlite.open' which takes a ByteString #1486

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion persistent-sqlite/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog for persistent-sqlite

## 2.13.2
## 2.13.3.0

* [#1486](https://github.com/yesodweb/persistent/pull/1486)
* Add Database.Sqlite.open' which takes a ByteString

## 2.13.2.0

* [#1488](https://github.com/yesodweb/persistent/pull/1488)
* Add `openRawSqliteConn` for creating `RawSqlite SqlBackend` connections
Expand Down
11 changes: 8 additions & 3 deletions persistent-sqlite/Database/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module Database.Sqlite (
-- > $ ([PersistInt64 1,PersistInt64 1],[PersistInt64 2,PersistInt64 2])

open,
open',
close,
prepare,
step,
Expand Down Expand Up @@ -221,10 +222,10 @@ sqlError maybeConnection functionName error = do
foreign import ccall "sqlite3_open_v2"
openC :: CString -> Ptr (Ptr ()) -> Int -> CString -> IO Int

openError :: Text -> IO (Either Connection Error)
openError :: BS.ByteString -> IO (Either Connection Error)
openError path' = do
let flag = sqliteFlagReadWrite .|. sqliteFlagCreate .|. sqliteFlagUri
BS.useAsCString (encodeUtf8 path') $ \path -> alloca $ \database -> do
BS.useAsCString path' $ \path -> alloca $ \database -> do
err <- decodeError <$> openC path database flag nullPtr
case err of
ErrorOK -> do database' <- peek database
Expand All @@ -238,7 +239,11 @@ openError path' = do
sqliteFlagUri = 0x40

open :: Text -> IO Connection
open path = do
open path = open' (encodeUtf8 path)

-- @since 2.13.2.0
open' :: BS.ByteString -> IO Connection
open' path = do
databaseOrError <- openError path
case databaseOrError of
Left database -> return database
Expand Down
Loading