on dev help sign in
Merge branch 'dev' into dev-oid
842fa50Eitan Chatav
squeal-postgresql/exe/Example.hs+4−1
@@ -38,6 +38,7 @@ type Schema =
38 , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
38 , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
39 , "email" ::: 'NoDef :=> 'Null 'PGtext
39 , "email" ::: 'NoDef :=> 'Null 'PGtext
40 ])
40 ])
41+ , "positive" ::: 'Typedef 'PGfloat4
41 ]
42 ]
42
43
43type Schemas = Public Schema
44type Schemas = Public Schema
@@ -57,9 +58,11 @@ setup =
57 ( primaryKey #id `as` #pk_emails :*
58 ( primaryKey #id `as` #pk_emails :*
58 foreignKey #user_id #users #id
59 foreignKey #user_id #users #id
59 OnDeleteCascade OnUpdateCascade `as` #fk_user_id )
60 OnDeleteCascade OnUpdateCascade `as` #fk_user_id )
61+ >>>
62+ createDomain #positive real (#value .> 0 .&& (#value & isNotNull))
60
63
61teardown :: Definition Schemas (Public '[])
64teardown :: Definition Schemas (Public '[])
62−teardown = dropTable #emails >>> dropTable #users
65+teardown = dropType #positive >>> dropTable #emails >>> dropTable #users
63
66
64insertUser :: Manipulation_ Schemas (Text, VarArray (Vector (Maybe Int16))) (Only Int32)
67insertUser :: Manipulation_ Schemas (Text, VarArray (Vector (Maybe Int16))) (Only Int32)
65insertUser = insertInto #users
68insertUser = insertInto #users
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+1−1
@@ -1034,7 +1034,7 @@ createDomain
1034 => QualifiedAlias sch dom
1034 => QualifiedAlias sch dom
1035 -> (forall nullity. TypeExpression schemas (nullity ty))
1035 -> (forall nullity. TypeExpression schemas (nullity ty))
1036 -> (forall tab. Condition '[] '[] 'Ungrouped schemas '[] '[tab ::: '["value" ::: 'Null ty]])
1036 -> (forall tab. Condition '[] '[] 'Ungrouped schemas '[] '[tab ::: '["value" ::: 'Null ty]])
1037− -> Definition schemas (Alter sch (Create alias ('Typedef ty) schema) schemas)
1037+ -> Definition schemas (Alter sch (Create dom ('Typedef ty) schema) schemas)
1038createDomain dom ty condition =
1038createDomain dom ty condition =
1039 UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
1039 UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
1040 <+> "AS" <+> renderTypeExpression ty
1040 <+> "AS" <+> renderTypeExpression ty
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+32−23
@@ -249,6 +249,7 @@ type family Manipulation_ (schemas :: SchemasType) (params :: Type) (row :: Type
249-- | Convert a `Query` into a `Manipulation`.
249-- | Convert a `Query` into a `Manipulation`.
250queryStatement
250queryStatement
251 :: Query '[] commons schemas params columns
251 :: Query '[] commons schemas params columns
252+ -- ^ `Query` to embed as a `Manipulation`
252 -> Manipulation commons schemas params columns
253 -> Manipulation commons schemas params columns
253queryStatement q = UnsafeManipulation $ renderSQL q
254queryStatement q = UnsafeManipulation $ renderSQL q
254
255
@@ -271,9 +272,13 @@ insertInto
271 , SOP.SListI columns
272 , SOP.SListI columns
272 , SOP.SListI row1 )
273 , SOP.SListI row1 )
273 => QualifiedAlias sch tab
274 => QualifiedAlias sch tab
275+ -- ^ table
274 -> QueryClause commons schemas params columns
276 -> QueryClause commons schemas params columns
277+ -- ^ what to insert
275 -> ConflictClause tab commons schemas params table
278 -> ConflictClause tab commons schemas params table
279+ -- ^ what to do in case of conflict
276 -> ReturningClause commons schemas params '[tab ::: row0] row1
280 -> ReturningClause commons schemas params '[tab ::: row0] row1
281+ -- ^ what to return
277 -> Manipulation commons schemas params row1
282 -> Manipulation commons schemas params row1
278insertInto tab qry conflict ret = UnsafeManipulation $
283insertInto tab qry conflict ret = UnsafeManipulation $
279 "INSERT" <+> "INTO" <+> renderSQL tab
284 "INSERT" <+> "INTO" <+> renderSQL tab
@@ -281,7 +286,7 @@ insertInto tab qry conflict ret = UnsafeManipulation $
281 <> renderSQL conflict
286 <> renderSQL conflict
282 <> renderSQL ret
287 <> renderSQL ret
283
288
284−-- | Like `insertInto` but with `OnConflictDoRaise` and no `ReturningClause`.
289+-- | Like `insertInto` but with `OnConflictDoRaise` and `no` `ReturningClause`.
285insertInto_
290insertInto_
286 :: ( Has sch schemas schema
291 :: ( Has sch schemas schema
287 , Has tab schema ('Table table)
292 , Has tab schema ('Table table)
@@ -289,30 +294,33 @@ insertInto_
289 , row ~ TableToRow table
294 , row ~ TableToRow table
290 , SOP.SListI columns )
295 , SOP.SListI columns )
291 => QualifiedAlias sch tab
296 => QualifiedAlias sch tab
297+ -- ^ table
292 -> QueryClause commons schemas params columns
298 -> QueryClause commons schemas params columns
299+ -- ^ what to insert
293 -> Manipulation commons schemas params '[]
300 -> Manipulation commons schemas params '[]
294insertInto_ tab qry =
301insertInto_ tab qry =
295 insertInto tab qry OnConflictDoRaise (Returning_ Nil)
302 insertInto tab qry OnConflictDoRaise (Returning_ Nil)
296
303
297-- | A `QueryClause` describes what to `insertInto` a table.
304-- | A `QueryClause` describes what to `insertInto` a table.
298data QueryClause commons schemas params columns where
305data QueryClause commons schemas params columns where
299− -- | `Values` describes `NP` lists of `Aliased` `Optional` `Expression`s
300− -- whose `ColumnsType` must match the tables'.
301 Values
306 Values
302 :: SOP.SListI columns
307 :: SOP.SListI columns
303 => NP (Aliased (Optional (Expression '[] commons 'Ungrouped schemas params '[]))) columns
308 => NP (Aliased (Optional (Expression '[] commons 'Ungrouped schemas params '[]))) columns
309+ -- ^ row of values
304 -> [NP (Aliased (Optional (Expression '[] commons 'Ungrouped schemas params '[]))) columns]
310 -> [NP (Aliased (Optional (Expression '[] commons 'Ungrouped schemas params '[]))) columns]
311+ -- ^ additional rows of values
305 -> QueryClause commons schemas params columns
312 -> QueryClause commons schemas params columns
306− -- | `Select` describes a subquery that permits use of `Optional` `Expression`s.
307 Select
313 Select
308 :: SOP.SListI columns
314 :: SOP.SListI columns
309 => NP (Aliased (Optional (Expression '[] commons grp schemas params from))) columns
315 => NP (Aliased (Optional (Expression '[] commons grp schemas params from))) columns
316+ -- ^ row of values
310 -> TableExpression '[] commons grp schemas params from
317 -> TableExpression '[] commons grp schemas params from
318+ -- ^ from a table expression
311 -> QueryClause commons schemas params columns
319 -> QueryClause commons schemas params columns
312− -- | `Subquery` describes a subquery whose `RowType` must match the tables'.
313 Subquery
320 Subquery
314 :: ColumnsToRow columns ~ row
321 :: ColumnsToRow columns ~ row
315 => Query '[] commons schemas params row
322 => Query '[] commons schemas params row
323+ -- ^ subquery to insert
316 -> QueryClause commons schemas params columns
324 -> QueryClause commons schemas params columns
317
325
318instance RenderSQL (QueryClause commons schemas params columns) where
326instance RenderSQL (QueryClause commons schemas params columns) where
@@ -350,6 +358,7 @@ instance RenderSQL (QueryClause commons schemas params columns) where
350pattern Values_
358pattern Values_
351 :: SOP.SListI columns
359 :: SOP.SListI columns
352 => NP (Aliased (Optional (Expression '[] commons 'Ungrouped schemas params '[]))) columns
360 => NP (Aliased (Optional (Expression '[] commons 'Ungrouped schemas params '[]))) columns
361+ -- ^ row of values
353 -> QueryClause commons schemas params columns
362 -> QueryClause commons schemas params columns
354pattern Values_ vals = Values vals []
363pattern Values_ vals = Values vals []
355
364
@@ -374,7 +383,7 @@ instance (forall x. RenderSQL (expr x)) => RenderSQL (Optional expr ty) where
374-- deleted will be returned. For example, if a row was locked
383-- deleted will be returned. For example, if a row was locked
375-- but not updated because an `OnConflict` `DoUpdate` condition was not satisfied,
384-- but not updated because an `OnConflict` `DoUpdate` condition was not satisfied,
376-- the row will not be returned. `Returning` `Star` will return all columns
385-- the row will not be returned. `Returning` `Star` will return all columns
377−-- in the row. Use @Returning Nil@ in the common case where no return
386+-- in the row. Use `Returning` `Nil` in the common case where no return
378-- values are desired.
387-- values are desired.
379newtype ReturningClause commons schemas params from row =
388newtype ReturningClause commons schemas params from row =
380 Returning (Selection '[] commons 'Ungrouped schemas params from row)
389 Returning (Selection '[] commons 'Ungrouped schemas params from row)
@@ -388,6 +397,7 @@ instance RenderSQL (ReturningClause commons schemas params from row) where
388pattern Returning_
397pattern Returning_
389 :: SOP.SListI row
398 :: SOP.SListI row
390 => NP (Aliased (Expression '[] commons 'Ungrouped schemas params from)) row
399 => NP (Aliased (Expression '[] commons 'Ungrouped schemas params from)) row
400+ -- ^ row of values
391 -> ReturningClause commons schemas params from row
401 -> ReturningClause commons schemas params from row
392pattern Returning_ list = Returning (List list)
402pattern Returning_ list = Returning (List list)
393
403
@@ -400,7 +410,9 @@ data ConflictClause tab commons schemas params table where
400 OnConflictDoRaise :: ConflictClause tab commons schemas params table
410 OnConflictDoRaise :: ConflictClause tab commons schemas params table
401 OnConflict
411 OnConflict
402 :: ConflictTarget constraints
412 :: ConflictTarget constraints
413+ -- ^ conflict target
403 -> ConflictAction tab commons schemas params columns
414 -> ConflictAction tab commons schemas params columns
415+ -- ^ conflict action
404 -> ConflictClause tab commons schemas params (constraints :=> columns)
416 -> ConflictClause tab commons schemas params (constraints :=> columns)
405
417
406-- | Render a `ConflictClause`.
418-- | Render a `ConflictClause`.
@@ -418,12 +430,12 @@ the exact details of the `update` action to be performed in case of a conflict.
418The `Set` and WHERE `Condition`s in `OnConflict` `DoUpdate` have access to the
430The `Set` and WHERE `Condition`s in `OnConflict` `DoUpdate` have access to the
419existing row using the table's name (or an alias), and to rows proposed
431existing row using the table's name (or an alias), and to rows proposed
420for insertion using the special @#excluded@ table.
432for insertion using the special @#excluded@ table.
433+`OnConflict` `DoNothing` simply avoids inserting a row as its alternative action.
434+`OnConflict` `DoUpdate` updates the existing row that conflicts
435+with the row proposed for insertion as its alternative action.
421-}
436-}
422data ConflictAction tab commons schemas params columns where
437data ConflictAction tab commons schemas params columns where
423− -- | `OnConflict` `DoNothing` simply avoids inserting a row as its alternative action.
424 DoNothing :: ConflictAction tab commons schemas params columns
438 DoNothing :: ConflictAction tab commons schemas params columns
425− -- | `OnConflict` `DoUpdate` updates the existing row that conflicts
426− -- with the row proposed for insertion as its alternative action.
427 DoUpdate
439 DoUpdate
428 :: ( row ~ ColumnsToRow columns
440 :: ( row ~ ColumnsToRow columns
429 , SOP.SListI columns
441 , SOP.SListI columns
@@ -473,7 +485,6 @@ UPDATE statements
473update
485update
474 :: ( SOP.SListI columns
486 :: ( SOP.SListI columns
475 , SOP.SListI row1
487 , SOP.SListI row1
476− , db ~ (commons :=> schemas)
477 , Has sch schemas schema
488 , Has sch schemas schema
478 , Has tab schema ('Table table)
489 , Has tab schema ('Table table)
479 , row0 ~ TableToRow table
490 , row0 ~ TableToRow table
@@ -498,7 +509,6 @@ update tab columns wh returning = UnsafeManipulation $
498-- | Update a row returning `Nil`.
509-- | Update a row returning `Nil`.
499update_
510update_
500 :: ( SOP.SListI columns
511 :: ( SOP.SListI columns
501− , db ~ (commons :=> schemas)
502 , Has sch schemas schema
512 , Has sch schemas schema
503 , Has tab schema ('Table table)
513 , Has tab schema ('Table table)
504 , row ~ TableToRow table
514 , row ~ TableToRow table
@@ -517,25 +527,25 @@ update_ tab columns wh = update tab columns wh (Returning_ Nil)
517DELETE statements
527DELETE statements
518-----------------------------------------}
528-----------------------------------------}
519
529
520−-- | Specify additional tables.
530+-- | Specify additional tables with `Using`
531+-- an `also` list of table expressions, allowing columns
532+-- from other tables to appear in the WHERE condition.
533+-- This is similar to the list of tables that can be specified
534+-- in the FROM Clause of a SELECT statement;
535+-- for example, an alias for the table name can be specified.
536+-- Do not repeat the target table in the `Using` list,
537+-- unless you wish to set up a self-join.
538+-- `NoUsing` if no additional tables are to be used.
521data UsingClause commons schemas params from where
539data UsingClause commons schemas params from where
522− -- | No `UsingClause`
523 NoUsing :: UsingClause commons schemas params '[]
540 NoUsing :: UsingClause commons schemas params '[]
524− -- | An `also` list of table expressions, allowing columns
525− -- from other tables to appear in the WHERE condition.
526− -- This is similar to the list of tables that can be specified
527− -- in the FROM Clause of a SELECT statement;
528− -- for example, an alias for the table name can be specified.
529− -- Do not repeat the target table in the `Using` list,
530− -- unless you wish to set up a self-join.
531 Using
541 Using
532 :: FromClause '[] commons schemas params from
542 :: FromClause '[] commons schemas params from
543+ -- ^ what to use
533 -> UsingClause commons schemas params from
544 -> UsingClause commons schemas params from
534
545
535-- | Delete rows from a table.
546-- | Delete rows from a table.
536deleteFrom
547deleteFrom
537 :: ( SOP.SListI row1
548 :: ( SOP.SListI row1
538− , db ~ (commons :=> schemas)
539 , Has sch schemas schema
549 , Has sch schemas schema
540 , Has tab schema ('Table table)
550 , Has tab schema ('Table table)
541 , row0 ~ TableToRow table
551 , row0 ~ TableToRow table
@@ -557,8 +567,7 @@ deleteFrom tab using wh returning = UnsafeManipulation $
557
567
558-- | Delete rows returning `Nil`.
568-- | Delete rows returning `Nil`.
559deleteFrom_
569deleteFrom_
560− :: ( db ~ (commons :=> schemas)
561− , Has sch schemas schema
570+ :: ( Has sch schemas schema
562 , Has tab schema ('Table table)
571 , Has tab schema ('Table table)
563 , row ~ TableToRow table
572 , row ~ TableToRow table
564 , columns ~ TableToColumns table )
573 , columns ~ TableToColumns table )