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] 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