From 81223ffd4738ff72e3cc3dc3d6dc8692262c9d25 Mon Sep 17 00:00:00 2001 From: userMacieG Date: Tue, 22 Mar 2022 17:46:46 +0100 Subject: [PATCH] Updated docs for oxmysql --- docs/oxmysql/Getting Started/placeholders.md | 11 +++++++++ docs/oxmysql/Usage/index.md | 26 +++++++++++++++++++- docs/oxmysql/Usage/insert.md | 10 ++++---- docs/oxmysql/Usage/query.md | 2 +- docs/oxmysql/Usage/transaction.md | 22 +++++++++++------ docs/oxmysql/Usage/update.md | 8 +++--- 6 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 docs/oxmysql/Getting Started/placeholders.md diff --git a/docs/oxmysql/Getting Started/placeholders.md b/docs/oxmysql/Getting Started/placeholders.md new file mode 100644 index 00000000..4fdb8c88 --- /dev/null +++ b/docs/oxmysql/Getting Started/placeholders.md @@ -0,0 +1,11 @@ +# Placeholders + +This allows queries to be properly prepared and escaped; the following lines are equivalent. + +``` +"SELECT group FROM users WHERE identifier = ?", {identifier} +"SELECT group FROM users WHERE identifier = :identifier", {identifier = identifier} +"SELECT group FROM users WHERE identifier = @identifier", {['@identifier'] = identifier} +``` + +Named placeholders are deprecated and should be avoided as much as possible. diff --git a/docs/oxmysql/Usage/index.md b/docs/oxmysql/Usage/index.md index 2ce5b7a7..6a3219ae 100644 --- a/docs/oxmysql/Usage/index.md +++ b/docs/oxmysql/Usage/index.md @@ -2,4 +2,28 @@ title: Usage --- -todo - include some information about proper formatting, placeholders, bad practices, etc. +```lua +-- Lua +MySQL.query('SELECT * from users WHERE identifier = ?', {identifier}, function(result) + -- callback response + -- same as MySQL.Async.fetchAll +end) +CreateThread(function() + local result = MySQL.query.await('SELECT * from users WHERE identifier = ?', {identifier}) + -- await a promise to resolve + -- same as MySQL.Sync.fetchAll +end) +``` + +```js +// JS +exports.oxmysql.query('SELECT * from users WHERE identifier = ?', [identifier], (result) => { + // callback response +})(async () => { + const result = await exports.oxmysql.query_async('SELECT * from users WHERE identifier = ?', [identifier]); + // await a promise to resolve +})(); +exports.oxmysql.query_async('SELECT * from users WHERE identifier = ?', [identifier]).then((result) => { + // utilise .then to resolve a promise like a callback +}); +``` diff --git a/docs/oxmysql/Usage/insert.md b/docs/oxmysql/Usage/insert.md index d4e2b6ff..85fd3ad6 100644 --- a/docs/oxmysql/Usage/insert.md +++ b/docs/oxmysql/Usage/insert.md @@ -8,7 +8,7 @@ Inserts a new entry into the database and returns the insert id for the row, if ```lua -- Alias: exports.oxmysql:insert -- Alias: MySQL.Async.insert -MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName,lastName}, function(id) +MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', {playerIdentifier, firstName, lastName}, function(id) print(id) end) ``` @@ -18,7 +18,7 @@ end) -- 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}) + local id = MySQL.insert.await('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', {playerIdentifier, firstName, lastName}) print(id) end) ``` @@ -27,7 +27,7 @@ end) ### Callback ```js -exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName], function(id) { +exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName], function(id) { console.log(id) }) ``` @@ -35,8 +35,8 @@ exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALU ### Promise ```js (async () => { - const id = exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', + 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/query.md b/docs/oxmysql/Usage/query.md index 55b0ba3b..079a8479 100644 --- a/docs/oxmysql/Usage/query.md +++ b/docs/oxmysql/Usage/query.md @@ -19,7 +19,7 @@ end) ``` ### Promise ```lua --- Alias: exports.oxmysql:query +-- Alias: exports.oxmysql:query_async -- Alias: MySQL.Sync.query CreateThread(function() diff --git a/docs/oxmysql/Usage/transaction.md b/docs/oxmysql/Usage/transaction.md index 5ef24e44..567d0a44 100644 --- a/docs/oxmysql/Usage/transaction.md +++ b/docs/oxmysql/Usage/transaction.md @@ -1,6 +1,4 @@ # 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. @@ -19,12 +17,16 @@ local queries = { } -- NOTE, the 'values' tables can be named 'parameters' here for MySQL-Async compatibility. -- Async -exports.oxmysql:transaction(queries, function(result) - print(result) +-- Alias: exports.oxmysql:transaction +-- Alias: MySQL.Async.transaction +MySQL.transaction(queries, function(result) + print(result) end) -- Sync -local result = exports.oxmysql:transactionSync(queries) +-- Alias: exports.oxmysql:transaction_async +-- Alias: MySQL.Sync.transaction +local result = MySQL.transaction.await(queries) print(result) ``` @@ -59,12 +61,16 @@ local queries = { local parameters = { ['someid'] = 2, ['somename'] = 'John Doe', ['newname'] = 'John Notdoe' } -- Async -exports.oxmysql:transaction(queries, parameters, function(result) - print(result) +-- Alias: exports.oxmysql:transaction +-- Alias: MySQL.Async.transaction +MySQL.transaction(queries, parameters, function(result) + print(result) end) -- Sync -local result = exports.oxmysql:transactionSync(queries, parameters) +-- Alias: exports.oxmysql:transaction_async +-- Alias: MySQL.Sync.transaction +local result = MySQL.transaction.await(queries, parameters) print(result) ``` diff --git a/docs/oxmysql/Usage/update.md b/docs/oxmysql/Usage/update.md index f8de44bb..f6da55ae 100644 --- a/docs/oxmysql/Usage/update.md +++ b/docs/oxmysql/Usage/update.md @@ -6,9 +6,9 @@ Returns the number of affected rows by the query. ### Callback ```lua -- Alias: exports.oxmysql:update --- Alias: MySQL.Async.update +-- Alias: MySQL.Async.execute -MySQL.Async.update('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}, function(affectedRows) +MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ?', {newName, playerIdentifier}, function(affectedRows) if affectedRows then print(affectedRows) end @@ -18,10 +18,10 @@ end) ### Promise ```lua -- Alias: exports.oxmysql:update_async --- Alias: MySQL.update +-- Alias: MySQL.Sync.execute CreateThread(function() - local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}) + local affectedRows = MySQL.update.await('UPDATE users SET firstname = ? WHERE identifier = ?', {newName, playerIdentifier}) if affectedRows then print(affectedRows) end