mirror of
https://github.com/darksaid98/overextended.github.io.git
synced 2026-12-02 06:28:54 +00:00
rewrite oxmysql docs
This commit is contained in:
1 parent
864a1b3858
commit
3298244c9a
17 files changed
+630
-520
No files matched your search
@@ -1,63 +0,0 @@
|
||||
# Usage
|
||||
|
||||
## lib/MySQL
|
||||
|
||||
Resources can import oxmysql methods by including our library, granting some type-checking and minor performance improvements over raw export calls.
|
||||
|
||||
### Lua
|
||||
|
||||
Modify `fxmanifest.lua` for your resource, and add the following above any other script files.
|
||||
|
||||
```lua
|
||||
server_script '@oxmysql/lib/MySQL.lua'
|
||||
```
|
||||
|
||||
If you are using VSCode with [sumneko Lua](https://marketplace.visualstudio.com/items?itemName=sumneko.lua) you can get access to some basic types and intellisense.
|
||||
|
||||
```json
|
||||
"Lua.workspace.library": [
|
||||
"C:\\pathtoserver\\resources\\oxmysql\\lib\\define.lua",
|
||||
]
|
||||
```
|
||||
|
||||
### JavaScript
|
||||
|
||||
Use your favourite package manager to install our [node package](https://www.npmjs.com/package/@overextended/oxmysql).
|
||||
|
||||
```bash
|
||||
# With pnpm
|
||||
pnpm add @overextended/oxmysql
|
||||
|
||||
# With Yarn
|
||||
yarn add @overextended/oxmysql
|
||||
|
||||
# With npm
|
||||
npm install @overextended/oxmysql
|
||||
```
|
||||
|
||||
Import the oxmysql object into your resource.
|
||||
|
||||
```js
|
||||
import { oxmysql as MySQL } from '@overextended/oxmysql';
|
||||
```
|
||||
|
||||
## Placeholders
|
||||
|
||||
Safely format queries by using placeholders in the query, and sending arguments in an array or object.
|
||||
Named placeholders are not recommended, and unsupported when using MySQL.prepare.
|
||||
|
||||
```lua
|
||||
MySQL.query('SELECT * FROM users WHERE identifier = ?', { 'license:abcdefg' })
|
||||
MySQL.query('SELECT * FROM users WHERE identifier = :identifier', { identifier = 'license:abcdefg' })
|
||||
```
|
||||
|
||||
## Upsert
|
||||
|
||||
When uncertain if a row should be inserted into the database, or an existing row should be updated, queries should check for duplicate keys.
|
||||
|
||||
```lua
|
||||
MySQL.prepare('INSERT INTO ox_inventory (owner, name, data) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)', { owner, dbId, inventory })
|
||||
```
|
||||
|
||||
This is preferred over checking the existence of a row, then inserting or updating depending on the result.
|
||||
Furthermore, unlike using 'REPLACE INTO', the row is not deleted and re-inserted.
|
||||
@@ -1,53 +1,75 @@
|
||||
---
|
||||
keywords: ['execute', 'insert']
|
||||
keywords: ['execute', 'insert']
|
||||
title: Insert
|
||||
---
|
||||
|
||||
# Insert
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
Inserts a new entry into the database and returns the insert id for the row, if valid.
|
||||
|
||||
## Lua
|
||||
## Promise
|
||||
|
||||
### Callback
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:insert
|
||||
-- alias: MySQL.Async.insert
|
||||
-- alias: exports.ghmattimysql:execute
|
||||
local id = MySQL.insert.await('INSERT INTO `users` (identifier, firstname, lastname) VALUES (?, ?, ?)', {
|
||||
identifier, firstName, lastName
|
||||
})
|
||||
|
||||
MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', {playerIdentifier, firstName, lastName}, function(id)
|
||||
print(id)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
const id = await MySQL.insert('INSERT INTO `users` (identifier, firstname, lastname) VALUES (?, ?, ?)', [
|
||||
identifier, firstName, lastName
|
||||
])
|
||||
|
||||
console.log(id)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
**Aliases**
|
||||
|
||||
- `MySQL.Sync.insert`
|
||||
- `exports.ghmattimysql.executeSync`
|
||||
- `exports.oxmysql.insert_async`
|
||||
|
||||
## Callback
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
MySQL.insert('INSERT INTO `users` (identifier, firstname, lastname) VALUES (?, ?, ?)', {
|
||||
identifier, firstName, lastName
|
||||
}, function(id)
|
||||
print(id)
|
||||
end)
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:insert_async
|
||||
-- alias: MySQL.Sync.insert
|
||||
-- alias: exports.ghmattimysql:executeSync
|
||||
|
||||
local id = MySQL.insert.await('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', {playerIdentifier, firstName, lastName})
|
||||
print(id)
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
|
||||
### Callback
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.insert
|
||||
|
||||
MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName], (id) => {
|
||||
MySQL.insert('INSERT INTO `users` (identifier, firstname, lastname) VALUES (?, ?, ?)', [
|
||||
identifier, firstName, lastName
|
||||
], (id) => {
|
||||
console.log(id)
|
||||
})
|
||||
```
|
||||
|
||||
### Promise
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.insert_async
|
||||
**Aliases**
|
||||
|
||||
const id = await MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName])
|
||||
console.log(id)
|
||||
```
|
||||
- `MySQL.Async.insert`
|
||||
- `exports.ghmattimysql.execute`
|
||||
- `exports.oxmysql.insert`
|
||||
@@ -1,60 +1,78 @@
|
||||
# Prepare
|
||||
---
|
||||
title: Prepare
|
||||
---
|
||||
|
||||
The only function that provides true [prepared statements](https://github.com/sidorares/node-mysql2#using-prepared-statements), granting native protection and query planning by MySQL.
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
The only function that provides true prepared statements, granting native protection and query planning by MySQL.
|
||||
Prepare can be used to execute frequently called queries faster and accepts multiple sets of parameters to be used with a single query.
|
||||
|
||||
- Date will not return the datestring commonly used in FiveM
|
||||
- TINYINT 1 and BIT will not return a boolean
|
||||
- You can only use `?` value placeholders, `??` column placeholders and named placeholders will throw an error
|
||||
- You can only use ? value placeholders, ?? column placeholders and named placeholders will throw an error
|
||||
|
||||
When using SELECT, the return value will match `query, single, or scalar` depending on the number of columns and rows selected.
|
||||
When using SELECT, the return value will match `query`, `single`, or `scalar` depending on the number of columns and rows selected.
|
||||
|
||||
## Lua
|
||||
## Promise
|
||||
|
||||
### Callback
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:prepare
|
||||
local response = MySQL.prepare.await('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
|
||||
identifier
|
||||
})
|
||||
|
||||
MySQL.prepare('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
||||
if result then
|
||||
print(json.encode(result, {indent=true}))
|
||||
end
|
||||
|
||||
print(json.encode(response, { indent = true, sort_keys = true }))
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
const response = await MySQL.prepare('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', [
|
||||
identifier
|
||||
])
|
||||
|
||||
console.log(JSON.stringify(response))
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
**Aliases**
|
||||
|
||||
- `exports.oxmysql.prepare_async`
|
||||
|
||||
## Callback
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
MySQL.prepare('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
|
||||
identifier
|
||||
}, function(response)
|
||||
print(json.encode(response, { indent = true, sort_keys = true }))
|
||||
end)
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:prepare_async
|
||||
|
||||
local result = MySQL.prepare.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
||||
if result then
|
||||
print(json.encode(result, {indent=true}))
|
||||
end
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
|
||||
### Callback
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.prepare
|
||||
|
||||
MySQL.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => {
|
||||
if (result) {
|
||||
console.log(result.identifier, result.firstname, result.lastname)
|
||||
}
|
||||
MySQL.prepare('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', [
|
||||
identifier
|
||||
], (response) => {
|
||||
console.log(JSON.stringify(response))
|
||||
})
|
||||
```
|
||||
|
||||
### Promise
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.prepare_async
|
||||
**Aliases**
|
||||
|
||||
const result = await MySQL.prepare_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
|
||||
if (result) {
|
||||
console.log(result.identifier, result.firstname, result.lastname)
|
||||
}
|
||||
```
|
||||
- `exports.oxmysql.prepare`
|
||||
+73
-52
@@ -1,72 +1,93 @@
|
||||
---
|
||||
keywords: ['execute', 'fetchall']
|
||||
keywords: ['fetchAll', 'execute']
|
||||
title: Query
|
||||
---
|
||||
|
||||
# Query
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
Generic function that can be utilised for any query.
|
||||
When selecting data it will return all matching rows and columns, otherwise it will return field data such as insertid, affectedRows, etc.
|
||||
When selecting data, returns all matching rows and columns; otherwise, returns data like insertId, affectedRows, etc.
|
||||
|
||||
## Lua
|
||||
## Promise
|
||||
|
||||
### Callback
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:query
|
||||
-- alias: MySQL.Async.fetchAll
|
||||
-- alias: exports.ghmattimysql:execute
|
||||
local response = MySQL.query.await('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
|
||||
identifier
|
||||
})
|
||||
|
||||
MySQL.query('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
||||
if result then
|
||||
for i = 1, #result do
|
||||
local row = result[i]
|
||||
print(row.identifier, row.firstname, row.lastname)
|
||||
end
|
||||
end
|
||||
end)
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:query_async
|
||||
-- alias: MySQL.Sync.fetchAll
|
||||
-- alias: exports.ghmattimysql:executeSync
|
||||
|
||||
local result = MySQL.query.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
||||
if result then
|
||||
for i = 1, #result do
|
||||
local row = result[i]
|
||||
print(row.identifier, row.firstname, row.lastname)
|
||||
if response then
|
||||
for i = 1, #response do
|
||||
local row = response[i]
|
||||
print(row.firstname, row.lastname)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
|
||||
### Callback
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.query
|
||||
const response = await MySQL.query('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', [
|
||||
identifier
|
||||
])
|
||||
|
||||
MySQL.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => {
|
||||
if (result) {
|
||||
result.forEach((row) => {
|
||||
console.log(row.identifier, row.firstname, row.lastname)
|
||||
})
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.query_async
|
||||
|
||||
const result = await MySQL.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
|
||||
if (result) {
|
||||
result.forEach((row) => {
|
||||
if (response) {
|
||||
response.forEach((row) => {
|
||||
console.log(row.identifier, row.firstname, row.lastname)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
**Aliases**
|
||||
|
||||
- `MySQL.Sync.fetchAll`
|
||||
- `exports.ghmattimysql.execute`
|
||||
- `exports.oxmysql.query_async`
|
||||
|
||||
## Callback
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
MySQL.query('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', {
|
||||
identifier
|
||||
}, function(response)
|
||||
if response then
|
||||
for i = 1, #response do
|
||||
local row = response[i]
|
||||
print(row.firstname, row.lastname)
|
||||
end
|
||||
end
|
||||
end)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
MySQL.query('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ?', [
|
||||
identifier
|
||||
], (response) => {
|
||||
if (response) {
|
||||
response.forEach((row) => {
|
||||
console.log(row.firstname, row.lastname)
|
||||
})
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
**Aliases**
|
||||
|
||||
- `MySQL.Async.fetchAll`
|
||||
- `exports.ghmattimysql.execute`
|
||||
- `exports.oxmysql.query`
|
||||
@@ -1,53 +1,75 @@
|
||||
---
|
||||
keywords: ['scalar', 'fetchscalar']
|
||||
keywords: ['scalar', 'fetchScalar']
|
||||
title: Scalar
|
||||
---
|
||||
|
||||
# Scalar
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
Returns the first column for a single row.
|
||||
|
||||
## Lua
|
||||
## Promise
|
||||
|
||||
### Callback
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:scalar
|
||||
-- alias: MySQL.Async.fetchScalar
|
||||
-- alias: exports.ghmattimysql:scalar
|
||||
local firstName = MySQL.scalar.await('SELECT `firstname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
|
||||
identifier
|
||||
})
|
||||
|
||||
MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(firstname)
|
||||
print(firstname)
|
||||
print(firstName)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
const firstName = await MySQL.scalar('SELECT `firstname` FROM `users` WHERE `identifier` = ? LIMIT 1', [
|
||||
identifier
|
||||
])
|
||||
|
||||
console.log(firstName)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
**Aliases**
|
||||
|
||||
- `MySQL.Sync.fetchScalar`
|
||||
- `exports.ghmattimysql.scalar`
|
||||
- `exports.oxmysql.scalar_async`
|
||||
|
||||
## Callback
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
MySQL.scalar('SELECT `firstname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
|
||||
identifier
|
||||
}, function(firstName)
|
||||
print(firstName)
|
||||
end)
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:scalar_async
|
||||
-- alias: MySQL.Sync.fetchScalar
|
||||
-- alias: exports.ghmattimysql:scalarSync
|
||||
|
||||
local firstname = MySQL.scalar.await('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier})
|
||||
print(firstname)
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
|
||||
### Callback
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.scalar
|
||||
|
||||
MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], (firstname) => {
|
||||
console.log(firstname)
|
||||
MySQL.scalar('SELECT `firstname` FROM `users` WHERE `identifier` = ? LIMIT 1', [
|
||||
identifier
|
||||
], (firstName) => {
|
||||
console.log(firstName)
|
||||
})
|
||||
```
|
||||
|
||||
### Promise
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.scalar_async
|
||||
**Aliases**
|
||||
|
||||
const firstname = await MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier])
|
||||
console.log(firstname)
|
||||
```
|
||||
- `MySQL.Async.fetchScalar`
|
||||
- `exports.ghmattimysql.scalar`
|
||||
- `exports.oxmysql.scalar`
|
||||
@@ -1,53 +1,78 @@
|
||||
# Single
|
||||
---
|
||||
title: Single
|
||||
---
|
||||
|
||||
Returns the columns for a single row.
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
## Lua
|
||||
Returns all selected columns for a single row.
|
||||
|
||||
### Callback
|
||||
## Promise
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:single
|
||||
local row = MySQL.scalar.await('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
|
||||
identifier
|
||||
})
|
||||
|
||||
MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(row)
|
||||
if row then
|
||||
print(row.identifier, row.firstname, row.lastname)
|
||||
end
|
||||
if not row then return end
|
||||
|
||||
print(row.firstname, row.lastname)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
const row = await MySQL.scalar('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ? LIMIT 1', [
|
||||
identifier
|
||||
])
|
||||
|
||||
if (!row) return;
|
||||
|
||||
console.log(row.firstname, row.lastname)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
**Aliases**
|
||||
|
||||
- `exports.oxmysql.single_async`
|
||||
|
||||
## Callback
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
MySQL.scalar('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ? LIMIT 1', {
|
||||
identifier
|
||||
}, function(row)
|
||||
if not row then return end
|
||||
|
||||
print(row.firstname, row.lastname)
|
||||
end)
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:single_async
|
||||
|
||||
local row = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
||||
if row then
|
||||
print(row.identifier, row.firstname, row.lastname)
|
||||
end
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
|
||||
### Callback
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.single
|
||||
MySQL.scalar('SELECT `firstname`, `lastname` FROM `users` WHERE `identifier` = ? LIMIT 1', [
|
||||
identifier
|
||||
], (row) => {
|
||||
if (!row) return;
|
||||
|
||||
MySQL.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier],(row) => {
|
||||
if (row) {
|
||||
console.log(row.identifier, row.firstname, row.lastname)
|
||||
}
|
||||
console.log(row.firstname, row.lastname)
|
||||
})
|
||||
```
|
||||
|
||||
### Promise
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.single_async
|
||||
**Aliases**
|
||||
|
||||
const row = await MySQL.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
|
||||
if (row) {
|
||||
console.log(row.identifier, row.firstname, row.lastname)
|
||||
}
|
||||
```
|
||||
- `exports.oxmysql.single`
|
||||
@@ -1,147 +1,125 @@
|
||||
---
|
||||
keywords: ['transaction']
|
||||
title: Transaction
|
||||
---
|
||||
|
||||
# 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.
|
||||
A transaction executes multiple queries and commits them only if all succeed.
|
||||
If one fails, none of the queries are committed.
|
||||
|
||||
## Specific Format
|
||||
The return value is a boolean, which is the result of the transaction.
|
||||
|
||||
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.
|
||||
## Specific format
|
||||
|
||||
This is useful for transactions where the queries do not share many SQL variables.
|
||||
|
||||
### Lua
|
||||
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 (: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 compatibility.
|
||||
{ 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' } },
|
||||
}
|
||||
```
|
||||
|
||||
#### Callback
|
||||
## 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
|
||||
-- alias: exports.oxmysql:transaction
|
||||
-- alias: MySQL.Async.transaction
|
||||
-- alias: exports.ghmattimysql:transaction
|
||||
-- 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)
|
||||
```
|
||||
|
||||
#### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:transaction_async
|
||||
-- alias: MySQL.Sync.transaction
|
||||
-- alias: exports.ghmattimysql:transactionSync
|
||||
|
||||
local success = MySQL.transaction.await(queries)
|
||||
print(success)
|
||||
```
|
||||
|
||||
### JavaScript
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```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 compatibility.
|
||||
```
|
||||
|
||||
#### Callback
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.transaction
|
||||
|
||||
// specific
|
||||
MySQL.transaction(queries, (success) => {
|
||||
console.log(success);
|
||||
});
|
||||
console.log(success)
|
||||
})
|
||||
|
||||
// shared
|
||||
MySQL.transaction(queries, values, (success) => {
|
||||
console.log(success)
|
||||
})
|
||||
```
|
||||
|
||||
#### Promise
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.transaction_async
|
||||
**Aliases**
|
||||
|
||||
const success = await MySQL.transaction(queries);
|
||||
console.log(success);
|
||||
```
|
||||
|
||||
## 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.
|
||||
|
||||
### 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' }
|
||||
```
|
||||
|
||||
#### Callback
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:transaction
|
||||
-- alias: MySQL.Async.transaction
|
||||
|
||||
MySQL.transaction(queries, parameters, function(success)
|
||||
print(success)
|
||||
end)
|
||||
```
|
||||
|
||||
#### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:transaction_async
|
||||
-- alias: MySQL.Sync.transaction
|
||||
|
||||
local success = MySQL.transaction.await(queries, parameters)
|
||||
print(success)
|
||||
```
|
||||
|
||||
### 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' };
|
||||
```
|
||||
|
||||
#### Callback
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.transaction
|
||||
|
||||
MySQL.transaction(queries, parameters, (success) => {
|
||||
console.log(success);
|
||||
});
|
||||
```
|
||||
|
||||
#### Promise
|
||||
|
||||
```js
|
||||
// exports.oxmysql.transaction_async
|
||||
|
||||
const success = await MySQL.transaction(queries, parameters);
|
||||
console.log(success);
|
||||
```
|
||||
- `MySQL.Async.transaction`
|
||||
- `exports.ghmattimysql.transaction`
|
||||
- `exports.oxmysql.transaction`
|
||||
|
||||
## 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`.
|
||||
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,61 +1,75 @@
|
||||
---
|
||||
keywords: ['execute']
|
||||
title: Update
|
||||
---
|
||||
|
||||
# Update
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
Returns the number of affected rows by the query.
|
||||
Returns the number of rows affected by the query.
|
||||
|
||||
## Lua
|
||||
## Promise
|
||||
|
||||
### Callback
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:update
|
||||
-- alias: MySQL.Async.execute
|
||||
-- alias: exports.ghmattimysql:execute
|
||||
local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ?', {
|
||||
newName, identifier
|
||||
})
|
||||
|
||||
MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ?', {newName, playerIdentifier}, function(affectedRows)
|
||||
if affectedRows then
|
||||
print(affectedRows)
|
||||
end
|
||||
print(affectedRows)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
const affectedRows = await MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ?', [
|
||||
newName, identifier
|
||||
])
|
||||
|
||||
console.log(affectedRows)
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
**Aliases**
|
||||
|
||||
- `MySQL.Sync.execute`
|
||||
- `exports.ghmattimysql.executeSync`
|
||||
- `exports.oxmysql.update_async`
|
||||
|
||||
## Callback
|
||||
|
||||
<Tabs>
|
||||
<TabItem value="1" label="Lua">
|
||||
|
||||
```lua
|
||||
MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ?', {
|
||||
newName, identifier
|
||||
}, function(affectedRows)
|
||||
print(affectedRows)
|
||||
end)
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```lua
|
||||
-- alias: exports.oxmysql:update_async
|
||||
-- alias: MySQL.Sync.execute
|
||||
-- alias: exports.ghmattimysql:executeSync
|
||||
|
||||
local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ?', {newName, playerIdentifier})
|
||||
if affectedRows then
|
||||
print(affectedRows)
|
||||
end
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
|
||||
### Callback
|
||||
</TabItem>
|
||||
<TabItem value="2" label="JS">
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.update
|
||||
|
||||
MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], (affectedRows) => {
|
||||
if (affectedRows) {
|
||||
console.log(affectedRows);
|
||||
}
|
||||
});
|
||||
MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ?', [
|
||||
newName, identifier
|
||||
], (affectedRows) => {
|
||||
console.log(affectedRows)
|
||||
})
|
||||
```
|
||||
|
||||
### Promise
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
```js
|
||||
// alias: exports.oxmysql.update_async
|
||||
**Aliases**
|
||||
|
||||
const id = MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]);
|
||||
if (affectedRows) {
|
||||
console.log(affectedRows);
|
||||
}
|
||||
```
|
||||
- `MySQL.Async.execute`
|
||||
- `exports.ghmattimysql.execute`
|
||||
- `exports.oxmysql.update`
|
||||
Reference in new issue
Block a user