From 9d3bbf46562546ca9cae89db42540e9353b112d9 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 23 Mar 2023 13:10:52 -0400 Subject: [PATCH] add open' which takes a ByteString This allows using a sqlite database that is in a directory whose name cannot be expressed using Text. Fixes https://github.com/yesodweb/persistent/issues/1481 --- persistent-sqlite/ChangeLog.md | 5 +++++ persistent-sqlite/Database/Sqlite.hs | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/persistent-sqlite/ChangeLog.md b/persistent-sqlite/ChangeLog.md index fcfbcf3b7..0f094571e 100644 --- a/persistent-sqlite/ChangeLog.md +++ b/persistent-sqlite/ChangeLog.md @@ -1,5 +1,10 @@ # Changelog for persistent-sqlite +## 2.13.2.0 (unreleased) + +* [#1486](https://github.com/yesodweb/persistent/pull/1486) + * Add Database.Sqlite.open' which takes a ByteString + ## 2.13.1.1 * [#1459](https://github.com/yesodweb/persistent/pull/1459) diff --git a/persistent-sqlite/Database/Sqlite.hs b/persistent-sqlite/Database/Sqlite.hs index 65a335755..a6a978284 100644 --- a/persistent-sqlite/Database/Sqlite.hs +++ b/persistent-sqlite/Database/Sqlite.hs @@ -52,6 +52,7 @@ module Database.Sqlite ( -- > $ ([PersistInt64 1,PersistInt64 1],[PersistInt64 2,PersistInt64 2]) open, + open', close, prepare, step, @@ -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 @@ -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