@@ -13,7 +13,7 @@ import Effect.Aff (Aff)
13
13
import Run (Run )
14
14
import Run as Run
15
15
import Run.Except (EXCEPT , throwAt )
16
- import Run.State (STATE , getAt , modifyAt , putAt )
16
+ import Run.State (STATE , evalStateAt , getAt , modifyAt , putAt )
17
17
import Selda (Col , FullQuery , Table )
18
18
import Selda.PG.Class (class InsertRecordIntoTableReturning , BackendPGClass )
19
19
import Selda.PG.Class (deleteFrom , insert , insert1 , insert1_ , query , query1 , update ) as PG.Class
@@ -51,11 +51,6 @@ _selda = SProxy ∷ SProxy "selda"
51
51
type PgError r
52
52
= ( pgError ∷ EXCEPT PGError | r )
53
53
54
- type PgConnection r
55
- = ( pg ∷ STATE { conn ∷ Maybe PG.ConnectResult , inTransaction ∷ Boolean , pool ∷ PG.Pool } | r )
56
-
57
- _pg = SProxy ∷ SProxy " pg"
58
-
59
54
_pgError = SProxy ∷ SProxy " pgError"
60
55
61
56
query ∷
@@ -128,71 +123,80 @@ pgQuery ∷
128
123
Run (Selda + eff ) (Array o )
129
124
pgQuery q i = Run .lift _selda (PgQueryF (\conn → PG .query conn q i))
130
125
126
+ type Pg r
127
+ = ( pg ∷ STATE { conn ∷ Maybe PG.ConnectResult , inTransaction ∷ Boolean } | r )
128
+
129
+ _pg = SProxy ∷ SProxy " pg"
130
+
131
131
run ∷
132
132
∀ eff .
133
- Run (AffRow + EffRow + PgConnection + PgError + Selda + eff )
134
- ~> Run (AffRow + EffRow + PgConnection + PgError + eff )
135
- run action = do
136
- a ← Run .run (Run .on _selda handleSelda Run .send) action
137
- getAt _pg >>= _.conn
138
- >>> case _ of
139
- Just { connection, done } → do
140
- inTransaction
141
- >>= flip when do
142
- execute " ROLLBACK TRANSACTION"
143
- modifyAt _pg _ { inTransaction = false }
144
- Run .liftEffect $ done
145
- modifyAt _pg _ { conn = Nothing }
146
- Nothing → pure unit
147
- pure a
148
- where
149
- inTransaction = getAt _pg <#> _.inTransaction
150
-
151
- conn = do
152
- pg ← getAt _pg
153
- case pg.conn of
154
- Nothing →
155
- (Run .liftAff (PG .connect pg.pool))
156
- >>= case _ of
157
- Right result → do
158
- putAt _pg (pg { conn = Just result })
159
- pure result.connection
160
- Left err → throwAt _pgError err
161
- Just { connection } → pure connection
162
-
163
- execute q = do
164
- c ← conn
165
- (Run .liftAff $ PG .execute c (PG.Query q) Row0 )
166
- >>= case _ of
167
- Just err → throwAt _pgError err
168
- Nothing → pure unit
169
-
170
- handleSelda ∷ ∀ a . SeldaF a → Run (AffRow + EffRow + PgConnection + PgError + eff ) a
171
- handleSelda = case _ of
172
- SeldaF q → do
173
- c ← conn
174
- Run .liftAff (runReaderT (runExceptT q) c)
175
- >>= case _ of
176
- Right next → pure next
177
- Left err → throwAt _pgError err
178
- PgExecuteF q next → do
179
- execute q
180
- pure next
181
- PgQueryF q → do
133
+ PG.Pool →
134
+ Run (AffRow + EffRow + Pg + PgError + Selda + eff )
135
+ ~> Run (AffRow + EffRow + PgError + eff )
136
+ run pool =
137
+ let
138
+ initial = { conn: Nothing , inTransaction: false }
139
+
140
+ inTransaction = getAt _pg <#> _.inTransaction
141
+
142
+ conn = do
143
+ pg ← getAt _pg
144
+ case pg.conn of
145
+ Nothing →
146
+ (Run .liftAff (PG .connect pool))
147
+ >>= case _ of
148
+ Right result → do
149
+ putAt _pg (pg { conn = Just result })
150
+ pure result.connection
151
+ Left err → throwAt _pgError err
152
+ Just { connection } → pure connection
153
+
154
+ execute q = do
182
155
c ← conn
183
- (Run .liftAff (q c) )
156
+ (Run .liftAff $ PG .execute c ( PG.Query q) Row0 )
184
157
>>= case _ of
185
- Left err → throwAt _pgError err
186
- Right next → pure next
187
- PgOpenTransactionF next → do
188
- inTransaction >>= not
189
- >>> flip when do
190
- execute " BEGIN TRANSACTION"
191
- modifyAt _pg _ { inTransaction = true }
192
- pure next
193
- PgCloseTransactionF next → do
194
- inTransaction
195
- >>= flip when do
196
- execute " END TRANSACTION"
197
- modifyAt _pg _ { inTransaction = false }
198
- pure next
158
+ Just err → throwAt _pgError err
159
+ Nothing → pure unit
160
+
161
+ handleSelda ∷ ∀ a . SeldaF a → Run (AffRow + EffRow + Pg + PgError + eff ) a
162
+ handleSelda = case _ of
163
+ SeldaF q → do
164
+ c ← conn
165
+ Run .liftAff (runReaderT (runExceptT q) c)
166
+ >>= case _ of
167
+ Right next → pure next
168
+ Left err → throwAt _pgError err
169
+ PgExecuteF q next → do
170
+ execute q
171
+ pure next
172
+ PgQueryF q → do
173
+ c ← conn
174
+ (Run .liftAff (q c))
175
+ >>= case _ of
176
+ Left err → throwAt _pgError err
177
+ Right next → pure next
178
+ PgOpenTransactionF next → do
179
+ inTransaction >>= not
180
+ >>> flip when do
181
+ execute " BEGIN TRANSACTION"
182
+ modifyAt _pg _ { inTransaction = true }
183
+ pure next
184
+ PgCloseTransactionF next → do
185
+ inTransaction
186
+ >>= flip when do
187
+ execute " END TRANSACTION"
188
+ modifyAt _pg _ { inTransaction = false }
189
+ pure next
190
+ in
191
+ \action →
192
+ evalStateAt _pg initial do
193
+ a ← Run .run (Run .on _selda handleSelda Run .send) action
194
+ getAt _pg >>= _.conn
195
+ >>> case _ of
196
+ Just { connection, done } → do
197
+ inTransaction
198
+ >>= flip when do
199
+ execute " ROLLBACK TRANSACTION"
200
+ Run .liftEffect $ done
201
+ Nothing → pure unit
202
+ pure a
0 commit comments