From 7f636ad7e7df3e25ef04b8782b0f4d8f5d2f9864 Mon Sep 17 00:00:00 2001 From: N-fire <55892717+N-fire@users.noreply.github.com> Date: Wed, 2 Mar 2022 13:20:06 +0100 Subject: [PATCH 1/3] feat(mysql): start moving docs --- docs/oxmysql/Installation/1issues.md | 13 +++ docs/oxmysql/Installation/2benchmark.md | 56 ++++++++++++ docs/oxmysql/Installation/index.md | 35 +++++++- docs/oxmysql/Usage/index.md | 5 ++ docs/oxmysql/Usage/insert.md | 41 +++++++++ docs/oxmysql/Usage/prepare.md | 114 ++++++++++++++++++++++++ docs/oxmysql/Usage/query.md | 60 +++++++++++++ docs/oxmysql/Usage/scalar.md | 43 +++++++++ docs/oxmysql/Usage/single.md | 49 ++++++++++ docs/oxmysql/Usage/transaction.md | 102 +++++++++++++++++++++ docs/oxmysql/Usage/update.md | 49 ++++++++++ 11 files changed, 566 insertions(+), 1 deletion(-) create mode 100644 docs/oxmysql/Installation/1issues.md create mode 100644 docs/oxmysql/Installation/2benchmark.md create mode 100644 docs/oxmysql/Usage/index.md create mode 100644 docs/oxmysql/Usage/insert.md create mode 100644 docs/oxmysql/Usage/prepare.md create mode 100644 docs/oxmysql/Usage/query.md create mode 100644 docs/oxmysql/Usage/scalar.md create mode 100644 docs/oxmysql/Usage/single.md create mode 100644 docs/oxmysql/Usage/transaction.md create mode 100644 docs/oxmysql/Usage/update.md diff --git a/docs/oxmysql/Installation/1issues.md b/docs/oxmysql/Installation/1issues.md new file mode 100644 index 00000000..dad144a0 --- /dev/null +++ b/docs/oxmysql/Installation/1issues.md @@ -0,0 +1,13 @@ +--- +title: Common issues +--- +### Unable to establish a connection +This is usually the result of incorrect database settings or your password containing reserved characters (typically `; , / ? : @ & = + $ #`). +:::tip Solution +Ensure you have entered the correct database settings in the mysql_connection_string convar. You can try using the semicolon-separated format if your password contains reserved characters. +::: +### No such export ... in resource oxmysql +Typically the result of failing to follow instructions. +:::tip Solution +Download the latest release _build_ (not source) of oxmysql, and ensure it is starting before any resources that require it. +::: \ No newline at end of file diff --git a/docs/oxmysql/Installation/2benchmark.md b/docs/oxmysql/Installation/2benchmark.md new file mode 100644 index 00000000..7773a9c2 --- /dev/null +++ b/docs/oxmysql/Installation/2benchmark.md @@ -0,0 +1,56 @@ +--- +title: Benchmark +--- +``` +Lua: Low: 0.2167ms | Avg: 0.28788843ms | Total: 2878.8843ms (10000 queries) +JS: Low: 0.2098ms | Avg: 0.29384436ms | Total: 2938.4436ms (10000 queries) +``` +These benchmarks await the previous response rather than occuring asynchronously, and measure the total time spent executing the export. +The `MySQL.Sync.prepare` function calls `exports.oxmysql:execute_async`, as used in the JS sample. + +#### Lua (5.4) +```lua +local val = 10000 +RegisterCommand('luasync', function() + local queryTimesLocal = {} + local result + local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'}) + for i=1, val do + local time = os.nanotime() + local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'}) + queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000 + if i==1 then result = r end + end + local queryMsLow, queryMsSum = 1000, 0 + for _, v in pairs(queryTimesLocal) do + queryMsSum = queryMsSum + v + end + for _, v in pairs(queryTimesLocal) do + if v < queryMsLow then queryMsLow = v end + end + local averageQueryTime = queryMsSum / #queryTimesLocal + print(json.encode(result)) + print('Low: '.. queryMsLow ..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)') +end) + +``` + +#### JavaScript +```js +const val = 10000 +RegisterCommand('jssync', async() => { + const queryTimesLocal = []; + let result + for(let i=0; i < val; i++) { + const startTime = process.hrtime() + const r = await exports.oxmysql.execute_async('SELECT identifier from users WHERE lastname = ?', ['Linden']) + queryTimesLocal.push(process.hrtime(startTime)[1] / 1000000) + if (i === 0) result = r + } + const queryMsSum = queryTimesLocal.reduce((a, b) => a + b, 0) + const queryMsLow = queryTimesLocal.sort((a, b) => a - b)[0] + const averageQueryTime = queryMsSum / queryTimesLocal.length + console.log(result) + console.log('Low: '+ queryMsLow +'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)') +}) +``` \ No newline at end of file diff --git a/docs/oxmysql/Installation/index.md b/docs/oxmysql/Installation/index.md index c86b0797..d97b805c 100644 --- a/docs/oxmysql/Installation/index.md +++ b/docs/oxmysql/Installation/index.md @@ -1,3 +1,36 @@ --- -title: Installation +title: Getting Started --- + +## Installation +- Download the [latest build](https://github.com/overextended/oxmysql/releases/latest) of oxmysql (not the source code). +- Extract the contents of the archive to your resources folder. +- Start the resource near the top of your resources in your `server.cfg`. +- If you have a lot of streamed assets, load them first to prevent timing out the connection. + + +## Configuration +You can change the configuration settings by using convars inside your `server.cfg`. +Reference the following for an idea of how to set your connection options. +You must include one of the following lines, adjusted for your connection and database settings. +``` +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" +``` +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. +``` +set mysql_slow_query_warning 150 +set mysql_debug true +``` +### Using the UI +:::info +Before using the UI first you have to make sure you have the `mysql_ui` convar set to true: +``` +set mysql_ui true +``` +Also make sure that you have `command` ace permission access, then you should be able to use the +`mysql` command in game to open up the UI and see your query data. +::: +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). + diff --git a/docs/oxmysql/Usage/index.md b/docs/oxmysql/Usage/index.md new file mode 100644 index 00000000..2ce5b7a7 --- /dev/null +++ b/docs/oxmysql/Usage/index.md @@ -0,0 +1,5 @@ +--- +title: Usage +--- + +todo - include some information about proper formatting, placeholders, bad practices, etc. diff --git a/docs/oxmysql/Usage/insert.md b/docs/oxmysql/Usage/insert.md new file mode 100644 index 00000000..1ced34d2 --- /dev/null +++ b/docs/oxmysql/Usage/insert.md @@ -0,0 +1,41 @@ +--- +title: insert +--- +Inserts a new entry into the database and returns the insert id for the row, if valid. + +:::tip Example +Lua + +**Callback** +```lua +-- Alias: exports.oxmysql:insert +-- Alias: MySQL.Async.insert +MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName,lastName}, function(id) + print(id) +end) +``` +**Promise** +```lua +-- Alias: exports.oxmysql:insert_async +-- Alias: MySQL.Sync.insert +CreateThread(function() +local id = MySQL.insert.await('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier,firstName, lastName}) + print(id) +end) +``` +=== JavaScript" + **Callback** + ```js + exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, +firstName, lastName], function(id) { + console.log(id) + }) + ``` + **Promise** + ```js + (async () => { + const id = exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', +[playerIdentifier, firstName, lastName]) { + console.log(id) + })() + ``` \ No newline at end of file diff --git a/docs/oxmysql/Usage/prepare.md b/docs/oxmysql/Usage/prepare.md new file mode 100644 index 00000000..bea72b6c --- /dev/null +++ b/docs/oxmysql/Usage/prepare.md @@ -0,0 +1,114 @@ +--- +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 + -- Alias: exports.oxmysql:prepare + -- Alias: MySQL.Async.prepare + + MySQL.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 + -- Alias: exports.oxmysql:prepare_async + -- Alias: MySQL.Sync.prepare + + + CreateThread(function() + local result = MySQL.prepare.await('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.prepare('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.prepare_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 +``` \ No newline at end of file diff --git a/docs/oxmysql/Usage/query.md b/docs/oxmysql/Usage/query.md new file mode 100644 index 00000000..1cd28fac --- /dev/null +++ b/docs/oxmysql/Usage/query.md @@ -0,0 +1,60 @@ +--- +title: 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" + + **Callback** + ```lua + -- Alias: exports.oxmysql:query + -- Alias: MySQL.Async.query + + MySQL.query('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 + -- Alias: exports.oxmysql:query + -- Alias: MySQL.Sync.query + + CreateThread(function() + local result = MySQL.query.await('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.query('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.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { + if (result) { + result.forEach((v) => { + console.log(v.identifier, v.firstname, v.lastname) + }) + } + })() + ``` \ No newline at end of file diff --git a/docs/oxmysql/Usage/scalar.md b/docs/oxmysql/Usage/scalar.md new file mode 100644 index 00000000..b99ef712 --- /dev/null +++ b/docs/oxmysql/Usage/scalar.md @@ -0,0 +1,43 @@ +--- +title: scalar +--- +Returns the first column for a single row. + +!!! info "Example" + === "Lua" + + **Callback** + ```lua + -- Alias: exports.oxmysql:scalar + -- Alias: MySQL.Async.fetchScalar + + MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(firstname) + print(firstname) + end) + ``` + **Promise** + ```lua + -- Alias: exports.oxmysql:scalar_async + -- Alias: MySQL.Sync.fetchScalar + + CreateThread(function() + local firstname = MySQL.scalar.await('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}) + print(firstname) + end) + ``` + + === "JavaScript" + + **Callback** + ```js + exports.oxmysql.scalar('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) + })() + ``` \ No newline at end of file diff --git a/docs/oxmysql/Usage/single.md b/docs/oxmysql/Usage/single.md new file mode 100644 index 00000000..bb3389c0 --- /dev/null +++ b/docs/oxmysql/Usage/single.md @@ -0,0 +1,49 @@ +--- +title: single +--- +Returns the columns for a single row. + +!!! info "Example" + === "Lua" + + **Callback** + ```lua + -- Alias: exports.oxmysql:single + -- Alias: MySQL.Async.fetchSingle + + MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) + if result then + print(result.identifier, result.firstname, result.lastname) + end + end) + ``` + **Promise** + ```lua + -- Alias: exports.oxmysql:single_async + -- Alias: MySQL.Sync.fetchSingle + + CreateThread(function() + local result = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) + if result then + print(result.identifier, result.firstname, result.lastname) + end + end) + ``` + + === "JavaScript" + + **Callback** + ```js + exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { + if (result) + console.log(result.identifier, result.firstname, result.lastname) + }) + ``` + **Promise** + ```js + setImmediate(async () => { + const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { + if (result) + console.log(result.identifier, result.firstname, result.lastname) + }) + ``` \ No newline at end of file diff --git a/docs/oxmysql/Usage/transaction.md b/docs/oxmysql/Usage/transaction.md new file mode 100644 index 00000000..f45b5fb6 --- /dev/null +++ b/docs/oxmysql/Usage/transaction.md @@ -0,0 +1,102 @@ +--- +title: transaction +--- +todo - update formatting and information + + +# 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 +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 +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. + +-- 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 compatibility. + +// 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. + +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) +``` + +## 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 | diff --git a/docs/oxmysql/Usage/update.md b/docs/oxmysql/Usage/update.md new file mode 100644 index 00000000..4545e2fb --- /dev/null +++ b/docs/oxmysql/Usage/update.md @@ -0,0 +1,49 @@ +--- +title: update +--- +Returns the number of affected rows by the query. + +!!! info "Example" + === "Lua" + + **Callback** + ```lua + -- Alias: exports.oxmysql:update + -- Alias: MySQL.Async.update + + MySQL.Async.update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}, function(affectedRows) + if affectedRows then + print(affectedRows) + end + end) + ``` + **Promise** + ```lua + -- Alias: exports.oxmysql:update_async + -- Alias: MySQL.update + + CreateThread(function() + local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}) + if affectedRows then + print(affectedRows) + end + end) + ``` + + === "JavaScript" + + **Callback** + ```js + exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) { + if (affectedRows) + console.log(affectedRows) + }) + ``` + **Promise** + ```js + setImmediate(async () => { + const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) { + if (affectedRows) + console.log(affectedRows) + }) + ``` From b912eec843662a2cf16323131039a59296b1a477 Mon Sep 17 00:00:00 2001 From: N-fire <55892717+N-fire@users.noreply.github.com> Date: Wed, 2 Mar 2022 13:40:28 +0100 Subject: [PATCH 2/3] finish moving ox_mysql docs --- docs/oxmysql/Usage/insert.md | 32 +++++------ docs/oxmysql/Usage/prepare.md | 102 +++++++++++++++++----------------- docs/oxmysql/Usage/query.md | 99 +++++++++++++++++---------------- docs/oxmysql/Usage/scalar.md | 67 +++++++++++----------- docs/oxmysql/Usage/single.md | 79 +++++++++++++------------- docs/oxmysql/Usage/update.md | 79 +++++++++++++------------- 6 files changed, 231 insertions(+), 227 deletions(-) diff --git a/docs/oxmysql/Usage/insert.md b/docs/oxmysql/Usage/insert.md index 1ced34d2..74a6540e 100644 --- a/docs/oxmysql/Usage/insert.md +++ b/docs/oxmysql/Usage/insert.md @@ -3,7 +3,7 @@ title: insert --- Inserts a new entry into the database and returns the insert id for the row, if valid. -:::tip Example +:::info Example Lua **Callback** @@ -23,19 +23,19 @@ local id = MySQL.insert.await('INSERT INTO users (identifier, firstname, lastnam print(id) end) ``` -=== JavaScript" - **Callback** - ```js - exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, -firstName, lastName], function(id) { - console.log(id) - }) - ``` - **Promise** - ```js - (async () => { - const id = exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', +JavaScript + +**Callback** +```js +exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName], function(id) { + console.log(id) +}) +``` +**Promise** +```js +(async () => { + const id = exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName]) { - console.log(id) - })() - ``` \ No newline at end of file + console.log(id) +})() +``` \ No newline at end of file diff --git a/docs/oxmysql/Usage/prepare.md b/docs/oxmysql/Usage/prepare.md index bea72b6c..99119091 100644 --- a/docs/oxmysql/Usage/prepare.md +++ b/docs/oxmysql/Usage/prepare.md @@ -10,61 +10,61 @@ Prepare can be used to execute frequently called queries faster and accepts mult When using SELECT, the return value will match `fetchAll, fetchSingle, or fetchScalar` depending on the number of columns and rows selected. -!!! info "Example" - === "Lua" +:::info Example +Lua - **Callback** - ```lua - -- Alias: exports.oxmysql:prepare - -- Alias: MySQL.Async.prepare +**Callback** +```lua +-- Alias: exports.oxmysql:prepare +-- Alias: MySQL.Async.prepare - MySQL.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 - -- Alias: exports.oxmysql:prepare_async - -- Alias: MySQL.Sync.prepare +MySQL.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 +-- Alias: exports.oxmysql:prepare_async +-- Alias: MySQL.Sync.prepare - - CreateThread(function() - local result = MySQL.prepare.await('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) - ``` +CreateThread(function() + local result = MySQL.prepare.await('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" +JavaScript - **Callback** - ```js - exports.oxmysql.prepare('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.prepare_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { - if (result) { - result.forEach((v) => { - console.log(v.identifier, v.firstname, v.lastname) - }) - } - })() - ``` +**Callback** +```js +exports.oxmysql.prepare('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.prepare_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. @@ -111,4 +111,4 @@ ESX.SavePlayers = function(cb) end) end end -``` \ No newline at end of file +``` diff --git a/docs/oxmysql/Usage/query.md b/docs/oxmysql/Usage/query.md index 1cd28fac..885ac5ef 100644 --- a/docs/oxmysql/Usage/query.md +++ b/docs/oxmysql/Usage/query.md @@ -4,57 +4,58 @@ title: 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" +:::info Example +Lua - **Callback** - ```lua - -- Alias: exports.oxmysql:query - -- Alias: MySQL.Async.query +**Callback** +```lua +-- Alias: exports.oxmysql:query +-- Alias: MySQL.Async.query - MySQL.query('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 - -- Alias: exports.oxmysql:query - -- Alias: MySQL.Sync.query +MySQL.query('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 +-- Alias: exports.oxmysql:query +-- Alias: MySQL.Sync.query - CreateThread(function() - local result = MySQL.query.await('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) - ``` +CreateThread(function() + local result = MySQL.query.await('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" +JavaScript - **Callback** - ```js - exports.oxmysql.query('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.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { - if (result) { - result.forEach((v) => { - console.log(v.identifier, v.firstname, v.lastname) - }) - } - })() - ``` \ No newline at end of file +**Callback** +```js +exports.oxmysql.query('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.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { + if (result) { + result.forEach((v) => { + console.log(v.identifier, v.firstname, v.lastname) + }) + } +})() +``` +::: \ No newline at end of file diff --git a/docs/oxmysql/Usage/scalar.md b/docs/oxmysql/Usage/scalar.md index b99ef712..a09b742b 100644 --- a/docs/oxmysql/Usage/scalar.md +++ b/docs/oxmysql/Usage/scalar.md @@ -3,41 +3,42 @@ title: scalar --- Returns the first column for a single row. -!!! info "Example" - === "Lua" +:::info Example +Lua - **Callback** - ```lua - -- Alias: exports.oxmysql:scalar - -- Alias: MySQL.Async.fetchScalar +**Callback** +```lua +-- Alias: exports.oxmysql:scalar +-- Alias: MySQL.Async.fetchScalar - MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(firstname) - print(firstname) - end) - ``` - **Promise** - ```lua - -- Alias: exports.oxmysql:scalar_async - -- Alias: MySQL.Sync.fetchScalar +MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(firstname) + print(firstname) +end) +``` +**Promise** +```lua +-- Alias: exports.oxmysql:scalar_async +-- Alias: MySQL.Sync.fetchScalar - CreateThread(function() - local firstname = MySQL.scalar.await('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}) - print(firstname) - end) - ``` +CreateThread(function() + local firstname = MySQL.scalar.await('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}) + print(firstname) +end) +``` - === "JavaScript" +JavaScript - **Callback** - ```js - exports.oxmysql.scalar('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) - })() - ``` \ No newline at end of file +**Callback** +```js +exports.oxmysql.scalar('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) +})() +``` +::: \ No newline at end of file diff --git a/docs/oxmysql/Usage/single.md b/docs/oxmysql/Usage/single.md index bb3389c0..6ccf37be 100644 --- a/docs/oxmysql/Usage/single.md +++ b/docs/oxmysql/Usage/single.md @@ -3,47 +3,48 @@ title: single --- Returns the columns for a single row. -!!! info "Example" - === "Lua" +:::info Example +Lua - **Callback** - ```lua - -- Alias: exports.oxmysql:single - -- Alias: MySQL.Async.fetchSingle +**Callback** +```lua +-- Alias: exports.oxmysql:single +-- Alias: MySQL.Async.fetchSingle - MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) - if result then - print(result.identifier, result.firstname, result.lastname) - end - end) - ``` - **Promise** - ```lua - -- Alias: exports.oxmysql:single_async - -- Alias: MySQL.Sync.fetchSingle +MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) + if result then + print(result.identifier, result.firstname, result.lastname) + end +end) +``` +**Promise** +```lua +-- Alias: exports.oxmysql:single_async +-- Alias: MySQL.Sync.fetchSingle - CreateThread(function() - local result = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) - if result then - print(result.identifier, result.firstname, result.lastname) - end - end) - ``` +CreateThread(function() + local result = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) + if result then + print(result.identifier, result.firstname, result.lastname) + end +end) +``` - === "JavaScript" +JavaScript - **Callback** - ```js - exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { - if (result) - console.log(result.identifier, result.firstname, result.lastname) - }) - ``` - **Promise** - ```js - setImmediate(async () => { - const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { - if (result) - console.log(result.identifier, result.firstname, result.lastname) - }) - ``` \ No newline at end of file +**Callback** +```js +exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { + if (result) + console.log(result.identifier, result.firstname, result.lastname) +}) +``` +**Promise** +```js +setImmediate(async () => { + const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { + if (result) + console.log(result.identifier, result.firstname, result.lastname) +}) +``` +::: \ No newline at end of file diff --git a/docs/oxmysql/Usage/update.md b/docs/oxmysql/Usage/update.md index 4545e2fb..561b4764 100644 --- a/docs/oxmysql/Usage/update.md +++ b/docs/oxmysql/Usage/update.md @@ -3,47 +3,48 @@ title: update --- Returns the number of affected rows by the query. -!!! info "Example" - === "Lua" +:::info Example +Lua - **Callback** - ```lua - -- Alias: exports.oxmysql:update - -- Alias: MySQL.Async.update +**Callback** +```lua +-- Alias: exports.oxmysql:update +-- Alias: MySQL.Async.update - MySQL.Async.update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}, function(affectedRows) - if affectedRows then - print(affectedRows) - end - end) - ``` - **Promise** - ```lua - -- Alias: exports.oxmysql:update_async - -- Alias: MySQL.update +MySQL.Async.update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}, function(affectedRows) + if affectedRows then + print(affectedRows) + end +end) +``` +**Promise** +```lua +-- Alias: exports.oxmysql:update_async +-- Alias: MySQL.update - CreateThread(function() - local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}) - if affectedRows then - print(affectedRows) - end - end) - ``` +CreateThread(function() + local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}) + if affectedRows then + print(affectedRows) + end +end) +``` - === "JavaScript" +JavaScript - **Callback** - ```js - exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) { - if (affectedRows) - console.log(affectedRows) - }) - ``` - **Promise** - ```js - setImmediate(async () => { - const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) { - if (affectedRows) - console.log(affectedRows) - }) - ``` +**Callback** +```js +exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) { + if (affectedRows) + console.log(affectedRows) +}) +``` +**Promise** +```js +setImmediate(async () => { + const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) { + if (affectedRows) + console.log(affectedRows) +}) +``` +::: \ No newline at end of file From 0d2d82cfcee574c42a45878d61de644b5bf56e96 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Fri, 4 Mar 2022 10:08:32 +1100 Subject: [PATCH 3/3] Update mysql docs --- docs/oxmysql/Getting Started/benchmark.md | 63 +++++++++++++++++ .../index.md | 8 +-- .../1issues.md => Getting Started/issues.md} | 13 ++-- docs/oxmysql/Installation/2benchmark.md | 56 --------------- docs/oxmysql/Usage/insert.md | 21 +++--- docs/oxmysql/Usage/prepare.md | 70 +++---------------- docs/oxmysql/Usage/query.md | 18 ++--- docs/oxmysql/Usage/scalar.md | 20 +++--- docs/oxmysql/Usage/single.md | 20 +++--- docs/oxmysql/Usage/transaction.md | 16 ++--- docs/oxmysql/Usage/update.md | 20 +++--- 11 files changed, 137 insertions(+), 188 deletions(-) create mode 100644 docs/oxmysql/Getting Started/benchmark.md rename docs/oxmysql/{Installation => Getting Started}/index.md (96%) rename docs/oxmysql/{Installation/1issues.md => Getting Started/issues.md} (64%) delete mode 100644 docs/oxmysql/Installation/2benchmark.md diff --git a/docs/oxmysql/Getting Started/benchmark.md b/docs/oxmysql/Getting Started/benchmark.md new file mode 100644 index 00000000..4d56a278 --- /dev/null +++ b/docs/oxmysql/Getting Started/benchmark.md @@ -0,0 +1,63 @@ +# Benchmark + +Internal benchmark +``` +Low: 0.1222ms | Avg: 0.14188357ms | Total: 1418.8357ms (10000 queries) +``` + +Round-trip-time for exports +``` +Low: 0.1997ms | Avg: 0.25898800ms | Total: 2589.8800ms (10000 queries) +``` + +#### Lua (5.4) +```lua +local val = 10000 +RegisterCommand('luasync', function() + local queryTimesLocal = {} + local result + MySQL.prepare.await('SELECT identifier from users WHERE lastname = ?', {'Linden'}) + + for i=1, val do + local time = os.nanotime() + local r = MySQL.prepare.await('SELECT identifier from users WHERE lastname = ?', {'Linden'}) + queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000 + if i==1 then result = r end + end + + local queryMsLow, queryMsSum = 1000, 0 + + for _, v in pairs(queryTimesLocal) do queryMsSum = queryMsSum + v end + for _, v in pairs(queryTimesLocal) do + if v < queryMsLow then queryMsLow = v end + end + + local averageQueryTime = queryMsSum / #queryTimesLocal + print(json.encode(result)) + print('Low: '.. queryMsLow ..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)') +end) +``` + +#### JavaScript +```js +const val = 10000 +RegisterCommand('jssync', async() => { + const queryTimesLocal = []; + let result + await exports.oxmysql.prepare_async('SELECT identifier from users WHERE lastname = ?', ['Linden']) + + for(let i=0; i < val; i++) { + const startTime = process.hrtime() + const r = await exports.oxmysql.prepare_async('SELECT identifier from users WHERE lastname = ?', ['Linden']) + queryTimesLocal.push(process.hrtime(startTime)[1] / 1000000) + if (i === 0) result = r + } + + const queryMsSum = queryTimesLocal.reduce((a, b) => a + b, 0) + const queryMsLow = queryTimesLocal.sort((a, b) => a - b)[0] + const averageQueryTime = queryMsSum / queryTimesLocal.length + + console.log(result) + console.log('Low: '+ queryMsLow +'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)') +}) +``` \ No newline at end of file diff --git a/docs/oxmysql/Installation/index.md b/docs/oxmysql/Getting Started/index.md similarity index 96% rename from docs/oxmysql/Installation/index.md rename to docs/oxmysql/Getting Started/index.md index d97b805c..105ac370 100644 --- a/docs/oxmysql/Installation/index.md +++ b/docs/oxmysql/Getting Started/index.md @@ -1,6 +1,4 @@ ---- -title: Getting Started ---- +# Getting Started ## Installation - Download the [latest build](https://github.com/overextended/oxmysql/releases/latest) of oxmysql (not the source code). @@ -18,6 +16,9 @@ set mysql_connection_string "mysql://root:12345@localhost/es_extended?charset=ut 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. +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). + + You can also add the following convars if you require extra information when testing queries. ``` set mysql_slow_query_warning 150 @@ -32,5 +33,4 @@ set mysql_ui true Also make sure that you have `command` ace permission access, then you should be able to use the `mysql` command in game to open up the UI and see your query data. ::: -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). diff --git a/docs/oxmysql/Installation/1issues.md b/docs/oxmysql/Getting Started/issues.md similarity index 64% rename from docs/oxmysql/Installation/1issues.md rename to docs/oxmysql/Getting Started/issues.md index dad144a0..daa49c85 100644 --- a/docs/oxmysql/Installation/1issues.md +++ b/docs/oxmysql/Getting Started/issues.md @@ -1,13 +1,14 @@ ---- -title: Common issues ---- +# Common Issues + ### Unable to establish a connection -This is usually the result of incorrect database settings or your password containing reserved characters (typically `; , / ? : @ & = + $ #`). +This is usually the result of incorrect database settings or your password containing reserved characters (`; , / ? : @ & = + $ #`). :::tip Solution -Ensure you have entered the correct database settings in the mysql_connection_string convar. You can try using the semicolon-separated format if your password contains reserved characters. +Ensure you have entered the correct database settings in the mysql_connection_string convar. +You can try using the semicolon-separated format if your password contains reserved characters. ::: + ### No such export ... in resource oxmysql Typically the result of failing to follow instructions. :::tip Solution Download the latest release _build_ (not source) of oxmysql, and ensure it is starting before any resources that require it. -::: \ No newline at end of file +::: diff --git a/docs/oxmysql/Installation/2benchmark.md b/docs/oxmysql/Installation/2benchmark.md deleted file mode 100644 index 7773a9c2..00000000 --- a/docs/oxmysql/Installation/2benchmark.md +++ /dev/null @@ -1,56 +0,0 @@ ---- -title: Benchmark ---- -``` -Lua: Low: 0.2167ms | Avg: 0.28788843ms | Total: 2878.8843ms (10000 queries) -JS: Low: 0.2098ms | Avg: 0.29384436ms | Total: 2938.4436ms (10000 queries) -``` -These benchmarks await the previous response rather than occuring asynchronously, and measure the total time spent executing the export. -The `MySQL.Sync.prepare` function calls `exports.oxmysql:execute_async`, as used in the JS sample. - -#### Lua (5.4) -```lua -local val = 10000 -RegisterCommand('luasync', function() - local queryTimesLocal = {} - local result - local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'}) - for i=1, val do - local time = os.nanotime() - local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'}) - queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000 - if i==1 then result = r end - end - local queryMsLow, queryMsSum = 1000, 0 - for _, v in pairs(queryTimesLocal) do - queryMsSum = queryMsSum + v - end - for _, v in pairs(queryTimesLocal) do - if v < queryMsLow then queryMsLow = v end - end - local averageQueryTime = queryMsSum / #queryTimesLocal - print(json.encode(result)) - print('Low: '.. queryMsLow ..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)') -end) - -``` - -#### JavaScript -```js -const val = 10000 -RegisterCommand('jssync', async() => { - const queryTimesLocal = []; - let result - for(let i=0; i < val; i++) { - const startTime = process.hrtime() - const r = await exports.oxmysql.execute_async('SELECT identifier from users WHERE lastname = ?', ['Linden']) - queryTimesLocal.push(process.hrtime(startTime)[1] / 1000000) - if (i === 0) result = r - } - const queryMsSum = queryTimesLocal.reduce((a, b) => a + b, 0) - const queryMsLow = queryTimesLocal.sort((a, b) => a - b)[0] - const averageQueryTime = queryMsSum / queryTimesLocal.length - console.log(result) - console.log('Low: '+ queryMsLow +'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)') -}) -``` \ No newline at end of file diff --git a/docs/oxmysql/Usage/insert.md b/docs/oxmysql/Usage/insert.md index 74a6540e..d4e2b6ff 100644 --- a/docs/oxmysql/Usage/insert.md +++ b/docs/oxmysql/Usage/insert.md @@ -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 (?, ?, ?) ', diff --git a/docs/oxmysql/Usage/prepare.md b/docs/oxmysql/Usage/prepare.md index 99119091..e51f984e 100644 --- a/docs/oxmysql/Usage/prepare.md +++ b/docs/oxmysql/Usage/prepare.md @@ -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 -``` diff --git a/docs/oxmysql/Usage/query.md b/docs/oxmysql/Usage/query.md index 885ac5ef..55b0ba3b 100644 --- a/docs/oxmysql/Usage/query.md +++ b/docs/oxmysql/Usage/query.md @@ -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 } })() ``` -::: \ No newline at end of file diff --git a/docs/oxmysql/Usage/scalar.md b/docs/oxmysql/Usage/scalar.md index a09b742b..91c936ab 100644 --- a/docs/oxmysql/Usage/scalar.md +++ b/docs/oxmysql/Usage/scalar.md @@ -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) })() ``` -::: \ No newline at end of file diff --git a/docs/oxmysql/Usage/single.md b/docs/oxmysql/Usage/single.md index 6ccf37be..c7c0383d 100644 --- a/docs/oxmysql/Usage/single.md +++ b/docs/oxmysql/Usage/single.md @@ -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) }) ``` -::: \ No newline at end of file diff --git a/docs/oxmysql/Usage/transaction.md b/docs/oxmysql/Usage/transaction.md index f45b5fb6..5ef24e44 100644 --- a/docs/oxmysql/Usage/transaction.md +++ b/docs/oxmysql/Usage/transaction.md @@ -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)', diff --git a/docs/oxmysql/Usage/update.md b/docs/oxmysql/Usage/update.md index 561b4764..f8de44bb 100644 --- a/docs/oxmysql/Usage/update.md +++ b/docs/oxmysql/Usage/update.md @@ -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) }) ``` -::: \ No newline at end of file