@@ -49,24 +49,17 @@ 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+ "$" <> renderNat (Proxy @n) <+> "::"
57+ <+> renderTypeExpression (pgtype @(BaseType ty))
58+instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
59+ => HasParameter 1 (ty1:tys) ty1
5660instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
5761 => HasParameter n (ty' : params) ty
5862
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−
7063class KnownSymbol column => HasColumn column columns ty
7164 | column columns -> ty where
7265 getColumn
@@ -146,12 +139,12 @@ instance PGNum ty
146139 . fromString
147140 . show
148141
149−instance PGFractional ty => Fractional
142+instance PGFloating ty => Fractional
150143 (Expression params tables grouping ('Required (nullity ty))) where
151144 (/) = unsafeBinaryOp "/"
152145 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
153146
154−instance (PGFloating ty, PGCast 'PGNumeric ty, PGTyped ty) => Floating
147+instance (PGFloating ty, PGTyped ty) => Floating
155148 (Expression params tables grouping ('Required (nullity ty))) where
156149 pi = UnsafeExpression "pi()"
157150 exp = unsafeFunction "exp"
@@ -164,12 +157,12 @@ instance (PGFloating ty, PGCast 'PGNumeric ty, PGTyped ty) => Floating
164157 logBaseNumeric
165158 :: Expression params tables grouping ('Required (nullity ty))
166159 -> Expression params tables grouping ('Required (nullity ty))
167− -> Expression params tables grouping ('Required (nullity 'PGNumeric))
160+ -> Expression params tables grouping ('Required (nullity 'PGnumeric))
168161 logBaseNumeric b' y' = UnsafeExpression $ mconcat
169162 [ "log("
170− , renderExpression b' <> "::numeric"
163+ , renderExpression (cast numeric b')
171164 , ", "
172− , renderExpression y' <> "::numeric"
165+ , renderExpression (cast numeric y')
173166 , ")"
174167 ]
175168 sin = unsafeFunction "sin"
@@ -193,75 +186,62 @@ atan2
193186atan2 y x = UnsafeExpression $
194187 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
195188
196−class PGCast (ty0 :: PGType) (ty1 :: PGType) where
197− cast
198− :: TypeExpression ('Required ('Null ty1))
199− -> Expression params tables grouping ('Required (nullity ty0))
200− -> Expression params tables grouping ('Required (nullity ty1))
201− cast ty x = UnsafeExpression $
202− "(" <> renderExpression x <> "::" <> renderTypeExpression ty <> ")"
203−instance PGCast 'PGInt2 'PGInt2
204−instance PGCast 'PGInt2 'PGInt4
205−instance PGCast 'PGInt2 'PGInt8
206−instance PGCast 'PGInt4 'PGInt2
207−instance PGCast 'PGInt4 'PGInt4
208−instance PGCast 'PGInt4 'PGInt8
209−instance PGCast 'PGInt8 'PGInt2
210−instance PGCast 'PGInt8 'PGInt4
211−instance PGCast 'PGInt8 'PGInt8
212−instance PGCast 'PGInt8 'PGFloat4
213−instance PGCast 'PGInt8 'PGFloat8
214−instance PGCast 'PGInt8 'PGNumeric
189+cast
190+ :: TypeExpression ('Required ('Null ty1))
191+ -> Expression params tables grouping ('Required (nullity ty0))
192+ -> Expression params tables grouping ('Required (nullity ty1))
193+cast ty x = UnsafeExpression $ parenthesized $
194+ renderExpression x <+> "::" <+> renderTypeExpression ty
215195
216196div
217− :: (PGNum ty, PGTyped ty, PGCast 'PGInt8 ty, PGCast ty 'PGInt8)
197+ :: PGIntegral ty
218198 => Expression params tables grouping ('Required (nullity ty))
219199 -> Expression params tables grouping ('Required (nullity ty))
220200 -> Expression params tables grouping ('Required (nullity ty))
221201div = unsafeBinaryOp "/"
222202
223203mod
224− :: (PGNum ty, PGTyped ty, PGCast 'PGInt8 ty, PGCast ty 'PGInt8)
204+ :: PGIntegral ty
225205 => Expression params tables grouping ('Required (nullity ty))
226206 -> Expression params tables grouping ('Required (nullity ty))
227207 -> Expression params tables grouping ('Required (nullity ty))
228208mod = unsafeBinaryOp "%"
229209
230210truncate
231− :: (PGFractional frac, PGCast 'PGInt4 int, PGTyped int)
211+ :: (PGFloating frac, PGTyped int)
232212 => Expression params tables grouping ('Required (nullity frac))
233213 -> Expression params tables grouping ('Required (nullity int))
234214truncate = cast pgtype . truncate'
235215 where
236216 truncate'
237217 :: Expression params tables grouping ('Required (nullity frac))
238− -> Expression params tables grouping ('Required (nullity 'PGInt4))
218+ -> Expression params tables grouping ('Required (nullity frac))
239219 truncate' = unsafeFunction "trunc"
240220
241221round
242− :: (PGFractional frac, PGCast 'PGInt4 int, PGTyped int)
222+ :: (PGFloating frac, PGTyped int)
243223 => Expression params tables grouping ('Required (nullity frac))
244224 -> Expression params tables grouping ('Required (nullity int))
245225round = cast pgtype . round'
246226 where
247227 round'
248228 :: Expression params tables grouping ('Required (nullity frac))
249− -> Expression params tables grouping ('Required (nullity 'PGInt4))
229+ -> Expression params tables grouping ('Required (nullity frac))
250230 round' = unsafeFunction "round"
251231
252232ceiling
253− :: (PGFractional frac, PGCast 'PGInt4 int, PGTyped int)
233+ :: (PGFloating frac, PGTyped int)
254234 => Expression params tables grouping ('Required (nullity frac))
255235 -> Expression params tables grouping ('Required (nullity int))
256236ceiling = cast pgtype . ceiling'
257237 where
258238 ceiling'
259239 :: Expression params tables grouping ('Required (nullity frac))
260− -> Expression params tables grouping ('Required (nullity 'PGInt4))
240+ -> Expression params tables grouping ('Required (nullity frac))
261241 ceiling' = unsafeFunction "ceiling"
262242
263243instance IsString
264− (Expression params tables grouping ('Required (nullity 'PGText))) where
244+ (Expression params tables grouping ('Required (nullity 'PGtext))) where
265245 fromString str = UnsafeExpression $
266246 "E\'" <> fromString (escape =<< str) <> "\'"
267247 where
@@ -277,7 +257,7 @@ instance IsString
277257 c -> [c]
278258
279259type Condition params tables grouping =
280− Expression params tables grouping ('Required ('NotNull 'PGBool))
260+ Expression params tables grouping ('Required ('NotNull 'PGbool))
281261
282262true :: Condition params tables grouping
283263true = UnsafeExpression "TRUE"
@@ -329,37 +309,37 @@ ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
329309(==*)
330310 :: Expression params tables grouping ('Required (nullity ty))
331311 -> Expression params tables grouping ('Required (nullity ty))
332− -> Expression params tables grouping ('Required (nullity 'PGBool))
312+ -> Expression params tables grouping ('Required (nullity 'PGbool))
333313(==*) = unsafeBinaryOp "="
334314
335315(/=*)
336316 :: Expression params tables grouping ('Required (nullity ty))
337317 -> Expression params tables grouping ('Required (nullity ty))
338− -> Expression params tables grouping ('Required (nullity 'PGBool))
318+ -> Expression params tables grouping ('Required (nullity 'PGbool))
339319(/=*) = unsafeBinaryOp "<>"
340320
341321(>=*)
342322 :: Expression params tables grouping ('Required (nullity ty))
343323 -> Expression params tables grouping ('Required (nullity ty))
344− -> Expression params tables grouping ('Required (nullity 'PGBool))
324+ -> Expression params tables grouping ('Required (nullity 'PGbool))
345325(>=*) = unsafeBinaryOp ">="
346326
347327(<*)
348328 :: Expression params tables grouping ('Required (nullity ty))
349329 -> Expression params tables grouping ('Required (nullity ty))
350− -> Expression params tables grouping ('Required (nullity 'PGBool))
330+ -> Expression params tables grouping ('Required (nullity 'PGbool))
351331(<*) = unsafeBinaryOp "<"
352332
353333(<=*)
354334 :: Expression params tables grouping ('Required (nullity ty))
355335 -> Expression params tables grouping ('Required (nullity ty))
356− -> Expression params tables grouping ('Required (nullity 'PGBool))
336+ -> Expression params tables grouping ('Required (nullity 'PGbool))
357337(<=*) = unsafeBinaryOp "<="
358338
359339(>*)
360340 :: Expression params tables grouping ('Required (nullity ty))
361341 -> Expression params tables grouping ('Required (nullity ty))
362− -> Expression params tables grouping ('Required (nullity 'PGBool))
342+ -> Expression params tables grouping ('Required (nullity 'PGbool))
363343(>*) = unsafeBinaryOp ">"
364344
365345data SortExpression params tables grouping where
@@ -428,7 +408,7 @@ class (KnownSymbol table, KnownSymbol column)
428408 :: (tables ~ '[table ::: columns], HasColumn column columns ty)
429409 => Proxy# column
430410 -> Expression params tables ('Grouped bys) ty
431− getGroup1 column = UnsafeExpression $ fromString (symbolVal' column)
411+ getGroup1 column = UnsafeExpression $ renderSymbol column
432412 getGroup2
433413 :: (HasTable table tables columns, HasColumn column columns ty)
434414 => Alias table
@@ -484,7 +464,7 @@ newtype Table
484464class KnownSymbol table => HasTable table tables columns
485465 | table tables -> columns where
486466 getTable :: Proxy# table -> Table tables columns
487− getTable table = UnsafeTable $ fromString $ symbolVal' table
467+ getTable table = UnsafeTable $ renderSymbol table
488468instance {-# OVERLAPPING #-} KnownSymbol table
489469 => HasTable table ((table ::: columns) ': tables) columns
490470instance {-# OVERLAPPABLE #-}
@@ -646,91 +626,86 @@ newtype TypeExpression (ty :: ColumnType)
646626 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
647627 deriving (Show,Eq)
648628
649−bool :: TypeExpression ('Required ('Null 'PGBool))
629+bool :: TypeExpression ('Required ('Null 'PGbool))
650630bool = UnsafeTypeExpression "bool"
651−int2 :: TypeExpression ('Required ('Null 'PGInt2))
631+int2 :: TypeExpression ('Required ('Null 'PGint2))
652632int2 = UnsafeTypeExpression "int2"
653−smallint :: TypeExpression ('Required ('Null 'PGInt2))
633+smallint :: TypeExpression ('Required ('Null 'PGint2))
654634smallint = UnsafeTypeExpression "smallint"
655−int4 :: TypeExpression ('Required ('Null 'PGInt4))
635+int4 :: TypeExpression ('Required ('Null 'PGint4))
656636int4 = UnsafeTypeExpression "int4"
657−int :: TypeExpression ('Required ('Null 'PGInt4))
637+int :: TypeExpression ('Required ('Null 'PGint4))
658638int = UnsafeTypeExpression "int"
659−integer :: TypeExpression ('Required ('Null 'PGInt4))
639+integer :: TypeExpression ('Required ('Null 'PGint4))
660640integer = UnsafeTypeExpression "integer"
661−int8 :: TypeExpression ('Required ('Null 'PGInt8))
641+int8 :: TypeExpression ('Required ('Null 'PGint8))
662642int8 = UnsafeTypeExpression "int8"
663−bigint :: TypeExpression ('Required ('Null 'PGInt8))
643+bigint :: TypeExpression ('Required ('Null 'PGint8))
664644bigint = UnsafeTypeExpression "bigint"
665−numeric :: TypeExpression ('Required ('Null 'PGNumeric))
645+numeric :: TypeExpression ('Required ('Null 'PGnumeric))
666646numeric = UnsafeTypeExpression "numeric"
667−float4 :: TypeExpression ('Required ('Null 'PGFloat4))
647+float4 :: TypeExpression ('Required ('Null 'PGfloat4))
668648float4 = UnsafeTypeExpression "float4"
669−real :: TypeExpression ('Required ('Null 'PGFloat4))
649+real :: TypeExpression ('Required ('Null 'PGfloat4))
670650real = UnsafeTypeExpression "real"
671−float8 :: TypeExpression ('Required ('Null 'PGFloat8))
651+float8 :: TypeExpression ('Required ('Null 'PGfloat8))
672652float8 = UnsafeTypeExpression "float8"
673−doublePrecision :: TypeExpression ('Required ('Null 'PGFloat8))
653+doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
674654doublePrecision = UnsafeTypeExpression "double precision"
675−serial2 :: TypeExpression ('Optional ('NotNull 'PGInt2))
655+serial2 :: TypeExpression ('Optional ('NotNull 'PGint2))
676656serial2 = UnsafeTypeExpression "serial2"
677−smallserial :: TypeExpression ('Optional ('NotNull 'PGInt2))
657+smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
678658smallserial = UnsafeTypeExpression "smallserial"
679−serial4 :: TypeExpression ('Optional ('NotNull 'PGInt4))
659+serial4 :: TypeExpression ('Optional ('NotNull 'PGint4))
680660serial4 = UnsafeTypeExpression "serial4"
681−serial :: TypeExpression ('Optional ('NotNull 'PGInt4))
661+serial :: TypeExpression ('Optional ('NotNull 'PGint4))
682662serial = UnsafeTypeExpression "serial"
683−serial8 :: TypeExpression ('Optional ('NotNull 'PGInt8))
663+serial8 :: TypeExpression ('Optional ('NotNull 'PGint8))
684664serial8 = UnsafeTypeExpression "serial8"
685−bigserial :: TypeExpression ('Optional ('NotNull 'PGInt8))
665+bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
686666bigserial = UnsafeTypeExpression "bigserial"
687−money :: TypeExpression ('Required ('Null 'PGMoney))
688−money = UnsafeTypeExpression "money"
689−text :: TypeExpression ('Required ('Null 'PGText))
667+text :: TypeExpression ('Required ('Null 'PGtext))
690668text = UnsafeTypeExpression "text"
691669char
692670 :: KnownNat n
693671 => proxy n
694− -> TypeExpression ('Required ('Null ('PGChar n)))
695−char (_ :: proxy n) = UnsafeTypeExpression $
696− "char(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
672+ -> TypeExpression ('Required ('Null ('PGchar n)))
673+char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
697674character
698675 :: KnownNat n
699676 => proxy n
700− -> TypeExpression ('Required ('Null ('PGChar n)))
701−character (_ :: proxy n) = UnsafeTypeExpression $
702− "character(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
677+ -> TypeExpression ('Required ('Null ('PGchar n)))
678+character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
703679varchar
704680 :: KnownNat n
705681 => proxy n
706− -> TypeExpression ('Required ('Null ('PGVarChar n)))
707−varchar (_ :: proxy n) = UnsafeTypeExpression $
708− "varchar(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
682+ -> TypeExpression ('Required ('Null ('PGvarchar n)))
683+varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
709684characterVarying
710685 :: KnownNat n
711686 => proxy n
712− -> TypeExpression ('Required ('Null ('PGVarChar n)))
713−characterVarying (_ :: proxy n) = UnsafeTypeExpression $
714− "character varying(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
715−bytea :: TypeExpression ('Required ('Null ('PGBytea)))
687+ -> TypeExpression ('Required ('Null ('PGvarchar n)))
688+characterVarying p = UnsafeTypeExpression $
689+ "character varying(" <> renderNat p <> ")"
690+bytea :: TypeExpression ('Required ('Null ('PGbytea)))
716691bytea = UnsafeTypeExpression "bytea"
717−timestamp :: TypeExpression ('Required ('Null ('PGTimestamp)))
692+timestamp :: TypeExpression ('Required ('Null ('PGtimestamp)))
718693timestamp = UnsafeTypeExpression "timestamp"
719−timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGTimestampTZ)))
694+timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGtimestamptz)))
720695timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
721−date :: TypeExpression ('Required ('Null ('PGDate)))
696+date :: TypeExpression ('Required ('Null ('PGdate)))
722697date = UnsafeTypeExpression "date"
723−time :: TypeExpression ('Required ('Null ('PGTime)))
698+time :: TypeExpression ('Required ('Null ('PGtime)))
724699time = UnsafeTypeExpression "time"
725−timeWithTimeZone :: TypeExpression ('Required ('Null ('PGTimeTZ)))
700+timeWithTimeZone :: TypeExpression ('Required ('Null ('PGtimetz)))
726701timeWithTimeZone = UnsafeTypeExpression "time with time zone"
727−interval :: TypeExpression ('Required ('Null ('PGInterval)))
702+interval :: TypeExpression ('Required ('Null ('PGinterval)))
728703interval = UnsafeTypeExpression "interval"
729−uuid :: TypeExpression ('Required ('Null ('PGUuid)))
704+uuid :: TypeExpression ('Required ('Null ('PGuuid)))
730705uuid = UnsafeTypeExpression "uuid"
731−json :: TypeExpression ('Required ('Null ('PGJson)))
706+json :: TypeExpression ('Required ('Null ('PGjson)))
732707json = UnsafeTypeExpression "json"
733−jsonb :: TypeExpression ('Required ('Null ('PGJsonb)))
708+jsonb :: TypeExpression ('Required ('Null ('PGjsonb)))
734709jsonb = UnsafeTypeExpression "jsonb"
735710
736711notNull
@@ -746,27 +721,26 @@ default_ x ty = UnsafeTypeExpression $
746721
747722class PGTyped (ty :: PGType) where
748723 pgtype :: TypeExpression ('Required ('Null ty))
749−instance PGTyped 'PGBool where pgtype = bool
750−instance PGTyped 'PGInt2 where pgtype = int2
751−instance PGTyped 'PGInt4 where pgtype = int4
752−instance PGTyped 'PGInt8 where pgtype = int8
753−instance PGTyped 'PGNumeric where pgtype = numeric
754−instance PGTyped 'PGFloat4 where pgtype = float4
755−instance PGTyped 'PGFloat8 where pgtype = float8
756−instance PGTyped 'PGMoney where pgtype = money
757−instance PGTyped 'PGText where pgtype = text
758−instance KnownNat n => PGTyped ('PGChar n) where pgtype = char (Proxy @n)
759−instance KnownNat n => PGTyped ('PGVarChar n) where pgtype = varchar (Proxy @n)
760−instance PGTyped 'PGBytea where pgtype = bytea
761−instance PGTyped 'PGTimestamp where pgtype = timestamp
762−instance PGTyped 'PGTimestampTZ where pgtype = timestampWithTimeZone
763−instance PGTyped 'PGDate where pgtype = date
764−instance PGTyped 'PGTime where pgtype = time
765−instance PGTyped 'PGTimeTZ where pgtype = timeWithTimeZone
766−instance PGTyped 'PGInterval where pgtype = interval
767−instance PGTyped 'PGUuid where pgtype = uuid
768−instance PGTyped 'PGJson where pgtype = json
769−instance PGTyped 'PGJsonb where pgtype = jsonb
724+instance PGTyped 'PGbool where pgtype = bool
725+instance PGTyped 'PGint2 where pgtype = int2
726+instance PGTyped 'PGint4 where pgtype = int4
727+instance PGTyped 'PGint8 where pgtype = int8
728+instance PGTyped 'PGnumeric where pgtype = numeric
729+instance PGTyped 'PGfloat4 where pgtype = float4
730+instance PGTyped 'PGfloat8 where pgtype = float8
731+instance PGTyped 'PGtext where pgtype = text
732+instance KnownNat n => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
733+instance KnownNat n => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
734+instance PGTyped 'PGbytea where pgtype = bytea
735+instance PGTyped 'PGtimestamp where pgtype = timestamp
736+instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
737+instance PGTyped 'PGdate where pgtype = date
738+instance PGTyped 'PGtime where pgtype = time
739+instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
740+instance PGTyped 'PGinterval where pgtype = interval
741+instance PGTyped 'PGuuid where pgtype = uuid
742+instance PGTyped 'PGjson where pgtype = json
743+instance PGTyped 'PGjsonb where pgtype = jsonb
770744
771745{-----------------------------------------
772746statements
@@ -1049,8 +1023,7 @@ dropNotNull
10491023dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
10501024
10511025alterType
1052− :: PGCast ty0 ty1
1053− => TypeExpression (optionality (nullity ty1))
1026+ :: TypeExpression (optionality (nullity ty1))
10541027 -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
10551028alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
10561029
@@ -1216,3 +1189,6 @@ renderCommaSeparatedMaybe render
12161189
12171190renderSymbol :: KnownSymbol str => Proxy# str -> ByteString
12181191renderSymbol p = fromString $ symbolVal' p
1192+
1193+renderNat :: KnownNat n => proxy n -> ByteString
1194+renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))