@@ -1,5 +1,6 @@
1{-# LANGUAGE
1{-# LANGUAGE
3 , FlexibleInstances
4 , FlexibleInstances
5 , MagicHash
6 , MagicHash
@@ -19,6 +20,8 @@ module Squeel.PostgreSQL.Schema
20 , Alias (Alias)
21 , Alias (Alias)
21 , Aliased (As)
22 , Aliased (As)
22 , renderAliased
25 , renderAliased
23 , module GHC.OverloadedLabels
26 , module GHC.OverloadedLabels
24 , module GHC.TypeLits
27 , module GHC.TypeLits
@@ -97,3 +100,58 @@ renderAliased
97 -> ByteString
100 -> ByteString
98renderAliased render (expression `As` Alias alias) =
101renderAliased render (expression `As` Alias alias) =
99 render expression <> " AS " <> fromString (symbolVal' alias)
102 render expression <> " AS " <> fromString (symbolVal' alias)
104+class KnownPGType (ty :: PGType) where
105+ renderPGType :: Proxy# ty -> ByteString
106+instance KnownPGType 'PGBool where renderPGType _ = "bool"
107+instance KnownPGType 'PGInt2 where renderPGType _ = "int2"
108+instance KnownPGType 'PGInt4 where renderPGType _ = "int4"
109+instance KnownPGType 'PGInt8 where renderPGType _ = "int8"
110+instance KnownPGType 'PGNumeric where renderPGType _ = "numeric"
111+instance KnownPGType 'PGFloat4 where renderPGType _ = "float4"
112+instance KnownPGType 'PGFloat8 where renderPGType _ = "float8"
113+instance KnownPGType 'PGSerial2 where renderPGType _ = "serial2"
114+instance KnownPGType 'PGSerial4 where renderPGType _ = "serial4"
115+instance KnownPGType 'PGSerial8 where renderPGType _ = "serial8"
116+instance KnownPGType 'PGMoney where renderPGType _ = "money"
117+instance KnownNat n => KnownPGType ('PGChar n) where
118+ renderPGType _ = "char("
119+ <> fromString (show (natVal' (proxy# :: Proxy# n)))
121+instance KnownNat n => KnownPGType ('PGVarChar n) where
122+ renderPGType _ = "varchar("
123+ <> fromString (show (natVal' (proxy# :: Proxy# n)))
125+instance KnownPGType 'PGText where renderPGType _ = "text"
126+instance KnownPGType 'PGBytea where renderPGType _ = "bytea"
127+instance KnownPGType 'PGTimestamp where renderPGType _ = "timestamp"
128+instance KnownPGType 'PGTimestampTZ where renderPGType _ = "timestampTZ"
129+instance KnownPGType 'PGDate where renderPGType _ = "date"
130+instance KnownPGType 'PGTime where renderPGType _ = "time"
131+instance KnownPGType 'PGTimeTZ where renderPGType _ = "timeTZ"
132+instance KnownPGType 'PGInterval where renderPGType _ = "interval"
133+instance KnownPGType 'PGUuid where renderPGType _ = "uuid"
134+instance KnownPGType 'PGJson where renderPGType _ = "json"
135+instance KnownPGType 'PGJsonb where renderPGType _ = "jsonb"
137+class KnownColumns (columns :: [(Symbol,PGType)]) where
138+ renderColumns :: Proxy# columns -> ByteString
140+ ( KnownSymbol column
142+ ) => KnownColumns '[column ::: ty] where
144+ fromString (symbolVal' (proxy# :: Proxy# column))
146+ <> renderPGType (proxy# :: Proxy# ty)
148+ ( KnownSymbol column
150+ , KnownColumns (columnsHead ': columnsTail)
151+ ) => KnownColumns ((column ::: ty) ': columnsHead ': columnsTail) where
153+ fromString (symbolVal' (proxy# :: Proxy# column))
155+ <> renderPGType (proxy# :: Proxy# ty)
157+ <> renderColumns (proxy# :: Proxy# (columnsHead ': columnsTail))