Droplet.Language.Internal.Translate
- Package
- purescript-droplet
- Repository
- easafe/purescript-droplet
ToQuery
, a type class to generate parameterized SQL statement strings
Do not import this module directly, it will break your code and make it not type safe. Use the sanitized Droplet.Driver
instead
#FilteredQuery Source
class FilteredQuery :: Type -> Row Type -> Constraint
class FilteredQuery (q :: Type) (outer :: Row Type)
would be nice to dry this like tocondition, but gotta find a way to only check Path
s
Checks for invalid qualified columns usage in conditional clauses
Instances
(FilteredQuery cond outer) => FilteredQuery (Where cond rest) outer
(FilteredQuery cond outer) => FilteredQuery (Join k f q r a (On cond rest)) outer
(FilteredQuery (Op a b) outer, FilteredQuery (Op c d) outer) => FilteredQuery (Op (Op a b) (Op c d)) outer
(FilteredQuery (Op a b) outer) => FilteredQuery (Op Not (Op a b)) outer
(AppendPath alias name fullPath, Cons fullPath t e outer, IsNullable t) => FilteredQuery (Op IsNotNull (Path alias name)) outer
(AppendPath alias name fullPath, Cons fullPath t e outer, IsNullable t) => FilteredQuery (Op IsNull (Path alias name)) outer
(AggregatedQuery s rest, QueryMustNotBeAliased rest, QualifiedProjection s outer o, Union outer fields os, Nub os allOut, FilteredQuery rest allOut) => FilteredQuery (Op Exists (Select s p (From f fields rest))) outer
(AppendPath alias name fullPath, Cons fullPath t e outer, UnwrapDefinition t u, UnwrapNullable u v) => FilteredQuery (Op In (Op (Path alias name) (NonEmptyArray v))) outer
(AppendPath alias name fullPath, Cons fullPath t e outer, UnwrapDefinition t v, UnwrapNullable v w, AppendPath otherAlias otherName otherFullPath, Cons otherFullPath u f outer, UnwrapDefinition u z, UnwrapNullable z w) => FilteredQuery (Op (Path alias name) (Path otherAlias otherName)) outer
(AppendPath alias name fullPath, Cons fullPath t e outer, UnwrapDefinition t v, UnwrapNullable v w, Cons otherName u f outer, UnwrapDefinition u z, UnwrapNullable z w) => FilteredQuery (Op (Path alias name) (Proxy otherName)) outer
(Cons name t f outer, UnwrapDefinition t v, UnwrapNullable v w, AppendPath alias otherName fullPath, Cons fullPath u e outer, UnwrapDefinition u z, UnwrapNullable z w) => FilteredQuery (Op (Proxy name) (Path alias otherName)) outer
(AppendPath alias name fullPath, Cons fullPath t e outer, UnwrapDefinition t u, UnwrapNullable u v) => FilteredQuery (Op (Path alias name) v) outer
(AppendPath alias name fullPath, Cons fullPath t e outer, UnwrapDefinition t u, UnwrapNullable u v) => FilteredQuery (Op v (Path alias name)) outer
FilteredQuery e outer
#TranslateFieldDefinition Source
class TranslateFieldDefinition :: RowList Type -> Constraint
class TranslateFieldDefinition (list :: RowList Type) where
Members
translateFieldDefinition :: Proxy list -> Array String
Instances
TranslateFieldDefinition Nil
(Reflectable name String, ToType t, ToNullableDefinition t, ToConstraintDefinition c, TranslateFieldDefinition rest) => TranslateFieldDefinition (Cons name (Column t c) rest)
(ToType t, ToNullableDefinition t, TranslateFieldDefinition rest, Reflectable name String) => TranslateFieldDefinition (Cons name t rest)
#QualifiedProjection Source
class QualifiedProjection :: Type -> Row Type -> Row Type -> Constraint
class QualifiedProjection (s :: Type) (outer :: Row Type) (projection :: Row Type) | s -> outer projection
Validates qualified columns that ToProjection
didn't have the context for
Instances
(AppendPath alias name fullPath, Cons fullPath t e outer, JoinedToMaybe t u, UnwrapDefinition u v, Cons fullPath v () projection) => QualifiedProjection (Path alias name) outer projection
(AppendPath table name fullPath, Cons fullPath t e outer, JoinedToMaybe t u, UnwrapDefinition u v, Cons alias v () projection) => QualifiedProjection (As alias (Path table name)) outer projection
(AppendPath table name fullPath, AppendPath tb nm ftn, Cons fullPath t e outer, Cons ftn s g outer, Cons alias out () projection) => QualifiedProjection (As alias (Aggregate (Path table name) (OrderBy (Path tb nm) rd) fd out)) outer projection
(AppendPath table name fullPath, Cons fullPath t d outer, Cons alias out () projection) => QualifiedProjection (As alias (Aggregate (Proxy n) (OrderBy (Path table name) rd) fd out)) outer projection
(AppendPath table name fullPath, Cons fullPath t e outer, Cons alias out () projection) => QualifiedProjection (As alias (Aggregate (Path table name) rest fd out)) outer projection
(QualifiedProjection s outer some, QualifiedProjection t outer more, Union some more projection) => QualifiedProjection (Tuple s t) outer projection
(Union outer fields all, Nub all nubbed, FilteredQuery rest nubbed, FilteredQuery f nubbed, QualifiedProjection s nubbed projection, RowToList projection list, SingleQualifiedColumn list rest single) => QualifiedProjection (Select s p (From f fields rest)) outer single
(QualifiedProjection s outer projection) => QualifiedProjection (Distinct s) outer projection
QualifiedProjection s outer ()
#TranslateSource Source
class TranslateSource (q :: Type) where
Print field source
Members
translateSource :: q -> State QueryState String
Instances
(Translate (Select s ppp more)) => TranslateSource (Select s ppp more)
(Reflectable name String, Reflectable alias String) => TranslateSource (As alias (Table name fd))
(Reflectable name String) => TranslateSource (Table name fd)
(ToJoinType k, Translate (Join k fields l r a rest)) => TranslateSource (Join k fields l r a rest)
#ToNakedProjection Source
class ToNakedProjection :: Type -> Row Type -> Constraint
class ToNakedProjection (s :: Type) (projection :: Row Type)
Naked selects may be composed of subqueries whose projections need to be checked and merged individually
Instances
(ToNakedProjection s some, ToNakedProjection t more, Union some more projection) => ToNakedProjection (Tuple s t) projection
(ToProjection (Select s p (From f fields rest)) () Nil projection, QualifiedProjection s fields refs, RowToList projection pro, ToSingleColumn pro name t, Cons name t () single) => ToNakedProjection (Select s p (From f fields rest)) single
(ToNakedProjection s projection) => ToNakedProjection (Distinct s) projection
(ToProjection s () Nil projection) => ToNakedProjection s projection
#SingleQualifiedColumn Source
class SingleQualifiedColumn :: RowList Type -> Type -> Row Type -> Constraint
class SingleQualifiedColumn (fields :: RowList Type) (q :: Type) (single :: Row Type) | fields -> q single
Projects a single qualified column, if it exists
Instances
SingleQualifiedColumn Nil q ()
(QueryOptionallyAliased q name alias, Cons alias (Maybe t) () single) => SingleQualifiedColumn (Cons name (Maybe t) Nil) q single
(QueryOptionallyAliased q name alias, Cons alias (Maybe t) () single) => SingleQualifiedColumn (Cons name t Nil) q single
#TranslateConditions Source
class TranslateConditions c where
Print logical conditions
Members
translateConditions :: c -> State QueryState String
Instances
(Translate (Select s p (From f fd rest))) => TranslateConditions (Op Exists (Select s p (From f fd rest)))
(TranslateConditions a) => TranslateConditions (Op IsNotNull a)
(TranslateConditions a) => TranslateConditions (Op IsNull a)
(TranslateConditions a) => TranslateConditions (Op Not a)
(TranslateConditions a, TranslateConditions b) => TranslateConditions (Op In (Op a (NonEmptyArray b)))
(TranslateConditions a, TranslateConditions b) => TranslateConditions (Op a b)
(Reflectable name String) => TranslateConditions (Proxy name)
(Reflectable alias String, Reflectable name String) => TranslateConditions (Path alias name)
(ToValue v) => TranslateConditions v
#TranslateColumn Source
class TranslateColumn q where
Print selected columns
Members
translateColumn :: q -> State QueryState String
Instances
(Reflectable name String) => TranslateColumn (Proxy name)
(Reflectable fullPath String, Reflectable name String, Reflectable alias String, AppendPath alias name fullPath) => TranslateColumn (Path alias name)
TranslateColumn Star
(Reflectable name String) => TranslateColumn (As name Int)
(Reflectable name String, NameList inp, ArgumentList rest) => TranslateColumn (As name (Aggregate inp rest fields out))
(Reflectable name String, ArgumentList args) => TranslateColumn (As name (PgFunction inp args fields out))
(Reflectable name String, Reflectable alias String) => TranslateColumn (As alias (Proxy name))
(Reflectable fullPath String, Reflectable alias String, Reflectable name String, Reflectable table String, AppendPath table name fullPath) => TranslateColumn (As alias (Path table name))
(TranslateColumn s, TranslateColumn t) => TranslateColumn (Tuple s t)
(TranslateColumn s) => TranslateColumn (Distinct s)
(Translate q) => TranslateColumn q
#NoAggregations Source
class NoAggregations :: Type -> Boolean -> Constraint
class NoAggregations (q :: Type) (is :: Boolean) | q -> is
Are all columns not aggregated?
Instances
NoAggregations (As n (Aggregate i rest f o)) False
(NoAggregations a isa, NoAggregations b isb, And isa isb is) => NoAggregations (Tuple a b) is
NoAggregations s True
#AggregatedQuery Source
class AggregatedQuery (s :: Type) (q :: Type)
Asserts that queries not using GROUP BY do not mix aggregated and non aggregated columns
Instances
(AggregatedQuery s rest) => AggregatedQuery s (Where c rest)
AggregatedQuery s (GroupBy f rest)
(AggregatedQuery s q) => AggregatedQuery (Distinct s) q
(NoAggregations s no, OnlyAggregations s yes, IsValidAggregation no yes) => AggregatedQuery s q
#ToReferenceDefinition Source
class ToReferenceDefinition :: RowList Type -> Constraint
class ToReferenceDefinition (source :: RowList Type) where
Members
toReferenceDefinition :: Proxy source -> String
Instances
ToReferenceDefinition Nil
(Reflectable tableName String, CompositeFieldList (Cons fieldName (Composite tableName) rest)) => ToReferenceDefinition (Cons fieldName (Composite tableName) rest)
#IsValidAggregation Source
class IsValidAggregation :: Boolean -> Boolean -> Constraint
class IsValidAggregation (s :: Boolean) (t :: Boolean)
Check aggregation results
Having a separated type class leads to better error messages
Instances
(Fail (Text "Projection cannot include aggregations. Are you missing a GROUP BY clause?")) => IsValidAggregation False False
IsValidAggregation s t
#CompositeConstraints Source
class CompositeConstraints :: RowList Type -> RowList Type -> Constraint
class CompositeConstraints (source :: RowList Type) (constraints :: RowList Type) | source -> constraints
Flatten constraints into a list
Instances
CompositeConstraints Nil Nil
(CompositeFields name rest finishing, RowListSet starting t finishing fields, RowListRemove name rest later, CompositeConstraints later tail) => CompositeConstraints (Cons name (C starting t) rest) (Cons name (C fields t) tail)
(CompositeConstraints rest all) => CompositeConstraints (Cons name r rest) all
#CompositeFields Source
class CompositeFields :: Symbol -> RowList Type -> RowList Type -> Constraint
class CompositeFields (name :: Symbol) (rest :: RowList Type) (fields :: RowList Type) | name rest -> fields
Find all fields that are part of a composite constraint
Instances
CompositeFields name Nil Nil
(CompositeFields name rest tail, RowListSet field t tail all) => CompositeFields name (Cons name (C field t) rest) all
(CompositeFields name rest all) => CompositeFields name (Cons n t rest) all
#CompositeFieldList Source
class CompositeFieldList :: RowList Type -> Constraint
class CompositeFieldList (fields :: RowList Type) where
Members
compositeFieldList :: Proxy fields -> Array String
Instances
CompositeFieldList Nil
(CompositeFieldList rest, Reflectable name String) => CompositeFieldList (Cons name t rest)
#TranslateCompositeConstraint Source
class TranslateCompositeConstraint :: RowList Type -> Constraint
class TranslateCompositeConstraint (list :: RowList Type) where
Members
translateCompositeConstraint :: Proxy list -> Array String
Instances
TranslateCompositeConstraint Nil
(Reflectable name String, CompositeFieldList fields, ToCompositeConstraintDefinition t, TranslateCompositeConstraint rest, ReferenceList fields constraints, ToReferenceDefinition constraints) => TranslateCompositeConstraint (Cons name (C fields t) rest)
#ToJoinType Source
class ToJoinType :: Side -> Constraint
class ToJoinType (k :: Side) where
Print join type
Members
toJoinType :: Proxy k -> String
Instances
#SingleColumn Source
class SingleColumn :: Symbol -> Type -> RowList Type -> Constraint
class SingleColumn (name :: Symbol) (c :: Type) (output :: RowList Type) | name c -> output
Instances
SingleColumn name (Proxy t) (Cons name t Nil)
SingleColumn name (Column t constraints) (Cons name (Column t constraints) Nil)
#ToConstraintDefinition Source
class ToConstraintDefinition (c :: Type) where
String representation of constraints
Members
toConstraintDefinition :: Proxy c -> String
Instances
ToConstraintDefinition Identity
ToConstraintDefinition Unique
ToConstraintDefinition PrimaryKey
(Reflectable tableName String, Reflectable fieldName String) => ToConstraintDefinition (ForeignKey fieldName (Table tableName f))
ToConstraintDefinition (Constraint (Composite n) t)
(Reflectable name String, ToConstraintDefinition t) => ToConstraintDefinition (Constraint name t)
(ToConstraintDefinition some, ToConstraintDefinition more) => ToConstraintDefinition (Tuple some more)
#ToNullableDefinition Source
class ToNullableDefinition (t :: Type) where
Members
toNullableDefinition :: Proxy t -> String
Instances
#ArgumentList Source
class ArgumentList v where
Name list for functions, or when fields and parameters can be mixed
Members
argumentList :: v -> State QueryState String
Instances
(Reflectable name String) => ArgumentList (Proxy name)
(Reflectable alias String, Reflectable name String) => ArgumentList (Path alias name)
(ArgumentList s, Translate (OrderBy f E)) => ArgumentList (OrderBy f s)
(Reflectable name String) => ArgumentList (Sort (Proxy name))
(Reflectable alias String, Reflectable name String) => ArgumentList (Sort (Path alias name))
(ArgumentList args) => ArgumentList (PgFunction input args fields output)
ArgumentList Unit
ArgumentList E
(ArgumentList f, ArgumentList rest) => ArgumentList (Tuple f rest)
(ValueList v) => ArgumentList v
#ReferenceList Source
class ReferenceList :: RowList Type -> RowList Type -> Constraint
class ReferenceList (source :: RowList Type) (constraints :: RowList Type) | source -> constraints
For foreign keys, we need a list of referenced fields
Instances
ReferenceList Nil Nil
(ReferenceList rest tail, RowListSet name (Composite tableName) tail all) => ReferenceList (Cons n (ForeignKey name (Table tableName fields)) rest) all
ReferenceList (Cons n t rest) Nil
#ToCompositeConstraintDefinition Source
class ToCompositeConstraintDefinition (t :: Type) where
While the rest stays the same, CONSTRAINT syntax uses and extra keyword for foreign keys
Members
Instances
#QueryMustNotBeAliased Source
class QueryMustNotBeAliased (q :: Type)
Prevents top level queries to end in AS
Instances
(QueryMustNotBeAliased rest) => QueryMustNotBeAliased (Where c rest)
(QueryMustNotBeAliased rest) => QueryMustNotBeAliased (GroupBy f rest)
(QueryMustNotBeAliased rest) => QueryMustNotBeAliased (OrderBy f rest)
(QueryMustNotBeAliased rest) => QueryMustNotBeAliased (Limit n rest)
(QueryMustNotBeAliased rest) => QueryMustNotBeAliased (Offset rest)
(Fail (Text "AS statement cannot be top level")) => QueryMustNotBeAliased (As alias E)
QueryMustNotBeAliased E
QueryMustNotBeAliased (Query projection)
#ToQuery Source
class ToQuery :: Type -> Row Type -> Constraint
class ToQuery (q :: Type) (projection :: Row Type) | q -> projection where
Builds a SQL query
Members
toQuery :: q -> State QueryState String
Instances
(AggregatedQuery s rest, QueryMustNotBeAliased rest, FilteredQuery f fields, FilteredQuery rest fields, QualifiedProjection s fields qual, Union qual projection all, Nub all final, Translate (Select s projection (From f fields rest))) => ToQuery (Select s projection (From f fields rest)) final
Fully formed queries in the shape of SELECT ... FROM ...
(ToNakedProjection s projection, Nub projection unique, UniqueColumnNames projection unique, Translate (Select s p E)) => ToQuery (Select s p E) unique
"Naked" queries in the shape of SELECT ...
(ToQuery q final, ToQuery r p, Translate (Union q r)) => ToQuery (Union q r) final
UNION
(ToProjection f fields Nil projection, Translate (Insert (Into name fields DefaultValues (Returning f)))) => ToQuery (Insert (Into name fields DefaultValues (Returning f))) projection
INSERT DEFAULT VALUES ... RETURNING
(ToProjection f fields Nil projection, Translate (Insert (Into name fields fieldNames (Values v (Returning f))))) => ToQuery (Insert (Into name fields fieldNames (Values v (Returning f)))) projection
INSERT ... RETURNING
(Translate (Insert (Into name fields DefaultValues E))) => ToQuery (Insert (Into name fields DefaultValues E)) ()
INSERT DEFAULT VALUES
(Translate (Insert (Into name fields fieldNames (Values v E)))) => ToQuery (Insert (Into name fields fieldNames (Values v E))) ()
INSERT
(Translate (Update table fields (Set values rest))) => ToQuery (Update table fields (Set values rest)) ()
UPDATE
(Translate (Delete (From f fields rest))) => ToQuery (Delete (From f fields rest)) ()
DELETE
(Translate (Create (Table name fields))) => ToQuery (Create (Table name fields)) ()
CREATE TABLE
(Translate (Alter (T (Table name fields) rest))) => ToQuery (Alter (T (Table name fields) rest)) ()
ALTER TABLE
(Translate (Drop (Table name fields))) => ToQuery (Drop (Table name fields)) ()
DROP TABLE
ToQuery (Query projection) projection
Unsafe queries
#TranslateNakedColumn Source
class TranslateNakedColumn q where
Print naked selects
Members
translateNakedColumn :: q -> State QueryState String
Instances
(Reflectable name String) => TranslateNakedColumn (As name Int)
(Reflectable name String, ArgumentList args) => TranslateNakedColumn (As name (PgFunction inp args fields out))
(TranslateNakedColumn s, TranslateNakedColumn t) => TranslateNakedColumn (Tuple s t)
(Translate (Select s ss (From f fields rest))) => TranslateNakedColumn (Select s ss (From f fields rest))
#NameList Source
class NameList fieldNames where
Names (possibly) separated by comma
Used by INSERT, ORDER BY
Members
Instances
#NameValuePairs Source
class NameValuePairs pairs where
UPDATE list
Members
nameValuePairs :: pairs -> State QueryState String
Instances
(Reflectable name String) => NameValuePairs (Op (Proxy name) Default)
(Reflectable name String, ToValue p) => NameValuePairs (Op (Proxy name) p)
(NameValuePairs p, NameValuePairs rest) => NameValuePairs (Tuple p rest)
#Translate Source
class Translate q where
Print SQL statements
Members
translate :: q -> State QueryState String
Instances
Translate E
End of query
(Translate s) => Translate (Prepare s)
PREPARE
(TranslateNakedColumn s) => Translate (Select s pp E)
(TranslateColumn s, Translate rest) => Translate (Select s projection rest)
(Reflectable name String, Translate rest) => Translate (From (Table name fields) fields rest)
FROM
(Translate (Join k fields l r a more), Translate rest) => Translate (From (Join k fields l r a more) fields rest)
(TranslateSource q, Translate rest) => Translate (From q fields rest)
(ToJoinType k, TranslateSource l, TranslateSource r, Translate rest) => Translate (Join k fields l r a rest)
JOIN
(TranslateConditions c, Translate rest) => Translate (On c rest)
ON
(Reflectable name String) => Translate (As name E)
(SELECT ... FROM ...) AS
(TranslateConditions c, Translate rest) => Translate (Where c rest)
WHERE
(NameList f, Translate rest) => Translate (GroupBy f rest)
GROUP BY
(Translate s, Translate r) => Translate (Union s r)
UNION
(Reflectable name String, Translate rest) => Translate (Insert (Into name fields DefaultValues rest))
INSERT DEFAULT VALUES
(Reflectable name String, NameList fieldNames, ValueList v, Translate rest) => Translate (Insert (Into name fields fieldNames (Values v rest)))
(Reflectable name String, NameValuePairs pairs, Translate rest) => Translate (Update name fields (Set pairs rest))
UPDATE
(Translate (From f fields rest)) => Translate (Delete (From f fields rest))
DELETE
(NameList fieldNames) => Translate (Returning fieldNames)
RETURNING
(ArgumentList f, Translate rest) => Translate (OrderBy f rest)
ORDER BY
(Reflectable n Int, Translate rest) => Translate (Limit n rest)
LIMIT
(Translate rest) => Translate (Offset rest)
OFFSET
(RowToList columns columnList, TranslateFieldDefinition columnList, ConstraintsToRowList columnList list, CompositeConstraints list composites, TranslateCompositeConstraint composites, Reflectable name String) => Translate (Create (Table name columns))
CREATE TABLE
(Reflectable name String, Translate rest) => Translate (Alter (T (Table name fields) rest))
ALTER TABLE
(Reflectable name String, SingleColumn name column columnList, TranslateFieldDefinition columnList) => Translate (Add name column)
ADD
(Reflectable name String) => Translate (Drop (Table name fields))
DROP TABLE
#buildQuery Source
buildQuery :: forall q projection. ToQuery q projection => q -> Query projection
#unsafeBuildQuery Source
unsafeBuildQuery :: forall projection parameters parameterList. RowToList parameters parameterList => ToParameters parameters parameterList => Maybe Plan -> String -> Record parameters -> Query projection
- Modules
- Droplet.
Driver - Droplet.
Driver. Internal. Migration - Droplet.
Driver. Internal. Pool - Droplet.
Driver. Internal. Query - Droplet.
Driver. Migration - Droplet.
Driver. Unsafe - Droplet.
Language - Droplet.
Language. Internal. Condition - Droplet.
Language. Internal. Definition - Droplet.
Language. Internal. Function - Droplet.
Language. Internal. Syntax - Droplet.
Language. Internal. Token - Droplet.
Language. Internal. Translate
Unsafe queries