on dev help sign in
more renderSQL
commitc571bae42f469de7b8fdaabd5bb7783ae7190b70 authorEitan Chatav <eitan.chatav@gmail.com> dateFri 7 Dec 2018, 15:39 (8 years ago) parentdbb403c
5 files changed+62−66
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+16−13 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+5−5 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+3−3 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+15−16 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+23−29
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+16−13
@@ -119,7 +119,7 @@ instance RenderSQL (Definition schemas0 schemas1) where
119119instance Category Definition where
120120 id = UnsafeDefinition ";"
121121 ddl1 . ddl0 = UnsafeDefinition $
122− renderDefinition ddl0 <> "\n" <> renderDefinition ddl1
122+ renderSQL ddl0 <> "\n" <> renderSQL ddl1
123123
124124{-----------------------------------------
125125CREATE statements
@@ -225,7 +225,7 @@ renderCreation
225225 -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
226226 -- ^ constraints that must hold for the table
227227 -> ByteString
228−renderCreation tab columns constraints = renderQualifiedAlias tab
228+renderCreation tab columns constraints = renderSQL tab
229229 <+> parenthesized
230230 ( renderCommaSeparated renderColumnDef columns
231231 <> ( case constraints of
@@ -241,7 +241,7 @@ renderCreation tab columns constraints = renderQualifiedAlias tab
241241 :: Aliased (TableConstraintExpression sch tab schemas) constraint
242242 -> ByteString
243243 renderConstraint (constraint `As` alias) =
244− "CONSTRAINT" <+> renderSQL alias <+> renderTableConstraintExpression constraint
244+ "CONSTRAINT" <+> renderSQL alias <+> renderSQL constraint
245245
246246-- | Data types are a way to limit the kind of data that can be stored in a
247247-- table. For many applications, however, the constraint they provide is
@@ -263,6 +263,9 @@ newtype TableConstraintExpression
263263 = UnsafeTableConstraintExpression
264264 { renderTableConstraintExpression :: ByteString }
265265 deriving (GHC.Generic,Show,Eq,Ord,NFData)
266+instance RenderSQL
267+ (TableConstraintExpression sch tab schemas constraint) where
268+ renderSQL = renderTableConstraintExpression
266269
267270{-| A `check` constraint is the most generic `TableConstraint` type.
268271It allows you to specify that the value in a certain column must satisfy
@@ -298,7 +301,7 @@ check
298301 -- ^ a closed `Condition` on those subcolumns
299302 -> TableConstraintExpression sch tab schemas ('Check aliases)
300303check _cols condition = UnsafeTableConstraintExpression $
301− "CHECK" <+> parenthesized (renderExpression condition)
304+ "CHECK" <+> parenthesized (renderSQL condition)
302305
303306{-| A `unique` constraint ensure that the data contained in a column,
304307or a group of columns, is unique among all the rows in the table.
@@ -541,7 +544,7 @@ dropTable
541544 , Has tab schema ('Table table))
542545 => QualifiedAlias sch tab -- ^ table to remove
543546 -> Definition schemas (Alter sch (Drop tab schema) schemas)
544−dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderQualifiedAlias tab <> ";"
547+dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
545548
546549{-----------------------------------------
547550ALTER statements
@@ -555,7 +558,7 @@ alterTable
555558 -> Definition schemas (Alter sch (Alter tab ('Table table1) schema) schemas)
556559alterTable tab alteration = UnsafeDefinition $
557560 "ALTER TABLE"
558− <+> renderQualifiedAlias tab
561+ <+> renderSQL tab
559562 <+> renderAlterTable alteration
560563 <> ";"
561564
@@ -605,7 +608,7 @@ addConstraint
605608 -> AlterTable sch tab schemas table1
606609addConstraint alias constraint = UnsafeAlterTable $
607610 "ADD" <+> "CONSTRAINT" <+> renderSQL alias
608− <+> renderTableConstraintExpression constraint
611+ <+> renderSQL constraint
609612
610613-- | A `dropConstraint` drops a table constraint.
611614--
@@ -843,7 +846,7 @@ createView
843846 -> Query ('[] :=> schemas) '[] view -- ^ query
844847 -> Definition schemas (Alter sch (Create vw ('View view) schema) schemas)
845848createView alias query = UnsafeDefinition $
846− "CREATE" <+> "VIEW" <+> renderQualifiedAlias alias <+> "AS"
849+ "CREATE" <+> "VIEW" <+> renderSQL alias <+> "AS"
847850 <+> renderQuery query <> ";"
848851
849852-- | Drop a view.
@@ -862,7 +865,7 @@ dropView
862865 :: (Has sch schemas schema, Has vw schema ('View view))
863866 => QualifiedAlias sch vw -- ^ view to remove
864867 -> Definition schemas (Alter sch (Drop vw schema) schemas)
865−dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderQualifiedAlias vw <> ";"
868+dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
866869
867870-- | Enumerated types are created using the `createTypeEnum` command, for example
868871--
@@ -876,8 +879,8 @@ createTypeEnum
876879 -- ^ labels of the enumerated type
877880 -> Definition schemas (Alter sch (Create enum ('Typedef ('PGenum labels)) schema) schemas)
878881createTypeEnum enum labels = UnsafeDefinition $
879− "CREATE" <+> "TYPE" <+> renderQualifiedAlias enum <+> "AS" <+> "ENUM" <+>
880− parenthesized (commaSeparated (renderLabels labels)) <> ";"
882+ "CREATE" <+> "TYPE" <+> renderSQL enum <+> "AS" <+> "ENUM" <+>
883+ parenthesized (renderSQL labels) <> ";"
881884
882885-- | Enumerated types can also be generated from a Haskell type, for example
883886--
@@ -930,7 +933,7 @@ createTypeComposite
930933 -- ^ list of attribute names and data types
931934 -> Definition schemas (Alter sch (Create ty ('Typedef ('PGcomposite fields)) schema) schemas)
932935createTypeComposite ty fields = UnsafeDefinition $
933− "CREATE" <+> "TYPE" <+> renderQualifiedAlias ty <+> "AS" <+> parenthesized
936+ "CREATE" <+> "TYPE" <+> renderSQL ty <+> "AS" <+> parenthesized
934937 (renderCommaSeparated renderField fields) <> ";"
935938 where
936939 renderField :: Aliased (TypeExpression schemas) x -> ByteString
@@ -981,7 +984,7 @@ dropType
981984 => QualifiedAlias sch td
982985 -- ^ name of the user defined type
983986 -> Definition schemas (Alter sch (Drop td schema) schemas)
984−dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderQualifiedAlias tydef <> ";"
987+dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
985988
986989-- | `ColumnTypeExpression`s are used in `createTable` commands.
987990newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+5−5
@@ -452,7 +452,7 @@ index n expr = UnsafeExpression $
452452
453453instance (KnownSymbol label, label `In` labels) => IsPGlabel label
454454 (Expression db params grp from (nullity ('PGenum labels))) where
455− label = UnsafeExpression $ renderLabel (PGlabel @label)
455+ label = UnsafeExpression $ renderSQL (PGlabel @label)
456456
457457-- | A row constructor is an expression that builds a row value
458458-- (also called a composite value) using values for its member fields.
@@ -494,7 +494,7 @@ field
494494 -> Expression (commons :=> schemas) params grp from ('NotNull ('PGcomposite row))
495495 -> Expression (commons :=> schemas) params grp from ty
496496field td fld expr = UnsafeExpression $
497− parenthesized (renderSQL expr <> "::" <> renderQualifiedAlias td)
497+ parenthesized (renderSQL expr <> "::" <> renderSQL td)
498498 <> "." <> renderSQL fld
499499
500500instance Semigroup
@@ -1637,7 +1637,7 @@ typedef
16371637 :: (Has sch schemas schema, Has td schema ('Typedef ty))
16381638 => QualifiedAlias sch td
16391639 -> TypeExpression schemas (nullity ty)
1640−typedef = UnsafeTypeExpression . renderQualifiedAlias
1640+typedef = UnsafeTypeExpression . renderSQL
16411641
16421642-- | The composite type corresponding to a `Table` definition can be expressed
16431643-- by its alias.
@@ -1645,7 +1645,7 @@ typetable
16451645 :: (Has sch schemas schema, Has tab schema ('Table table))
16461646 => QualifiedAlias sch tab
16471647 -> TypeExpression schemas (nullity ('PGcomposite (TableToRow table)))
1648−typetable = UnsafeTypeExpression . renderQualifiedAlias
1648+typetable = UnsafeTypeExpression . renderSQL
16491649
16501650-- | The composite type corresponding to a `View` definition can be expressed
16511651-- by its alias.
@@ -1653,7 +1653,7 @@ typeview
16531653 :: (Has sch schemas schema, Has vw schema ('View view))
16541654 => QualifiedAlias sch vw
16551655 -> TypeExpression schemas (nullity ('PGcomposite view))
1656−typeview = UnsafeTypeExpression . renderQualifiedAlias
1656+typeview = UnsafeTypeExpression . renderSQL
16571657
16581658-- | logical Boolean (true/false)
16591659bool :: TypeExpression schemas (nullity 'PGbool)
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+3−3
@@ -240,7 +240,7 @@ insertInto
240240 -> ReturningClause db params '[tab ::: row0] row1
241241 -> Manipulation db params row1
242242insertInto tab qry conflict ret = UnsafeManipulation $
243− "INSERT" <+> "INTO" <+> renderQualifiedAlias tab
243+ "INSERT" <+> "INTO" <+> renderSQL tab
244244 <+> renderQueryClause qry
245245 <> renderConflictClause conflict
246246 <> renderSQL ret
@@ -508,7 +508,7 @@ update
508508 -> Manipulation db params row1
509509update tab columns wh returning = UnsafeManipulation $
510510 "UPDATE"
511− <+> renderQualifiedAlias tab
511+ <+> renderSQL tab
512512 <+> "SET"
513513 <+> renderCommaSeparated renderColumnExpression columns
514514 <+> "WHERE" <+> renderExpression wh
@@ -558,7 +558,7 @@ deleteFrom
558558 -> Manipulation db params row1
559559deleteFrom tab using wh returning = UnsafeManipulation $
560560 "DELETE FROM"
561− <+> renderQualifiedAlias tab
561+ <+> renderSQL tab
562562 <> case using of
563563 NoUsing -> ""
564564 Using tables -> " USING" <+> renderFromClause tables
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+15−16
@@ -97,7 +97,6 @@ module Squeal.PostgreSQL.Query
9797 , renderHavingClause
9898 -- * Sorting
9999 , SortExpression (..)
100− , renderSortExpression
101100 -- * Subquery Expressions
102101 , in_
103102 , rowIn
@@ -439,7 +438,7 @@ instance
439438
440439instance RenderSQL (Selection db params grp from row) where
441440 renderSQL = \case
442− List list -> renderCommaSeparated (renderAliasedAs renderSQL) list
441+ List list -> renderCommaSeparated (renderAliased renderSQL) list
443442 Star -> "*"
444443 DotStar tab -> renderSQL tab <> ".*"
445444 Also right left -> renderSQL left <> ", " <> renderSQL right
@@ -609,7 +608,7 @@ renderTableExpression
609608 renderOrderByClause = \case
610609 [] -> ""
611610 srts -> " ORDER BY"
612− <+> commaSeparated (renderSortExpression <$> srts)
611+ <+> commaSeparated (renderSQL <$> srts)
613612 renderLimits = \case
614613 [] -> ""
615614 lims -> " LIMIT" <+> fromString (show (minimum lims))
@@ -845,13 +844,13 @@ table
845844 => Aliased (QualifiedAlias sch) (alias ::: tab)
846845 -> FromClause (commons :=> schemas) params '[alias ::: TableToRow table]
847846table (tab `As` alias) = UnsafeFromClause $
848− renderQualifiedAlias tab <+> "AS" <+> renderSQL alias
847+ renderSQL tab <+> "AS" <+> renderSQL alias
849848
850849-- | `subquery` derives a table from a `Query`.
851850subquery
852851 :: Aliased (Query db params) query
853852 -> FromClause db params '[query]
854−subquery = UnsafeFromClause . renderAliasedAs (parenthesized . renderQuery)
853+subquery = UnsafeFromClause . renderAliased (parenthesized . renderQuery)
855854
856855-- | `view` derives a table from a `View`.
857856view
@@ -859,7 +858,7 @@ view
859858 => Aliased (QualifiedAlias sch) (alias ::: vw)
860859 -> FromClause (commons :=> schemas) params '[alias ::: view]
861860view (vw `As` alias) = UnsafeFromClause $
862− renderQualifiedAlias vw <+> "AS" <+> renderSQL alias
861+ renderSQL vw <+> "AS" <+> renderSQL alias
863862
864863common
865864 :: Has cte commons common
@@ -1069,16 +1068,16 @@ data SortExpression db params grp from where
10691068deriving instance Show (SortExpression db params grp from)
10701069
10711070-- | Render a `SortExpression`.
1072−renderSortExpression :: SortExpression db params grp from -> ByteString
1073−renderSortExpression = \case
1074− Asc expression -> renderSQL expression <+> "ASC"
1075− Desc expression -> renderSQL expression <+> "DESC"
1076− AscNullsFirst expression -> renderSQL expression
1077− <+> "ASC NULLS FIRST"
1078− DescNullsFirst expression -> renderSQL expression
1079− <+> "DESC NULLS FIRST"
1080− AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
1081− DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
1071+instance RenderSQL (SortExpression db params grp from) where
1072+ renderSQL = \case
1073+ Asc expression -> renderSQL expression <+> "ASC"
1074+ Desc expression -> renderSQL expression <+> "DESC"
1075+ AscNullsFirst expression -> renderSQL expression
1076+ <+> "ASC NULLS FIRST"
1077+ DescNullsFirst expression -> renderSQL expression
1078+ <+> "DESC NULLS FIRST"
1079+ AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
1080+ DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
10821081
10831082unsafeSubqueryExpression
10841083 :: ByteString
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+23−29
@@ -70,7 +70,7 @@ module Squeal.PostgreSQL.Schema
7070 , IsLabel (..)
7171 , Aliased (As)
7272 , Aliasable (as)
73− , renderAliasedAs
73+ , renderAliased
7474 , Has
7575 , HasUnique
7676 , HasAll
@@ -80,12 +80,9 @@ module Squeal.PostgreSQL.Schema
8080 , IsNotElem
8181 , AllUnique
8282 , DefaultAliasable (..)
83− , renderQualifiedAlias
8483 -- * Enumerated Labels
8584 , IsPGlabel (..)
8685 , PGlabel (..)
87− , renderLabel
88− , renderLabels
8986 , LabelsPG
9087 -- * Grouping
9188 , Grouping (..)
@@ -374,14 +371,14 @@ instance alias1 ~ alias2 => IsLabel alias1 (Alias alias2) where
374371 fromLabel = Alias
375372instance aliases ~ '[alias] => IsLabel alias (NP Alias aliases) where
376373 fromLabel = fromLabel :* Nil
377−-- | >>> renderSQL #jimbob
378−-- "\"jimbob\""
374+-- | >>> printSQL (#jimbob :: Alias "jimbob")
375+-- "jimbob"
379376instance KnownSymbol alias => RenderSQL (Alias alias) where
380377 renderSQL = doubleQuoted . fromString . symbolVal
381378
382379-- | >>> import Generics.SOP (NP(..))
383−-- >>> renderAliases (#jimbob :* #kandi)
384−-- ["\"jimbob\"","\"kandi\""]
380+-- >>> printSQL (#jimbob :* #kandi :: NP Alias '["jimbob", "kandi"])
381+-- "jimbob", "kandi"
385382instance All KnownSymbol aliases => RenderSQL (NP Alias aliases) where
386383 renderSQL
387384 = commaSeparated
@@ -427,13 +424,13 @@ instance (KnownSymbol alias, tys ~ '[alias ::: ty]) => Aliasable alias
427424 expression `as` alias = expression `As` alias :* Nil
428425
429426-- | >>> let renderMaybe = fromString . maybe "Nothing" (const "Just")
430−-- >>> renderAliasedAs renderMaybe (Just (3::Int) `As` #an_int)
427+-- >>> renderAliased renderMaybe (Just (3::Int) `As` #an_int)
431428-- "Just AS \"an_int\""
432−renderAliasedAs
429+renderAliased
433430 :: (forall ty. expression ty -> ByteString)
434431 -> Aliased expression aliased
435432 -> ByteString
436−renderAliasedAs render (expression `As` alias) =
433+renderAliased render (expression `As` alias) =
437434 render expression <> " AS " <> renderSQL alias
438435
439436-- | @HasUnique alias fields field@ is a constraint that proves that
@@ -505,15 +502,14 @@ instance (q ~ "public", a0 ~ a1, a1 ~ a2, KnownSymbol a2) =>
505502 IsLabel a0 (Aliased (QualifiedAlias q) (a1 ::: a2)) where
506503 fromLabel = QualifiedAlias `As` Alias
507504
508−renderQualifiedAlias
509− :: forall q a. (KnownSymbol q, KnownSymbol a)
510− => QualifiedAlias q a -> ByteString
511−renderQualifiedAlias _ =
512− let
513− qualifier = renderSQL (Alias @q)
514− alias = renderSQL (Alias @a)
515− in
516− if qualifier == "\"public\"" then alias else qualifier <> "." <> alias
505+instance (KnownSymbol q, KnownSymbol a)
506+ => RenderSQL (QualifiedAlias q a) where
507+ renderSQL _ =
508+ let
509+ qualifier = renderSQL (Alias @q)
510+ alias = renderSQL (Alias @a)
511+ in
512+ if qualifier == "\"public\"" then alias else qualifier <> "." <> alias
517513
518514-- | @Elem@ is a promoted `Data.List.elem`.
519515type family Elem x xs where
@@ -721,15 +717,13 @@ instance labels ~ '[label]
721717 => IsPGlabel label (NP PGlabel labels) where label = PGlabel :* Nil
722718-- | A `PGlabel` unit type with an `IsPGlabel` instance
723719data PGlabel (label :: Symbol) = PGlabel
724−-- | Renders a label
725−renderLabel :: KnownSymbol label => proxy label -> ByteString
726−renderLabel (_ :: proxy label) =
727− "\'" <> renderSymbol @label <> "\'"
728−-- | Renders a list of labels
729−renderLabels
730− :: All KnownSymbol labels => NP PGlabel labels -> [ByteString]
731−renderLabels = hcollapse
732− . hcmap (Proxy @KnownSymbol) (K . renderLabel)
720+instance KnownSymbol label => RenderSQL (PGlabel label) where
721+ renderSQL _ = "\'" <> renderSymbol @label <> "\'"
722+instance All KnownSymbol labels => RenderSQL (NP PGlabel labels) where
723+ renderSQL
724+ = commaSeparated
725+ . hcollapse
726+ . hcmap (Proxy @KnownSymbol) (K . renderSQL)
733727
734728{- | The `PG` type family embeds a subset of Haskell types
735729as Postgres types. As an open type family, `PG` is extensible.