Skip to content

Commit

Permalink
refactor(lib): Update formatting for improved LDoc usage
Browse files Browse the repository at this point in the history
- This makes it easier to see which function is being defined, but also allows LDocs / Emmy Annotations / whatever to display the full function name.
  • Loading branch information
thelindat committed Oct 18, 2021
1 parent f7ed2d5 commit 5965442
Showing 1 changed file with 163 additions and 169 deletions.
332 changes: 163 additions & 169 deletions lib/MySQL.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,172 +23,166 @@ local function safeArgs(query, parameters, cb, transaction)
return query, parameters, cb
end

MySQL = {
--- Result is returned to a callback function, without interupting the execution of code
Async = {
---@param query string
---@param parameters? table|function
---@param cb? function
---@return integer result
---returns number of rows updated by the executed query
execute = function(query, parameters, cb)
Ox:update(safeArgs(query, parameters, cb))
end,

---@param query string
---@param parameters? table|function
---@param cb? function
---@return table result
---returns array of matching rows or result data
fetchAll = function(query, parameters, cb)
Ox:execute(safeArgs(query, parameters, cb))
end,

---@param query string
---@param parameters? table|function
---@param cb? function
---@return integer|string
---returns value of the first column of a single row
fetchScalar = function(query, parameters, cb)
Ox:scalar(safeArgs(query, parameters, cb))
end,

---@param query string
---@param parameters? table|function
---@param cb? function
---@return table result
---returns table containing key value pairs
fetchSingle = function(query, parameters, cb)
Ox:single(safeArgs(query, parameters, cb))
end,

---@param query string
---@param parameters? table|function
---@param cb? function
---@return table result
---returns the last inserted id
insert = function(query, parameters, cb)
Ox:insert(safeArgs(query, parameters, cb))
end,

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

---@param query string
---@param cb? function
---@return integer result
---returns the id used to reference a stored query string
store = function(query, cb)
assert(type(query) == 'string', 'The SQL Query must be a string')
local store = #Store+1
Store[store] = query
cb(store)
end,
},

--- Result is assigned in-line to a variable, causing the current thread to await the result
Sync = {
---@param query string
---@param parameters? table|function
---@return integer result
---returns number of rows updated by the executed query
execute = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:update(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end,

---@param query string
---@param parameters? table|function
---@return table result
---returns array of matching rows or result data
fetchAll = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:execute(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end,

---@param query string
---@param parameters? table|function
---@return integer|string
---returns value of the first column of a single row
fetchScalar = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:scalar(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end,

---@param query string
---@param parameters? table|function
---@return table result
---returns table containing key value pairs
fetchSingle = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:single(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end,

---@param query string
---@param parameters? table|function
---@return table result
---returns the last inserted id
insert = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:insert(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end,

---@param queries table
---@param parameters? table|function
---@return boolean result
---returns true when the transaction has succeeded
transaction = function(queries, parameters)
queries, parameters = safeArgs(queries, parameters, true)
local promise = promise.new()
Ox:transaction(queries, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end,

---@param query string
---@return integer result
---returns the id used to reference a stored query string
store = function(query)
assert(type(query) == 'string', 'The SQL Query must be a string')
local store = #Store+1
Store[store] = query
return store
end,
},

ready = function(cb)
CreateThread(function()
repeat
Wait(50)
until GetResourceState('oxmysql') == 'started'
cb()
end)
end
}
MySQL = { Async = {}, Sync = {} }

---@param query string
---@param parameters? table|function
---@param cb? function
---@return integer result
---returns number of rows updated by the executed query
MySQL.Async.execute = function(query, parameters, cb)
Ox:update(safeArgs(query, parameters, cb))
end

---@param query string
---@param parameters? table|function
---@param cb? function
---@return table result
---returns array of matching rows or result data
MySQL.Async.fetchAll = function(query, parameters, cb)
Ox:execute(safeArgs(query, parameters, cb))
end

---@param query string
---@param parameters? table|function
---@param cb? function
---@return integer|string
---returns value of the first column of a single row
MySQL.Async.fetchScalar = function(query, parameters, cb)
Ox:scalar(safeArgs(query, parameters, cb))
end

---@param query string
---@param parameters? table|function
---@param cb? function
---@return table result
---returns table containing key value pairs
MySQL.Async.fetchSingle = function(query, parameters, cb)
Ox:single(safeArgs(query, parameters, cb))
end

---@param query string
---@param parameters? table|function
---@param cb? function
---@return table result
---returns the last inserted id
MySQL.Async.insert = function(query, parameters, cb)
Ox:insert(safeArgs(query, parameters, cb))
end

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

---@param query string
---@param cb? function
---@return integer result
---returns the id used to reference a stored query string
MySQL.Async.store = function(query, cb)
assert(type(query) == 'string', 'The SQL Query must be a string')
local store = #Store+1
Store[store] = query
cb(store)
end

---@param query string
---@param parameters? table|function
---@return integer result
---returns number of rows updated by the executed query
MySQL.Sync.execute = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:update(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end

---@param query string
---@param parameters? table|function
---@return table result
---returns array of matching rows or result data
MySQL.Sync.fetchAll = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:execute(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end

---@param query string
---@param parameters? table|function
---@return integer|string
---returns value of the first column of a single row
MySQL.Sync.fetchScalar = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:scalar(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end

---@param query string
---@param parameters? table|function
---@return table result
---returns table containing key value pairs
MySQL.Sync.fetchSingle = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:single(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end

---@param query string
---@param parameters? table|function
---@return table result
---returns the last inserted id
MySQL.Sync.insert = function(query, parameters)
query, parameters = safeArgs(query, parameters)
local promise = promise.new()
Ox:insert(query, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end

---@param queries table
---@param parameters? table|function
---@return boolean result
---returns true when the transaction has succeeded
MySQL.Sync.transaction = function(queries, parameters)
queries, parameters = safeArgs(queries, parameters, true)
local promise = promise.new()
Ox:transaction(queries, parameters, function(result)
promise:resolve(result)
end, Resource)
return Citizen.Await(promise)
end

---@param query string
---@return integer result
---returns the id used to reference a stored query string
MySQL.Sync.store = function(query)
assert(type(query) == 'string', 'The SQL Query must be a string')
local store = #Store+1
Store[store] = query
return store
end

MySQL.ready = function(cb)
CreateThread(function()
repeat
Wait(50)
until GetResourceState('oxmysql') == 'started'
cb()
end)
end

0 comments on commit 5965442

Please sign in to comment.