Skip to content

Commit 63dfa08

Browse files
committed
Remove direct dependence of beam-sqlite on either unix or windows, allowing to build on a wider variety of platforms
1 parent df0080e commit 63dfa08

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

beam-sqlite/ChangeLog.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1+
# 0.5.4.0
2+
3+
## Added features
4+
5+
* Removed the reliance on either the `unix` or `windows` package, which should enable (#738)
6+
`beam-sqlite` to be buildable on a wider variety of platforms.
7+
18
# 0.5.3.1
29

3-
# Added features
10+
## Added features
411

512
* Replaced use of deprecated functions.
613

714
# 0.5.3.0
815

9-
# Added features
16+
## Added features
1017

1118
* Loosen some version bounds
1219
* `HasSqlEqualityCheck` instance for `Day`
1320

1421
# 0.5.2.0
1522

16-
# Bug fixes
23+
## Bug fixes
1724

1825
* Fix encoding for `UTCTime`
1926

20-
# Addded features
27+
## Addded features
2128

2229
* `IN (SELECT ...)` syntax via `inQuery_`
2330

beam-sqlite/Database/Beam/Sqlite/Connection.hs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# OPTIONS_GHC -fno-warn-orphans #-}
22
{-# LANGUAGE BangPatterns #-}
3-
{-# LANGUAGE CPP #-}
43
{-# LANGUAGE DataKinds #-}
54
{-# LANGUAGE InstanceSigs #-}
65
{-# LANGUAGE UndecidableInstances #-}
@@ -72,6 +71,7 @@ import Data.ByteString.Builder (toLazyByteString)
7271
import qualified Data.ByteString.Char8 as BS
7372
import qualified Data.ByteString.Lazy.Char8 as BL
7473
import qualified Data.DList as D
74+
import Data.Hashable (hash)
7575
import Data.Int
7676
import Data.Maybe (mapMaybe)
7777
import Data.Proxy (Proxy(..))
@@ -82,21 +82,13 @@ import qualified Data.Text.Encoding as T (decodeUtf8)
8282
import qualified Data.Text.Lazy as TL
8383
import qualified Data.Text.Lazy.Encoding as TL (decodeUtf8)
8484
import Data.Time ( LocalTime, UTCTime, Day
85-
, ZonedTime, utc, utcToLocalTime )
85+
, ZonedTime, utc, utcToLocalTime, getCurrentTime )
8686
import Data.Typeable (cast)
8787
import Data.Word
8888
import GHC.TypeLits
8989

9090
import Network.URI
9191

92-
#ifdef UNIX
93-
import System.Posix.Process (getProcessID)
94-
#elif defined(WINDOWS)
95-
import System.Win32.Process (getCurrentProcessId)
96-
#else
97-
#error Need either POSIX or Win32 API for MonadBeamInsertReturning
98-
#endif
99-
10092
import Text.Read (readMaybe)
10193

10294
-- | The SQLite backend. Used to parameterize 'MonadBeam' and 'FromBackendRow'
@@ -388,34 +380,34 @@ runInsertReturningList SqlInsertNoRows = pure []
388380
runInsertReturningList (SqlInsert tblSettings insertStmt_@(SqliteInsertSyntax nm _ _ _)) =
389381
do (logger, conn) <- SqliteM ask
390382
SqliteM . liftIO $ do
391-
392-
#ifdef UNIX
393-
processId <- fromString . show <$> getProcessID
394-
#elif defined(WINDOWS)
395-
processId <- fromString . show <$> getCurrentProcessId
396-
#else
397-
#error Need either POSIX or Win32 API for MonadBeamInsertReturning
398-
#endif
383+
384+
-- We create a pseudo-random savepoint identification that can be referenced
385+
-- throughout this operation. -- This used to be based on the process ID
386+
-- (e.g. `System.Posix.Process.getProcessID` for UNIX),
387+
-- but using timestamps is more portable; see #738
388+
--
389+
-- Note that `hash` can return negative numbers, hence the use of `abs`.
390+
savepointId <- fromString . show . abs . hash <$> getCurrentTime
399391

400392
let tableNameTxt = T.decodeUtf8 (BL.toStrict (sqliteRenderSyntaxScript (fromSqliteTableName nm)))
401393

402394
startSavepoint =
403-
execute_ conn (Query ("SAVEPOINT insert_savepoint_" <> processId))
395+
execute_ conn (Query ("SAVEPOINT insert_savepoint_" <> savepointId))
404396
rollbackToSavepoint =
405-
execute_ conn (Query ("ROLLBACK TRANSACTION TO SAVEPOINT insert_savepoint_" <> processId))
397+
execute_ conn (Query ("ROLLBACK TRANSACTION TO SAVEPOINT insert_savepoint_" <> savepointId))
406398
releaseSavepoint =
407-
execute_ conn (Query ("RELEASE SAVEPOINT insert_savepoint_" <> processId))
399+
execute_ conn (Query ("RELEASE SAVEPOINT insert_savepoint_" <> savepointId))
408400

409401
createInsertedValuesTable =
410-
execute_ conn (Query ("CREATE TEMPORARY TABLE inserted_values_" <> processId <> " AS SELECT * FROM " <> tableNameTxt <> " LIMIT 0"))
402+
execute_ conn (Query ("CREATE TEMPORARY TABLE inserted_values_" <> savepointId <> " AS SELECT * FROM " <> tableNameTxt <> " LIMIT 0"))
411403
dropInsertedValuesTable =
412-
execute_ conn (Query ("DROP TABLE inserted_values_" <> processId))
404+
execute_ conn (Query ("DROP TABLE inserted_values_" <> savepointId))
413405

414406
createInsertTrigger =
415-
execute_ conn (Query ("CREATE TEMPORARY TRIGGER insert_trigger_" <> processId <> " AFTER INSERT ON " <> tableNameTxt <> " BEGIN " <>
416-
"INSERT INTO inserted_values_" <> processId <> " SELECT * FROM " <> tableNameTxt <> " WHERE ROWID=last_insert_rowid(); END" ))
407+
execute_ conn (Query ("CREATE TEMPORARY TRIGGER insert_trigger_" <> savepointId <> " AFTER INSERT ON " <> tableNameTxt <> " BEGIN " <>
408+
"INSERT INTO inserted_values_" <> savepointId <> " SELECT * FROM " <> tableNameTxt <> " WHERE ROWID=last_insert_rowid(); END" ))
417409
dropInsertTrigger =
418-
execute_ conn (Query ("DROP TRIGGER insert_trigger_" <> processId))
410+
execute_ conn (Query ("DROP TRIGGER insert_trigger_" <> savepointId))
419411

420412

421413
mask $ \restore -> do
@@ -430,7 +422,7 @@ runInsertReturningList (SqlInsert tblSettings insertStmt_@(SqliteInsertSyntax nm
430422
allBeamValues (\(Columnar' projField) -> quotedIdentifier (_fieldName projField)) $
431423
tblSettings
432424

433-
fmap (\(BeamSqliteRow r) -> r) <$> query_ conn (Query ("SELECT " <> columns <> " FROM inserted_values_" <> processId))
425+
fmap (\(BeamSqliteRow r) -> r) <$> query_ conn (Query ("SELECT " <> columns <> " FROM inserted_values_" <> savepointId))
434426
releaseSavepoint
435427
return x
436428

beam-sqlite/beam-sqlite.cabal

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: beam-sqlite
2-
version: 0.5.3.1
2+
version: 0.5.4.0
33
synopsis: Beam driver for SQLite
44
description: Beam driver for the <https://sqlite.org/ SQLite> embedded database.
55
See <https://haskell-beam.github.io/beam/user-guide/backends/beam-sqlite/ here>
@@ -50,13 +50,6 @@ library
5050
if flag(werror)
5151
ghc-options: -Werror
5252

53-
if os(windows)
54-
cpp-options: -DWINDOWS
55-
build-depends: Win32 >=2.4 && <2.8
56-
if os(freebsd) || os(netbsd) || os(openbsd) || os(darwin) || os(linux) || os(solaris) || os(android)
57-
cpp-options: -DUNIX
58-
build-depends: unix >=2.0 && <2.9
59-
6053
test-suite beam-sqlite-tests
6154
type: exitcode-stdio-1.0
6255
hs-source-dirs: test

0 commit comments

Comments
 (0)