@@ -8,6 +8,7 @@
88 , MagicHash
99 , OverloadedStrings
1010 , ScopedTypeVariables
11+ , TypeApplications
1112 , TypeFamilies
1213 , TypeOperators
1314 , UndecidableInstances
@@ -15,7 +16,6 @@
1516
1617module Squeel.PostgreSQL.Statement where
1718
18−import Control.Category
1919import Data.Boolean
2020import Data.Boolean.Numbers
2121import Data.ByteString (ByteString)
@@ -27,7 +27,7 @@ import Generics.SOP
2727import GHC.Exts
2828import GHC.OverloadedLabels
2929import GHC.TypeLits
30−import Prelude hiding (id,(.),RealFrac(..))
30+import Prelude hiding (RealFrac(..))
3131
3232import qualified Data.ByteString as ByteString
3333
@@ -134,7 +134,11 @@ instance PGNum ty
134134 (*) = unsafeBinaryOp "*"
135135 abs = unsafeFunction "abs"
136136 signum = unsafeFunction "sign"
137− fromInteger = UnsafeExpression . (<> ".") . fromString . show
137+ fromInteger
138+ = UnsafeExpression
139+ . (<> if decimal (Proxy :: Proxy ty) then "." else "")
140+ . fromString
141+ . show
138142
139143instance PGFractional ty
140144 => Fractional (Expression params tables ('Required (nullity ty))) where
@@ -334,11 +338,6 @@ newtype Statement
334338 = UnsafeStatement { renderStatement :: ByteString }
335339 deriving (Show,Eq)
336340
337−instance Category (Statement '[] '[]) where
338− id = UnsafeStatement ";"
339− statement2 . statement1 = UnsafeStatement $
340− renderStatement statement1 <> " " <> renderStatement statement2
341−
342341newtype PreparedStatement
343342 (params :: [ColumnType])
344343 (columns :: [(Symbol,ColumnType)])
@@ -615,53 +614,53 @@ serial8 :: TypeExpression ('Optional ('NotNull 'PGInt8))
615614serial8 = UnsafeTypeExpression "serial8"
616615bigserial :: TypeExpression ('Optional ('NotNull 'PGInt8))
617616bigserial = UnsafeTypeExpression "bigserial"
618−money :: TypeExpression ('Optional ('NotNull 'PGMoney))
617+money :: TypeExpression ('Required ('Null 'PGMoney))
619618money = UnsafeTypeExpression "money"
620619text :: TypeExpression ('Required ('Null 'PGText))
621620text = UnsafeTypeExpression "text"
622621char
623622 :: KnownNat n
624623 => proxy n
625− -> TypeExpression ('Optional ('NotNull ('PGChar n)))
624+ -> TypeExpression ('Required ('Null ('PGChar n)))
626625char (_ :: proxy n) = UnsafeTypeExpression $
627626 "char(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
628627character
629628 :: KnownNat n
630629 => proxy n
631− -> TypeExpression ('Optional ('NotNull ('PGChar n)))
630+ -> TypeExpression ('Required ('Null ('PGChar n)))
632631character (_ :: proxy n) = UnsafeTypeExpression $
633632 "character(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
634633varchar
635634 :: KnownNat n
636635 => proxy n
637− -> TypeExpression ('Optional ('NotNull ('PGVarChar n)))
636+ -> TypeExpression ('Required ('Null ('PGVarChar n)))
638637varchar (_ :: proxy n) = UnsafeTypeExpression $
639638 "varchar(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
640639characterVarying
641640 :: KnownNat n
642641 => proxy n
643− -> TypeExpression ('Optional ('NotNull ('PGVarChar n)))
642+ -> TypeExpression ('Required ('Null ('PGVarChar n)))
644643characterVarying (_ :: proxy n) = UnsafeTypeExpression $
645644 "character varying(" <> fromString (show (natVal' (proxy# :: Proxy# n))) <> ")"
646−bytea :: TypeExpression ('Optional ('NotNull ('PGBytea)))
645+bytea :: TypeExpression ('Required ('Null ('PGBytea)))
647646bytea = UnsafeTypeExpression "bytea"
648−timestamp :: TypeExpression ('Optional ('NotNull ('PGTimestamp)))
647+timestamp :: TypeExpression ('Required ('Null ('PGTimestamp)))
649648timestamp = UnsafeTypeExpression "timestamp"
650−timestampWithTimeZone :: TypeExpression ('Optional ('NotNull ('PGTimestampTZ)))
649+timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGTimestampTZ)))
651650timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
652−date :: TypeExpression ('Optional ('NotNull ('PGDate)))
651+date :: TypeExpression ('Required ('Null ('PGDate)))
653652date = UnsafeTypeExpression "date"
654−time :: TypeExpression ('Optional ('NotNull ('PGTime)))
653+time :: TypeExpression ('Required ('Null ('PGTime)))
655654time = UnsafeTypeExpression "time"
656−timeWithTimeZone :: TypeExpression ('Optional ('NotNull ('PGTimeTZ)))
655+timeWithTimeZone :: TypeExpression ('Required ('Null ('PGTimeTZ)))
657656timeWithTimeZone = UnsafeTypeExpression "time with time zone"
658−interval :: TypeExpression ('Optional ('NotNull ('PGInterval)))
657+interval :: TypeExpression ('Required ('Null ('PGInterval)))
659658interval = UnsafeTypeExpression "interval"
660−uuid :: TypeExpression ('Optional ('NotNull ('PGUuid)))
659+uuid :: TypeExpression ('Required ('Null ('PGUuid)))
661660uuid = UnsafeTypeExpression "uuid"
662−json :: TypeExpression ('Optional ('NotNull ('PGJson)))
661+json :: TypeExpression ('Required ('Null ('PGJson)))
663662json = UnsafeTypeExpression "json"
664−jsonb :: TypeExpression ('Optional ('NotNull ('PGJsonb)))
663+jsonb :: TypeExpression ('Required ('Null ('PGJsonb)))
665664jsonb = UnsafeTypeExpression "jsonb"
666665
667666notNull
@@ -677,11 +676,27 @@ default_ x ty = UnsafeTypeExpression $
677676
678677class PGTyped (ty :: PGType) where
679678 pgtype :: TypeExpression ('Required ('Null ty))
679+instance PGTyped 'PGBool where pgtype = bool
680680instance PGTyped 'PGInt2 where pgtype = int2
681681instance PGTyped 'PGInt4 where pgtype = int4
682682instance PGTyped 'PGInt8 where pgtype = int8
683−instance PGTyped 'PGBool where pgtype = bool
683+instance PGTyped 'PGNumeric where pgtype = numeric
684+instance PGTyped 'PGFloat4 where pgtype = float4
685+instance PGTyped 'PGFloat8 where pgtype = float8
686+instance PGTyped 'PGMoney where pgtype = money
684687instance PGTyped 'PGText where pgtype = text
688+instance KnownNat n => PGTyped ('PGChar n) where pgtype = char (Proxy @n)
689+instance KnownNat n => PGTyped ('PGVarChar n) where pgtype = varchar (Proxy @n)
690+instance PGTyped 'PGBytea where pgtype = bytea
691+instance PGTyped 'PGTimestamp where pgtype = timestamp
692+instance PGTyped 'PGTimestampTZ where pgtype = timestampWithTimeZone
693+instance PGTyped 'PGDate where pgtype = date
694+instance PGTyped 'PGTime where pgtype = time
695+instance PGTyped 'PGTimeTZ where pgtype = timeWithTimeZone
696+instance PGTyped 'PGInterval where pgtype = interval
697+instance PGTyped 'PGUuid where pgtype = uuid
698+instance PGTyped 'PGJson where pgtype = json
699+instance PGTyped 'PGJsonb where pgtype = jsonb
685700
686701createTable
687702 :: (KnownSymbol table, SListI columns)
@@ -711,8 +726,8 @@ class KnownSymbol table => DropTable table schema0 schema1
711726 dropTable (Alias table) = UnsafeStatement $
712727 "DROP TABLE " <> fromString (symbolVal' table) <> ";"
713728instance {-# OVERLAPPING #-}
714− (KnownSymbol table, table ~ table')
715− => DropTable table ((table' ::: columns) ': schema) schema
729+ (KnownSymbol table, table ~ table', schema ~ schema')
730+ => DropTable table ((table' ::: columns) ': schema) schema'
716731instance {-# OVERLAPPABLE #-}
717732 DropTable table schema0 schema1
718733 => DropTable table (table' ': schema0) (table' ': schema1)