Skip to content

Commit

Permalink
fix(lib): Allow transactions to utilise safeArgs function
Browse files Browse the repository at this point in the history
- Also updates luadocs
  • Loading branch information
thelindat committed Oct 15, 2021
1 parent ce87bc4 commit d4e367b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
22 changes: 13 additions & 9 deletions lib/MySQL.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ local Ox = exports.oxmysql
local Resource = GetCurrentResourceName()
local Store = {}

local function safeArgs(query, parameters, cb)
local function safeArgs(query, parameters, cb, transaction)
if type(query) == 'number' then query = Store[query] end
assert(type(query) == 'string', ('A string was expected for the query, but instead received %s'):format(query))
if transaction then
assert(type(query) == 'table', ('A table was expected for the transaction, but instead received %s'):format(query))
else
assert(type(query) == 'string', ('A string was expected for the query, but instead received %s'):format(query))
end
if cb then
assert(type(cb) == 'function', ('A callback function was expected, but instead received %s'):format(cb))
end
Expand Down Expand Up @@ -67,13 +71,13 @@ MySQL = {
Ox:insert(safeArgs(query, parameters, cb))
end,

---@param query string
---@param queries table
---@param parameters? table|function
---@param cb? function
---@return boolean result
---returns true when the transaction has succeeded
transaction = function(query, parameters, cb)
Ox:transaction(safeArgs(query, parameters, cb))
transaction = function(queries, parameters, cb)
Ox:transaction(safeArgs(queries, parameters, cb, true))
end,

---@param query string
Expand Down Expand Up @@ -155,14 +159,14 @@ MySQL = {
return Citizen.Await(promise)
end,

---@param query string
---@param queries table
---@param parameters? table|function
---@return boolean result
---returns true when the transaction has succeeded
transaction = function(query, parameters)
query, parameters = safeArgs(query, parameters)
transaction = function(queries, parameters)
queries, parameters = safeArgs(queries, parameters, true)
local promise = promise.new()
Ox:transaction(query, parameters, function(result)
Ox:transaction(queries, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
Expand Down
36 changes: 29 additions & 7 deletions wrapper.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
-- @retval Fetch all query results
---@param query string
---@param parameters? table|function
---@return integer result
---returns number of rows updated by the executed query
exports('executeSync', function(query, parameters)
local p = promise.new()
exports['oxmysql']:execute(query, parameters, function(result)
Expand All @@ -7,7 +10,11 @@ exports('executeSync', function(query, parameters)
return Citizen.Await(p)
end)

-- @retval Fetch all query results (leftover for compatibility, same as execute)
---@deprecated true
---@param query string
---@param parameters? table|function
---@return table result
---returns array of matching rows or result data
exports('fetchSync', function(query, parameters)
local p = promise.new()
exports['oxmysql']:fetch(query, parameters, function(result)
Expand All @@ -16,7 +23,10 @@ exports('fetchSync', function(query, parameters)
return Citizen.Await(p)
end)

-- @retval Returns the first row
---@param query string
---@param parameters? table|function
---@return table result
---returns table containing key value pairs
exports('singleSync', function(query, parameters)
local p = promise.new()
exports['oxmysql']:single(query, parameters, function(result)
Expand All @@ -25,7 +35,10 @@ exports('singleSync', function(query, parameters)
return Citizen.Await(p)
end)

-- @retval Returns a single result from a column
---@param query string
---@param parameters? table|function
---@return integer|string
---returns value of the first column of a single row
exports('scalarSync', function(query, parameters)
local p = promise.new()
exports['oxmysql']:scalar(query, parameters, function(result)
Expand All @@ -34,7 +47,10 @@ exports('scalarSync', function(query, parameters)
return Citizen.Await(p)
end)

-- @retval Returns the id of the newly inserted row
---@param query string
---@param parameters? table|function
---@return table result
---returns the last inserted id
exports('insertSync', function(query, parameters)
local p = promise.new()
exports['oxmysql']:insert(query, parameters, function(result)
Expand All @@ -43,7 +59,10 @@ exports('insertSync', function(query, parameters)
return Citizen.Await(p)
end)

-- @retval Number of affected rows
---@param query string
---@param parameters? table|function
---@return integer result
---returns number of rows updated by the executed query
exports('updateSync', function(query, parameters)
local p = promise.new()
exports['oxmysql']:update(query, parameters, function(result)
Expand All @@ -52,7 +71,10 @@ exports('updateSync', function(query, parameters)
return Citizen.Await(p)
end)

-- @retval Returns true/false when the given queries are executed
---@param queries table
---@param parameters? table|function
---@return boolean result
---returns true when the transaction has succeeded
exports('transactionSync', function(queries, parameters)
local p = promise.new()
exports['oxmysql']:transaction(queries, parameters, function(result)
Expand Down

0 comments on commit d4e367b

Please sign in to comment.