Files
overextended.github.io/docs/oxmysql/Usage/transaction.md
T
2023-05-31 21:23:24 +10:00

130 lines
2.9 KiB
Markdown

---
title: Transaction
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
A transaction executes multiple queries and commits them only if all succeed.
If one fails, none of the queries are committed.
The return value is a boolean, which is the result of the transaction.
## Specific format
When using this format, you must pass an array containing sets of queries and parameters to the transaction method.
In this case, your queries do not necessarily match and the values are unique to each query.
```lua
-- You might rename "values" as "parameters" for mysql-async compatibility.
local queries = {
{ query = 'INSERT INTO `test` (id) VALUES (?)', values = { 1 }},
{ query = 'INSERT INTO `test` (id, name) VALUES (?, ?)', values = { 2, 'bob' }},
}
-- You can also pass an array of arrays.
local queries = {
{ 'INSERT INTO `test` (id) VALUES (?)', { 1 } },
{ 'INSERT INTO `test` (id, name) VALUES (?, ?)', { 2, 'bob' } },
}
```
## Shared format
When using this format, you must pass an array containing queries and a set containing shared parameters to the transaction method.
In this case, your queries do not necessarily match and the values are unique to each query.
```lua
-- You might rename "values" as "parameters" for mysql-async compatibility.
local queries = {
'INSERT INTO `test` (id, name) VALUES (@someid, @somename)',
'SET `name` = @newname IN `test` WHERE `id` = @someid'
}
local values = {
someid = 2,
somename = 'John Doe',
newname = 'John Notdoe'
}
```
## Promise
<Tabs>
<TabItem value="1" label="Lua">
```lua
local success = MySQL.transaction.await(queries, values --[[leave nil for specific format]])
print(success)
```
</TabItem>
<TabItem value="2" label="JS">
```js
const success = await MySQL.transaction(queries, values /*leave nil for specific format*/)
console.log(success)
```
</TabItem>
</Tabs>
**Aliases**
- `MySQL.Sync.transaction`
- `exports.ghmattimysql.transaction`
- `exports.oxmysql.transaction_async`
## Callback
<Tabs>
<TabItem value="1" label="Lua">
```lua
-- specific
MySQL.transaction(queries, values, function(success)
print(success)
end)
-- shared
MySQL.transaction(queries, function(success)
print(success)
end)
```
</TabItem>
<TabItem value="2" label="JS">
```js
// specific
MySQL.transaction(queries, (success) => {
console.log(success)
})
// shared
MySQL.transaction(queries, values, (success) => {
console.log(success)
})
```
</TabItem>
</Tabs>
**Aliases**
- `MySQL.Async.transaction`
- `exports.ghmattimysql.transaction`
- `exports.oxmysql.transaction`
## Transaction Isolation Level
This can be set through the convar `mysql_transaction_isolation_level`, and is an integer ranging from `1-4`.
The default value is 2.
| Convar Value | Result |
| ------------ | ---------------- |
| 1 | Repeatable Read |
| 2 | Read Committed |
| 3 | Read Uncommitted |
| 4 | Serializable |