Skip to content
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
30 changes: 15 additions & 15 deletions persistent-postgresql/Database/Persist/Postgresql.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down Expand Up @@ -47,46 +47,46 @@ module Database.Persist.Postgresql
import qualified Database.PostgreSQL.LibPQ as LibPQ

import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.Internal as PG
import qualified Database.PostgreSQL.Simple.FromField as PGFF
import qualified Database.PostgreSQL.Simple.Internal as PG
import Database.PostgreSQL.Simple.Ok (Ok(..))
import qualified Database.PostgreSQL.Simple.ToField as PGTF
import qualified Database.PostgreSQL.Simple.Transaction as PG
import qualified Database.PostgreSQL.Simple.Types as PG
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as PS
import Database.PostgreSQL.Simple.Ok (Ok (..))
import qualified Database.PostgreSQL.Simple.Types as PG

import Control.Arrow
import Control.Exception (Exception, throw, throwIO)
import Control.Monad
import Control.Monad.Except
import Control.Monad.IO.Unlift (MonadIO (..), MonadUnliftIO)
import Control.Monad.IO.Unlift (MonadIO(..), MonadUnliftIO)
import Control.Monad.Logger (MonadLoggerIO, runNoLoggingT)
import Control.Monad.Trans.Reader (ReaderT(..), runReaderT, asks)
import Control.Monad.Trans.Reader (ReaderT(..), asks, runReaderT)
import Control.Monad.Trans.Writer (WriterT(..), runWriterT)

