Skip to content

Commit d447079

Browse files
committed
store transaction status after query
1 parent f787ab4 commit d447079

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

pgmoon/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ do
735735
end
736736
row_desc, data_rows, command_complete = nil
737737
elseif MSG_TYPE_B.ready_for_query == _exp_0 then
738+
self.transaction_status = msg
738739
break
739740
elseif MSG_TYPE_B.notification == _exp_0 then
740741
if not (notifications) then
@@ -936,6 +937,7 @@ do
936937
return nil, self:parse_error(msg)
937938
end
938939
if MSG_TYPE_B.ready_for_query == t then
940+
self.transaction_status = msg
939941
break
940942
end
941943
end

pgmoon/init.moon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ class Postgres
745745

746746
row_desc, data_rows, command_complete = nil
747747
when MSG_TYPE_B.ready_for_query
748+
@transaction_status = msg
748749
break
749750
when MSG_TYPE_B.notification
750751
notifications = {} unless notifications
@@ -924,7 +925,9 @@ class Postgres
924925
if MSG_TYPE_B.error == t
925926
return nil, @parse_error(msg)
926927

927-
break if MSG_TYPE_B.ready_for_query == t
928+
if MSG_TYPE_B.ready_for_query == t
929+
@transaction_status = msg
930+
break
928931

929932
true
930933

spec/pgmoon_spec.moon

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,42 @@ describe "pgmoon with server", ->
220220
assert.falsy status, "connection should fail if it could not establish ssl"
221221
assert.same [[the server does not support SSL connections]], err
222222

223+
describe "transaction_status", ->
224+
it "is idle after connect", ->
225+
assert.same "I", pg.transaction_status
226+
227+
it "is in transaction after BEGIN", ->
228+
assert pg\query "BEGIN"
229+
assert.same "T", pg.transaction_status
230+
assert pg\query "ROLLBACK"
231+
assert.same "I", pg.transaction_status
232+
233+
it "is in error state after failed query in transaction", ->
234+
assert pg\query "BEGIN"
235+
assert.same "T", pg.transaction_status
236+
237+
-- query a non-existent table to trigger an error
238+
res, err = pg\query "SELECT * FROM nonexistent_table_12345"
239+
assert.falsy res
240+
assert.same "E", pg.transaction_status
241+
242+
-- further queries should fail until rollback
243+
res, err = pg\query "SELECT 1"
244+
assert.falsy res
245+
assert.same "E", pg.transaction_status
246+
247+
-- rollback should restore to idle
248+
assert pg\query "ROLLBACK"
249+
assert.same "I", pg.transaction_status
250+
251+
it "returns to idle after COMMIT", ->
252+
assert pg\query "BEGIN"
253+
assert.same "T", pg.transaction_status
254+
assert pg\query "SELECT 1"
255+
assert.same "T", pg.transaction_status
256+
assert pg\query "COMMIT"
257+
assert.same "I", pg.transaction_status
258+
223259
describe "extended_query", ->
224260
it "query with no params", ->
225261
res = assert pg\extended_query "select 1 as one"

0 commit comments

Comments
 (0)