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
+99 -123

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