on dev help sign in
Found some issues with `TypeExpression`s and `ColumnTypeExpression`s that have to be resolved
06033d9Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+60
@@ -6,6 +6,66 @@ Maintainer: eitan@morphism.tech
6Stability: experimental
6Stability: experimental
7
7
8Binary encoding and decoding between Haskell and PostgreSQL types.
8Binary encoding and decoding between Haskell and PostgreSQL types.
9+
10+Instances are governed by the `Generic` and `HasDatatype` typeclasses.
11+
12+>>> import Control.Monad
13+>>> import Control.Monad.Base
14+
15+>>> import Squeal.PostgreSQL
16+
17+>>> data Schwarma = Beef | Lamb | Chicken deriving (Show, GHC.Generic)
18+>>> instance Generic Schwarma
19+>>> instance HasDatatypeInfo Schwarma
20+
21+>>> let q = values_ (label @"Beef" `As` #fromOnly :* Nil) :: Query '[] '[] '["fromOnly" ::: 'NotNull (EnumWith Schwarma)]
22+
23+>>> :{
24+void . withConnection "host=localhost port=5432 dbname=exampledb" $ do
25+ result <- runQuery q
26+ Just (Only schwarma) <- firstRow result
27+ liftBase $ print (schwarma :: Schwarma)
28+:}
29+Beef
30+
31+>>> :{
32+type family Schema :: SchemaType where
33+ Schema =
34+ '[ "schwarma" ::: 'Typedef (EnumWith Schwarma)
35+ , "tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull (EnumWith Schwarma)])
36+ ]
37+:}
38+
39+>>> :{
40+let
41+ setup =
42+ createTypeEnumWith @Schwarma #schwarma >>>
43+ createTable #tab (#schwarma `As` #col :* Nil) Nil
44+ :: Definition '[] Schema
45+ teardown =
46+ dropTable #tab >>>
47+ dropType #schwarma
48+ :: Definition Schema '[]
49+ manip =
50+ insertRow_ #tab (Set (param @1) `As` #col :* Nil)
51+ :: Manipulation Schema '[ 'NotNull (EnumWith Schwarma)] '[]
52+ qry =
53+ select (#col `As` #fromOnly :* Nil) (from (table #tab))
54+ :: Query Schema '[] '["fromOnly" ::: 'NotNull (EnumWith Schwarma)]
55+ session = do
56+ manipulateParams manip (Only Chicken)
57+ result <- runQuery qry
58+ Just (Only schwarma) <- firstRow result
59+ liftBase $ print (schwarma :: Schwarma)
60+:}
61+
62+>>> :{
63+void . withConnection "host=localhost port=5432 dbname=exampledb" $
64+ define setup
65+ & pqThen session
66+ & pqThen (define teardown)
67+:}
68+Chicken
9-}
69-}
10
70
11{-# LANGUAGE
71{-# LANGUAGE
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+1−1
@@ -896,7 +896,7 @@ newtype ColumnTypeExpression (schema :: SchemaType) (ty :: ColumnType)
896
896
897instance (Has alias schema ('Typedef ty))
897instance (Has alias schema ('Typedef ty))
898 => IsLabel alias (ColumnTypeExpression schema ('NoDef :=> 'NotNull ty)) where
898 => IsLabel alias (ColumnTypeExpression schema ('NoDef :=> 'NotNull ty)) where
899− fromLabel = UnsafeColumnTypeExpression (renderAlias (fromLabel @alias))
899+ fromLabel = UnsafeColumnTypeExpression (renderAlias (fromLabel @alias) <+> "NOT NULL")
900
900
901-- | used in `createTable` commands as a column constraint to note that
901-- | used in `createTable` commands as a column constraint to note that
902-- @NULL@ may be present in a column
902-- @NULL@ may be present in a column
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+11
@@ -37,6 +37,7 @@ module Squeal.PostgreSQL.Query
37 , selectDotStar
37 , selectDotStar
38 , selectDistinctDotStar
38 , selectDistinctDotStar
39 , values
39 , values
40+ , values_
40 -- * Table Expressions
41 -- * Table Expressions
41 , TableExpression (..)
42 , TableExpression (..)
42 , renderTableExpression
43 , renderTableExpression
@@ -440,6 +441,7 @@ values
440 => NP (Aliased (Expression '[] 'Ungrouped params)) cols
441 => NP (Aliased (Expression '[] 'Ungrouped params)) cols
441 -> [NP (Aliased (Expression '[] 'Ungrouped params)) cols]
442 -> [NP (Aliased (Expression '[] 'Ungrouped params)) cols]
442 -- ^ When more than one row is specified, all the rows must
443 -- ^ When more than one row is specified, all the rows must
444+ -- must have the same number of elements
443 -> Query schema params cols
445 -> Query schema params cols
444values rw rws = UnsafeQuery $ "SELECT * FROM"
446values rw rws = UnsafeQuery $ "SELECT * FROM"
445 <+> parenthesized (
447 <+> parenthesized (
@@ -455,6 +457,15 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
455 renderAliasPart (_ `As` name) = renderAlias name
457 renderAliasPart (_ `As` name) = renderAlias name
456 renderValuePart (value `As` _) = renderExpression value
458 renderValuePart (value `As` _) = renderExpression value
457
459
460+-- | `values_` computes a row value or set of row values
461+-- specified by value expressions.
462+values_
463+ :: SListI cols
464+ => NP (Aliased (Expression '[] 'Ungrouped params)) cols
465+ -- ^ one row of values
466+ -> Query schema params cols
467+values_ rw = values rw []
468+
458{-----------------------------------------
469{-----------------------------------------
459Table Expressions
470Table Expressions
460-----------------------------------------}
471-----------------------------------------}