on dev help sign in
remove default instances and modify params
commitf258d523964e1e8ce24c33bafef1eb1b8c7d3b9e authorEitan Chatav <eitan.chatav@gmail.com> dateSat 12 Aug 2017, 17:51 (9 years ago) parent52c66ee
4 files changed+51−75
squeel-postgresql/exe/Example.hs+26−29 squeel-postgresql/src/Squeel/PostgreSQL/Binary2.hs+12−31 squeel-postgresql/src/Squeel/PostgreSQL/Schema.hs+4 squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+9−15
squeel-postgresql/exe/Example.hs+26−29
@@ -30,32 +30,29 @@ main = do
3030 "host=localhost port=5432 dbname=exampledb"
3131 Char8.putStrLn $ "connecting to " <> connectionString
3232 connection0 <- connectdb connectionString
33− Char8.putStrLn "setting up database"
33+ Char8.putStrLn "setting up schema"
3434 connection1 <- runPQ (connection0 :: Connection '[]) $ pqExec $
35− createTable #students
36− ( (text & notNull) `As` #name
37− :* Nil ) []
35+ createTable #students ((text & notNull) `As` #name :* Nil ) []
3836 >>>
3937 createTable #table1
40− ( (int4 & notNull) `As` #col1
41− :* (int4 & notNull) `As` #col2
42− :* Nil ) []
43− Char8.putStrLn "querying"
44− connection2 <- runPQ (connection1 :: Connection Tables) $ do
38+ ((int4 & notNull) `As` #col1 :* (int4 & notNull) `As` #col2 :* Nil) []
39+ connection2 <- runPQ (connection1 :: Connection Schema) $ do
4540 let
46− statement :: Manipulation Tables
41+ manipulation :: Manipulation Schema
4742 '[ 'Required ('NotNull 'PGInt4)
4843 , 'Required ('NotNull 'PGInt4)
4944 , 'Required ('NotNull 'PGInt4)
5045 , 'Required ('NotNull 'PGInt4)
5146 ] '[]
52− statement =
47+ manipulation =
5348 insertInto #table1
54− (Values
55− (param1 `As` #col1 :* param2 `As` #col2 :* Nil)
56− [param3 `As` #col1 :* param4 `As` #col2 :* Nil] )
57− Conflict ReturningNil
58− _ <- pqExecParams statement (1::Int32,2::Int32,3::Int32,4::Int32)
49+ ( Values
50+ (param (Proxy @1) `As` #col1 :* param (Proxy @2) `As` #col2 :* Nil)
51+ [param (Proxy @3) `As` #col1 :* param (Proxy @4) `As` #col2 :* Nil]
52+ ) Conflict ReturningNil
53+ liftBase $ Char8.putStrLn "manipulating"
54+ _ <- pqExecParams manipulation (1::Int32,2::Int32,3::Int32,4::Int32)
55+ liftBase $ Char8.putStrLn "querying"
5956 Just result <- pqExecNil . query $
6057 selectStar (from (Table (#table1 `As` #table1)))
6158 value00 <- getValue (RowNumber 0) (columnNumber @0) result
@@ -69,21 +66,21 @@ main = do
6966 print (value01 :: Int32)
7067 print (value10 :: Int32)
7168 print (value11 :: Int32)
72− print (row0 :: ColumnsRecord)
73− print (row1 :: ColumnsRecord)
74− Char8.putStrLn "tearing down database"
75− connection3 <- runPQ (connection2 :: Connection Tables) $ pqExec $
69+ print (row0 :: Table1Row)
70+ print (row1 :: Table1Row)
71+ Char8.putStrLn "tearing down schema"
72+ connection3 <- runPQ (connection2 :: Connection Schema) $ pqExec $
7673 dropTable #table1 >>> dropTable #students
7774 finish (connection3 :: Connection '[])
7875
79−type Columns =
80− '[ "col1" ::: 'Required ('NotNull 'PGInt4)
81− , "col2" ::: 'Required ('NotNull 'PGInt4)
82− ]
83−data ColumnsRecord = ColumnsRecord { col1 :: Int32, col2 :: Int32 }
76+data Table1Row = Table1Row { col1 :: Int32, col2 :: Int32 }
8477 deriving (Show, GHC.Generic)
85−instance Generic ColumnsRecord
86−instance FromRow Columns ColumnsRecord
78+instance Generic Table1Row
8779
88−type StudentsColumns = '["name" ::: 'Required ('NotNull 'PGText)]
89−type Tables = '["students" ::: StudentsColumns, "table1" ::: Columns]
80+type Schema =
81+ '[ "students" ::: '["name" ::: 'Required ('NotNull 'PGText)]
82+ , "table1" :::
83+ '[ "col1" ::: 'Required ('NotNull 'PGInt4)
84+ , "col2" ::: 'Required ('NotNull 'PGInt4)
85+ ]
86+ ]
squeel-postgresql/src/Squeel/PostgreSQL/Binary2.hs+12−31
@@ -10,6 +10,7 @@
1010 , ScopedTypeVariables
1111 , TypeApplications
1212 , TypeOperators
13+ , UndecidableInstances
1314#-}
1415
1516module Squeel.PostgreSQL.Binary2 where
@@ -76,32 +77,13 @@ instance ToParam x pg => ToColumnParam (Maybe x) ('Optional (nullity pg)) where
7677
7778class SListI tys => ToParams x (tys :: [ColumnType]) where
7879 toParams :: x -> NP (K (Maybe Strict.ByteString)) tys
79− default toParams
80− :: (IsProductType x xs, AllZip ToColumnParam xs tys)
81− => x -> NP (K (Maybe Strict.ByteString)) tys
82− toParams
83− = htrans (Proxy @ToColumnParam) (toColumnParam . unI)
84− . unZ . unSOP . from
85−instance ToParams () '[]
8680instance ToColumnParam x ty => ToParams x '[ty] where
8781 toParams x = toColumnParam x :* Nil
88−instance (ToColumnParam x1 ty1, ToColumnParam x2 ty2)
89− => ToParams (x1,x2) '[ty1,ty2]
90−instance (ToColumnParam x1 ty1, ToColumnParam x2 ty2, ToColumnParam x3 ty3)
91− => ToParams (x1,x2,x3) '[ty1,ty2,ty3]
92−instance
93− ( ToColumnParam x1 ty1
94− , ToColumnParam x2 ty2
95− , ToColumnParam x3 ty3
96− , ToColumnParam x4 ty4
97− ) => ToParams (x1,x2,x3,x4) '[ty1,ty2,ty3,ty4]
98−instance
99− ( ToColumnParam x1 ty1
100− , ToColumnParam x2 ty2
101− , ToColumnParam x3 ty3
102− , ToColumnParam x4 ty4
103− , ToColumnParam x5 ty5
104− ) => ToParams (x1,x2,x3,x4,x5) '[ty1,ty2,ty3,ty4,ty5]
82+instance (SListI tys, IsProductType x xs, AllZip ToColumnParam xs tys)
83+ => ToParams x tys where
84+ toParams
85+ = htrans (Proxy @ToColumnParam) (toColumnParam . unI)
86+ . unZ . unSOP . from
10587
10688class FromValue (pg :: PGType) y where
10789 fromValue :: proxy pg -> Decoding.Value y
@@ -161,12 +143,11 @@ instance FromValue pg y
161143
162144class SListI columns => FromRow (columns :: [(Symbol,ColumnType)]) y where
163145 fromRow :: NP (K (Maybe Strict.ByteString)) columns -> y
164− default fromRow
165− :: (IsProductType y ys, AllZip FromColumnValue columns ys)
166− => NP (K (Maybe Strict.ByteString)) columns -> y
167− fromRow
168− = to . SOP . Z
169− . htrans (Proxy @FromColumnValue) (I . fromColumnValue)
170−instance FromRow '[] ()
171146instance FromColumnValue ty y => FromRow '[ty] y where
172147 fromRow (y :* Nil) = fromColumnValue y
148+instance
149+ ( SListI columns, IsProductType y ys
150+ , AllZip FromColumnValue columns ys
151+ ) => FromRow columns y where
152+ fromRow
153+ = to . SOP . Z . htrans (Proxy @FromColumnValue) (I . fromColumnValue)
squeel-postgresql/src/Squeel/PostgreSQL/Schema.hs+4
@@ -30,6 +30,7 @@ module Squeel.PostgreSQL.Schema
3030 , NullifyColumns
3131 , NullifyTables
3232 , ColumnType (..)
33+ , BaseType
3334 , Create
3435 , Drop
3536 , Alter
@@ -134,6 +135,9 @@ data NullityType = Null PGType | NotNull PGType
134135
135136data ColumnType = Optional NullityType | Required NullityType
136137
138+type family BaseType (ty :: ColumnType) where
139+ BaseType (optionality (nullity pg)) = pg
140+
137141type family NullifyType (ty :: ColumnType) where
138142 NullifyType (optionality ('Null ty)) = optionality ('Null ty)
139143 NullifyType (optionality ('NotNull ty)) = optionality ('Null ty)
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+9−15
@@ -49,24 +49,18 @@ newtype Expression
4949 = UnsafeExpression { renderExpression :: ByteString }
5050 deriving (Show,Eq)
5151
52−class KnownNat n => HasParameter (n :: Nat) params ty | n params -> ty where
53− param :: Proxy# n -> Expression params tables grouping ty
54− param p = UnsafeExpression $ ("$" <>) $ fromString $ show $ natVal' p
55−instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
52+class (PGTyped (BaseType ty), KnownNat n)
53+ => HasParameter (n :: Nat) params ty | n params -> ty where
54+ param :: proxy n -> Expression params tables grouping ty
55+ param _ = UnsafeExpression $ parenthesized $
56+ "$" <> paramNum <+> "::" <+> renderTypeExpression (pgtype @(BaseType ty))
57+ where
58+ paramNum = fromString $ show $ natVal (Proxy @n)
59+instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
60+ => HasParameter 1 (ty1:tys) ty1
5661instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
5762 => HasParameter n (ty' : params) ty
5863
59−param1 :: Expression (ty1:tys) tables grouping ty1
60−param1 = param (proxy# :: Proxy# 1)
61−param2 :: Expression (ty1:ty2:tys) tables grouping ty2
62−param2 = param (proxy# :: Proxy# 2)
63−param3 :: Expression (ty1:ty2:ty3:tys) tables grouping ty3
64−param3 = param (proxy# :: Proxy# 3)
65−param4 :: Expression (ty1:ty2:ty3:ty4:tys) tables grouping ty4
66−param4 = param (proxy# :: Proxy# 4)
67−param5 :: Expression (ty1:ty2:ty3:ty4:ty5:tys) tables grouping ty5
68−param5 = param (proxy# :: Proxy# 5)
69−
7064class KnownSymbol column => HasColumn column columns ty
7165 | column columns -> ty where
7266 getColumn