Add not operator.

This commit is contained in:
Kei Hibino 2013-07-29 14:04:17 +09:00
parent 59d614b6ea
commit 4885ebe91d
3 changed files with 33 additions and 10 deletions

View File

@ -20,7 +20,7 @@ module Database.Relational.Query.Projectable (
value,
valueTrue, valueFalse,
values,
valueNull,
unsafeValueNull,
-- * Placeholders
PlaceHolders, addPlaceHolders,
@ -35,7 +35,7 @@ module Database.Relational.Query.Projectable (
(.=.), (.<.), (.<=.), (.>.), (.>=.), (.<>.),
in', isNull, and, or,
in', isNull, isNotNull, and, or, not,
(.+.), (.-.), (./.), (.*.),
@ -47,7 +47,7 @@ module Database.Relational.Query.Projectable (
ProjectableMaybe (just, flattenMaybe)
) where
import Prelude hiding (and, or, pi)
import Prelude hiding (and, or, not, pi)
import Data.List (intercalate)
import Control.Applicative ((<$>))
@ -148,8 +148,8 @@ unsafeProjectSql :: SqlProjectable p => String -> p t
unsafeProjectSql = unsafeProjectSqlTerms . (:[])
-- | Polymorphic projection of SQL null value.
valueNull :: SqlProjectable p => p (Maybe a)
valueNull = unsafeProjectSql "NULL"
unsafeValueNull :: SqlProjectable p => p (Maybe a)
unsafeValueNull = unsafeProjectSql "NULL"
-- | Generate polymorphic projection of SQL constant values from Haskell value.
value :: (ShowConstantSQL t, SqlProjectable p) => t -> p t
@ -194,6 +194,12 @@ type SqlBinOp = String -> String -> String
sqlBinOp :: String -> SqlBinOp
sqlBinOp = SQLs.defineBinOp . SQL.word
-- | Unsafely make projection unary operator from SQL operator string.
unsafeUniOp :: (SqlProjectable p, ProjectableShowSql p)
=> (String -> String)
-> p a -> p b
unsafeUniOp op = unsafeProjectSql . paren . op . unsafeShowSql
-- | Unsafely make projection binary operator from SQL operator string.
unsafeBinOp :: (SqlProjectable p, ProjectableShowSql p)
=> SqlBinOp
@ -254,6 +260,11 @@ or :: (SqlProjectable p, ProjectableShowSql p)
=> p ft -> p ft -> p (Maybe Bool)
or = compareBinOp SQLs.or
-- | Logical operator corresponding SQL /NOT/ .
not :: (SqlProjectable p, ProjectableShowSql p)
=> p (Maybe Bool) -> p (Maybe Bool)
not = unsafeUniOp SQLs.not
-- | Unsafely make number projection binary operator from SQL operator string.
numBinOp' :: (SqlProjectable p, ProjectableShowSql p, Num a)
=> String -> p a -> p a -> p a
@ -287,8 +298,12 @@ in' = unsafeBinOp (SQLs.in')
-- | Operator corresponding SQL /IS NULL/ .
isNull :: (SqlProjectable p, ProjectableShowSql p)
=> p (Maybe t) -> p (Maybe Bool)
isNull x = compareBinOp (SQLs.defineBinOp SQL.IS) x valueNull
isNull x = compareBinOp (SQLs.defineBinOp SQL.IS) x unsafeValueNull
-- | Operator corresponding SQL /NOT (... IS NULL)/ .
isNotNull :: (SqlProjectable p, ProjectableShowSql p)
=> p (Maybe t) -> p (Maybe Bool)
isNotNull = not . isNull
-- | Placeholder parameter type which has real parameter type arguemnt 'p'.
data PlaceHolders p = PlaceHolders

View File

@ -25,10 +25,10 @@ module Language.SQL.Keyword.Concat (
(.||.),
(.=.), (.<.), (.<=.), (.>.), (.>=.), (.<>.),
and, or, in'
and, or, not, in',
) where
import Prelude hiding (and, or)
import Prelude hiding (and, or, not)
import Data.List (intersperse)
import Language.SQL.Keyword.Type (Keyword (..), word, wordShow, unwordsSQL)
@ -116,6 +116,10 @@ and = defineBinOp AND
or :: Keyword -> Keyword -> Keyword
or = defineBinOp OR
-- | Uni `NOT` operator for SQL boolean expression.
not :: Keyword -> Keyword
not e = unwords' [NOT, e]
-- | Binary `IN` operator for SQL.
in' :: Keyword -> Keyword -> Keyword
in' = defineBinOp IN

View File

@ -22,11 +22,11 @@ module Language.SQL.Keyword.ConcatString (
(.||.),
(.=.), (.<.), (.<=.), (.>.), (.>=.), (.<>.),
and, or, in'
and, or, not, in'
) where
import Prelude hiding (and, or)
import Prelude hiding (and, or, not)
import Data.List (intersperse)
import Language.SQL.Keyword.Type (Keyword (..), wordShow)
@ -85,6 +85,10 @@ and = defineBinOp AND
or :: String -> String -> String
or = defineBinOp OR
-- | Uni `NOT` operator for SQL boolean expression.
not :: String -> String
not e = unwords [wordShow NOT, e]
-- | Binary `IN` operator for SQL.
in' :: String -> String -> String
in' = defineBinOp IN