HORIZON HASKELLDocslts/ghc-10.0.x8c2f8e82026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8c2f8e8 · 2026-10-19

Moduleopaleye-0.10.8.0Haskell2010

Opaleye.Table

Table fields can be required or optional and, independently, nullable or non-nullable.

A required non-nullable SqlInt4 (for example) is defined with requiredTableField and gives rise to a

TableFields (Field SqlInt4) (Field SqlInt4)

The leftmost argument is the type of writes. When you insert or update into this field you must give it a Field SqlInt4 (which you can define with sqlInt4 :: Int -> Field SqlInt4).

A required nullable SqlInt4 is defined with requiredTableField and gives rise to a

TableFields (FieldNullable SqlInt4) (FieldNullable SqlInt4)

When you insert or update into this field you must give it a FieldNullable SqlInt4, which you can define either with sqlInt4 and toNullable :: Field a -> FieldNullable a, or with null :: FieldNullable a.

An optional non-nullable SqlInt4 is defined with optionalTableField and gives rise to a

TableFields (Maybe (Field SqlInt4)) (Field SqlInt4)

Optional fields are those that can be omitted on writes, such as those that have DEFAULTs or those that are SERIAL. When you insert or update into this field you must give it a Maybe (Field SqlInt4). If you provide Nothing then the field will be omitted from the query and the default value will be used. Otherwise you have to provide a Just containing a Field SqlInt4.

An optional nullable SqlInt4 is defined with optionalTableField and gives rise to a

TableFields (Maybe (FieldNullable SqlInt4)) (FieldNullable SqlInt4)

Optional fields are those that can be omitted on writes, such as those that have DEFAULTs or those that are SERIAL. When you insert or update into this field you must give it a Maybe (FieldNullable SqlInt4). If you provide Nothing then the default value will be used. Otherwise you have to provide a Just containing a FieldNullable SqlInt4 (which can be null).

  • 2 types
  • 1 class
  • 8 values
  • Packageopaleye-0.10.8.0
  • Exports12
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTable.hs

Defining tables

8 declarations
valuetable
  1. :: String

    Table name

  2. -> TableFields writeFields viewFields
  3. -> Table writeFields viewFields
#

Define a table with an unqualified name.

datadata Table writeFields viewFields
#

Define a table as follows, where "id", "color", "location", "quantity" and "radius" are the table's fields in Postgres and the types are given in the type signature. The id field is an autoincrementing field (i.e. optional for writes).

data Widget a b c d e = Widget { wid      :: a
                               , color    :: b
                               , location :: c
                               , quantity :: d
                               , radius   :: e }

$(makeAdaptorAndInstanceInferrable "pWidget" ''Widget)

widgetTable :: Table (Widget (Maybe (Field SqlInt4)) (Field SqlText) (Field SqlText)
                             (Field SqlInt4) (Field SqlFloat8))
                     (Widget (Field SqlInt4) (Field SqlText) (Field SqlText)
                             (Field SqlInt4) (Field SqlFloat8))
widgetTable = table "widgetTable"
                     (pWidget Widget { wid      = tableField "id"
                                     , color    = tableField "color"
                                     , location = tableField "location"
                                     , quantity = tableField "quantity"
                                     , radius   = tableField "radius" })
Instances2Profunctor, Functor
  • Profunctor TableDefined in opaleye-0.10.8.0 · Opaleye.Internal.Table
  • Functor (Table a)Defined in opaleye-0.10.8.0 · Opaleye.Internal.Table

optionalTableField is for fields that you can be set to DEFAULT on writes, such as fields which have defaults or which are SERIAL. Setting the write value to Nothing uses SQL DEFAULT in the generated update.

classclass InferrableTableField w (n :: Nullability) r | w -> n, w -> r where
#

You should not define your own instances of InferrableTableField.

Instances2InferrableTableField
  • InferrableTableField (Maybe (Field_ n r)) n rDefined in opaleye-0.10.8.0 · Opaleye.Internal.Table

    Equivalent to defining the column with optionalTableField. If the write type is Maybe (Field_ n r) (i.e. DEFAULT can be written to it) then the write type is Field_ n r.

  • InferrableTableField (Field_ n r) n rDefined in opaleye-0.10.8.0 · Opaleye.Internal.Table

    Equivalent to defining the column with requiredTableField. If the write type is Field_ n r then the read type is also Field_ n r.

Selecting from tables

1 declaration
valueselectTable
  1. :: Default Unpackspec fields fields
  2. => Table a fields

    ͘

  3. -> Select fields
#

Example type specialization:

selectTable :: Table w (Field a, Field b)
            -> Select (Field a, Field b)

Assuming the makeAdaptorAndInstanceInferrable splice has been run for the product type Foo:

selectTable :: Table w (Foo (Field a) (Field b) (Field c))
            -> Select (Foo (Field a) (Field b) (Field c))

Data types

1 declaration

Explicit versions

1 declaration

Deprecated versions

1 declaration