leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
390 stars 93 forks source link

Support for transactions #113

Closed bt-maps closed 2 years ago

bt-maps commented 2 years ago

Does pgmoon support postgresql transactions when sending multiple queries like below?

local res, num_queries = pg:query([[ UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice'; UPDATE accounts SET balance = balance - 155.00 WHERE name = 'Bob'; ]])

Or would we need to use a raw string to wrap the queries with BEGIN; UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice'; UPDATE accounts SET balance = balance - 155.00 WHERE name = 'Bob'; -- etc etc COMMIT;

leafo commented 2 years ago

pgmoon issues the query string exactly as provided to the database server. Both the examples you gave will work. From what I remember, postgres will create an implicit transaction for each string sent as a query. If you need explicit control over the transaction then you can use BEGIN, etc. as needed.