Update mysql docs

This commit is contained in:
Linden committed 2022-03-04 10:08:32 +11:00
1 parent b912eec843
commit 0d2d82cfce
11 files changed
+137 -188

No files matched your search

+11 -10
View File
@@ -1,12 +1,10 @@
---
title: insert
---
# Insert
Inserts a new entry into the database and returns the insert id for the row, if valid.
:::info Example
Lua
## Lua
**Callback**
### Callback
```lua
-- Alias: exports.oxmysql:insert
-- Alias: MySQL.Async.insert
@@ -14,7 +12,8 @@ MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?,
print(id)
end)
```
**Promise**
### Promise
```lua
-- Alias: exports.oxmysql:insert_async
-- Alias: MySQL.Sync.insert
@@ -23,15 +22,17 @@ local id = MySQL.insert.await('INSERT INTO users (identifier, firstname, lastnam
print(id)
end)
```
JavaScript
**Callback**
## JavaScript
### Callback
```js
exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName], function(id) {
console.log(id)
})
```
**Promise**
### Promise
```js
(async () => {
const id = exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ',
+11 -59
View File
@@ -1,6 +1,5 @@
---
title: prepare
---
# 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.
@@ -8,12 +7,11 @@ Prepare can be used to execute frequently called queries faster and accepts mult
- 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.
When using SELECT, the return value will match `query, single, or scalar` depending on the number of columns and rows selected.
:::info Example
Lua
## Lua
**Callback**
### Callback
```lua
-- Alias: exports.oxmysql:prepare
-- Alias: MySQL.Async.prepare
@@ -26,7 +24,8 @@ MySQL.prepare('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, fu
end
end)
```
**Promise**
### Promise
```lua
-- Alias: exports.oxmysql:prepare_async
-- Alias: MySQL.Sync.prepare
@@ -41,9 +40,9 @@ CreateThread(function()
end)
```
JavaScript
## JavaScript
**Callback**
### Callback
```js
exports.oxmysql.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
if (result) {
@@ -53,7 +52,8 @@ exports.oxmysql.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdent
}
})
```
**Promise**
### Promise
```js
(async () => {
const result = await exports.oxmysql.prepare_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
@@ -64,51 +64,3 @@ exports.oxmysql.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdent
}
})()
```
:::
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
```
+7 -11
View File
@@ -1,13 +1,10 @@
---
title: query
---
# Query
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.
:::info Example
Lua
## Lua
**Callback**
### Callback
```lua
-- Alias: exports.oxmysql:query
-- Alias: MySQL.Async.query
@@ -20,7 +17,7 @@ MySQL.query('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, func
end
end)
```
**Promise**
### Promise
```lua
-- Alias: exports.oxmysql:query
-- Alias: MySQL.Sync.query
@@ -35,9 +32,9 @@ CreateThread(function()
end)
```
JavaScript
## JavaScript
**Callback**
### Callback
```js
exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
if (result) {
@@ -47,7 +44,7 @@ exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', [playerIdentif
}
})
```
**Promise**
### Promise
```js
(async () => {
const result = await exports.oxmysql.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
@@ -58,4 +55,3 @@ exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', [playerIdentif
}
})()
```
:::
+9 -11
View File
@@ -1,12 +1,9 @@
---
title: scalar
---
# Scalar
Returns the first column for a single row.
:::info Example
Lua
## Lua
**Callback**
### Callback
```lua
-- Alias: exports.oxmysql:scalar
-- Alias: MySQL.Async.fetchScalar
@@ -15,7 +12,8 @@ MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifi
print(firstname)
end)
```
**Promise**
### Promise
```lua
-- Alias: exports.oxmysql:scalar_async
-- Alias: MySQL.Sync.fetchScalar
@@ -26,19 +24,19 @@ CreateThread(function()
end)
```
JavaScript
## JavaScript
**Callback**
### Callback
```js
exports.oxmysql.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], function(firstname) {
console.log(firstname)
})
```
**Promise**
### Promise
```js
(async () => {
const firstname = await exports.oxmysql.scalar_async('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier]) {
console.log(firstname)
})()
```
:::
+9 -11
View File
@@ -1,12 +1,9 @@
---
title: single
---
# Single
Returns the columns for a single row.
:::info Example
Lua
## Lua
**Callback**
### Callback
```lua
-- Alias: exports.oxmysql:single
-- Alias: MySQL.Async.fetchSingle
@@ -17,7 +14,8 @@ MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, fun
end
end)
```
**Promise**
### Promise
```lua
-- Alias: exports.oxmysql:single_async
-- Alias: MySQL.Sync.fetchSingle
@@ -30,16 +28,17 @@ CreateThread(function()
end)
```
JavaScript
## JavaScript
**Callback**
### Callback
```js
exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) {
if (result)
console.log(result.identifier, result.firstname, result.lastname)
})
```
**Promise**
### Promise
```js
setImmediate(async () => {
const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) {
@@ -47,4 +46,3 @@ setImmediate(async () => {
console.log(result.identifier, result.firstname, result.lastname)
})
```
:::
+7 -9
View File
@@ -1,19 +1,17 @@
---
title: transaction
---
# Transaction
todo - update formatting and information
# Transactions
## Transactions
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
### 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.
### Usage
Lua
#### Lua
```lua
local queries = {
{ query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 1 } },
@@ -30,7 +28,7 @@ local result = exports.oxmysql:transactionSync(queries)
print(result)
```
JavaScript
#### JavaScript
```js
const queries = [
{ query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { someid = 1 } },
@@ -51,7 +49,7 @@ console.log(result)
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
```lua
local queries = {
'INSERT INTO `test` (id, name) VALUES (:someid, :somename)',
@@ -70,7 +68,7 @@ local result = exports.oxmysql:transactionSync(queries, parameters)
print(result)
```
JavaScript
#### JavaScript
```js
const queries = [
'INSERT INTO `test` (id, name) VALUES (:someid, :somename)',
+9 -11
View File
@@ -1,12 +1,9 @@
---
title: update
---
# Update
Returns the number of affected rows by the query.
:::info Example
Lua
## Lua
**Callback**
### Callback
```lua
-- Alias: exports.oxmysql:update
-- Alias: MySQL.Async.update
@@ -17,7 +14,8 @@ MySQL.Async.update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newN
end
end)
```
**Promise**
### Promise
```lua
-- Alias: exports.oxmysql:update_async
-- Alias: MySQL.update
@@ -30,16 +28,17 @@ CreateThread(function()
end)
```
JavaScript
## JavaScript
**Callback**
### Callback
```js
exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) {
if (affectedRows)
console.log(affectedRows)
})
```
**Promise**
### Promise
```js
setImmediate(async () => {
const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) {
@@ -47,4 +46,3 @@ setImmediate(async () => {
console.log(affectedRows)
})
```
:::