{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}


{- |

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

 A required non-nullable @SqlInt4@ (for example) is defined with
 'T.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 column you must give it a @Field SqlInt4@ (which you
 can define with @sqlInt4 :: Int -> Field SqlInt4@).

 A required nullable @SqlInt4@ is defined with 'T.requiredTableField' and gives rise
 to a

 @
 TableFields (FieldNullable SqlInt4) (FieldNullable SqlInt4)
 @

 When you insert or update into this column 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 'T.optionalTableField' and gives
 rise to a

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

 Optional columns are those that can be omitted on writes, such as
 those that have @DEFAULT@s or those that are @SERIAL@.
 When you insert or update into this column you must give it a @Maybe
 (Field SqlInt4)@. If you provide @Nothing@ then the column 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 'T.optionalTableField' and gives
 rise to a

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

 Optional columns are those that can be omitted on writes, such as
 those that have @DEFAULT@s or those that are @SERIAL@.
 When you insert or update into this column 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).

-}

module Opaleye.Table (-- * Defining tables
                      table,
                      tableWithSchema,
                      T.Table,
                      T.tableField,
                      T.optionalTableField,
                      T.readOnlyTableField,
                      T.requiredTableField,
                      -- * Selecting from tables
                      selectTable,
                      -- * Data types
                      T.TableColumns,
                      TableFields,
                      -- * Explicit versions
                      selectTableExplicit,
                      -- * Deprecated
                      T.optional,
                      T.readOnly,
                      T.required,
                      T.tableColumn,
                      View,
                      Writer,
                      T.Table(T.Table, T.TableWithSchema),
                      queryTable,
                      queryTableExplicit) where

import qualified Opaleye.Internal.QueryArr as Q
import qualified Opaleye.Internal.Table as T
import           Opaleye.Internal.Table (View, Table, Writer,
                                         TableFields)

import qualified Opaleye.Internal.Tag as Tag
import qualified Opaleye.Internal.Unpackspec as U

import qualified Opaleye.Select                  as S

import qualified Data.Profunctor.Product.Default as D

-- | Example type specialization:
--
-- @
-- selectTable :: Table w (Field a, Field b)
--             -> Select (Field a, Field b)
-- @
--
-- Assuming the @makeAdaptorAndInstance@ 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))
-- @
selectTable :: D.Default U.Unpackspec fields fields
            => Table a fields
            -- ^
            -> S.Select fields
selectTable :: Table a fields -> Select fields
selectTable = Unpackspec fields fields -> Table a fields -> Select fields
forall tablefields fields a.
Unpackspec tablefields fields
-> Table a tablefields -> Select fields
selectTableExplicit Unpackspec fields fields
forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def

-- | Define a table with an unqualified name.
table :: String
      -- ^ Table name
      -> TableFields writeFields viewFields
      -> Table writeFields viewFields
table :: String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
table = String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
forall writeFields viewFields.
String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
T.Table

-- | Define a table with a qualified name.
tableWithSchema :: String
                -- ^ Schema name
                -> String
                -- ^ Table name
                -> TableFields writeFields viewFields
                -> Table writeFields viewFields
tableWithSchema :: String
-> String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
tableWithSchema = String
-> String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
forall writeFields viewFields.
String
-> String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
T.TableWithSchema

-- * Explicit versions

selectTableExplicit :: U.Unpackspec tablefields fields
                    -- ^
                    -> Table a tablefields
                    -- ^
                    -> S.Select fields
selectTableExplicit :: Unpackspec tablefields fields
-> Table a tablefields -> Select fields
selectTableExplicit Unpackspec tablefields fields
cm Table a tablefields
table' = (((), Tag) -> (fields, PrimQuery, Tag)) -> Select fields
forall a b. ((a, Tag) -> (b, PrimQuery, Tag)) -> QueryArr a b
Q.productQueryArr ((), Tag) -> (fields, PrimQuery, Tag)
f where
  f :: ((), Tag) -> (fields, PrimQuery, Tag)
f ((), Tag
t0) = (fields
retwires, PrimQuery
primQ, Tag -> Tag
Tag.next Tag
t0) where
    (fields
retwires, PrimQuery
primQ) = Unpackspec tablefields fields
-> Table a tablefields -> Tag -> (fields, PrimQuery)
forall viewColumns columns writeColumns.
Unpackspec viewColumns columns
-> Table writeColumns viewColumns -> Tag -> (columns, PrimQuery)
T.queryTable Unpackspec tablefields fields
cm Table a tablefields
table' Tag
t0

-- * Deprecated versions

{-# DEPRECATED queryTable "Use 'selectTable' instead.  Will be removed in version 0.8." #-}
queryTable :: D.Default U.Unpackspec fields fields =>
              Table a fields -> S.Select fields
queryTable :: Table a fields -> Select fields
queryTable = Table a fields -> Select fields
forall fields a.
Default Unpackspec fields fields =>
Table a fields -> Select fields
selectTable

{-# DEPRECATED queryTableExplicit "Use 'selectTableExplicit' instead.  Will be removed in version 0.8." #-}
queryTableExplicit :: U.Unpackspec tablefields fields ->
                     Table a tablefields -> S.Select fields
queryTableExplicit :: Unpackspec tablefields fields
-> Table a tablefields -> Select fields
queryTableExplicit = Unpackspec tablefields fields
-> Table a tablefields -> Select fields
forall tablefields fields a.
Unpackspec tablefields fields
-> Table a tablefields -> Select fields
selectTableExplicit