on dev help sign in
replace old query types
6d34ebfEitan Chatav
squeel-postgresql/squeel-postgresql.cabal−1
@@ -16,7 +16,6 @@ library
16 Squeel.PostgreSQL
16 Squeel.PostgreSQL
17 Squeel.PostgreSQL.PQ
17 Squeel.PostgreSQL.PQ
18 Squeel.PostgreSQL.Query
18 Squeel.PostgreSQL.Query
19− Squeel.PostgreSQL.Query2
20 Squeel.PostgreSQL.Type
19 Squeel.PostgreSQL.Type
21 Squeel.PostgreSQL.Value
20 Squeel.PostgreSQL.Value
22 default-language: Haskell2010
21 default-language: Haskell2010
squeel-postgresql/src/Squeel/PostgreSQL/PQ.hs+17−13
@@ -1,5 +1,7 @@
1{-# LANGUAGE
1{-# LANGUAGE
2+ DataKinds
3+ , PolyKinds
2− DeriveFunctor
4+ , DeriveFunctor
3 , FlexibleContexts
5 , FlexibleContexts
4 , FlexibleInstances
6 , FlexibleInstances
5 , MultiParamTypeClasses
7 , MultiParamTypeClasses
@@ -7,7 +9,6 @@
7 , ScopedTypeVariables
9 , ScopedTypeVariables
8 , TypeApplications
10 , TypeApplications
9 , TypeFamilies
11 , TypeFamilies
10− , TypeInType
11 , TypeOperators
12 , TypeOperators
12 , UndecidableInstances
13 , UndecidableInstances
13#-}
14#-}
@@ -32,7 +33,7 @@ import Squeel.PostgreSQL.Type
32
33
33newtype Connection db = Connection { unConnection :: LibPQ.Connection }
34newtype Connection db = Connection { unConnection :: LibPQ.Connection }
34
35
35−newtype PQ (db0 :: [Table]) (db1 :: [Table]) m x = PQ
36+newtype PQ db0 db1 m x = PQ
36 { runPQ :: Connection db0 -> m (x, Connection db1) }
37 { runPQ :: Connection db0 -> m (x, Connection db1) }
37 deriving Functor
38 deriving Functor
38
39
@@ -58,24 +59,24 @@ class MonadPQ pq where
58
59
59 pqExec
60 pqExec
60 :: MonadBase IO io
61 :: MonadBase IO io
61− => Query db0 db1 '[] xs
62+ => Query '[] db0 db1 xs
62 -> pq db0 db1 io (Maybe (Result xs))
63 -> pq db0 db1 io (Maybe (Result xs))
63
64
64 pqExecParams
65 pqExecParams
65 :: (MonadBase IO io, ToOids ps, ToValues xs ps)
66 :: (MonadBase IO io, ToOids ps, ToValues xs ps)
66− => Query db0 db1 ps ys
67+ => Query ps db0 db1 ys
67 -> Rec Identity xs
68 -> Rec Identity xs
68 -> pq db0 db1 io (Maybe (Result ys))
69 -> pq db0 db1 io (Maybe (Result ys))
69
70
70 pqPrepare
71 pqPrepare
71 :: (MonadBase IO io, ToOids ps)
72 :: (MonadBase IO io, ToOids ps)
72 => ByteString
73 => ByteString
73− -> Query db0 db1 ps xs
74+ -> Query ps db0 db1 xs
74 -> pq db0 db1 io (Maybe (Result []), PreparedQuery db0 db1 ps xs)
75 -> pq db0 db1 io (Maybe (Result []), PreparedQuery db0 db1 ps xs)
75
76
76 pqExecPrepared
77 pqExecPrepared
77 :: (MonadBase IO io, ToValues xs ps)
78 :: (MonadBase IO io, ToValues xs ps)
78− => PreparedQuery db0 db1 ps ys
79+ => PreparedQuery ps db0 db1 ys
79 -> Rec Identity xs
80 -> Rec Identity xs
80 -> pq db0 db1 io (Maybe (Result ys))
81 -> pq db0 db1 io (Maybe (Result ys))
81
82
@@ -90,11 +91,11 @@ instance MonadPQ PQ where
90 (x', conn') <- x conn
91 (x', conn') <- x conn
91 runPQ (f x') conn'
92 runPQ (f x') conn'
92
93
93− pqExec (Query q) = PQ $ \ (Connection conn) -> do
94+ pqExec (UnsafeQuery q) = PQ $ \ (Connection conn) -> do
94 result <- liftBase $ LibPQ.exec conn q
95 result <- liftBase $ LibPQ.exec conn q
95 return (Result <$> result, Connection conn)
96 return (Result <$> result, Connection conn)
96
97
97− pqExecParams (Query q :: Query db0 db1 ps ys) params =
98+ pqExecParams (UnsafeQuery q :: Query ps db0 db1 ys) params =
98 PQ $ \ (Connection conn) -> do
99 PQ $ \ (Connection conn) -> do
99 let
100 let
100 params' =
101 params' =
@@ -105,19 +106,22 @@ instance MonadPQ PQ where
105 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
106 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
106 return (Result <$> result, Connection conn)
107 return (Result <$> result, Connection conn)
107
108
108− pqPrepare statementName (Query q :: Query db0 db1 ps ys) =
109+ pqPrepare statementName (UnsafeQuery q :: Query ps db0 db1 ys) =
109 PQ $ \ (Connection conn) -> do
110 PQ $ \ (Connection conn) -> do
110 result <- liftBase $
111 result <- liftBase $
111 LibPQ.prepare conn statementName q (Just (toOids (Proxy @ps)))
112 LibPQ.prepare conn statementName q (Just (toOids (Proxy @ps)))
112− return ((Result <$> result,PreparedQuery statementName), Connection conn)
113+ return
114+ ( ( Result <$> result
115+ , UnsafePreparedQuery statementName
116+ ) , Connection conn )
113
117
114− pqExecPrepared (PreparedQuery q :: PreparedQuery db0 db1 ps ys) params =
118+ pqExecPrepared (q :: PreparedQuery ps db0 db1 ys) params =
115 PQ $ \ (Connection conn) -> do
119 PQ $ \ (Connection conn) -> do
116 let
120 let
117 params' =
121 params' =
118 [Just (param, LibPQ.Binary) | param <- toValues (Proxy @ps) params]
122 [Just (param, LibPQ.Binary) | param <- toValues (Proxy @ps) params]
119 result <- liftBase $
123 result <- liftBase $
120− LibPQ.execPrepared conn q params' LibPQ.Binary
124+ LibPQ.execPrepared conn (renderPreparedQuery q) params' LibPQ.Binary
121 return (Result <$> result, Connection conn)
125 return (Result <$> result, Connection conn)
122
126
123instance Monad m => Applicative (PQ db db m) where
127instance Monad m => Applicative (PQ db db m) where
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+103−179
@@ -1,207 +1,131 @@
1{-# LANGUAGE
1{-# LANGUAGE
2− PolyKinds
3− , DataKinds
2+ DataKinds
4− , FlexibleContexts
5 , FlexibleInstances
3 , FlexibleInstances
6 , GADTs
4 , GADTs
7− , KindSignatures
5+ , MultiParamTypeClasses
8− , LambdaCase
9 , OverloadedStrings
6 , OverloadedStrings
10− , RankNTypes
7+ , PolyKinds
11 , ScopedTypeVariables
8 , ScopedTypeVariables
12 , TypeApplications
9 , TypeApplications
13− , TypeInType
10+ , TypeFamilies
14 , TypeOperators
11 , TypeOperators
15#-}
12#-}
16
13
17−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
18−
19module Squeel.PostgreSQL.Query where
14module Squeel.PostgreSQL.Query where
20
15
21−import Data.ByteString (ByteString)
16+import Data.Boolean
17+import Data.ByteString
22import Data.Monoid
18import Data.Monoid
23import Data.Proxy
19import Data.Proxy
24import Data.String
20import Data.String
25−import Data.Vinyl
26−import Data.Vinyl.Functor
27import GHC.TypeLits
21import GHC.TypeLits
28
22
29import qualified Data.ByteString.Char8 as Char8
23import qualified Data.ByteString.Char8 as Char8
30
24
31import Squeel.PostgreSQL.Type
25import Squeel.PostgreSQL.Type
32
26
33−data Expression ps cols x =
27+newtype Expression ps xs y =
34− Expression { unExpression :: ByteString } deriving Show
28+ UnsafeExpression { renderExpression :: ByteString }
35
29
36param1 :: Expression (p1:ps) cols p1
30param1 :: Expression (p1:ps) cols p1
37−param1 = Expression "$1"
31+param1 = UnsafeExpression "$1"
38−
39param2 :: Expression (p1:p2:ps) cols p2
32param2 :: Expression (p1:p2:ps) cols p2
40−param2 = Expression "$2"
33+param2 = UnsafeExpression "$2"
41−
42param3 :: Expression (p1:p2:p3:ps) cols p3
34param3 :: Expression (p1:p2:p3:ps) cols p3
43−param3 = Expression "$3"
35+param3 = UnsafeExpression "$3"
44−
45param4 :: Expression (p1:p2:p3:p4:ps) cols p4
36param4 :: Expression (p1:p2:p3:p4:ps) cols p4
46−param4 = Expression "$4"
37+param4 = UnsafeExpression "$4"
47−
48param5 :: Expression (p1:p2:p3:p4:p5:ps) cols p5
38param5 :: Expression (p1:p2:p3:p4:p5:ps) cols p5
49−param5 = Expression "$5"
39+param5 = UnsafeExpression "$5"
50−
40+
51−whenThen :: (Expression ps cols x,Expression ps cols y) -> ByteString
41+class HasField ident xs x where
52−whenThen (Expression when_,Expression then_) = mconcat
42+ fieldName :: Proxy ident -> Proxy xs -> Proxy x -> ByteString
53− [" WHEN ",when_," THEN ",then_,"\n"]
43+instance KnownSymbol name => HasField '[name] ((name ':= x) ': xs) x where
54−
44+ fieldName _ _ _ = Char8.pack (symbolVal (Proxy @name))
55−case_
45+instance HasField ident xs x => HasField ident (y ': xs) x where
56− :: [(Expression ps cols 'PGBool,Expression ps cols x)]
46+ fieldName _ _ _ = fieldName (Proxy @ident) (Proxy @xs) (Proxy @x)
57− -> Expression ps cols x
47+instance (KnownSymbol name, HasField names xs x)
58− -> Expression ps cols x
48+ => HasField (name ': names) ((name ':= xs) ': xss) x where
59−case_ whens (Expression else_) = Expression $ mconcat
49+ fieldName _ _ _ = Char8.pack (symbolVal (Proxy @name))
60− [ "CASE\n"
50+ <> "." <> fieldName (Proxy @names) (Proxy @xs) (Proxy @x)
61− , mconcat (map whenThen whens)
51+
62− , " ELSE ",else_
52+column_
63− , "\nEND"
53+ :: forall ident xs x ps
64− ]
54+ . HasField ident xs x
65−
55+ => Proxy ident
66−caseSwitch
67− :: Expression ps cols x
68− -> [(Expression ps cols x,Expression ps cols y)]
69− -> Expression ps cols y
56+ -> Expression ps xs x
70− -> Expression ps cols y
57+column_ (_ :: Proxy ident) = UnsafeExpression $
71−caseSwitch (Expression val) whens (Expression else_) = Expression $ mconcat
58+ fieldName (Proxy @ident) (Proxy @xs) (Proxy @x)
72− [ "CASE ",val,"\n"
59+
73− , mconcat (map whenThen whens)
60+data Identified ident x = (:=) ident x
74− , " ELSE ",else_
61+data Aliased obj named where AS :: obj x -> Aliased obj (name ':= x)
75− ,"\nEND"
62+
76− ]
63+unalias :: Aliased obj (name ':= x) -> obj x
64+unalias (AS obj) = obj
77
65
78column
66column
79− :: forall colName colType params columns
67+ :: forall ident xs x ps
80− . Alias (Expression params columns) (colName ':= colType)
68+ . HasField ident xs x
81− -> Expression params columns colType
69+ => Aliased (Expression ps xs) (ident ':= x)
82−column = \case
70+column = AS $ column_ (Proxy @ident)
83− As x -> x
71+
84− Elem -> Expression $ Char8.pack (symbolVal (Proxy @colName))
85−
86−true :: Expression ps cols 'PGBool
87−true = Expression "true"
88−
89−false :: Expression ps cols 'PGBool
90−false = Expression "false"
91−
92−(.==)
93− :: Expression ps cols x
94− -> Expression ps cols x
95− -> Expression ps cols 'PGBool
96−(.==) (Expression x1) (Expression x2) = Expression $ x1 <> "=" <> x2
97−
98−(./=)
99− :: Expression ps cols x
100− -> Expression ps cols x
101− -> Expression ps cols 'PGBool
102−(./=) (Expression x1) (Expression x2) = Expression $ x1 <> "<>" <> x2
103−
104−(.>)
105− :: Expression ps cols x
106− -> Expression ps cols x
107− -> Expression ps cols 'PGBool
108−(.>) (Expression x1) (Expression x2) = Expression $ x1 <> ">" <> x2
109−
110−(.<)
111− :: Expression ps cols x
112− -> Expression ps cols x
113− -> Expression ps cols 'PGBool
114−(.<) (Expression x1) (Expression x2) = Expression $ x1 <> "<" <> x2
115−
116−(.>=)
117− :: Expression ps cols x
118− -> Expression ps cols x
119− -> Expression ps cols 'PGBool
120−(.>=) (Expression x1) (Expression x2) = Expression $ x1 <> ">=" <> x2
121−
122−(.<=)
123− :: Expression ps cols x
124− -> Expression ps cols x
125− -> Expression ps cols 'PGBool
126−(.<=) (Expression x1) (Expression x2) = Expression $ x1 <> "<=" <> x2
127−
128−instance IsString (Expression ps cols 'PGText) where
72+instance IsString (Expression ps xs 'PGText) where
129− fromString str = Expression $ "\'" <> fromString str <> "\'"
73+ fromString str = UnsafeExpression $ "\'" <> fromString str <> "\'"
130−
74+
131−instance Num (Expression ps cols 'PGInt4) where
75+instance Num (Expression ps xs 'PGInt4) where
132− fromInteger n = Expression . fromString $ show n
76+ fromInteger n = UnsafeExpression . fromString $ show n
133− Expression x + Expression y = Expression $ x <> "+" <> y
77+ x + y = UnsafeExpression $ renderExpression x <> "+" <> renderExpression y
134− Expression x * Expression y = Expression $ x <> "*" <> y
78+ x * y = UnsafeExpression $ renderExpression x <> "*" <> renderExpression y
135− Expression x - Expression y = Expression $ x <> "+" <> y
79+ UnsafeExpression x - UnsafeExpression y = UnsafeExpression $ x <> "+" <> y
136− abs (Expression x) = Expression $ "@" <> x
80+ abs (UnsafeExpression x) = UnsafeExpression $ "@" <> x
137− signum (Expression x) = Expression $ "sign(" <> x <> ")"
81+ signum (UnsafeExpression x) = UnsafeExpression $ "sign(" <> x <> ")"
138−
82+
83+instance Boolean (Expression ps xs 'PGBool) where
84+ true = UnsafeExpression "TRUE"
85+ false = UnsafeExpression "FALSE"
86+ notB x = UnsafeExpression $ "NOT " <> renderExpression x
87+ x &&* y = UnsafeExpression $
88+ renderExpression x <> " AND " <> renderExpression y
89+ x ||* y = UnsafeExpression $
90+ renderExpression x <> " OR " <> renderExpression y
91+
92+type instance BooleanOf (Expression ps xs y) = Expression ps xs 'PGBool
93+
94+instance IfB (Expression ps xs y) where
95+ ifB predicate then_ else_ = UnsafeExpression $ mconcat
96+ [ "CASE"
97+ , "\n WHEN ",renderExpression predicate
98+ , "\n THEN ",renderExpression then_
99+ , "\n ELSE ",renderExpression else_
100+ , "\nEND"
101+ ]
102+
103+instance EqB (Expression ps xs y) where
104+ x ==* y = UnsafeExpression $ renderExpression x <> "=" <> renderExpression y
105+ x /=* y = UnsafeExpression $ renderExpression x <> "<>" <> renderExpression y
106+
107+instance OrdB (Expression ps xs y) where
108+ x >* y = UnsafeExpression $ renderExpression x <> ">" <> renderExpression y
109+ x >=* y = UnsafeExpression $ renderExpression x <> ">=" <> renderExpression y
110+ x <* y = UnsafeExpression $ renderExpression x <> "<" <> renderExpression y
111+ x <=* y = UnsafeExpression $ renderExpression x <> "<=" <> renderExpression y
112+
139−data Relation ps xs ys = Relation { unRelation :: ByteString } deriving Show
113+data Relation ps xs ys = UnsafeRelation { renderRelation :: ByteString }
140−
114+
141−star :: Relation ps xs xs
115+table_
142−star = Relation "*"
116+ :: forall ident xss xs ps
143−
117+ . HasField ident xss xs
118+ => Proxy ident
144−project :: Rec (Alias (Expression ps xs)) ys -> Relation ps xs ys
119+ -> Relation ps xss xs
145−project
120+table_ (_ :: Proxy ident) = UnsafeRelation $
146− = Relation
121+ fieldName (Proxy @ident) (Proxy @xss) (Proxy @xs)
147− . Char8.intercalate ", "
122+
148− . recordToList
123+table
149− . rmap (Const . expressionString)
124+ :: forall ident xss xs ps
150− where
125+ . HasField ident xss xs
151− expressionString :: Alias (Expression ps xs) y -> ByteString
126+ => Aliased (Relation ps xss) (ident ':= xs)
152− expressionString = \case
127+table = AS $ table_ (Proxy @ident)
153− (As (Expression expr) :: Alias (Expression ps xs) y)
128+
154− -> expr <> " AS " <> Char8.pack (symbolVal (Proxy @(Name y)))
129+newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
155− (Elem :: Alias (Expression ps xs) y)
130+newtype PreparedQuery ps xss yss zs =
156− -> Char8.pack (symbolVal (Proxy @(Name y)))
131+ UnsafePreparedQuery { renderPreparedQuery :: ByteString }
157−
158−-- star :: Relation db params xs xs
159−-- star = Relation "SELECT * FROM "
160−
161−-- table
162−-- :: forall tableName tableCols db params.
163−-- ( KnownSymbol tableName
164−-- , (tableName ':= tableCols) `Elem` Unnamed db ~ 'True
165−-- ) => Proxy tableName -> Relation db params '[] tableCols
166−-- table (_ :: Proxy tableName) = Relation $
167−-- "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
168−
169−-- starFrom
170−-- :: forall tableName tableCols db params columns
171−-- . Alias (Relation params columns db) (tableName ':= tableCols)
172−-- -> Relation params columns db tableCols
173−-- starFrom = \case
174−-- As x -> x
175−-- Elem -> Relation $
176−-- "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
177−
178−-- from
179−-- :: Relation db params ys zs
180−-- -> Relation db params xs ys
181−-- -> Relation db params xs zs
182−-- from (Relation rel1) (Relation rel2) = Relation $ rel1 <> rel2
183−
184−-- project :: Rec (Alias (Expression ps xs)) ys -> Relation db params xs ys
185−-- project exprs = Relation $
186−-- "SELECT " <> Char8.intercalate "," (exprStrings exprs) <> " FROM "
187−-- where
188−-- exprStrings :: Rec (Alias (Expression ps xs)) ys -> [ByteString]
189−-- exprStrings = \case
190−-- RNil -> []
191−-- (As (Expression expr) :: Alias (Expression _ _) (exprName ':= _))
192−-- :& exprs ->
193−-- (expr <> " As " <> Char8.pack (symbolVal (Proxy @exprName)))
194−-- : expressionStrings exprs
195−
196−newtype Query
197− (db0 :: [Table])
198− (db1 :: [Table])
199− (ps :: [PGType])
200− (xs :: [Column])
201− = Query { unQuery :: ByteString }
202−
203−newtype PreparedQuery db0 db1 ps xs
204− = PreparedQuery { unPreparedQuery :: ByteString }
205−
206−-- select :: Relation ps ('[] :: [Column]) db xs -> Query db db ps xs
207−-- select (Relation x) = Query x
squeel-postgresql/src/Squeel/PostgreSQL/Query2.hsdeleted−111
@@ -1,111 +0,0 @@
1−{-# LANGUAGE
2− DataKinds
3− , FlexibleInstances
4− , GADTs
5− , MultiParamTypeClasses
6− , OverloadedStrings
7− , PolyKinds
8− , ScopedTypeVariables
9− , TypeApplications
10− , TypeFamilies
11− , TypeOperators
12−#-}
13−
14−module Squeel.PostgreSQL.Query2 where
15−
16−import Data.Boolean
17−import Data.ByteString
18−import Data.Monoid
19−import Data.Proxy
20−import Data.String
21−import GHC.TypeLits
22−
23−import qualified Data.ByteString.Char8 as Char8
24−
25−import Squeel.PostgreSQL.Type
26−
27−newtype Expression ps xs x =
28− UnsafeExpression { renderExpression :: ByteString }
29−
30−param1 :: Expression (p1:ps) cols p1
31−param1 = UnsafeExpression "$1"
32−param2 :: Expression (p1:p2:ps) cols p2
33−param2 = UnsafeExpression "$2"
34−param3 :: Expression (p1:p2:p3:ps) cols p3
35−param3 = UnsafeExpression "$3"
36−param4 :: Expression (p1:p2:p3:p4:ps) cols p4
37−param4 = UnsafeExpression "$4"
38−param5 :: Expression (p1:p2:p3:p4:p5:ps) cols p5
39−param5 = UnsafeExpression "$5"
40−
41−class HasField ident xs x where
42− fieldName :: Proxy ident -> Proxy xs -> Proxy x -> ByteString
43−instance KnownSymbol name => HasField '[name] ((name ':= x) ': xs) x where
44− fieldName _ _ _ = Char8.pack (symbolVal (Proxy @name))
45−instance HasField ident xs x => HasField ident (y ': xs) x where
46− fieldName _ _ _ = fieldName (Proxy @ident) (Proxy @xs) (Proxy @x)
47−instance (KnownSymbol name, HasField names xs x)
48− => HasField (name ': names) ((name ':= xs) ': xss) x where
49− fieldName _ _ _ = Char8.pack (symbolVal (Proxy @name))
50− <> "." <> fieldName (Proxy @names) (Proxy @xs) (Proxy @x)
51−
52−field
53− :: forall ident xs x ps
54− . HasField ident xs x
55− => Proxy ident
56− -> Expression ps xs x
57−field (_ :: Proxy ident) = UnsafeExpression $
58− fieldName (Proxy @ident) (Proxy @xs) (Proxy @x)
59−
60−data Aliased obj named where
61− AS :: obj x -> Aliased obj (name ':= x)
62−
63−unalias :: Aliased obj (name ':= x) -> obj x
64−unalias (AS obj) = obj
65−
66−fieldAs
67− :: forall ident xs x ps
68− . HasField ident xs x
69− => Aliased (Expression ps xs) (ident ':= x)
70−fieldAs = AS $ field (Proxy @ident)
71−
72−instance IsString (Expression ps xs 'PGText) where
73− fromString str = UnsafeExpression $ "\'" <> fromString str <> "\'"
74−
75−instance Num (Expression ps xs 'PGInt4) where
76− fromInteger n = UnsafeExpression . fromString $ show n
77− UnsafeExpression x + UnsafeExpression y = UnsafeExpression $ x <> "+" <> y
78− UnsafeExpression x * UnsafeExpression y = UnsafeExpression $ x <> "*" <> y
79− UnsafeExpression x - UnsafeExpression y = UnsafeExpression $ x <> "+" <> y
80− abs (UnsafeExpression x) = UnsafeExpression $ "@" <> x
81− signum (UnsafeExpression x) = UnsafeExpression $ "sign(" <> x <> ")"
82−
83−instance Boolean (Expression ps xs 'PGBool) where
84− true = UnsafeExpression "TRUE"
85− false = UnsafeExpression "FALSE"
86− notB x = UnsafeExpression $ " NOT " <> renderExpression x
87− x &&* y = UnsafeExpression $
88− renderExpression x <> " AND " <> renderExpression y
89− x ||* y = UnsafeExpression $
90− renderExpression x <> " OR " <> renderExpression y
91−
92−type instance BooleanOf (Expression ps xs x) = Expression ps xs 'PGBool
93−
94−instance IfB (Expression ps xs x) where
95− ifB predicate then_ else_ = UnsafeExpression $ mconcat
96− [ "CASE\n"
97− , "\n WHEN ",renderExpression predicate
98− , "\n THEN ",renderExpression then_
99− , "\n ELSE ",renderExpression else_
100− , "\nEND"
101− ]
102−
103−instance EqB (Expression ps xs x) where
104− x ==* y = UnsafeExpression $ renderExpression x <> "=" <> renderExpression y
105− x /=* y = UnsafeExpression $ renderExpression x <> "<>" <> renderExpression y
106−
107−instance OrdB (Expression ps xs x) where
108− x >* y = UnsafeExpression $ renderExpression x <> ">" <> renderExpression y
109− x >=* y = UnsafeExpression $ renderExpression x <> ">=" <> renderExpression y
110− x <* y = UnsafeExpression $ renderExpression x <> "<" <> renderExpression y
111− x <=* y = UnsafeExpression $ renderExpression x <> "<=" <> renderExpression y
squeel-postgresql/src/Squeel/PostgreSQL/Type.hs−21
@@ -1,10 +1,8 @@
1{-# LANGUAGE
1{-# LANGUAGE
2 DataKinds
2 DataKinds
3− , GADTs
4 , PolyKinds
3 , PolyKinds
5 , ScopedTypeVariables
4 , ScopedTypeVariables
6 , TypeApplications
5 , TypeApplications
7− , TypeFamilies
8 , TypeOperators
6 , TypeOperators
9#-}
7#-}
10
8
@@ -41,25 +39,6 @@ data PGType
41 | PGJson
39 | PGJson
42 | PGJsonb
40 | PGJsonb
43
41
44−data Named k = (:=) Symbol k
45−type family Unnamed named where Unnamed (name ':= x) = x
46−type family Name k where Name (name ':= x) = name
47−
48−type Column = Named PGType
49−type Table = Named [Column]
50−type Database = Named [Table]
51−type Schema = Named [Database]
52−data Alias (obj :: k -> *) (named :: Named k) where
53− As :: KnownSymbol name => obj x -> Alias obj (name ':= x)
54− Elem
55− :: (KnownSymbol name, (name ':= x) `Elem` list ~ 'True)
56− => Alias (obj list) (name ':= x)
57−
58−type family Elem x xs where
59− x `Elem` '[] = 'False
60− x `Elem` (x ': xs) = 'True
61− x `Elem` (x' ': xs) = x `Elem` xs
62−
63class ToOid pg where toOid :: Proxy pg -> LibPQ.Oid
42class ToOid pg where toOid :: Proxy pg -> LibPQ.Oid
64class ToOids pgs where toOids :: Proxy pgs -> [LibPQ.Oid]
43class ToOids pgs where toOids :: Proxy pgs -> [LibPQ.Oid]
65instance ToOids '[] where toOids _ = []
44instance ToOids '[] where toOids _ = []