opaleye-0.8.0.1: An SQL-generating DSL targeting PostgreSQL
Safe HaskellNone
LanguageHaskell2010

Opaleye.Operators

Synopsis

Restriction operators

where_ :: Field SqlBool -> Select () Source #

Keep only the rows of a query satisfying a given condition, using an SQL WHERE clause. It is equivalent to the Haskell function

where_ :: Bool -> [()]
where_ True  = [()]
where_ False = []

restrict :: SelectArr (Field SqlBool) () Source #

You would typically use restrict if you want to write your query using Arrow notation. If you want to use monadic style then where_ will suit you better.

restrictExists :: SelectArr a b -> SelectArr a () Source #

Add a WHERE EXISTS clause to the current query.

restrictNotExists :: SelectArr a b -> SelectArr a () Source #

Add a WHERE NOT EXISTS clause to the current query.

Numerical operators

Numeric Column / Field types are instances of Num and Fractional, so you can use the standard Haskell numerical operators (e.g.. *, /, +, -) on them and you can create them with numerical literals such as 3.14 :: Field SqlFloat8.

(+) :: Num a => a -> a -> a infixl 6 #

(-) :: Num a => a -> a -> a infixl 6 #

(*) :: Num a => a -> a -> a infixl 7 #

(/) :: Fractional a => a -> a -> a infixl 7 #

Fractional division.

fromInteger :: Num a => Integer -> a #

Conversion from an Integer. An integer literal represents the application of the function fromInteger to the appropriate value of type Integer, so such literals have type (Num a) => a.

abs :: Num a => a -> a #

Absolute value.

negate :: Num a => a -> a #

Unary negation.

signum :: Num a => a -> a #

Sign of a number. The functions abs and signum should satisfy the law:

abs x * signum x == x

For real numbers, the signum is either -1 (negative), 0 (zero) or 1 (positive).

Equality operators

(.==) :: Column a -> Column a -> Field SqlBool infix 4 Source #

(./=) :: Column a -> Column a -> Field SqlBool infix 4 Source #

(.===) :: Default EqPP fields fields => fields -> fields -> Field SqlBool infix 4 Source #

A polymorphic equality operator that works for all types that you have run makeAdaptorAndInstance on. This may be unified with .== in a future version.

(./==) :: Default EqPP fields fields => fields -> fields -> Field SqlBool infix 4 Source #

A polymorphic inequality operator that works for all types that you have run makeAdaptorAndInstance on. This may be unified with ./= in a future version.

Comparison operators

(.>) :: SqlOrd a => Column a -> Column a -> Field SqlBool infix 4 Source #

(.<) :: SqlOrd a => Column a -> Column a -> Field SqlBool infix 4 Source #

(.<=) :: SqlOrd a => Column a -> Column a -> Field SqlBool infix 4 Source #

(.>=) :: SqlOrd a => Column a -> Column a -> Field SqlBool infix 4 Source #

Numerical operators

quot_ :: SqlIntegral a => Column a -> Column a -> Column a Source #

Integral division, named after quot. It maps to the / operator in Postgres.

rem_ :: SqlIntegral a => Column a -> Column a -> Column a Source #

The remainder of integral division, named after rem. It maps to MOD (%) in Postgres, confusingly described as "modulo (remainder)".

Conditional operators

case_ :: [(Field SqlBool, Column a)] -> Column a -> Column a Source #

Select the first case for which the condition is true.

ifThenElse :: Field SqlBool -> Column a -> Column a -> Column a Source #

Monomorphic if/then/else.

This may be replaced by ifThenElseMany in a future version.

ifThenElseMany :: Default IfPP fields fields => Field SqlBool -> fields -> fields -> fields Source #

Polymorphic if/then/else.

Logical operators

(.||) :: Field SqlBool -> Field SqlBool -> Field SqlBool infixr 2 Source #

Boolean or

(.&&) :: Field SqlBool -> Field SqlBool -> Field SqlBool infixr 3 Source #

Boolean and

not :: Field SqlBool -> Field SqlBool Source #

Boolean not

ors :: Foldable f => f (Field SqlBool) -> Field SqlBool Source #

True when any element of the container is true

Text operators

lower :: Field SqlText -> Field SqlText Source #

To lowercase

upper :: Field SqlText -> Field SqlText Source #

To uppercase

like :: Field SqlText -> Field SqlText -> Field SqlBool Source #

Postgres LIKE operator

ilike :: Field SqlText -> Field SqlText -> Field SqlBool Source #

Postgres ILIKE operator

Containment operators

in_ :: (Functor f, Foldable f) => f (Column a) -> Column a -> Field SqlBool Source #

in_ is designed to be used in prefix form.

in_ validProducts product checks whether product is a valid product. in_ validProducts is a function which checks whether a product is a valid product.

inSelect :: Default EqPP fields fields => fields -> Select fields -> Select (Field SqlBool) Source #

True if the first argument occurs amongst the rows of the second, false otherwise.

This operation is equivalent to Postgres's IN operator.

JSON operators

class SqlIsJson a Source #

Class of Postgres types that represent json values. Used to overload functions and operators that work on both SqlJson and SqlJsonb.

