improve oxmysql examples

This commit is contained in:
Linden committed 2022-07-07 15:02:15 +10:00
1 parent 9fc1d064df
commit 80cfe6b7a2
7 files changed
+91 -115

No files matched your search

+11 -14
View File
@@ -6,8 +6,9 @@ Inserts a new entry into the database and returns the insert id for the row, if
### Callback ### Callback
```lua ```lua
-- Alias: exports.oxmysql:insert -- alias: exports.oxmysql:insert
-- Alias: MySQL.Async.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) print(id)
end) end)
@@ -15,28 +16,24 @@ end)
### Promise ### Promise
```lua ```lua
-- Alias: exports.oxmysql:insert_async -- alias: exports.oxmysql:insert_async
-- Alias: MySQL.Sync.insert -- 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) print(id)
end)
``` ```
## JavaScript ## JavaScript
### Callback ### Callback
```js ```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], (id) => {
console.log(id) console.log(id)
}) })
``` ```
### Promise ### Promise
```js ```js
(async () => { const id = await exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName])
const id = exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', console.log(id)
[playerIdentifier, firstName, lastName]) {
console.log(id)
})()
``` ```
+14 -25
View File
@@ -13,54 +13,43 @@ When using SELECT, the return value will match `query, single, or scalar` depend
### Callback ### Callback
```lua ```lua
-- Alias: exports.oxmysql:prepare -- alias: exports.oxmysql:prepare
-- Alias: MySQL.Async.prepare -- alias: MySQL.Async.prepare
MySQL.prepare('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) MySQL.prepare('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
if result then if result then
for _, v in pairs(result) do print(json.encode(result, {indent=true}))
print(v.identifier, v.firstname, v.lastname)
end
end end
end) end)
``` ```
### Promise ### Promise
```lua ```lua
-- Alias: exports.oxmysql:prepare_async -- alias: exports.oxmysql:prepare_async
-- Alias: MySQL.Sync.prepare -- alias: MySQL.Sync.prepare
CreateThread(function() local result = MySQL.prepare.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
local result = MySQL.prepare.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) if result then
if result then print(json.encode(result, {indent=true}))
for _, v in pairs(result) do end
print(v.identifier, v.firstname, v.lastname)
end
end
end)
``` ```
## JavaScript ## JavaScript
### Callback ### Callback
```js ```js
exports.oxmysql.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { exports.oxmysql.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => {
if (result) { if (result) {
result.forEach((v) => {
console.log(v.identifier, v.firstname, v.lastname) console.log(v.identifier, v.firstname, v.lastname)
})
} }
}) })
``` ```
### Promise ### Promise
```js ```js
(async () => {
const result = await exports.oxmysql.prepare_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { const result = await exports.oxmysql.prepare_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
if (result) { if (result) {
result.forEach((v) => {
console.log(v.identifier, v.firstname, v.lastname) console.log(v.identifier, v.firstname, v.lastname)
}) }
}
})()
``` ```
+21 -23
View File
@@ -6,52 +6,50 @@ When selecting data it will return all matching rows and columns, otherwise it w
### Callback ### Callback
```lua ```lua
-- Alias: exports.oxmysql:query -- alias: exports.oxmysql:query
-- Alias: MySQL.Async.query -- alias: MySQL.Async.query
MySQL.query('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) MySQL.query('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
if result then if result then
for _, v in pairs(result) do for i = 1, #result do
print(v.identifier, v.firstname, v.lastname) local row = result[i]
print(row.identifier, row.firstname, row.lastname)
end end
end end
end) end)
``` ```
### Promise ### Promise
```lua ```lua
-- Alias: exports.oxmysql:query_async -- alias: exports.oxmysql:query_async
-- Alias: MySQL.Sync.query -- alias: MySQL.Sync.query
CreateThread(function() local result = MySQL.query.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
local result = MySQL.query.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) if result then
if result then for i = 1, #result do
for _, v in pairs(result) do local row = result[i]
print(v.identifier, v.firstname, v.lastname) print(row.identifier, row.firstname, row.lastname)
end end
end end
end)
``` ```
## JavaScript ## JavaScript
### Callback ### Callback
```js ```js
exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => {
if (result) { if (result) {
result.forEach((v) => { result.forEach((row) => {
console.log(v.identifier, v.firstname, v.lastname) console.log(row.identifier, row.firstname, row.lastname)
}) })
} }
}) })
``` ```
### Promise ### Promise
```js ```js
(async () => { const result = await exports.oxmysql.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
const result = await exports.oxmysql.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { if (result) {
if (result) { result.forEach((row) => {
result.forEach((v) => { console.log(row.identifier, row.firstname, row.lastname)
console.log(v.identifier, v.firstname, v.lastname)
}) })
} }
})()
``` ```
+9 -13
View File
@@ -5,8 +5,8 @@ Returns the first column for a single row.
### Callback ### Callback
```lua ```lua
-- Alias: exports.oxmysql:scalar -- alias: exports.oxmysql:scalar
-- Alias: MySQL.Async.fetchScalar -- alias: MySQL.Async.fetchScalar
MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(firstname) MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(firstname)
print(firstname) print(firstname)
@@ -15,28 +15,24 @@ end)
### Promise ### Promise
```lua ```lua
-- Alias: exports.oxmysql:scalar_async -- alias: exports.oxmysql:scalar_async
-- Alias: MySQL.Sync.fetchScalar -- alias: MySQL.Sync.fetchScalar
CreateThread(function() local firstname = MySQL.scalar.await('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier})
local firstname = MySQL.scalar.await('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}) print(firstname)
print(firstname)
end)
``` ```
## JavaScript ## JavaScript
### Callback ### Callback
```js ```js
exports.oxmysql.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], function(firstname) { exports.oxmysql.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], (firstname) => {
console.log(firstname) console.log(firstname)
}) })
``` ```
### Promise ### Promise
```js ```js
(async () => { const firstname = await exports.oxmysql.scalar_async('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier])
const firstname = await exports.oxmysql.scalar_async('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier]) { console.log(firstname)
console.log(firstname)
})()
``` ```
+13 -15
View File
@@ -5,8 +5,8 @@ Returns the columns for a single row.
### Callback ### Callback
```lua ```lua
-- Alias: exports.oxmysql:single -- alias: exports.oxmysql:single
-- Alias: MySQL.Async.fetchSingle -- alias: MySQL.Async.fetchSingle
MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
if result then if result then
@@ -17,32 +17,30 @@ end)
### Promise ### Promise
```lua ```lua
-- Alias: exports.oxmysql:single_async -- alias: exports.oxmysql:single_async
-- Alias: MySQL.Sync.fetchSingle -- alias: MySQL.Sync.fetchSingle
CreateThread(function() local result = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
local result = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) if result then
if result then
print(result.identifier, result.firstname, result.lastname) print(result.identifier, result.firstname, result.lastname)
end end
end)
``` ```
## JavaScript ## JavaScript
### Callback ### Callback
```js ```js
exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier],(result) => {
if (result) if (result) {
console.log(result.identifier, result.firstname, result.lastname) console.log(result.identifier, result.firstname, result.lastname)
}
}) })
``` ```
### Promise ### Promise
```js ```js
setImmediate(async () => { const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { if (result) {
if (result)
console.log(result.identifier, result.firstname, result.lastname) console.log(result.identifier, result.firstname, result.lastname)
}) }
``` ```
+10 -10
View File
@@ -17,15 +17,15 @@ local queries = {
} -- NOTE, the 'values' tables can be named 'parameters' here for MySQL-Async compatibility. } -- NOTE, the 'values' tables can be named 'parameters' here for MySQL-Async compatibility.
-- Async -- Async
-- Alias: exports.oxmysql:transaction -- alias: exports.oxmysql:transaction
-- Alias: MySQL.Async.transaction -- alias: MySQL.Async.transaction
MySQL.transaction(queries, function(result) MySQL.transaction(queries, function(result)
print(result) print(result)
end) end)
-- Sync -- Sync
-- Alias: exports.oxmysql:transaction_async -- alias: exports.oxmysql:transaction_async
-- Alias: MySQL.Sync.transaction -- alias: MySQL.Sync.transaction
local result = MySQL.transaction.await(queries) local result = MySQL.transaction.await(queries)
print(result) print(result)
``` ```
@@ -33,8 +33,8 @@ print(result)
#### JavaScript #### JavaScript
```js ```js
const queries = [ const queries = [
{ query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid = 1 } }, { query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid: 1 } },
{ query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid = 2 } } { query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid: 2 } }
] // NOTE, the 'values' objects can be named 'parameters' here for MySQL-Async compatibility. ] // NOTE, the 'values' objects can be named 'parameters' here for MySQL-Async compatibility.
// Async // Async
@@ -61,15 +61,15 @@ local queries = {
local parameters = { ['someid'] = 2, ['somename'] = 'John Doe', ['newname'] = 'John Notdoe' } local parameters = { ['someid'] = 2, ['somename'] = 'John Doe', ['newname'] = 'John Notdoe' }
-- Async -- Async
-- Alias: exports.oxmysql:transaction -- alias: exports.oxmysql:transaction
-- Alias: MySQL.Async.transaction -- alias: MySQL.Async.transaction
MySQL.transaction(queries, parameters, function(result) MySQL.transaction(queries, parameters, function(result)
print(result) print(result)
end) end)
-- Sync -- Sync
-- Alias: exports.oxmysql:transaction_async -- alias: exports.oxmysql:transaction_async
-- Alias: MySQL.Sync.transaction -- alias: MySQL.Sync.transaction
local result = MySQL.transaction.await(queries, parameters) local result = MySQL.transaction.await(queries, parameters)
print(result) print(result)
``` ```
+13 -15
View File
@@ -5,8 +5,8 @@ Returns the number of affected rows by the query.
### Callback ### Callback
```lua ```lua
-- Alias: exports.oxmysql:update -- alias: exports.oxmysql:update
-- Alias: MySQL.Async.execute -- alias: MySQL.Async.execute
MySQL.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 if affectedRows then
@@ -17,32 +17,30 @@ end)
### Promise ### Promise
```lua ```lua
-- Alias: exports.oxmysql:update_async -- alias: exports.oxmysql:update_async
-- Alias: MySQL.Sync.execute -- 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
if affectedRows then
print(affectedRows) print(affectedRows)
end end
end)
``` ```
## JavaScript ## JavaScript
### Callback ### Callback
```js ```js
exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) { exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], (affectedRows) => {
if (affectedRows) if (affectedRows) {
console.log(affectedRows) console.log(affectedRows)
}
}) })
``` ```
### Promise ### Promise
```js ```js
setImmediate(async () => { const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier])
const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) { if (affectedRows) {
if (affectedRows)
console.log(affectedRows) console.log(affectedRows)
}) }
``` ```