mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-04 14:38:50 +00:00
update functions for 1.9.0, finally write prepare and common issues
This commit is contained in:
1 parent
c07dfc085c
commit
40dee21923
10 files changed
+206
-133
No files matched your search
+16
-40
@@ -17,44 +17,22 @@ You must include one of the following lines, adjusted for your connection and da
|
|||||||
set mysql_connection_string "mysql://root:12345@localhost/es_extended?charset=utf8mb4"
|
set mysql_connection_string "mysql://root:12345@localhost/es_extended?charset=utf8mb4"
|
||||||
set mysql_connection_string "user=root;database=es_extended;password=12345;charset=utf8mb4"
|
set mysql_connection_string "user=root;database=es_extended;password=12345;charset=utf8mb4"
|
||||||
```
|
```
|
||||||
|
Certain special characters are reserved or blocked and may cause issues when used in your password.
|
||||||
You can also add the following convars if you require extra information when testing queries.
|
You can also add the following convars if you require extra information when testing queries.
|
||||||
```
|
```
|
||||||
set mysql_slow_query_warning 100
|
set mysql_slow_query_warning 150
|
||||||
set mysql_debug true
|
set mysql_debug true
|
||||||
```
|
```
|
||||||
For more optional settings (such as multiple statements) you can reference [pool.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Pool.d.ts#L10) and [connection.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Connection.d.ts#L8).
|
For more optional settings (such as multiple statements) you can reference [pool.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Pool.d.ts#L10) and [connection.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Connection.d.ts#L8).
|
||||||
|
|
||||||
|
|
||||||
## Sync vs. async
|
|
||||||
Asychronous behaviour does not _truly_ exist in any of the scripting runtimes utilised, with tasks being performed as part of a queue each tick.
|
|
||||||
Two variants of exports are provided for users, and their names describe their behaviour as part of the "thread" being executed.
|
|
||||||
|
|
||||||
The following export is "asynchronous" and will execute in order, however the result will be delayed until a result is returned rather than pausing execution of following code.
|
|
||||||
```lua
|
|
||||||
print('This print will display first')
|
|
||||||
exports.oxmysql:scalar('SELECT group FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
|
||||||
print('Group: '..result)
|
|
||||||
end)
|
|
||||||
print('This print will display second')
|
|
||||||
```
|
|
||||||
|
|
||||||
Conversely, the following export will halt the active thread and wait for it to resolve before continuing.
|
|
||||||
Furthermore, the result will be returned inline (left-hand assignment) rather than being locally scoped to the callback function.
|
|
||||||
```lua
|
|
||||||
print('This print will display first')
|
|
||||||
local result = exports.oxmysql:scalarSync('SELECT group FROM users WHERE identifier = ?', {playerIdentifier})
|
|
||||||
print('Group: '..result)
|
|
||||||
print('This print will display third')
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## Benchmark
|
## Benchmark
|
||||||
```
|
```
|
||||||
Lua: Low: 0.2955ms | High: 16.7566ms | Avg: 0.36956378ms | Total: 3695.6378ms (10000 queries)
|
Lua: Low: 0.2167ms | Avg: 0.28788843ms | Total: 2878.8843ms (10000 queries)
|
||||||
JS: Low: 0.2831ms | High: 5.1899ms | Avg: 0.33495788ms | Total: 3349.5788ms (10000 queries)
|
JS: Low: 0.2098ms | Avg: 0.29384436ms | Total: 2938.4436ms (10000 queries)
|
||||||
```
|
```
|
||||||
These benchmark _exports_ rather than actual query speed, resulting in extra delays due to serialisation and overhead. Lua generally falls _slightly_ behind, but the amount is negligible.
|
These benchmarks await the previous response rather than occuring asynchronously, and measure the total time spent executing the export.
|
||||||
Furthermore we utilise the _sync_ variants for this, so a query will only execute once the previous one has returned.
|
The `MySQL.Sync.prepare` function calls `exports.oxmysql:execute_async`, as used in the JS sample.
|
||||||
|
|
||||||
#### Lua (5.4)
|
#### Lua (5.4)
|
||||||
```lua
|
```lua
|
||||||
@@ -62,26 +40,25 @@ local val = 10000
|
|||||||
RegisterCommand('luasync', function()
|
RegisterCommand('luasync', function()
|
||||||
local queryTimesLocal = {}
|
local queryTimesLocal = {}
|
||||||
local result
|
local result
|
||||||
|
local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'})
|
||||||
for i=1, val do
|
for i=1, val do
|
||||||
local time = os.nanotime()
|
local time = os.nanotime()
|
||||||
local r = exports.oxmysql:scalarSync('SELECT * from users')
|
local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'})
|
||||||
queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000
|
queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000
|
||||||
if i==1 then result = r end
|
if i==1 then result = r end
|
||||||
end
|
end
|
||||||
local queryMsLow, queryMsHigh, queryMsSum = 1000, 0, 0
|
local queryMsLow, queryMsSum = 1000, 0
|
||||||
for _, v in pairs(queryTimesLocal) do
|
for _, v in pairs(queryTimesLocal) do
|
||||||
queryMsSum = queryMsSum + v
|
queryMsSum = queryMsSum + v
|
||||||
end
|
end
|
||||||
for _, v in pairs(queryTimesLocal) do
|
|
||||||
if v > queryMsHigh then queryMsHigh = v end
|
|
||||||
end
|
|
||||||
for _, v in pairs(queryTimesLocal) do
|
for _, v in pairs(queryTimesLocal) do
|
||||||
if v < queryMsLow then queryMsLow = v end
|
if v < queryMsLow then queryMsLow = v end
|
||||||
end
|
end
|
||||||
local averageQueryTime = queryMsSum / #queryTimesLocal
|
local averageQueryTime = queryMsSum / #queryTimesLocal
|
||||||
print(json.encode(result))
|
print(json.encode(result))
|
||||||
print('Low: '.. queryMsLow ..'ms | High: '..queryMsHigh..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)')
|
print('Low: '.. queryMsLow ..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### JavaScript
|
#### JavaScript
|
||||||
@@ -91,16 +68,15 @@ RegisterCommand('jssync', async() => {
|
|||||||
const queryTimesLocal = [];
|
const queryTimesLocal = [];
|
||||||
let result
|
let result
|
||||||
for(let i=0; i < val; i++) {
|
for(let i=0; i < val; i++) {
|
||||||
const startTime = process.hrtime.bigint()
|
const startTime = process.hrtime()
|
||||||
const r = await exports.oxmysql.scalarSync('SELECT * from users')
|
const r = await exports.oxmysql.execute_async('SELECT identifier from users WHERE lastname = ?', ['Linden'])
|
||||||
queryTimesLocal.push(Number(process.hrtime.bigint() - startTime) / 1000000)
|
queryTimesLocal.push(process.hrtime(startTime)[1] / 1000000)
|
||||||
if (i === 0) result = 1
|
if (i === 0) result = r
|
||||||
}
|
}
|
||||||
const queryMsSum = queryTimesLocal.reduce((a, b) => a + b, 0)
|
const queryMsSum = queryTimesLocal.reduce((a, b) => a + b, 0)
|
||||||
const queryMsHigh = queryTimesLocal.sort((a, b) => b - a)[0]
|
|
||||||
const queryMsLow = queryTimesLocal.sort((a, b) => a - b)[0]
|
const queryMsLow = queryTimesLocal.sort((a, b) => a - b)[0]
|
||||||
const averageQueryTime = queryMsSum / queryTimesLocal.length
|
const averageQueryTime = queryMsSum / queryTimesLocal.length
|
||||||
console.log(result)
|
console.log(result)
|
||||||
console.log('Low: '+ queryMsLow +'ms | High: '+queryMsHigh+'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)')
|
console.log('Low: '+ queryMsLow +'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)')
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
+8
-3
@@ -1,7 +1,12 @@
|
|||||||
---
|
---
|
||||||
title: Common issues
|
title: Common issues
|
||||||
---
|
---
|
||||||
### Placeholder
|
### Unable to establish a connection [ER_ACCESS_DENIED_ERROR]
|
||||||
Integer faucibus justo neque. Vivamus vitae posuere tellus. Pellentesque aliquet semper nisi, sit amet pulvinar ligula viverra facilisis. Morbi lacus lacus, finibus quis hendrerit at, gravida quis arcu.
|
This is usually the result of incorrect database settings or your password containing reserved characters (typically `; , / ? : @ & = + $ #`).
|
||||||
!!! check "Solution"
|
!!! check "Solution"
|
||||||
Proin imperdiet ipsum nisi, et ullamcorper tellus gravida quis. Proin at ipsum et tortor bibendum gravida.
|
Ensure you have entered the correct database settings in the mysql_connection_string convar. You can try using the comma-separated format if your password contains reserved characters.
|
||||||
|
|
||||||
|
### No such export ... in resource oxmysql
|
||||||
|
Typically the result of failing to follow instructions.
|
||||||
|
!!! check "Solution"
|
||||||
|
Download the latest release _build_ (not source) of oxmysql, and ensure it is starting before any resources that require it.
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
---
|
---
|
||||||
title: Execute
|
title: fetchAll
|
||||||
---
|
---
|
||||||
Generic function that can be utilised for any query, synonymous with fetch.
|
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 it will return all matching rows and columns, otherwise it will return field data such as insertid, affectedRows, etc.
|
||||||
|
|
||||||
!!! info "Example"
|
!!! info "Example"
|
||||||
=== "Lua"
|
=== "Lua"
|
||||||
**Async**
|
**Callback**
|
||||||
```lua
|
```lua
|
||||||
exports.oxmysql:execute('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
MySQL.Async.fetchAll('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
||||||
if result then
|
if result then
|
||||||
for _, v in pairs(result) do
|
for _, v in pairs(result) do
|
||||||
print(v.identifier, v.firstname, v.lastname)
|
print(v.identifier, v.firstname, v.lastname)
|
||||||
@@ -16,10 +16,10 @@ When selecting data it will return all matching rows and columns, otherwise it w
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```lua
|
```lua
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
local result = exports.oxmysql:executeSync('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
local result = MySQL.Sync.fetchAll('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
||||||
if result then
|
if result then
|
||||||
for _, v in pairs(result) do
|
for _, v in pairs(result) do
|
||||||
print(v.identifier, v.firstname, v.lastname)
|
print(v.identifier, v.firstname, v.lastname)
|
||||||
@@ -28,9 +28,9 @@ When selecting data it will return all matching rows and columns, otherwise it w
|
|||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
**Async**
|
**Callback**
|
||||||
```js
|
```js
|
||||||
exports.oxmysql.execute('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
|
exports.oxmysql.query_callback('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
|
||||||
if (result) {
|
if (result) {
|
||||||
result.forEach((v) => {
|
result.forEach((v) => {
|
||||||
console.log(v.identifier, v.firstname, v.lastname)
|
console.log(v.identifier, v.firstname, v.lastname)
|
||||||
@@ -38,14 +38,14 @@ When selecting data it will return all matching rows and columns, otherwise it w
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```js
|
```js
|
||||||
setImmediate(async () => {
|
(async () => {
|
||||||
const result = await exports.oxmysql.executeSync('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
|
const result = await exports.oxmysql.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
|
||||||
if (result) {
|
if (result) {
|
||||||
result.forEach((v) => {
|
result.forEach((v) => {
|
||||||
console.log(v.identifier, v.firstname, v.lastname)
|
console.log(v.identifier, v.firstname, v.lastname)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})()
|
||||||
```
|
```
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
---
|
||||||
|
title: fetchScalar
|
||||||
|
---
|
||||||
|
Returns the first column for a single row.
|
||||||
|
|
||||||
|
!!! info "Example"
|
||||||
|
=== "Lua"
|
||||||
|
**Callback**
|
||||||
|
```lua
|
||||||
|
MySQL.Async.fetchScalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(firstname)
|
||||||
|
print(firstname)
|
||||||
|
end)
|
||||||
|
```
|
||||||
|
**Promise**
|
||||||
|
```lua
|
||||||
|
CreateThread(function()
|
||||||
|
local firstname = MySQL.Async.fetchScalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier})
|
||||||
|
print(firstname)
|
||||||
|
end)
|
||||||
|
```
|
||||||
|
=== "JavaScript"
|
||||||
|
**Callback**
|
||||||
|
```js
|
||||||
|
exports.oxmysql.scalar_callback('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], function(firstname) {
|
||||||
|
console.log(firstname)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
**Promise**
|
||||||
|
```js
|
||||||
|
(async () => {
|
||||||
|
const firstname = await exports.oxmysql.scalar_async('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier]) {
|
||||||
|
console.log(firstname)
|
||||||
|
})()
|
||||||
|
```
|
||||||
@@ -1,39 +1,39 @@
|
|||||||
---
|
---
|
||||||
title: Single
|
title: fetchSingle
|
||||||
---
|
---
|
||||||
Returns the columns for a single row.
|
Returns the columns for a single row.
|
||||||
|
|
||||||
!!! info "Example"
|
!!! info "Example"
|
||||||
=== "Lua"
|
=== "Lua"
|
||||||
**Async**
|
**Callback**
|
||||||
```lua
|
```lua
|
||||||
exports.oxmysql:single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
MySQL.Async.fetchSingle('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
||||||
if result then
|
if result then
|
||||||
print(result.identifier, result.firstname, result.lastname)
|
print(result.identifier, result.firstname, result.lastname)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```lua
|
```lua
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
local result = exports.oxmysql:singleSync('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
local result = MySQL.Sync.fetchSingle('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
||||||
if result then
|
if result then
|
||||||
print(result.identifier, result.firstname, result.lastname)
|
print(result.identifier, result.firstname, result.lastname)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
**Async**
|
**Callback**
|
||||||
```js
|
```js
|
||||||
exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
|
exports.oxmysql.single_callback('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
|
||||||
if (result)
|
if (result)
|
||||||
console.log(result.identifier, result.firstname, result.lastname)
|
console.log(result.identifier, result.firstname, result.lastname)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```js
|
```js
|
||||||
setImmediate(async () => {
|
setImmediate(async () => {
|
||||||
const result = await exports.oxmysql.singleSync('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
|
const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
|
||||||
if (result)
|
if (result)
|
||||||
console.log(result.identifier, result.firstname, result.lastname)
|
console.log(result.identifier, result.firstname, result.lastname)
|
||||||
})
|
})
|
||||||
+11
-17
@@ -1,40 +1,34 @@
|
|||||||
---
|
---
|
||||||
title: Insert
|
title: insert
|
||||||
---
|
---
|
||||||
Inserts a new entry into the database and returns the insert id for the row, if valid.
|
Inserts a new entry into the database and returns the insert id for the row, if valid.
|
||||||
|
|
||||||
!!! info "Example"
|
!!! info "Example"
|
||||||
=== "Lua"
|
=== "Lua"
|
||||||
**Async**
|
**Callback**
|
||||||
```lua
|
```lua
|
||||||
exports.oxmysql:insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName, lastName}, function(id)
|
MySQL.Async.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName, lastName}, function(id)
|
||||||
if id then
|
|
||||||
print(id)
|
print(id)
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```lua
|
```lua
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
local id = exports.oxmysql:insertSync('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName, lastName})
|
local id = MySQL.Sync.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName, lastName})
|
||||||
if id then
|
|
||||||
print(id)
|
print(id)
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
**Async**
|
**Callback**
|
||||||
```js
|
```js
|
||||||
exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName], function(id) {
|
exports.oxmysql.insert_callback('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName], function(id) {
|
||||||
if (id)
|
|
||||||
console.log(id)
|
console.log(id)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```js
|
```js
|
||||||
setImmediate(async () => {
|
(async () => {
|
||||||
const id = exports.oxmysql.insertSync('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName]) {
|
const id = exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName]) {
|
||||||
if (id)
|
|
||||||
console.log(id)
|
console.log(id)
|
||||||
})
|
})()
|
||||||
```
|
```
|
||||||
+103
-1
@@ -1 +1,103 @@
|
|||||||
TODO
|
---
|
||||||
|
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.
|
||||||
|
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
|
||||||
|
|
||||||
|
When using SELECT, the return value will match `fetchAll, fetchSingle, or fetchScalar` depending on the number of columns and rows selected.
|
||||||
|
|
||||||
|
!!! info "Example"
|
||||||
|
=== "Lua"
|
||||||
|
**Callback**
|
||||||
|
```lua
|
||||||
|
MySQL.Async.prepare('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
||||||
|
if result then
|
||||||
|
for _, v in pairs(result) do
|
||||||
|
print(v.identifier, v.firstname, v.lastname)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
```
|
||||||
|
**Promise**
|
||||||
|
```lua
|
||||||
|
CreateThread(function()
|
||||||
|
local result = MySQL.Sync.prepare('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
|
||||||
|
if result then
|
||||||
|
for _, v in pairs(result) do
|
||||||
|
print(v.identifier, v.firstname, v.lastname)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
```
|
||||||
|
=== "JavaScript"
|
||||||
|
**Callback**
|
||||||
|
```js
|
||||||
|
exports.oxmysql.execute_callback('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
|
||||||
|
if (result) {
|
||||||
|
result.forEach((v) => {
|
||||||
|
console.log(v.identifier, v.firstname, v.lastname)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
**Promise**
|
||||||
|
```js
|
||||||
|
(async () => {
|
||||||
|
const result = await exports.oxmysql.execute_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
|
||||||
|
if (result) {
|
||||||
|
result.forEach((v) => {
|
||||||
|
console.log(v.identifier, v.firstname, v.lastname)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
```
|
||||||
|
|
||||||
|
As mentioned above, you can utilise an array of parameters for a transaction-like query without the rollback; reducing the number of exports being performed reduces strain on the server. Included below is some examples of using prepare in ESX Legacy.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
ESX.SavePlayer = function(xPlayer, cb)
|
||||||
|
MySQL.Async.prepare("UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ? WHERE `identifier` = ?", {{
|
||||||
|
json.encode(xPlayer.getAccounts(true)),
|
||||||
|
xPlayer.job.name,
|
||||||
|
xPlayer.job.grade,
|
||||||
|
xPlayer.group,
|
||||||
|
json.encode(xPlayer.getCoords()),
|
||||||
|
json.encode(xPlayer.getInventory(true)),
|
||||||
|
xPlayer.identifier
|
||||||
|
}}, function(affectedRows)
|
||||||
|
if affectedRows == 1 then
|
||||||
|
print(('[^2INFO^7] Saved player ^5"%s^7"'):format(xPlayer.name))
|
||||||
|
end
|
||||||
|
if cb then cb() end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
ESX.SavePlayers = function(cb)
|
||||||
|
local xPlayers = ESX.GetExtendedPlayers()
|
||||||
|
local count = #xPlayers
|
||||||
|
if count > 0 then
|
||||||
|
local parameters = {}
|
||||||
|
for i=1, count do
|
||||||
|
local xPlayer = xPlayers[i]
|
||||||
|
parameters[#parameters+1] = {
|
||||||
|
json.encode(xPlayer.getAccounts(true)),
|
||||||
|
xPlayer.job.name,
|
||||||
|
xPlayer.job.grade,
|
||||||
|
xPlayer.group,
|
||||||
|
json.encode(xPlayer.getCoords()),
|
||||||
|
json.encode(xPlayer.getInventory(true)),
|
||||||
|
xPlayer.identifier
|
||||||
|
}
|
||||||
|
end
|
||||||
|
MySQL.Async.prepare("UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ? WHERE `identifier` = ?", parameters,
|
||||||
|
function(results)
|
||||||
|
if results then
|
||||||
|
if type(cb) == 'function' then cb() else print(('[^2INFO^7] Saved %s %s'):format(count, count > 1 and 'players' or 'player') end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
---
|
|
||||||
title: Scalar
|
|
||||||
---
|
|
||||||
Returns the first column for a single row.
|
|
||||||
|
|
||||||
!!! info "Example"
|
|
||||||
=== "Lua"
|
|
||||||
**Async**
|
|
||||||
```lua
|
|
||||||
exports.oxmysql:scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
|
||||||
if result then
|
|
||||||
print(result.firstname)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
**Sync**
|
|
||||||
```lua
|
|
||||||
CreateThread(function()
|
|
||||||
local result = exports.oxmysql:scalarSync('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier})
|
|
||||||
if result then
|
|
||||||
print(result.firstname)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
```
|
|
||||||
=== "JavaScript"
|
|
||||||
**Async**
|
|
||||||
```js
|
|
||||||
exports.oxmysql.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
|
|
||||||
if (result)
|
|
||||||
console.log(result.firstname)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
**Sync**
|
|
||||||
```js
|
|
||||||
setImmediate(async () => {
|
|
||||||
const result = await exports.oxmysql.scalarSync('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier]) {
|
|
||||||
if (result)
|
|
||||||
console.log(result.firstname)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
TODO: update information and formatting
|
TODO: update information and formatting
|
||||||
|
|
||||||
|
---
|
||||||
|
title: transaction
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+10
-10
@@ -1,39 +1,39 @@
|
|||||||
---
|
---
|
||||||
title: Update
|
title: update
|
||||||
---
|
---
|
||||||
Updates an entry in the database and returns the number of affected rows.
|
Returns the number of affected rows when executing an UPDATE or DELETE query.
|
||||||
|
|
||||||
!!! info "Example"
|
!!! info "Example"
|
||||||
=== "Lua"
|
=== "Lua"
|
||||||
**Async**
|
**Callback**
|
||||||
```lua
|
```lua
|
||||||
exports.oxmysql:update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}, function(affectedRows)
|
MySQL.Async.update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}, function(affectedRows)
|
||||||
if affectedRows then
|
if affectedRows then
|
||||||
print(affectedRows)
|
print(affectedRows)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```lua
|
```lua
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
local id = exports.oxmysql:updateSync('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier})
|
local id = MySQL.Sync.update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier})
|
||||||
if affectedRows then
|
if affectedRows then
|
||||||
print(affectedRows)
|
print(affectedRows)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
**Async**
|
**Callback**
|
||||||
```js
|
```js
|
||||||
exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) {
|
exports.oxmysql.update_callback('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) {
|
||||||
if (affectedRows)
|
if (affectedRows)
|
||||||
console.log(affectedRows)
|
console.log(affectedRows)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
**Sync**
|
**Promise**
|
||||||
```js
|
```js
|
||||||
setImmediate(async () => {
|
setImmediate(async () => {
|
||||||
const id = exports.oxmysql.updateSync('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) {
|
const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) {
|
||||||
if (affectedRows)
|
if (affectedRows)
|
||||||
console.log(affectedRows)
|
console.log(affectedRows)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user