@@ -1,5 +1,6 @@
11{-# LANGUAGE
22 DataKinds
3+ , FlexibleContexts
34 , FlexibleInstances
45 , GADTs
56 , MagicHash
@@ -19,6 +20,8 @@ module Squeel.PostgreSQL.Schema
1920 , (:::)
2021 , Alias (Alias)
2122 , Aliased (As)
23+ , KnownPGType (..)
24+ , KnownColumns (..)
2225 , renderAliased
2326 , module GHC.OverloadedLabels
2427 , module GHC.TypeLits
@@ -97,3 +100,58 @@ renderAliased
97100 -> ByteString
98101renderAliased render (expression `As` Alias alias) =
99102 render expression <> " AS " <> fromString (symbolVal' alias)
103+
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)))
120+ <> ")"
121+instance KnownNat n => KnownPGType ('PGVarChar n) where
122+ renderPGType _ = "varchar("
123+ <> fromString (show (natVal' (proxy# :: Proxy# n)))
124+ <> ")"
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"
136+
137+class KnownColumns (columns :: [(Symbol,PGType)]) where
138+ renderColumns :: Proxy# columns -> ByteString
139+instance
140+ ( KnownSymbol column
141+ , KnownPGType ty
142+ ) => KnownColumns '[column ::: ty] where
143+ renderColumns _ =
144+ fromString (symbolVal' (proxy# :: Proxy# column))
145+ <> " "
146+ <> renderPGType (proxy# :: Proxy# ty)
147+instance
148+ ( KnownSymbol column
149+ , KnownPGType ty
150+ , KnownColumns (columnsHead ': columnsTail)
151+ ) => KnownColumns ((column ::: ty) ': columnsHead ': columnsTail) where
152+ renderColumns _ =
153+ fromString (symbolVal' (proxy# :: Proxy# column))
154+ <> " "
155+ <> renderPGType (proxy# :: Proxy# ty)
156+ <> ", "
157+ <> renderColumns (proxy# :: Proxy# (columnsHead ': columnsTail))