on dev help sign in
create drop and query views
commit6cb59163b219985fccba29d36a12c0659b5f4379 authorEitan Chatav <eitan.chatav@gmail.com> dateMon 9 Apr 2018, 11:35 (9 years ago) parentdb940a0
3 files changed+49−9
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+29−9 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+13 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+7
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+29−9
@@ -64,6 +64,9 @@ module Squeal.PostgreSQL.Definition
6464 , setNotNull
6565 , dropNotNull
6666 , alterType
67+ -- * Views
68+ , createView
69+ , dropView
6770 ) where
6871
6972import Control.Category
@@ -77,6 +80,7 @@ import qualified Generics.SOP as SOP
7780import qualified GHC.Generics as GHC
7881
7982import Squeal.PostgreSQL.Expression
83+import Squeal.PostgreSQL.Query
8084import Squeal.PostgreSQL.Render
8185import Squeal.PostgreSQL.Schema
8286
@@ -122,8 +126,8 @@ createTable
122126 -> NP (Aliased (TableConstraintExpression schema1 table)) constraints
123127 -- ^ constraints that must hold for the table
124128 -> Definition schema0 schema1
125−createTable table columns constraints = UnsafeDefinition $
126− "CREATE TABLE" <+> renderCreation table columns constraints
129+createTable tab columns constraints = UnsafeDefinition $
130+ "CREATE TABLE" <+> renderCreation tab columns constraints
127131
128132-- | `createTableIfNotExists` creates a table if it doesn't exist, but does not add it to the schema.
129133-- Instead, the schema already has the table so if the table did not yet exist, the schema was wrong.
@@ -147,9 +151,9 @@ createTableIfNotExists
147151 -> NP (Aliased (TableConstraintExpression schema table)) constraints
148152 -- ^ constraints that must hold for the table
149153 -> Definition schema schema
150−createTableIfNotExists table columns constraints = UnsafeDefinition $
154+createTableIfNotExists tab columns constraints = UnsafeDefinition $
151155 "CREATE TABLE IF NOT EXISTS"
152− <+> renderCreation table columns constraints
156+ <+> renderCreation tab columns constraints
153157
154158-- helper function for `createTable` and `createTableIfNotExists`
155159renderCreation
@@ -162,7 +166,7 @@ renderCreation
162166 -> NP (Aliased (TableConstraintExpression schema table)) constraints
163167 -- ^ constraints that must hold for the table
164168 -> ByteString
165−renderCreation table columns constraints = renderAlias table
169+renderCreation tab columns constraints = renderAlias tab
166170 <+> parenthesized
167171 ( renderCommaSeparated renderColumnDef columns
168172 <> ( case constraints of
@@ -453,10 +457,10 @@ DROP statements
453457-- >>> renderDefinition $ dropTable #muh_table
454458-- "DROP TABLE \"muh_table\";"
455459dropTable
456− :: KnownSymbol table
460+ :: Has table schema ('Table t)
457461 => Alias table -- ^ table to remove
458462 -> Definition schema (Drop table schema)
459−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
463+dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderAlias tab <> ";"
460464
461465{-----------------------------------------
462466ALTER statements
@@ -468,9 +472,9 @@ alterTable
468472 => Alias alias -- ^ table to alter
469473 -> AlterTable alias table schema -- ^ alteration to perform
470474 -> Definition schema (Alter alias ('Table table) schema)
471−alterTable table alteration = UnsafeDefinition $
475+alterTable tab alteration = UnsafeDefinition $
472476 "ALTER TABLE"
473− <+> renderAlias table
477+ <+> renderAlias tab
474478 <+> renderAlterTable alteration
475479 <> ";"
476480
@@ -731,3 +735,19 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
731735-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" TYPE numeric NOT NULL;"
732736alterType :: TypeExpression ty -> AlterColumn ty0 ty
733737alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderTypeExpression ty
738+
739+createView
740+ :: KnownSymbol view
741+ => Alias view -- ^ the name of the table to add
742+ -> Query schema '[] relation
743+ -- ^ query
744+ -> Definition schema (Create view ('View relation) schema)
745+createView alias query = UnsafeDefinition $
746+ "CREATE" <+> "VIEW" <+> renderAlias alias <+> "AS"
747+ <+> renderQuery query <> ";"
748+
749+dropView
750+ :: Has view schema ('View v)
751+ => Alias view -- ^ view to remove
752+ -> Definition schema (Drop view schema)
753+dropView v = UnsafeDefinition $ "DROP VIEW" <+> renderAlias v <> ";"
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+13
@@ -99,6 +99,7 @@ module Squeal.PostgreSQL.Expression
9999 , max_, maxDistinct, min_, minDistinct
100100 -- * Tables
101101 , Table (UnsafeTable, renderTable)
102+ , View (UnsafeView, renderView)
102103 -- * Types
103104 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
104105 , PGTyped (pgtype)
@@ -1036,6 +1037,18 @@ instance
10361037 ) => IsLabel alias (Table schema relation) where
10371038 fromLabel = UnsafeTable $ renderAlias (Alias @alias)
10381039
1040+-- | A `View` from a table expression is a way
1041+-- to call a table reference by its alias.
1042+newtype View
1043+ (schema :: SchemaType)
1044+ (columns :: RelationType)
1045+ = UnsafeView { renderView :: ByteString }
1046+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
1047+instance
1048+ ( Has alias (ViewsOf schema) columns
1049+ ) => IsLabel alias (View schema columns) where
1050+ fromLabel = UnsafeView $ renderAlias (Alias @alias)
1051+
10391052{-----------------------------------------
10401053type expressions
10411054-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+7
@@ -58,6 +58,7 @@ module Squeal.PostgreSQL.Query
5858 , FromClause (..)
5959 , table
6060 , subquery
61+ , view
6162 , crossJoin
6263 , innerJoin
6364 , leftOuterJoin
@@ -612,6 +613,12 @@ subquery
612613 -> FromClause schema params '[table]
613614subquery = UnsafeFromClause . renderAliasedAs (parenthesized . renderQuery)
614615
616+-- | `view` derives a table from a `View`.
617+view
618+ :: Aliased (View schema) table
619+ -> FromClause schema params '[table]
620+view = UnsafeFromClause . renderAliasedAs renderView
621+
615622{- | @left & crossJoin right@. For every possible combination of rows from
616623 @left@ and @right@ (i.e., a Cartesian product), the joined table will contain
617624 a row consisting of all columns in @left@ followed by all columns in @right@.