From eb104b69f4a2dd2e4d7ac886ae393379d4959dc8 Mon Sep 17 00:00:00 2001 From: darksaid98 Date: Tue, 21 Sep 2021 19:36:35 +0200 Subject: [PATCH] Create transaction.md --- transaction.md | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 transaction.md diff --git a/transaction.md b/transaction.md new file mode 100644 index 0000000..4573500 --- /dev/null +++ b/transaction.md @@ -0,0 +1,111 @@ +# Transactions + +A transaction executes multiple queries and commit them only if all succeed. If one fails, none of the queries are commited. The return value is a `boolean`, which is the result of the transaction. + +## Usage + +### Specific Format + +When using the `Specific` format you must pass one parameter to the oxmysql export. In this case, the `queries` parameter contains your queries and the SQL variables unique to each query. + +This is useful for transactions where the queries do not share many SQL variables. + +#### Examples: + +##### Lua + +```Lua +local queries = { + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 1 } }, + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 2 } } +} -- NOTE, the 'values' tables can be named 'parameters' here for MySQL-Async compatability. + +-- Async +exports.oxmysql:transaction(queries, function(result) + print(result) +end) + +-- Sync +local result = exports.oxmysql:transactionSync(queries) +print(result) +``` + +##### JavaScript + +```Js +const queries = [ + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { someid = 1 } }, + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { someid = 2 } } +] // NOTE, the 'values' objects can be named 'parameters' here for MySQL-Async compatability. + +// Async +exports.oxmysql.transaction(queries, (result) => { + console.log(result) +}) + +// Sync +const result = await exports.oxmysql.transactionSync(queries) +console.log(result) +``` + +### Shared Format + +When using the `Shared` format you must pass two parameters to the oxmysql export. The `queries` and the `parameters` those queries will use. + +This is useful if your queries use the same SQL variables. + +#### Examples: + +##### Lua + +```Lua +local queries = { + 'INSERT INTO `test` (id, name) VALUES (:someid, :somename)', + 'SET `name` = :newname IN `test` WHERE `id` = :someid' +} + +local parameters = { ['someid'] = 2, ['somename'] = 'John Doe', ['newname'] = 'John Notdoe' } + +-- Async +exports.oxmysql:transaction(queries, parameters, function(result) + print(result) +end) + +-- Sync +local result = exports.oxmysql:transactionSync(queries, parameters) +print(result) +``` + +##### JavaScript + +```Js +const queries = [ + 'INSERT INTO `test` (id, name) VALUES (:someid, :somename)', + 'SET `name` = :newname IN `test` WHERE `id` = :someid' +] + +const parameters = { someid = 2, somename = 'John Doe', newname = 'John Notdoe' } + +// Async +exports.oxmysql.transaction(queries, parameters, (result) => { + console.log(result) +}) + +// Sync +const result = await exports.oxmysql.transactionSync(queries, parameters) +console.log(result) +``` + +## Advanced + +### Transaction Isolation Level + +This can be set through the convar `mysql_transaction_isolation_level` which should be an integer ranging from `1-4`. The default convar value set by oxmysql is `2`. + + +| Convar Value | Result | +| -------------- | ------------------ | +| 1 | Repeatable Read | +| 2 | Read Committed | +| 3 | Read Uncommitted | +| 4 | Serializable |