import qualified Blaze.ByteString.Builder.Char8 as BBB
import Data.Acquire (Acquire, mkAcquire, with)
import Data.Aeson
import Data.Aeson.Types (modifyFailure)
import qualified Data.Attoparsec.Text as AT
import qualified Data.Attoparsec.ByteString.Char8 as P
import qualified Data.Attoparsec.Text as AT
import Data.Bits ((.&.))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Char8 as B8
import Data.Char (ord)
import Data.Conduit
import qualified Data.Conduit.List as CL
import Data.Data ( Data, Typeable )
import Data.Data (Data, Typeable)
import Data.Either (partitionEithers)
import Data.Fixed (Fixed(..), Pico)
import Data.Function (on)
import Data.IORef
import Data.Int (Int64)
import qualified Data.IntMap as I
import Data.IORef
import Data.List (find, sort, groupBy, foldl')
import Data.List.NonEmpty (NonEmpty)
import Data.List (find, foldl', groupBy, sort)
import qualified Data.List as List
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NEL
import qualified Data.Map as Map
import Data.Maybe
Expand All @@ -99,7 +99,7 @@ import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import Data.Text.Read (rational)
import Data.Time (utc, NominalDiffTime, localTimeToUTC)
import Data.Time (NominalDiffTime, localTimeToUTC, utc)
import System.Environment (getEnvironment)

import Database.Persist.Sql
Expand Down Expand Up @@ -1808,7 +1808,7 @@ copyField = CopyField
--
-- Called thusly, this method will insert a new record (if none exists) OR update a recordField with a new value
-- assuming the condition in the last block is met.
--
--
-- @since 2.12.1.0
upsertWhere
:: ( backend ~ PersistEntityBackend record
Expand Down Expand Up @@ -1939,7 +1939,7 @@ mkBulkUpsertQuery records conn fieldValues updates filters =
fieldSets = map (\n -> T.concat [n, "=EXCLUDED.", n, ""]) updateFieldNames
upds = map (Util.mkUpdateText' (escapeF) (\n -> T.concat [nameOfTable, ".", n])) updates
updsValues = map (\(Update _ val _) -> toPersistValue val) updates
(wher, whereVals) = if null filters
(wher, whereVals) = if null filters
then ("", [])
else (filterClauseWithVals (Just PrefixTableName) conn filters)
updateText = case fieldSets <> upds <> condFieldSets of
Expand Down
4 changes: 2 additions & 2 deletions persistent-postgresql/persistent-postgresql.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-postgresql
version: 2.12.0.0
version: 2.12.1.0
license: MIT
license-file: LICENSE
author: Felipe Lessa, Michael Snoyman <michael@snoyman.com>
Expand All @@ -16,7 +16,7 @@ extra-source-files: ChangeLog.md

library
build-depends: base >= 4.9 && < 5
, persistent >= 2.12 && < 3
, persistent >= 2.12.1.0 && < 2.13
, aeson >= 1.0
, attoparsec
, blaze-builder
Expand Down
10 changes: 8 additions & 2 deletions persistent/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for persistent

## 2.12.1.0

* [#1226](https://github.com/yesodweb/persistent/pull/1226)
* Expose the `filterClause` and `filterClauseWithValues` functions to support
the `upsertWhere` functionality in `persistent-postgresql`.

## 2.12.0.2

* [#1123](https://github.com/yesodweb/persistent/pull/1223)
Expand All @@ -22,10 +28,10 @@
* Added `makeCompatibleInstances` and `makeCompatibleKeyInstances`, TemplateHaskell invocations for auto-generating standalone derivations using `Compatible` and `DerivingVia`.
* [#1207](https://github.com/yesodweb/persistent/pull/1207)
* @codygman discovered a bug in [issue #1199](https://github.com/yesodweb/persistent/issues/1199) where postgres connections were being returned to the `Pool SqlBackend` in an inconsistent state.
@parsonsmatt debugged the issue and determined that it had something to do with asynchronous exceptions.
@parsonsmatt debugged the issue and determined that it had something to do with asynchronous exceptions.
Declaring it to be "out of his pay grade," he ripped the `poolToAcquire` function out and replaced it with `Data.Pool.withResource`, which doesn't exhibit the bug.
Fortunately, this doesn't affect the public API, and can be a mere bug release.
* Removed the functions `unsafeAcquireSqlConnFromPool`, `acquireASqlConnFromPool`, and `acquireSqlConnFromPoolWithIsolation`.
* Removed the functions `unsafeAcquireSqlConnFromPool`, `acquireASqlConnFromPool`, and `acquireSqlConnFromPoolWithIsolation`.
For a replacement, see `runSqlPoolNoTransaction` and `runSqlPoolWithHooks`.
* Renaming values in persistent-template [#1203](https://github.com/yesodweb/persistent/pull/1203)
* [#1214](https://github.com/yesodweb/persistent/pull/1214):
Expand Down
11 changes: 5 additions & 6 deletions persistent/Database/Persist/Sql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Database.Persist.Sql
, deleteWhereCount
, updateWhereCount
, filterClause
, filterClauseHelper
, filterClauseWithVals
, FilterTablePrefix (..)
, transactionSave
Expand All @@ -31,13 +30,13 @@ import Control.Monad.IO.Class
import Control.Monad.Trans.Reader (ReaderT, ask)

import Database.Persist
import Database.Persist.Sql.Types
import Database.Persist.Sql.Types.Internal (IsolationLevel (..))
import Database.Persist.Sql.Class
import Database.Persist.Sql.Run hiding (rawAcquireSqlConn, rawRunSqlPool)
import Database.Persist.Sql.Raw
import Database.Persist.Sql.Migration
import Database.Persist.Sql.Internal
import Database.Persist.Sql.Migration
import Database.Persist.Sql.Raw
import Database.Persist.Sql.Run hiding (rawAcquireSqlConn, rawRunSqlPool)
import Database.Persist.Sql.Types
import Database.Persist.Sql.Types.Internal (IsolationLevel(..))

import Database.Persist.Sql.Orphan.PersistQuery
import Database.Persist.Sql.Orphan.PersistStore
Expand Down
27 changes: 24 additions & 3 deletions persistent/Database/Persist/Sql/Orphan/PersistQuery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,21 @@ getFiltsValues conn = snd . filterClauseHelper Nothing False conn OrNullNo

data OrNull = OrNullYes | OrNullNo

-- | Used when determining how to prefix a column name in a @WHERE@ clause.
--
-- @since 2.12.1.0
data FilterTablePrefix
= PrefixTableName
| PrefixExcluded
= PrefixTableName
-- ^ Prefix the column with the table name. This is useful if the column
-- name might be ambiguous.
--
-- @since 2.12.1.0
| PrefixExcluded
-- ^ Prefix the column name with the @EXCLUDED@ keyword. This is used with
-- the Postgresql backend when doing @ON CONFLICT DO UPDATE@ clauses - see
-- the documentation on @upsertWhere@ and @upsertManyWhere@.
--
-- @since 2.12.1.0

filterClauseHelper :: (PersistEntity val)
=> Maybe FilterTablePrefix -- ^ include table name or PostgresSQL EXCLUDED
Expand Down Expand Up @@ -397,13 +409,22 @@ filterClauseHelper tablePrefix includeWhere conn orNull filters =
showSqlFilter NotIn = " NOT IN "
showSqlFilter (BackendSpecificFilter s) = s

-- | Render a @['Filter' record]@ into a 'Text' value suitable for inclusion
-- into a SQL query.
--
-- @since 2.12.1.0
filterClause :: (PersistEntity val)
=> Maybe FilterTablePrefix -- ^ include table name or EXCLUDED
-> SqlBackend
-> [Filter val]
-> Text
filterClause b c = fst . filterClauseHelper b True c OrNullNo

-- | Render a @['Filter' record]@ into a 'Text' value suitable for inclusion
-- into a SQL query, as well as the @['PersistValue']@ to properly fill in the
-- @?@ place holders.
--
-- @since 2.12.1.0
filterClauseWithVals :: (PersistEntity val)
=> Maybe FilterTablePrefix -- ^ include table name or EXCLUDED
-> SqlBackend
Expand Down Expand Up @@ -450,4 +471,4 @@ decorateSQLWithLimitOffset nolimit (limit,offset) _ sql =
[ sql
, lim
, off
]
]
2 changes: 1 addition & 1 deletion persistent/persistent.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent
version: 2.12.0.2
version: 2.12.1.0
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
Expand Down