on dev help sign in
some cleanup and renaming

some cleanup and renaming

commitcebc2a70520cd53f8535b1d3cc82ffb61a5a5276 authorEitan Chatav <eitan.chatav@gmail.com> dateSat 17 Feb 2018, 10:58 (9 years ago) parentc614336
6 files changed+95−87
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+7−7 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+3−3 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+13−13 squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+4−4 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+5−5 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+63−55
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+7−7
@@ -88,8 +88,8 @@ statements
8888-- database, like a `createTable`, `dropTable`, or `alterTable` command.
8989-- `Definition`s may be composed using the `>>>` operator.
9090newtype Definition
91− (schema0 :: SchemaType)
92− (schema1 :: SchemaType)
91+ (schema0 :: TablesType)
92+ (schema1 :: TablesType)
9393 = UnsafeDefinition { renderDefinition :: ByteString }
9494 deriving (GHC.Generic,Show,Eq,Ord,NFData)
9595
@@ -153,7 +153,7 @@ createTable table columns constraints = UnsafeDefinition $
153153-- violate a constraint, an error is raised. This applies
154154-- even if the value came from the default value definition.
155155newtype TableConstraintExpression
156− (schema :: SchemaType)
156+ (schema :: TablesType)
157157 (columns :: ColumnsType)
158158 (tableConstraint :: TableConstraint)
159159 = UnsafeTableConstraintExpression
@@ -185,7 +185,7 @@ renderColumn (Column column) = renderAlias column
185185-- :}
186186-- "CREATE TABLE tab (a int NOT NULL, b int NOT NULL, CONSTRAINT inequality CHECK ((a > b)));"
187187check
188− :: Condition '[table ::: UnconstrainColumns columns] 'Ungrouped '[]
188+ :: Condition '[table ::: ColumnsToRelation columns] 'Ungrouped '[]
189189 -- ^ condition to check
190190 -> TableConstraintExpression schema columns 'Check
191191check condition = UnsafeTableConstraintExpression $
@@ -273,7 +273,7 @@ primaryKey columns = UnsafeTableConstraintExpression $
273273-- "CREATE TABLE users (id serial, username text NOT NULL, CONSTRAINT pk_id PRIMARY KEY (id)); CREATE TABLE emails (id serial, userid integer NOT NULL, email text NOT NULL, CONSTRAINT pk_id PRIMARY KEY (id), CONSTRAINT fk_user_id FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT);"
274274foreignKey
275275 :: ( Has table schema reftable
276− , refcolumns ~ UnconstrainTable reftable
276+ , refcolumns ~ TableToColumns reftable
277277 , SameTypes subcolumns refsubcolumns
278278 , AllNotNull subcolumns
279279 , SOP.SListI subcolumns
@@ -394,7 +394,7 @@ alterTableRename table0 table1 = UnsafeDefinition $
394394addConstraint
395395 :: KnownSymbol constraintName
396396 => Alias constraintName
397− -> TableConstraintExpression schema (UnconstrainTable table) constraint
397+ -> TableConstraintExpression schema (TableToColumns table) constraint
398398 -> AlterTable schema table (AddConstraint constraintName constraint table)
399399addConstraint constraintName constraint = UnsafeAlterTable $
400400 "ADD" <+> "CONSTRAINT" <+> renderAlias constraintName
@@ -412,7 +412,7 @@ dropConstraint constraint = UnsafeAlterTable $
412412-- | An `AlterTable` describes the alteration to perform on the columns
413413-- of a table.
414414newtype AlterTable
415− (schema :: SchemaType)
415+ (schema :: TablesType)
416416 (table0 :: TableType)
417417 (table1 :: TableType) =
418418 UnsafeAlterTable {renderAlterTable :: ByteString}
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+3−3
@@ -173,7 +173,7 @@ values from primitive expression using arithmetic, logical,
173173and other operations.
174174-}
175175newtype Expression
176− (relation :: RelationProduct)
176+ (relation :: RelationsType)
177177 (grouping :: Grouping)
178178 (params :: [NullityType])
179179 (ty :: NullityType)
@@ -939,13 +939,13 @@ tables
939939-- | A `Subtable` from a table expression is a way
940940-- to call a table reference by its alias.
941941newtype Subtable
942− (table :: RelationProduct)
942+ (table :: RelationsType)
943943 (columns :: RelationType)
944944 = UnsafeSubtable { renderSubtable :: ByteString }
945945 deriving (GHC.Generic,Show,Eq,Ord,NFData)
946946instance Has
947947 (subtable :: Symbol)
948− (table :: RelationProduct)
948+ (table :: RelationsType)
949949 (columns :: RelationType)
950950 => IsLabel subtable (Subtable table columns) where
951951 fromLabel = UnsafeSubtable $ renderAlias (Alias @subtable)
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+13−13
@@ -58,7 +58,7 @@ import Squeal.PostgreSQL.Schema
5858-- `deleteFrom`. A `Query` is also considered a `Manipulation` even though
5959-- it does not modify data.
6060newtype Manipulation
61− (schema :: SchemaType)
61+ (schema :: TablesType)
6262 (params :: [NullityType])
6363 (columns :: RelationType)
6464 = UnsafeManipulation { renderManipulation :: ByteString }
@@ -179,7 +179,7 @@ insertRows
179179 :: ( SOP.SListI columns
180180 , SOP.SListI results
181181 , Has tab schema table
182− , columns ~ UnconstrainTable table )
182+ , columns ~ TableToColumns table )
183183 => Alias tab -- ^ table to insert into
184184 -> NP (Aliased (ColumnValue '[] params)) columns
185185 -> [NP (Aliased (ColumnValue '[] params)) columns]
@@ -208,9 +208,9 @@ insertQuery
208208 :: ( SOP.SListI columns
209209 , SOP.SListI results
210210 , Has tab schema table
211− , columns ~ UnconstrainTable table )
211+ , columns ~ TableToColumns table )
212212 => Alias tab -- ^ table to insert into
213− -> Query schema params (UnconstrainColumns columns)
213+ -> Query schema params (ColumnsToRelation columns)
214214 -> ConflictClause columns params
215215 -- ^ what to do in case of constraint conflict
216216 -> ReturningClause columns params results -- ^ results to return
@@ -248,10 +248,10 @@ data ReturningClause
248248 (results :: RelationType)
249249 where
250250 ReturningStar
251− :: ReturningClause columns params (UnconstrainColumns columns)
251+ :: ReturningClause columns params (ColumnsToRelation columns)
252252 Returning
253253 :: NP
254− (Aliased (Expression '[table ::: (UnconstrainColumns columns)] 'Ungrouped params))
254+ (Aliased (Expression '[table ::: (ColumnsToRelation columns)] 'Ungrouped params))
255255 results
256256 -> ReturningClause columns params results
257257
@@ -275,8 +275,8 @@ data ConflictClause (columns :: ColumnsType) params where
275275 OnConflictDoRaise :: ConflictClause columns params
276276 OnConflictDoNothing :: ConflictClause columns params
277277 OnConflictDoUpdate
278− :: NP (Aliased (ColumnValue (UnconstrainColumns columns) params)) columns
279− -> [Condition '[table ::: UnconstrainColumns columns] 'Ungrouped params]
278+ :: NP (Aliased (ColumnValue (ColumnsToRelation columns) params)) columns
279+ -> [Condition '[table ::: ColumnsToRelation columns] 'Ungrouped params]
280280 -> ConflictClause columns params
281281
282282-- | Render a `ConflictClause`.
@@ -327,11 +327,11 @@ update
327327 :: ( SOP.SListI columns
328328 , SOP.SListI results
329329 , Has tab schema table
330− , columns ~ UnconstrainTable table )
330+ , columns ~ TableToColumns table )
331331 => Alias tab -- ^ table to update
332− -> NP (Aliased (ColumnValue (UnconstrainColumns columns) params)) columns
332+ -> NP (Aliased (ColumnValue (ColumnsToRelation columns) params)) columns
333333 -- ^ modified values to replace old values
334− -> Condition '[tab ::: UnconstrainColumns columns] 'Ungrouped params
334+ -> Condition '[tab ::: ColumnsToRelation columns] 'Ungrouped params
335335 -- ^ condition under which to perform update on a row
336336 -> ReturningClause columns params results -- ^ results to return
337337 -> Manipulation schema params results
@@ -374,9 +374,9 @@ DELETE statements
374374deleteFrom
375375 :: ( SOP.SListI results
376376 , Has tab schema table
377− , columns ~ UnconstrainTable table )
377+ , columns ~ TableToColumns table )
378378 => Alias tab -- ^ table to delete from
379− -> Condition '[tab ::: UnconstrainColumns columns] 'Ungrouped params
379+ -> Condition '[tab ::: ColumnsToRelation columns] 'Ungrouped params
380380 -- ^ condition under which to delete a row
381381 -> ReturningClause columns params results -- ^ results to return
382382 -> Manipulation schema params results
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+4−4
@@ -100,8 +100,8 @@ import qualified Control.Monad.Trans.RWS.Lazy as Lazy
100100import qualified Control.Monad.Trans.RWS.Strict as Strict
101101
102102-- | A `Connection` consists of a `Database.PostgreSQL.LibPQ`
103−-- `Database.PastgreSQL.LibPQ.Connection` and a phantom `SchemaType`
104−newtype Connection (schema :: SchemaType) =
103+-- `Database.PastgreSQL.LibPQ.Connection` and a phantom `TablesType`
104+newtype Connection (schema :: TablesType) =
105105 Connection { unConnection :: LibPQ.Connection }
106106
107107{- | Makes a new connection to the database server.
@@ -153,8 +153,8 @@ withConnection connString action = do
153153-- | We keep track of the schema via an Atkey indexed state monad transformer,
154154-- `PQ`.
155155newtype PQ
156− (schema0 :: SchemaType)
157− (schema1 :: SchemaType)
156+ (schema0 :: TablesType)
157+ (schema1 :: TablesType)
158158 (m :: Type -> Type)
159159 (x :: Type) =
160160 PQ { runPQ :: Connection schema0 -> m (x, Connection schema1) }
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+5−5
@@ -257,7 +257,7 @@ in renderQuery query
257257-}
258258
259259newtype Query
260− (schema :: SchemaType)
260+ (schema :: TablesType)
261261 (params :: [NullityType])
262262 (columns :: RelationType)
263263 = UnsafeQuery { renderQuery :: ByteString }
@@ -419,9 +419,9 @@ Table Expressions
419419-- to a table on disk, a so-called base table, but more complex expressions
420420-- can be used to modify or combine base tables in various ways.
421421data TableExpression
422− (schema :: SchemaType)
422+ (schema :: TablesType)
423423 (params :: [NullityType])
424− (tables :: RelationProduct)
424+ (tables :: RelationsType)
425425 (grouping :: Grouping)
426426 = TableExpression
427427 { fromClause :: FromClause schema params tables
@@ -612,7 +612,7 @@ their placement in SQL.
612612-}
613613data FromClause schema params tables where
614614 Table
615− :: Aliased (Subtable (UnconstrainSchema schema)) table
615+ :: Aliased (Subtable (TablesToRelations schema)) table
616616 -> FromClause schema params '[table]
617617 Subquery
618618 :: Aliased (Query schema params) table
@@ -669,7 +669,7 @@ Grouping
669669-- column @col@; otherwise @By2 (\#tab \! \#col)@ will reference a table
670670-- qualified column @tab.col@.
671671data By
672− (tables :: RelationProduct)
672+ (tables :: RelationsType)
673673 (by :: (Symbol,Symbol)) where
674674 By
675675 :: (HasUnique table tables columns, Has column columns ty)
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+63−55
@@ -39,9 +39,9 @@ module Squeal.PostgreSQL.Schema
3939 , ColumnType
4040 , ColumnsType
4141 , RelationType
42− , RelationProduct
42+ , RelationsType
4343 , TableType
44− , SchemaType
44+ , TablesType
4545 , Grouping (..)
4646 -- * Constraints
4747 , PGNum
@@ -77,11 +77,9 @@ module Squeal.PostgreSQL.Schema
7777 , SameFields
7878 -- * PostgreSQL Constraints
7979 , (:=>)
80− , Unconstraint
81− , UnconstrainColumn
82− , UnconstrainColumns
83− , UnconstrainTable
84− , UnconstrainSchema
80+ , ColumnsToRelation
81+ , TableToColumns
82+ , TablesToRelations
8583 , ColumnConstraint (..)
8684 , TableConstraint (..)
8785 , Aliases
@@ -131,44 +129,74 @@ data NullityType
131129 = Null PGType -- ^ @NULL@ may be present
132130 | NotNull PGType -- ^ @NULL@ is absent
133131
134−type RelationType = [(Symbol,NullityType)]
135−type RelationProduct = [(Symbol,RelationType)]
132+-- | `:=>` is a type level pair between an "constraint" and some type,
133+-- either a `ColumnConstraint` and a `NullityType` or a row of
134+-- `TableConstraint`s and a `TableType`.
135+type (:=>) constraint ty = '(constraint,ty)
136+infixr 7 :=>
137+
138+-- | `:::` is like a promoted version of `As`, a type level pair between
139+-- an alias and some type, like a column alias and either a `ColumnType` or
140+-- `NullityType` or a table alias and either a `TableType` or a `RelationType`
141+-- or a constraint alias and a `TableConstraint`.
142+type (:::) (alias :: Symbol) ty = '(alias,ty)
143+infixr 6 :::
144+
145+-- | `ColumnConstraint` encodes the allowance of @DEFAULT@.
146+data ColumnConstraint
147+ = Def -- ^ @DEFAULT@ is available for inserts and updates
148+ | NoDef -- ^ @DEFAULT@ is unavailable for inserts and updates
136149
137−-- | `ColumnType` encodes the allowance of @DEFAULT@ and the only way
138−-- to generate an `Optional` `Squeal.PostgreSQL.Expression.Expression`
139−-- is to use `Squeal.PostgreSQL.Expression.def`,
140−-- `Squeal.PostgreSQL.Expression.unDef` or
141−-- `Squeal.PostgreSQL.Expression.param`.
150+-- | `ColumnType` encodes the allowance of @DEFAULT@ and @NULL@ and the
151+-- base `PGType` for a column.
142152type ColumnType = (ColumnConstraint,NullityType)
143153
144−-- | `ColumnsType` is a kind synonym for a row of `ColumnType`s.
154+-- | `ColumnsType` is a row of `ColumnType`s.
145155type ColumnsType = [(Symbol,ColumnType)]
146156
147−type TableType = (TableConstraints,ColumnsType)
148−type SchemaType = [(Symbol,TableType)]
157+-- | `TableConstraint` encodes various forms of data constraints
158+-- of columns in a table.
159+data TableConstraint
160+ = Check
161+ | Unique [Symbol]
162+ | PrimaryKey [Symbol]
163+ | ForeignKey [Symbol] Symbol [Symbol]
149164
150−type family UnconstrainColumn (ty :: ColumnType) :: NullityType where
151− UnconstrainColumn (constraint :=> ty) = ty
165+-- | `TableType` encodes a row of constraints on a table as well as the types
166+-- of its columns.
167+type TableType = ([(Symbol,TableConstraint)],ColumnsType)
168+
169+-- | `TablesType` is a row of `TableType`s, thought of as a union.
170+type TablesType = [(Symbol,TableType)]
171+
172+-- | `RelationType` is a row of `NullityType`
173+type RelationType = [(Symbol,NullityType)]
152174
153−type family UnconstrainColumns (columns :: ColumnsType) :: RelationType where
154− UnconstrainColumns '[] = '[]
155− UnconstrainColumns (column ::: ty ': columns) =
156− column ::: UnconstrainColumn ty ': UnconstrainColumns columns
175+-- | `RelationsType` is a row of `RelationType`s, thought of as a product.
176+type RelationsType = [(Symbol,RelationType)]
157177
158−type family UnconstrainTable (table :: TableType) :: ColumnsType where
159− UnconstrainTable (constraints :=> columns) = columns
178+-- | `ColumnsToRelation` removes column constraints.
179+type family ColumnsToRelation (columns :: ColumnsType) :: RelationType where
180+ ColumnsToRelation '[] = '[]
181+ ColumnsToRelation (column ::: constraint :=> ty ': columns) =
182+ column ::: ty ': ColumnsToRelation columns
160183
161−type family UnconstrainSchema (schema :: SchemaType) :: RelationProduct where
162− UnconstrainSchema '[] = '[]
163− UnconstrainSchema (tab ::: constraints :=> columns ': tables) =
164− tab ::: UnconstrainColumns columns ': UnconstrainSchema tables
184+-- | `TableToColumns` removes table constraints.
185+type family TableToColumns (table :: TableType) :: ColumnsType where
186+ TableToColumns (constraints :=> columns) = columns
187+
188+-- | `TablesToRelations` removes both table and column constraints.
189+type family TablesToRelations (tables :: TablesType) :: RelationsType where
190+ TablesToRelations '[] = '[]
191+ TablesToRelations (alias ::: constraint :=> columns ': tables) =
192+ alias ::: ColumnsToRelation columns ': TablesToRelations tables
165193
166194-- | `Grouping` is an auxiliary namespace, created by
167195-- @GROUP BY@ clauses (`Squeal.PostgreSQL.Query.group`), and used
168196-- for typesafe aggregation
169197data Grouping
170− = Ungrouped
171− | Grouped [(Symbol,Symbol)]
198+ = Ungrouped -- ^ no aggregation permitted
199+ | Grouped [(Symbol,Symbol)] -- ^ aggregation required for any column which is not grouped
172200
173201-- | `PGNum` is a constraint on `PGType` whose
174202-- `Squeal.PostgreSQL.Expression.Expression`s have a `Num` constraint.
@@ -186,12 +214,6 @@ type PGFloating ty = In ty '[ 'PGfloat4, 'PGfloat8, 'PGnumeric]
186214-- `Squeal.PostgreSQL.Expression.mod_` functions.
187215type PGIntegral ty = In ty '[ 'PGint2, 'PGint4, 'PGint8]
188216
189−-- | `:::` is like a promoted version of `As`, a type level pair between
190−-- an alias and some type, usually a column alias and a `ColumnType` or
191−-- a table alias and a `ColumnsType`.
192−type (:::) (alias :: Symbol) (ty :: polykind) = '(alias,ty)
193−infixr 6 :::
194−
195217-- | `Alias`es are proxies for a type level string or `Symbol`
196218-- and have an `IsLabel` instance so that with @-XOverloadedLabels@
197219--
@@ -227,7 +249,7 @@ deriving instance Ord (expression ty)
227249 => Ord (Aliased expression (alias ::: ty))
228250instance KnownSymbol alias
229251 => IsLabel alias (expression ty -> Aliased expression (alias ::: ty))
230− where fromLabel = \ expression -> expression `As` Alias @alias
252+ where fromLabel expression = expression `As` Alias @alias
231253
232254-- | >>> let renderMaybe = fromString . maybe "Nothing" (const "Just")
233255-- >>> renderAliasedAs renderMaybe (Just (3::Int) `As` #an_int)
@@ -290,10 +312,10 @@ type family NullifyColumns (columns :: RelationType) :: RelationType where
290312 NullifyColumns (column ::: ty ': columns) =
291313 column ::: NullifyType ty ': NullifyColumns columns
292314
293−-- | `NullifyTables` is an idempotent that nullifies a `RelationProduct`
315+-- | `NullifyTables` is an idempotent that nullifies a `RelationsType`
294316-- used to nullify the left or right hand side of an outer join
295317-- in a `Squeal.PostgreSQL.Query.FromClause`.
296−type family NullifyTables (tables :: RelationProduct) :: RelationProduct where
318+type family NullifyTables (tables :: RelationsType) :: RelationsType where
297319 NullifyTables '[] = '[]
298320 NullifyTables (table ::: columns ': tables) =
299321 table ::: NullifyColumns columns ': NullifyTables tables
@@ -373,21 +395,7 @@ type family SameFields
373395 columns
374396 = AllZip SameField fields columns
375397
376−type (:=>) (constraints :: constraint) ty = '(constraints,ty)
377−infixr 7 :=>
378−
379−data ColumnConstraint
380− = Def
381− | NoDef
382−
383−data TableConstraint
384− = Check
385− | Unique [Symbol]
386− | PrimaryKey [Symbol]
387− | ForeignKey [Symbol] Symbol [Symbol]
388−
389−type TableConstraints = [(Symbol,TableConstraint)]
390−type Unconstraint = ('[] :: TableConstraints)
398+-- type Unconstraint = ('[] :: [(Symbol,TableConstraint)])
391399
392400type family Aliases xs where
393401 Aliases '[] = '[]