Warning: making additional instances of this class can lead to broken code!

Instances

Instances details
SqlIsJson SqlJsonb Source # 
Instance details

Defined in Opaleye.Operators

SqlIsJson SqlJson Source # 
Instance details

Defined in Opaleye.Operators

class SqlJsonIndex a Source #

Class of Postgres types that can be used to index json values.

Warning: making additional instances of this class can lead to broken code!

Instances

Instances details
SqlJsonIndex SqlText Source # 
Instance details

Defined in Opaleye.Operators

SqlJsonIndex SqlInt4 Source # 
Instance details

Defined in Opaleye.Operators

SqlJsonIndex SqlInt8 Source # 
Instance details

Defined in Opaleye.Operators

(.->) infixl 8 Source #

Arguments

:: (SqlIsJson a, SqlJsonIndex k) 
=> FieldNullable a 
-> Field k

key or index

-> FieldNullable a 

Get JSON object field by key.

(.->>) infixl 8 Source #

Arguments

:: (SqlIsJson a, SqlJsonIndex k) 
=> FieldNullable a 
-> Field k

key or index

-> FieldNullable SqlText 

Get JSON object field as text.

(.#>) infixl 8 Source #

Arguments

:: SqlIsJson a 
=> FieldNullable a 
-> Column (SqlArray SqlText)

path

-> FieldNullable a 

Get JSON object at specified path.

(.#>>) infixl 8 Source #

Get JSON object at specified path as text.

(.@>) :: Field SqlJsonb -> Field SqlJsonb -> Field SqlBool infix 4 Source #

Does the left JSON value contain within it the right value?

(.<@) :: Field SqlJsonb -> Field SqlJsonb -> Field SqlBool infix 4 Source #

Is the left JSON value contained within the right value?

(.?) :: Field SqlJsonb -> Field SqlText -> Field SqlBool infix 4 Source #

Does the key/element string exist within the JSON value?

(.?|) :: Field SqlJsonb -> Column (SqlArray SqlText) -> Field SqlBool infix 4 Source #

Do any of these key/element strings exist?

(.?&) :: Field SqlJsonb -> Column (SqlArray SqlText) -> Field SqlBool infix 4 Source #

Do all of these key/element strings exist?

jsonBuildObject :: JSONBuildObjectFields -> Column SqlJson Source #

Create an SqlJson object from a collection of fields.

Note: This is implemented as a variadic function in postgres, and as such, is limited to 50 arguments, or 25 key-value pairs.

jsonBuildObjectField Source #

Arguments

:: String

Field name

-> Column a

Field value

-> JSONBuildObjectFields 

Given a label and a column, generates a pair for use with jsonBuildObject

SqlArray operators

arrayPrepend :: Column a -> Column (SqlArray a) -> Column (SqlArray a) Source #

Prepend an element to a SqlArray

arrayRemove :: Column a -> Column (SqlArray a) -> Column (SqlArray a) Source #

Remove all instances of an element from a SqlArray

arrayRemoveNulls :: Column (SqlArray (Nullable a)) -> Column (SqlArray a) Source #

Remove all NULL values from a SqlArray

arrayPosition Source #

Arguments

:: Field (SqlArray a)

Haystack

-> Field a

Needle

-> Field (Nullable SqlInt4) 

Postgres's array_position

sqlElem Source #

Arguments

:: Field a

Needle

-> Field (SqlArray a)

Haystack

-> Field SqlBool 

Whether the element (needle) exists in the array (haystack). N.B. this is implemented hackily using array_position. If you need it to be implemented using = any then please open an issue.

Range operators

upperBound :: IsRangeType a => Column (SqlRange a) -> Column (Nullable a) Source #

Access the upper bound of a range. For discrete range types it is the exclusive bound.

lowerBound :: IsRangeType a => Column (SqlRange a) -> Column (Nullable a) Source #

Access the lower bound of a range. For discrete range types it is the inclusive bound.

Other operators

now :: Field SqlTimestamptz Source #

Current date and time (start of current transaction)

class IntervalNum from to | from -> to Source #

IntervalNum from to determines from which date or time types an interval can be added (addInterval) or subtracted (minusInterval) and which is the resulting type.

The instances should correspond to the interval + and - operations listed in:

https://www.postgresql.org/docs/current/functions-datetime.html#OPERATORS-DATETIME-TABLE

Instances

Instances details
IntervalNum SqlTimestamptz SqlTimestamptz Source # 
Instance details

Defined in Opaleye.Operators

IntervalNum SqlTimestamp SqlTimestamp Source # 
Instance details

Defined in Opaleye.Operators

IntervalNum SqlTime SqlTime Source # 
Instance details

Defined in Opaleye.Operators

IntervalNum SqlInterval SqlInterval Source # 
Instance details

Defined in Opaleye.Operators

IntervalNum SqlDate SqlTimestamp Source # 
Instance details

Defined in Opaleye.Operators

Deprecated

keepWhen :: (a -> Field SqlBool) -> SelectArr a a Source #

Deprecated: Use where_ or restrict instead. Will be removed in version 0.9.