Skip to content

Commit

Permalink
Merge pull request #16 from scrive/dev-thoferon-sqlwhereanye
Browse files Browse the repository at this point in the history
Add sqlWhereAnyE
  • Loading branch information
23Skidoo authored Nov 19, 2018
2 parents 21e3fed + 1af819e commit 75c20a6
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Database/PostgreSQL/PQTypes/SQL/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ module Database.PostgreSQL.PQTypes.SQL.Builder
, SqlDelete(..)

, sqlWhereAny
, sqlWhereAnyE

, SqlResult
, SqlSet
Expand Down Expand Up @@ -726,9 +727,22 @@ sqlWhereIsNULLE :: (MonadState v m, SqlWhere v, DBExtraException e, FromSQL a)
=> (a -> e) -> SQL -> m ()
sqlWhereIsNULLE exc col = sqlWhereEV (exc, col) $ col <+> "IS NULL"

-- | Add a condition in the WHERE statement that holds if any of the given
-- condition holds.
sqlWhereAny :: (MonadState v m, SqlWhere v) => [State SqlAll ()] -> m ()
sqlWhereAny [] = sqlWhere "FALSE"
sqlWhereAny l = sqlWhere $ "(" <+> smintercalate "OR" (map (parenthesize . toSQLCommand . flip execState (SqlAll [])) l) <+> ")"
sqlWhereAny = sqlWhere . sqlWhereAnyImpl

-- | Add a condition just like 'sqlWhereAny' but throw the given exception if
-- none of the given conditions hold.
sqlWhereAnyE :: (DBExtraException e, MonadState v m, SqlWhere v)
=> e -> [State SqlAll ()] -> m ()
sqlWhereAnyE e = sqlWhereE e . sqlWhereAnyImpl

sqlWhereAnyImpl :: [State SqlAll ()] -> SQL
sqlWhereAnyImpl [] = "FALSE"
sqlWhereAnyImpl l =
"(" <+> smintercalate "OR" (map (parenthesize . toSQLCommand
. flip execState (SqlAll [])) l) <+> ")"

class SqlFrom a where
sqlFrom1 :: a -> SQL -> a
Expand Down

0 comments on commit 75c20a6

Please sign in to comment.