diff --git a/lib/MySQL.lua b/lib/MySQL.lua index d0581aa..b0a3466 100644 --- a/lib/MySQL.lua +++ b/lib/MySQL.lua @@ -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 @@ -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 @@ -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) diff --git a/wrapper.lua b/wrapper.lua index 17ff76c..21dd5f0 100644 --- a/wrapper.lua +++ b/wrapper.lua @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)