Skip to content

Transctions support draft #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,44 @@ local conn,err = pg_conn("postgresql://user:[email protected]:5432/mydb?connect_ti
...
pg_close(conn);
```
## bool pg_tx_begin(userdata connection);
Begin new transaction for current connection (cannot create subtransaction)

**return** boolean
```lua
local conn,err = pg_conn("postgresql://user:[email protected]:5432/mydb?connect_timeout=3");
...
pg_tx_begin(conn);
local exec = pg_exec(conn, "INSERT INTO users (name, password, money) VALUES ($1,$2,$3)", "a man", "mypasswd", "13");
local exec = pg_exec(conn, "INSERT INTO users (name, password, money) VALUES ($1,$2,$3)", "a woman", "somepass", "44");
pg_tx_commit(conn);
```
## bool pg_tx_commit(userdata connection);
Commit transaction for current connection

**return** boolean
```lua
local conn,err = pg_conn("postgresql://user:[email protected]:5432/mydb?connect_timeout=3");
...
pg_tx_begin(conn);
local exec = pg_exec(conn, "INSERT INTO users (name, password, money) VALUES ($1,$2,$3)", "a man", "mypasswd", "13");
local exec = pg_exec(conn, "INSERT INTO users (name, password, money) VALUES ($1,$2,$3)", "a woman", "somepass", "44");
pg_tx_commit(conn);
```
## bool pg_tx_rollback(userdata connection);
Rollback transaction for current connection

**return** boolean
```lua
local conn,err = pg_conn("postgresql://user:[email protected]:5432/mydb?connect_timeout=3");
...
pg_tx_begin(conn);
local exec = pg_exec(conn, "INSERT INTO users (name, password, money) VALUES ($1,$2,$3)", "a man", "mypasswd", "13");
local exec = pg_exec(conn, "INSERT INTO users (name, password, money) VALUES ($1,$2,$3)", "a woman", "somepass", "44");
if (<state validation condition>) then
pg_tx_commit(conn);
else
pg_tx_rollback(conn);
end
```

9 changes: 7 additions & 2 deletions include/CFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*
* ml_base, External lua add-on module
*
* Copyright � 2003-2008 MTA. All Rights Reserved.
* Copyright � 2003-2008 MTA. All Rights Reserved.
*
* Grand Theft Auto is � 2002-2003 Rockstar North
* Grand Theft Auto is � 2002-2003 Rockstar North
*
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
Expand All @@ -33,6 +33,11 @@ extern ILuaModuleManager10 *pModuleManager;
class CFunctions
{
public:
// transaction functions
static int pg_tx_begin(lua_State* luaVM);
static int pg_tx_rollback(lua_State* luaVM);
static int pg_tx_commit(lua_State* luaVM);

static int pg_conn(lua_State* luaVM);
static int pg_query(lua_State* luaVM);
static int pg_poll(lua_State* luaVM);
Expand Down
Loading