Droplet.Language.Internal.Syntax
- Package
- purescript-droplet
- Repository
- easafe/purescript-droplet
This module defines the entire SQL eDSL, mostly because it'd be a pain to split it
Do not import this module directly, it will break your code and make it not type safe. Use the sanitized Droplet.Language
instead
#SortFieldsSource Source
class SortFieldsSource :: Type -> Row Type -> Type -> Row Type -> Row Type -> Constraint
class SortFieldsSource (s :: Type) (projection :: Row Type) (f :: Type) (fields :: Row Type) (available :: Row Type) | s -> available
Fields available for sorting this query
N.B: SELECT DISTINCT queries can only be sorted by fields in the projection
Instances
SortFieldsSource (Distinct s) projection f fields projection
(SourceAlias f alias, RowToList fields list, QualifiedFields list alias qual, Union projection fields pf, Union qual pf all) => SortFieldsSource s projection f fields all
#IncludeColumn Source
class IncludeColumn :: Symbol -> Type -> RowList Symbol -> RowList Type -> Constraint
class IncludeColumn (name :: Symbol) (t :: Type) (aliases :: SymbolList) (included :: RowList Type) | name -> included
Build a RowList
of the given name and type qualified with each alias
Instances
IncludeColumn name t Nil Nil
(AppendPath alias name fullPath, IncludeColumn name t rest included) => IncludeColumn name t (Cons alias alias rest) (Cons fullPath t included)
#UnwrapAll Source
#Resume Source
class Resume a b c | a -> b, a b -> c where
Most SQL statement constructors accept a rest
type parameter that refers to next statements
Such parameter is initially filled with E
, meaning that the query ends there
This type class replaces the (nested) final E
for the next statement
Members
resume :: a -> b -> c
Instances
(Resume rest b c) => Resume (Select s p rest) b (Select s p c)
(Resume rest b c) => Resume (From f fd rest) b (From f fd c)
(Resume rest b c) => Resume (Where cd rest) b (Where cd c)
(Resume rest b c) => Resume (GroupBy f rest) b (GroupBy f c)
(Resume rest b c) => Resume (OrderBy f rest) b (OrderBy f c)
(Resume rest b c) => Resume (Limit rest) b (Limit c)
(Resume rest b c) => Resume (Offset rest) b (Offset c)
(Resume rest b c) => Resume (Update n f rest) b (Update n f c)
(Resume rest b c) => Resume (Insert rest) b (Insert c)
(Resume rest b c) => Resume (Into n f fd rest) b (Into n f fd c)
(Resume rest b c) => Resume (Values v rest) b (Values v c)
(Resume rest b c) => Resume (Set p rest) b (Set p c)
(Resume rest b c) => Resume (Delete rest) b (Delete c)
Resume (As alias E) b (As alias b)
Resume E b b
(Resume b a c) => Resume a b c
#StarProjection Source
class StarProjection :: RowList Type -> Row Type -> RowList Symbol -> Row Type -> Constraint
class StarProjection (list :: RowList Type) (fields :: Row Type) (aliases :: SymbolList) (projection :: Row Type) | list -> projection
SELECT * FROM should:
- Display column unqualified if it appear both as qualified and unqualified
- Display column qualified if is projected with
Path
Instances
(IncludeAllColumns list aliases included, ListToRow included extended, Union fields extended all, Nub all nubbed, Union extended unqualified nubbed, RowToList unqualified ulist, UnwrapAll ulist projection) => StarProjection list fields aliases projection
#SymbolListSingleton Source
class SymbolListSingleton :: Symbol -> RowList Symbol -> Constraint
class SymbolListSingleton (alias :: Symbol) (list :: SymbolList) | alias -> list
Creates a SymbolList
single with a single entry
Instances
SymbolListSingleton alias (Cons alias alias Nil)
#SourceAlias Source
class SourceAlias :: Type -> Symbol -> Constraint
class SourceAlias (f :: Type) (alias :: Symbol) | f -> alias
Table/subquery alias or Empty
Instances
SourceAlias (As alias (Table name fields)) alias
(QueryOptionallyAliased rest Empty alias) => SourceAlias (Select s p (From f fd rest)) alias
SourceAlias f Empty
#QueryMustBeAliased Source
class QueryMustBeAliased :: Type -> Symbol -> Constraint
class QueryMustBeAliased (q :: Type) (alias :: Symbol) | q -> alias
Find this query's alias, or fail at compile time if query is not aliased
Instances
(QueryMustBeAliased rest alias) => QueryMustBeAliased (Where cd rest) alias
(QueryMustBeAliased rest alias) => QueryMustBeAliased (GroupBy f rest) alias
(QueryMustBeAliased rest alias) => QueryMustBeAliased (OrderBy f rest) alias
(QueryMustBeAliased rest alias) => QueryMustBeAliased (Limit rest) alias
(QueryMustBeAliased rest alias) => QueryMustBeAliased (Offset rest) alias
(Fail (Text "Expected query to end in AS statement")) => QueryMustBeAliased E alias
QueryMustBeAliased (As alias E) alias
#UniqueSources Source
class UniqueSources :: Row Type -> Row Type -> Constraint
class UniqueSources (some :: Row Type) (more :: Row Type)
Joined tables should not be the same
Instances
(Fail (Text "Cannot JOIN source to itself")) => UniqueSources fields fields
UniqueSources some more
#OuterScopeAlias Source
class OuterScopeAlias :: RowList Symbol -> RowList Symbol -> Boolean -> Constraint
class OuterScopeAlias (all :: SymbolList) (unique :: SymbolList) (y :: Boolean) | all unique -> y
Is this column present in the current field source?
Instances
OuterScopeAlias Nil Nil True
OuterScopeAlias (Cons alias alias some) Nil False
OuterScopeAlias Nil (Cons alias alias some) False
(OuterScopeAlias some more y) => OuterScopeAlias (Cons alias alias some) (Cons alias alias more) y
OuterScopeAlias (Cons alias alias some) (Cons otherAlias otherAlias more) False
#OnCondition Source
class OnCondition :: Type -> Row Type -> RowList Symbol -> Constraint
class OnCondition (c :: Type) (fields :: Row Type) (aliases :: SymbolList)
Comparision logic for ON statements
Instances
(OnCondition (Op a b) fields aliases, OnCondition (Op c d) fields aliases) => OnCondition (Op (Op a b) (Op c d)) fields aliases
(OnComparision a fields aliases t, OnComparision b fields aliases u, ValidComparision t u) => OnCondition (Op a b) fields aliases
#QueryOptionallyAliased Source
class QueryOptionallyAliased :: Type -> Symbol -> Symbol -> Constraint
class QueryOptionallyAliased (q :: Type) (name :: Symbol) (alias :: Symbol) | q -> name alias
If this query is in the form of (SELECT ...) AS alias, return alias
, otherwise keep name
Instances
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (Where cd rest) name alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (GroupBy f rest) name alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (OrderBy f rest) name alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (Limit rest) name alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (Offset rest) name alias
QueryOptionallyAliased E name name
QueryOptionallyAliased (As alias E) name alias
#ToJoin Source
class ToJoin :: Type -> Row Type -> RowList Symbol -> Constraint
class ToJoin (q :: Type) (fields :: Row Type) (aliases :: SymbolList) | q -> fields aliases
Given a source q
, compute its joined (non and qualified) fields
Instances
(ToJoin l left las, ToJoin r right ras, Union left right all, Nub all fields, RowListAppend las ras aliases) => ToJoin (Join Inner fd l r a (On c rest)) fields aliases
(ToJoin l left las, ToJoin r right ras, RowToList right list, ToOuterFields list out, Union left out all, Nub all fields, RowListAppend las ras aliases) => ToJoin (Join Outer fd l r a (On c rest)) fields aliases
(SourceFields q fields aliases) => ToJoin q fields aliases
#QualifiedColumn Source
class QualifiedColumn :: Boolean -> Symbol -> Row Type -> Type -> Constraint
class QualifiedColumn (unscoped :: Boolean) (fullPath :: Symbol) (fields :: Row Type) (t :: Type) | unscoped -> fullPath fields t
The type of a qualified column
Instances
QualifiedColumn True fullPath fields OuterScope
(Cons fullPath t d fields, UnwrapDefinition t u, UnwrapNullable u v) => QualifiedColumn False fullPath fields v
#OnComparision Source
class OnComparision :: Type -> Row Type -> RowList Symbol -> Type -> Constraint
class OnComparision (a :: Type) (fields :: Row Type) (aliases :: SymbolList) (t :: Type) | a -> t
Instances
(Cons name t d fields, UnwrapDefinition t u, UnwrapNullable u v) => OnComparision (Proxy name) fields aliases v
(SymbolListSingleton alias single, RowListAppend single aliases all, RowListNub all unique, OuterScopeAlias all unique y, AppendPath alias name fullPath, QualifiedColumn y fullPath fields t) => OnComparision (Path alias name) fields aliases t
OnComparision (Path alias name) fields aliases OuterScope
(ToValue t) => OnComparision t fields aliases t
#Join Source
data Join :: Side -> Row Type -> Type -> Type -> RowList Symbol -> Type -> Type
data Join (k :: Side) (fields :: Row Type) q r (aliases :: SymbolList) rest
Constructors
Join q r rest
Instances
(ToProjection s fields aliases selected, Nub selected unique, UniqueColumnNames selected unique) => ToFrom (Join k fields l r aliases (On c rest)) (Select s unique E) fields
(ToJoin l left las, ToJoin r right ras, Union left right all, Nub all fields, RowListAppend las ras aliases) => ToJoin (Join Inner fd l r a (On c rest)) fields aliases
JOIN ... ON
(ToJoin l left las, ToJoin r right ras, RowToList right list, ToOuterFields list out, Union left out all, Nub all fields, RowListAppend las ras aliases) => ToJoin (Join Outer fd l r a (On c rest)) fields aliases
GroupBySource (Join k fields q r a rest) fields
#SymbolList Source
type SymbolList = RowList Symbol
A RowList
of Symbol
s
#join Source
join :: forall r l aliases unique las ras right rf lf left all source fields. ToJoin l left las => ToJoin r right ras => UniqueSources left right => RowListAppend las ras aliases => RowListNub aliases unique => UniqueAliases aliases unique => Union right left all => Nub all source => Union left lf source => Union right rf source => Union lf rf fields => l -> r -> Join Inner fields l r aliases E
INNER JOIN statement
JOIN sources are the same as FROM
#leftJoin Source
leftJoin :: forall r l las ras list out aliases unique rf lf right left all fields source. ToJoin l left las => ToJoin r right ras => UniqueSources left right => RowListAppend las ras aliases => RowListNub aliases unique => UniqueAliases aliases unique => Union left right all => Nub all source => Union left lf source => Union right rf source => RowToList lf list => ToOuterFields list out => Union rf out fields => l -> r -> Join Outer fields l r aliases E
LEFT OUTER JOIN statement
JOIN sources are the same as FROM
#ValidGroupByProjection Source
class ValidGroupByProjection :: Type -> Row Type -> Constraint
class ValidGroupByProjection (s :: Type) (grouped :: Row Type) | s -> grouped
Asserts that a SELECT ... GROUP BY projection contains only grouped columns or aggregate functions
Instances
(Cons name t e grouped) => ValidGroupByProjection (Proxy name) grouped
(Cons name t e grouped) => ValidGroupByProjection (As alias (Proxy name)) grouped
(ValidGroupByProjection a grouped, ValidGroupByProjection b grouped) => ValidGroupByProjection (Tuple a b) grouped
(ValidGroupByProjection s grouped) => ValidGroupByProjection (Distinct s) grouped
ValidGroupByProjection q grouped
#GroupByFields Source
class GroupByFields :: Type -> Row Type -> Row Type -> Constraint
class GroupByFields (f :: Type) (fields :: Row Type) (grouped :: Row Type) | f -> fields grouped
Instances
(Cons name t e fields, Cons name t () grouped) => GroupByFields (Proxy name) fields grouped
(AppendPath alias name fullPath, Cons fullPath t e fields, Cons fullPath t () g, Cons name t g grouped) => GroupByFields (Path alias name) fields grouped
(GroupByFields a fields some, GroupByFields b fields more, Union some more grouped) => GroupByFields (Tuple a b) fields grouped
#ToGroupBy Source
#ToOuterFields Source
class ToOuterFields :: RowList Type -> Row Type -> Constraint
class ToOuterFields (list :: RowList Type) (fields :: Row Type) | list -> fields
OUTER JOINs make one side nullable, as a corresponding record may not be found
For ease of use, this class marks the nullable side fields with Joined
, later on ToProjection
will flatten it to Maybe
Instances
ToOuterFields Nil ()
(Cons name (Joined t) () head, ToOuterFields rest tail, Union head tail all) => ToOuterFields (Cons name (Joined t) rest) all
Avoid nesting
Joined
s(Cons name (Joined t) () head, ToOuterFields rest tail, Union head tail all) => ToOuterFields (Cons name t rest) all
#ToUnion Source
class ToUnion (q :: Type) (r :: Type)
Instances
(RowToList some slist, RowToList more mlist, CompatibleProjection slist mlist) => ToUnion (Select s some (From f fd rt)) (Select t more (From g ge es))
(ToUnion (Select s p (From f fd rt)) (Select t q (From g ge es))) => ToUnion (Union (Select s p (From f fd rt)) sel) (Select t q (From g ge es))
(ToUnion (Select s p (From f fd rt)) (Select t q (From g ge es))) => ToUnion (Select s p (From f fd rt)) (Union (Select t q (From g ge es)) sel)
#RequiredFields Source
class RequiredFields :: RowList Type -> Row Type -> Constraint
class RequiredFields (fieldList :: RowList Type) (required :: Row Type) | fieldList -> required
Instances
RequiredFields Nil ()
(RequiredFields rest required) => RequiredFields (Cons n (Auto t) rest) required
(RequiredFields rest required) => RequiredFields (Cons n (Default t) rest) required
(RequiredFields rest required) => RequiredFields (Cons n (Maybe t) rest) required
(RequiredFields rest tail, Cons name t () head, Lacks name tail, Union head tail required) => RequiredFields (Cons name t rest) required
#ToAs Source
#ToFrom Source
class ToFrom :: Type -> Type -> Row Type -> Constraint
class ToFrom (f :: Type) (q :: Type) (fields :: Row Type) | q f -> fields
Acceptable sources for FROM statements
Instances
ToFrom (Table name fields) (Delete E) fields
(DELETE) FROM table
(ToProjection s fields aliases selected, Nub selected unique, UniqueColumnNames selected unique) => ToFrom (Join k fields l r aliases (On c rest)) (Select s unique E) fields
(SourceFields f fields aliases, ToProjection s fields aliases selected, Nub selected unique, UniqueColumnNames selected unique) => ToFrom f (Select s unique E) fields
#GroupBySource Source
class GroupBySource :: Type -> Row Type -> Constraint
class GroupBySource (f :: Type) (fields :: Row Type) | f -> fields
Instances
GroupBySource (Table name fields) fields
(RowToList fields list, QualifiedFields list alias aliased, Union aliased fields all) => GroupBySource (As alias (Table name fields)) all
GroupBySource (Join k fields q r a rest) fields
(QueryMustBeAliased rest alias, RowToList projection list, QualifiedFields list alias aliased, Union aliased projection all) => GroupBySource (Select s projection (From f fd rest)) all
#InsertList Source
class InsertList :: Row Type -> Type -> Row Type -> Constraint
class InsertList (fields :: Row Type) (fieldNames :: Type) (inserted :: Row Type) | fieldNames -> fields inserted
Instances
(InvalidField t, Cons name t e fields, Cons name t () single) => InsertList fields (Proxy name) single
(InsertList fields f head, InsertList fields rest tail, Union head tail all) => InsertList fields (Tuple f rest) all
#InsertValues Source
class InsertValues :: Row Type -> Type -> Type -> Constraint
class InsertValues (fields :: Row Type) (fieldNames :: Type) (t :: Type)
Instances
(InsertValues fields (Proxy name) u) => InsertValues fields (Proxy name) (Array u)
Multiple values, single column
(Cons name (Default t) e fields) => InsertValues fields (Proxy name) (Default t)
(UnwrapDefinition t u, Cons name t e fields, ToValue u) => InsertValues fields (Proxy name) u
(InsertValues fields name value, InsertValues fields some more) => InsertValues fields (Tuple name some) (Tuple value more)
(InsertValues fields (Tuple name some) (Tuple value more)) => InsertValues fields (Tuple name some) (Array (Tuple value more))
#ToProjection Source
class ToProjection :: Type -> Row Type -> RowList Symbol -> Row Type -> Constraint
class ToProjection (s :: Type) (fields :: Row Type) (aliases :: SymbolList) (projection :: Row Type) | s -> fields projection
Computes SELECT projection as a Row Type
Instances
(Cons name t e fields, JoinedToMaybe t v, UnwrapDefinition v u, Cons name u () projection) => ToProjection (Proxy name) fields aliases projection
Columns
(SymbolListSingleton alias single, RowListAppend single aliases all, RowListNub all unique, OuterScopeAlias all unique y, AppendPath alias name fullPath, QualifiedColumn y fullPath fields t, Cons fullPath t () projection) => ToProjection (Path alias name) fields aliases projection
(Cons alias Int () projection) => ToProjection (As alias Int) fields aliases projection
(Cons alias t () projection) => ToProjection (As alias (Aggregate inp rest fields t)) fields aliases projection
(Cons alias t () projection) => ToProjection (As alias (PgFunction inp args fields t)) fields aliases projection
(Cons name t e fields, JoinedToMaybe t v, UnwrapDefinition v u, Cons alias u () projection) => ToProjection (As alias (Proxy name)) fields aliases projection
(SymbolListSingleton table single, RowListAppend single aliases all, RowListNub all unique, OuterScopeAlias all unique y, AppendPath table name fullPath, QualifiedColumn y fullPath fields t, Cons alias t () projection) => ToProjection (As alias (Path table name)) fields aliases projection
(RowToList fields list, StarProjection list fields aliases projection) => ToProjection Star fields aliases projection
(ToProjection s fields aliases some, ToProjection t fields aliases more, Union some more projection) => ToProjection (Tuple s t) fields aliases projection
(SourceAlias f table, SymbolListSingleton table single, ToProjection s fields single pro, RowToList pro list, ToSingleColumn list name t, QueryOptionallyAliased rest name alias, Cons alias t () projection) => ToProjection (Select s p (From f fields rest)) fd aliases projection
(ToProjection s fields aliases projection) => ToProjection (Distinct s) fields aliases projection
(Fail (Text "Cannot recognize projection")) => ToProjection x f a p
#ToSelect Source
class ToSelect (s :: Type)
Acceptable column type for SELECT statements
Instances
ToSelect (Proxy name)
ToSelect (Path table name)
ToSelect (As alias Int)
ToSelect (As alias (Proxy name))
ToSelect (As alias (Path table name))
ToSelect (As alias (Aggregate inp rest fields out))
ToSelect (As alias (PgFunction inp arg fields out))
(ToSelect r, ToSelect t) => ToSelect (Tuple r t)
ToSelect Star
ToSelect (Distinct s)
(ToSubExpression q) => ToSelect q
#ToSingleColumn Source
class ToSingleColumn :: RowList Type -> Symbol -> Type -> Constraint
class ToSingleColumn (fields :: RowList Type) (name :: Symbol) (t :: Type) | fields -> name t
Instances
ToSingleColumn (Cons name (Maybe t) Nil) name (Maybe t)
ToSingleColumn (Cons name t Nil) name (Maybe t)
#ToSubExpression Source
class ToSubExpression (s :: Type)
Only single columns can be projected by subqueries
Note: column subqueries may not return a value, thus their projection will be Maybe
unless the original column type is already Maybe
Instances
ToSubExpression (Select (Proxy name) projection rest)
ToSubExpression (Select (Path table name) projection rest)
ToSubExpression (Select (As alias Int) projection rest)
ToSubExpression (Select (As alias (Proxy name)) projection rest)
ToSubExpression (Select (As alias (Path table name)) projection rest)
ToSubExpression (Select (As alias (PgFunction inp arg fields out)) projection rest)
ToSubExpression (Select (As alias (Aggregate inp r fields out)) projection rest)
(Fail (Text "Subquery must return a single column")) => ToSubExpression (Select (Tuple a b) projection rest)
#IncludeAllColumns Source
class IncludeAllColumns :: RowList Type -> RowList Symbol -> RowList Type -> Constraint
class IncludeAllColumns (list :: RowList Type) (aliases :: SymbolList) (all :: RowList Type) | list -> all
Recursively call IncludeColumn
on the given list
Instances
IncludeAllColumns Nil aliases Nil
(IncludeColumn name t aliases included, IncludeAllColumns rest aliases more, RowListAppend included more all) => IncludeAllColumns (Cons name t rest) aliases all
#SourceFields Source
class SourceFields :: Type -> Row Type -> RowList Symbol -> Constraint
class SourceFields (f :: Type) (fields :: Row Type) (aliases :: SymbolList) | f -> fields aliases
Given a source f
, compute its (non and qualified) fields
Instances
SourceFields (Table name fields) fields Nil
Tables
(RowToList source list, QualifiedFields list alias aliased, Union aliased source fields, SymbolListSingleton alias single) => SourceFields (As alias (Table name source)) fields single
Aliased tables
(QueryMustBeAliased rest alias, RowToList projection list, QualifiedFields list alias aliased, Union projection aliased fields, SymbolListSingleton alias single) => SourceFields (Select s projection (From f fd rest)) fields single
Aliased subqueries
#ToUpdatePairs Source
class ToUpdatePairs :: Row Type -> Type -> Constraint
class ToUpdatePairs (fields :: Row Type) (pairs :: Type)
Instances
(Cons name (Default t) e fields) => ToUpdatePairs fields (Op (Proxy name) (Default t))
(InvalidField t, UnwrapDefinition t u, ToValue u, Cons name t e fields) => ToUpdatePairs fields (Op (Proxy name) u)
(ToUpdatePairs fields head, ToUpdatePairs fields tail) => ToUpdatePairs fields (Tuple head tail)
#ToReturning Source
class ToReturning (f :: Type) (q :: Type) | q -> f
Instances
(ToReturningFields f fields) => ToReturning f (Insert (Into tn fields fn (Values fv E)))
#ToReturningFields Source
class ToReturningFields :: Type -> Row Type -> Constraint
class ToReturningFields (f :: Type) (fields :: Row Type) | f -> fields
Instances
(Cons name t e fields) => ToReturningFields (Proxy name) fields
(ToReturningFields a fields, ToReturningFields b fields) => ToReturningFields (Tuple a b) fields
#UniqueAliases Source
class UniqueAliases :: RowList Symbol -> RowList Symbol -> Constraint
class UniqueAliases (some :: SymbolList) (more :: SymbolList)
Joined tables should not repeat table aliases
Instances
UniqueAliases aliases aliases
#QualifiedFields Source
class QualifiedFields :: RowList Type -> Symbol -> Row Type -> Constraint
class QualifiedFields (list :: RowList Type) (alias :: Symbol) (fields :: Row Type) | list alias -> fields
Computes all source fields with their alias
Instances
QualifiedFields Nil alias ()
(ToPath alias path, Append path name fullPath, Cons fullPath t () head, QualifiedFields rest alias tail, Union head tail fields) => QualifiedFields (Cons name t rest) alias fields
#On Source
data On c rest
Constructors
On c rest
Instances
(ToProjection s fields aliases selected, Nub selected unique, UniqueColumnNames selected unique) => ToFrom (Join k fields l r aliases (On c rest)) (Select s unique E) fields
(ToJoin l left las, ToJoin r right ras, Union left right all, Nub all fields, RowListAppend las ras aliases) => ToJoin (Join Inner fd l r a (On c rest)) fields aliases
JOIN ... ON
(ToJoin l left las, ToJoin r right ras, RowToList right list, ToOuterFields list out, Union left out all, Nub all fields, RowListAppend las ras aliases) => ToJoin (Join Outer fd l r a (On c rest)) fields aliases
#ToWhere Source
class ToWhere (c :: Type) (q :: Type)
WHERE can only follow FROM, UPDATE and DELETE
Instances
(SourceAlias f alias, ToCondition c fields alias) => ToWhere c (Select s projection (From f fields E))
(ToCondition c fields Empty) => ToWhere c (Update name fields (Set v E))
(ToCondition c fields Empty) => ToWhere c (Delete (From f fields E))
#JoinedToMaybe Source
class JoinedToMaybe (t :: Type) (v :: Type) | t -> v
Joined
fields appear as Maybe
in projections
Instances
JoinedToMaybe (Joined (f (Maybe t))) (Maybe t)
JoinedToMaybe (Joined (Maybe t)) (Maybe t)
(UnwrapDefinition t u) => JoinedToMaybe (Joined t) (Maybe u)
JoinedToMaybe t t
#CompatibleProjection Source
class CompatibleProjection :: RowList Type -> RowList Type -> Constraint
class CompatibleProjection (pro :: RowList Type) (jection :: RowList Type)
Instances
CompatibleProjection Nil Nil
(CompatibleProjection some more) => CompatibleProjection (Cons name t some) (Cons n t more)
(Fail (Text "UNION column types and count must match")) => CompatibleProjection a b
#UniqueColumnNames Source
class UniqueColumnNames :: Row Type -> Row Type -> Constraint
class UniqueColumnNames (some :: Row Type) (more :: Row Type)
Query projections should not repeat column names
Instances
UniqueColumnNames fields fields
#As Source
newtype As :: Symbol -> Type -> Type
newtype As (alias :: Symbol) rest
Constructors
As rest
Instances
ToSelect (As alias Int)
ToSelect (As alias (Proxy name))
ToSelect (As alias (Path table name))
ToSelect (As alias (Aggregate inp rest fields out))
ToSelect (As alias (PgFunction inp arg fields out))
ToSubExpression (Select (As alias Int) projection rest)
ToSubExpression (Select (As alias (Proxy name)) projection rest)
ToSubExpression (Select (As alias (Path table name)) projection rest)
ToSubExpression (Select (As alias (PgFunction inp arg fields out)) projection rest)
ToSubExpression (Select (As alias (Aggregate inp r fields out)) projection rest)
(RowToList fields list, QualifiedFields list alias aliased, Union aliased fields all) => GroupBySource (As alias (Table name fields)) all
(Cons name t e grouped) => ValidGroupByProjection (As alias (Proxy name)) grouped
(Cons alias Int () projection) => ToProjection (As alias Int) fields aliases projection
(Cons alias t () projection) => ToProjection (As alias (Aggregate inp rest fields t)) fields aliases projection
(Cons alias t () projection) => ToProjection (As alias (PgFunction inp args fields t)) fields aliases projection
(Cons name t e fields, JoinedToMaybe t v, UnwrapDefinition v u, Cons alias u () projection) => ToProjection (As alias (Proxy name)) fields aliases projection
(SymbolListSingleton table single, RowListAppend single aliases all, RowListNub all unique, OuterScopeAlias all unique y, AppendPath table name fullPath, QualifiedColumn y fullPath fields t, Cons alias t () projection) => ToProjection (As alias (Path table name)) fields aliases projection
SourceAlias (As alias (Table name fields)) alias
QueryMustBeAliased (As alias E) alias
QueryOptionallyAliased (As alias E) name alias
(RowToList source list, QualifiedFields list alias aliased, Union aliased source fields, SymbolListSingleton alias single) => SourceFields (As alias (Table name source)) fields single
Aliased tables
Resume (As alias E) b (As alias b)
#From Source
data From :: Type -> Row Type -> Type -> Type
data From f (fields :: Row Type) rest
Constructors
From f rest
Instances
ToPrepare (Select s p (From f fields rest))
ToPrepare (Delete (From f fields rest))
(SourceAlias f alias, ToCondition c fields alias) => ToWhere c (Select s projection (From f fields E))
(ToCondition c fields Empty) => ToWhere c (Delete (From f fields E))
(GroupBySource f fields) => ToGroupBy (Select s p (From f fd E)) s fields
(GroupBySource f fields) => ToGroupBy (Select s p (From f fd (Where cond E))) s fields
(QueryMustBeAliased rest alias, RowToList projection list, QualifiedFields list alias aliased, Union aliased projection all) => GroupBySource (Select s projection (From f fd rest)) all
ToAs (Select s p (From f fields rest)) alias
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields E))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (GroupBy g E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd (GroupBy g E))))
ToLimit (Select s projection (From fr fields (OrderBy f E)))
ToLimit (Select s projection (From fr fields (OrderBy f (Offset E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Offset E))))))
ToOffset (Select s projection (From fr fields (OrderBy f E)))
ToOffset (Select s projection (From fr fields (OrderBy f (Limit E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Limit E))))))
(RowToList some slist, RowToList more mlist, CompatibleProjection slist mlist) => ToUnion (Select s some (From f fd rt)) (Select t more (From g ge es))
(ToUnion (Select s p (From f fd rt)) (Select t q (From g ge es))) => ToUnion (Union (Select s p (From f fd rt)) sel) (Select t q (From g ge es))
(ToUnion (Select s p (From f fd rt)) (Select t q (From g ge es))) => ToUnion (Select s p (From f fd rt)) (Union (Select t q (From g ge es)) sel)
(SourceAlias f table, SymbolListSingleton table single, ToProjection s fields single pro, RowToList pro list, ToSingleColumn list name t, QueryOptionallyAliased rest name alias, Cons alias t () projection) => ToProjection (Select s p (From f fields rest)) fd aliases projection
(QueryOptionallyAliased rest Empty alias) => SourceAlias (Select s p (From f fd rest)) alias
(QueryMustBeAliased rest alias, RowToList projection list, QualifiedFields list alias aliased, Union projection aliased fields, SymbolListSingleton alias single) => SourceFields (Select s projection (From f fd rest)) fields single
Aliased subqueries
(Resume rest b c) => Resume (From f fd rest) b (From f fd c)
#OrderBy Source
data OrderBy f rest
Constructors
OrderBy f rest
Instances
(Cons name t e fields, Cons fd g h fields) => ToStringAgg (Proxy name) (OrderBy (Proxy fd) String) fields
(Cons name t e fields) => ToStringAgg (Path table fd) (OrderBy (Proxy name) String) fields
(Cons name t e fields) => ToStringAgg (Proxy name) (OrderBy (Path alias fd) String) fields
ToStringAgg (Path table name) (OrderBy (Path alias fd) String) fields
ToLimit (Select s projection (From fr fields (OrderBy f E)))
ToLimit (Select s projection (From fr fields (OrderBy f (Offset E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Offset E))))))
ToOffset (Select s projection (From fr fields (OrderBy f E)))
ToOffset (Select s projection (From fr fields (OrderBy f (Limit E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Limit E))))))
(QueryMustBeAliased rest alias) => QueryMustBeAliased (OrderBy f rest) alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (OrderBy f rest) name alias
(Resume rest b c) => Resume (OrderBy f rest) b (OrderBy f c)
#ToOrderBy Source
class ToOrderBy (f :: Type) (q :: Type)
ORDER BY must be last statement
Instances
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields E))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (GroupBy g E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd (GroupBy g E))))
ToOrderBy (Proxy name) String
ToOrderBy (Path alias name) String
#SortFields Source
class SortFields :: Type -> Row Type -> Constraint
class SortFields (f :: Type) (fields :: Row Type) | f -> fields
Instances
(Cons name t e fields) => SortFields (Proxy name) fields
(AppendPath alias name fullPath, Cons fullPath t e fields) => SortFields (Path alias name) fields
(Cons name t e fields) => SortFields (Sort (Proxy name)) fields
(AppendPath alias name fullPath, Cons fullPath t e fields) => SortFields (Sort (Path alias name)) fields
(Fail (Text "Cannot sort by void function")) => SortFields (PgFunction input args fields Unit) fields
SortFields (PgFunction input args fields output) fields
(SortFields a fields, SortFields b fields) => SortFields (Tuple a b) fields
#ToLimit Source
class ToLimit (q :: Type)
Instances
ToLimit (Select s projection (From fr fields (OrderBy f E)))
ToLimit (Select s projection (From fr fields (OrderBy f (Offset E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Offset E))))))
#Limit Source
data Limit rest
Constructors
Instances
ToOffset (Select s projection (From fr fields (OrderBy f (Limit E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Limit E))))))
(QueryMustBeAliased rest alias) => QueryMustBeAliased (Limit rest) alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (Limit rest) name alias
(Resume rest b c) => Resume (Limit rest) b (Limit c)
#groupBy Source
groupBy :: forall f s q sql grouped fields. ToGroupBy q s fields => GroupByFields f fields grouped => ValidGroupByProjection s grouped => Resume q (GroupBy f E) sql => f -> q -> sql
GROUP BY statement
#GroupBy Source
data GroupBy f rest
Constructors
GroupBy f rest
Instances
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (GroupBy g E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd (GroupBy g E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Offset E))))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Limit E))))))
(QueryMustBeAliased rest alias) => QueryMustBeAliased (GroupBy f rest) alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (GroupBy f rest) name alias
(Resume rest b c) => Resume (GroupBy f rest) b (GroupBy f c)
#Into Source
#Distinct Source
newtype Distinct s
Constructors
Distinct s
Instances
ToSelect (Distinct s)
(ValidGroupByProjection s grouped) => ValidGroupByProjection (Distinct s) grouped
SortFieldsSource (Distinct s) projection f fields projection
(ToProjection s fields aliases projection) => ToProjection (Distinct s) fields aliases projection
#Select Source
data Select :: Type -> Row Type -> Type -> Type
data Select s (projection :: Row Type) rest
SELECT representation. projection
refers to the final output of this statement
Constructors
Select s rest
Instances
ToPrepare (Select s p (From f fields rest))
ToSubExpression (Select (Proxy name) projection rest)
ToSubExpression (Select (Path table name) projection rest)
ToSubExpression (Select (As alias Int) projection rest)
ToSubExpression (Select (As alias (Proxy name)) projection rest)
ToSubExpression (Select (As alias (Path table name)) projection rest)
ToSubExpression (Select (As alias (PgFunction inp arg fields out)) projection rest)
ToSubExpression (Select (As alias (Aggregate inp r fields out)) projection rest)
(Fail (Text "Subquery must return a single column")) => ToSubExpression (Select (Tuple a b) projection rest)
(ToProjection s fields aliases selected, Nub selected unique, UniqueColumnNames selected unique) => ToFrom (Join k fields l r aliases (On c rest)) (Select s unique E) fields
(SourceFields f fields aliases, ToProjection s fields aliases selected, Nub selected unique, UniqueColumnNames selected unique) => ToFrom f (Select s unique E) fields
(SourceAlias f alias, ToCondition c fields alias) => ToWhere c (Select s projection (From f fields E))
(GroupBySource f fields) => ToGroupBy (Select s p (From f fd E)) s fields
(GroupBySource f fields) => ToGroupBy (Select s p (From f fd (Where cond E))) s fields
(QueryMustBeAliased rest alias, RowToList projection list, QualifiedFields list alias aliased, Union aliased projection all) => GroupBySource (Select s projection (From f fd rest)) all
ToAs (Select s p (From f fields rest)) alias
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields E))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (GroupBy g E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd (GroupBy g E))))
ToLimit (Select s projection (From fr fields (OrderBy f E)))
ToLimit (Select s projection (From fr fields (OrderBy f (Offset E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Offset E))))))
ToOffset (Select s projection (From fr fields (OrderBy f E)))
ToOffset (Select s projection (From fr fields (OrderBy f (Limit E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Limit E))))))
(RowToList some slist, RowToList more mlist, CompatibleProjection slist mlist) => ToUnion (Select s some (From f fd rt)) (Select t more (From g ge es))
(ToUnion (Select s p (From f fd rt)) (Select t q (From g ge es))) => ToUnion (Union (Select s p (From f fd rt)) sel) (Select t q (From g ge es))
(ToUnion (Select s p (From f fd rt)) (Select t q (From g ge es))) => ToUnion (Select s p (From f fd rt)) (Union (Select t q (From g ge es)) sel)
(SourceAlias f table, SymbolListSingleton table single, ToProjection s fields single pro, RowToList pro list, ToSingleColumn list name t, QueryOptionallyAliased rest name alias, Cons alias t () projection) => ToProjection (Select s p (From f fields rest)) fd aliases projection
(QueryOptionallyAliased rest Empty alias) => SourceAlias (Select s p (From f fd rest)) alias
(QueryMustBeAliased rest alias, RowToList projection list, QualifiedFields list alias aliased, Union projection aliased fields, SymbolListSingleton alias single) => SourceFields (Select s projection (From f fd rest)) fields single
Aliased subqueries
(Resume rest b c) => Resume (Select s p rest) b (Select s p c)
#Update Source
#Offset Source
data Offset rest
Constructors
Instances
ToLimit (Select s projection (From fr fields (OrderBy f (Offset E))))
ToLimit (Select s projection (From fr fields (GroupBy fg (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Offset E))))))
(QueryMustBeAliased rest alias) => QueryMustBeAliased (Offset rest) alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (Offset rest) name alias
(Resume rest b c) => Resume (Offset rest) b (Offset c)
#ToOffset Source
class ToOffset (q :: Type)
Instances
ToOffset (Select s projection (From fr fields (OrderBy f E)))
ToOffset (Select s projection (From fr fields (OrderBy f (Limit E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f E))))
ToOffset (Select s projection (From fr fields (GroupBy fg (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Limit E))))))
#Where Source
data Where c rest
Constructors
Where c rest
Instances
(GroupBySource f fields) => ToGroupBy (Select s p (From f fd (Where cond E))) s fields
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd E)))
(SortFieldsSource s projection f fields available, SortFields st available) => ToOrderBy st (Select s projection (From f fields (Where cd (GroupBy g E))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToLimit (Select s projection (From fr fields (Where cd (OrderBy f (Offset E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToLimit (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Offset E))))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f E))))
ToOffset (Select s projection (From fr fields (Where cd (OrderBy f (Limit E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f E)))))
ToOffset (Select s projection (From fr fields (Where cd (GroupBy fg (OrderBy f (Limit E))))))
(QueryMustBeAliased rest alias) => QueryMustBeAliased (Where cd rest) alias
(QueryOptionallyAliased rest name alias) => QueryOptionallyAliased (Where cd rest) name alias
(Resume rest b c) => Resume (Where cd rest) b (Where cd c)
#Sort Source
data Sort (f :: Type)
Constructors
Instances
(Cons name t e fields) => SortFields (Sort (Proxy name)) fields
(AppendPath alias name fullPath, Cons fullPath t e fields) => SortFields (Sort (Path alias name)) fields
#from Source
from :: forall f q fields sql. ToFrom f q fields => Resume q (From f fields E) sql => f -> q -> sql
FROM accepts the following sources
- Tables
- Inner and outer joins
- Aliased tables
- Aliased SELECT statements
Due to how SQL binding works, joins and subqueries require brackets to be parsed correctly. For example:
SELECT column FROM (SELECT column FROM table) AS alias
should beselect column # from (select column # from table # as alias)
)SELECT column FROM table alias JOIN other_table other_alias
should beselect column # from ((table # as alias)
join(other_table # as other_alias))
)
To aid composition, SELECT projections are only validated on FROM
#into Source
into :: forall tableName fields fieldNames fieldList required e inserted. RowToList fields fieldList => RequiredFields fieldList required => InsertList fields fieldNames inserted => Union required e inserted => Table tableName fields -> fieldNames -> Insert E -> Insert (Into tableName fields fieldNames E)
#prepare Source
prepare :: forall q. ToPrepare q => Plan -> q -> Prepare q
https://www.postgresql.org/docs/current/sql-prepare.html
PREPARE statements can be employed to reuse execution plans, and thus optimize performance
Note: droplet always creates server-side parameters for literal values in queries. In the case of PREPARE, however, literals will be be parsed as statement parameters
#select Source
select :: forall s projection. ToSelect s => s -> Select s projection E
SELECT can project literals, columns and subqueries with the following considerations:
- Multiple columns are represented by tuples.
Data.Tuple.Nested./\
is convenient for this - Literal values (e.g., numbers) must be aliased (with AS)
- Columns names in projections must be unique, or referenced by different table aliases (e.g., u.name, t.name)
- Subqueries must return a single column
- Modules
- Droplet.
Driver - Droplet.
Driver. Internal. Pool - Droplet.
Driver. Internal. Query - Droplet.
Driver. Unsafe - Droplet.
Language - Droplet.
Language. Internal. Condition - Droplet.
Language. Internal. Definition - Droplet.
Language. Internal. Function - Droplet.
Language. Internal. Keyword - Droplet.
Language. Internal. Query - Droplet.
Language. Internal. Syntax
JOIN ... ON