Skip to content

Commit

Permalink
Add execute to project
Browse files Browse the repository at this point in the history
  • Loading branch information
ThymonA committed Dec 31, 2020
1 parent ec81100 commit c069bcc
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 11 deletions.
2 changes: 1 addition & 1 deletion MySQL.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
-- ➤ Author: Thymon Arens <ThymonA>
-- ➤ Name: FiveM MySQL
-- ➤ Version: 1.0.0
-- ➤ Version: 1.0.1
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
2 changes: 1 addition & 1 deletion example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
-- ➤ Author: Thymon Arens <ThymonA>
-- ➤ Name: FiveM MySQL
-- ➤ Version: 1.0.0
-- ➤ Version: 1.0.1
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
-- ➤ Author: Thymon Arens <ThymonA>
-- ➤ Name: FiveM MySQL
-- ➤ Version: 1.0.0
-- ➤ Version: 1.0.1
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
308 changes: 308 additions & 0 deletions lib/MySQL.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
---
---
--- USE THIS FILE IF YOU WANT TO REPLACE `fivem-mysql-async` by `brouznouf` with `fivem-mysql` by `ThymonA`.
--- THIS FILE HAS THE SAME STRUCTURE AS https://github.com/brouznouf/fivem-mysql-async/blob/master/lib/MySQL.lua
--- MAKES IT EASER TO TRANSFORM `fivem-mysql-async` to `fivem-mysql`
---
--- REPLACE `@mysql-async/lib/MySQL.lua` with `@fivem-mysql/lib/MySQL.lua` YOUR GOOD TO GO.
---
--- MISSING IMPLEMENTATIONS:
--- MySQL.Sync.store
--- MySQL.Sync.transaction
--- MySQL.Async.store
--- MySQL.Async.transaction
---

--- Cached globals
local assert = assert
local type = type
local rawget = rawget
local next = next
local setmetatable = setmetatable
local GetResourceState = GetResourceState
local GetCurrentResourceName = GetCurrentResourceName
local CreateThread = Citizen.CreateThread
local Wait = Citizen.Wait

--- Variables
local resource_name = GetCurrentResourceName()

--- MySQL global variable
local MySQL = setmetatable({ Async = {}, Sync = {} }, {
__call = function(self, query, params)
return self.Sync.execute(query, params)
end
})

---
-- Returns the real type of given `input`
--
-- @param input any
--
-- @return string Type of given input like `type` but better :D
--
local function typeof(input)
if (input == nil) then
return 'nil';
end

local t = type(input);

if (t ~= 'table') then
return t;
end

if (rawget(input, '__cfx_functionReference') ~= nil or
rawget(input, '__cfx_async_retval') ~= nil) then
return 'function';
end

if (rawget(input, '__cfx_functionSource') ~= nil) then
return 'number';
end

return t;
end

---
-- Make sure that the given parameters are save to execute
--
-- @param params any
--
-- @return table Parameters or empty parameters
--
local function safeParameters(params)
if (typeof(params) == 'nil') then params = {} end
if (typeof(params) ~= 'table') then params = {} end
if (next(params) == nil) then params = {} end

return params
end

---
-- Execute a query with no result required, sync version
--
-- @param query
-- @param params
--
-- @return int Number of rows updated
--
MySQL.Sync.execute = function(query, params)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')

local res, finished = 0, false

MySQL.Async.execute(query, params, function(result)
res = result
finished = true
end)

repeat Citizen.Wait(0) until finished == true

return res
end
---
-- Execute a query and fetch all results in an sync way
--
-- @param query
-- @param params
--
-- @return table Query results
--
MySQL.Sync.fetchAll = function(query, params)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')

local res, finished = 0, false

MySQL.Async.fetchAll(query, params, function(result)
res = result
finished = true
end)

repeat Citizen.Wait(0) until finished == true

return res
end

---
-- Execute a query and fetch the first column of the first row, sync version
-- Useful for count function by example
--
-- @param query
-- @param params
--
-- @return mixed Value of the first column in the first row
--
MySQL.Sync.fetchScalar = function(query, params)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')

local res, finished = 0, false

MySQL.Async.fetchScalar(query, params, function(result)
res = result
finished = true
end)

repeat Citizen.Wait(0) until finished == true

return res
end

---
-- Execute a query and retrieve the last id insert, sync version
--
-- @param query
-- @param params
--
-- @return mixed Value of the last insert id
--
MySQL.Sync.insert = function(query, params)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')

local res, finished = 0, false

MySQL.Async.insert(query, params, function(result)
res = result
finished = true
end)

repeat Citizen.Wait(0) until finished == true

return res
end

---
-- Stores a query for later execution
--
-- @param query
--
MySQL.Sync.store = function(query)
error('`MySQL.Sync.store` hasn\'t been implemented, please replace this function.')
end

---
-- Execute a List of querys and returns bool true when all are executed successfully
--
-- @param querys
-- @param params
--
-- @return bool if the transaction was successful
--
MySQL.Sync.transaction = function(querys, params)
error('`MySQL.Sync.transaction` hasn\'t been implemented, please replace this function.')
end

---
-- Execute a query with no result required, async version
--
-- @param query
-- @param params
-- @param func(int)
--
MySQL.Async.execute = function(query, params, callback)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')
assert(typeof(callback) == 'function', 'Callback must be a function')

exports['fivem-mysql']:executeAsync(query, params, callback, resource_name)
end

---
-- Execute a query and fetch all results in an async way
--
-- @param query
-- @param params
-- @param func(table)
--
MySQL.Async.fetchAll = function(query, params, callback)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')
assert(typeof(callback) == 'function', 'Callback must be a function')

exports['fivem-mysql']:fetchAllAsync(query, params, callback, resource_name)
end

---
-- Execute a query and fetch the first column of the first row, async version
-- Useful for count function by example
--
-- @param query
-- @param params
-- @param func(mixed)
--
MySQL.Async.fetchScalar = function(query, params, callback)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')
assert(typeof(callback) == 'function', 'Callback must be a function')

exports['fivem-mysql']:fetchScalarAsync(query, params, callback, resource_name)
end

---
-- Execute a query and retrieve the last id insert, async version
--
-- @param query
-- @param params
-- @param func(string)
--
MySQL.Async.insert = function(query, params, callback)
params = safeParameters(params or {})

assert(typeof(query) == 'string', 'SQL query must be a string')
assert(typeof(params) == 'table', 'Parameters must be a table')
assert(typeof(callback) == 'function', 'Callback must be a function')

exports['fivem-mysql']:insertAsync(query, params, callback, resource_name)
end

---
-- Stores a query for later execution
--
-- @param query
-- @param func(number)
--
MySQL.Async.store = function(query, func)
error('`MySQL.Async.store` hasn\'t been implemented, please replace this function.')
end

---
-- Execute a List of querys and returns bool true when all are executed successfully
--
-- @param querys
-- @param params
-- @param func(bool)
--
MySQL.Async.transaction = function(querys, params, func)
error('`MySQL.Async.transaction` hasn\'t been implemented, please replace this function.')
end

function MySQL.ready(callback)
CreateThread(function ()
local cb = callback or function() end

assert(typeof(cb) == 'function', 'Callback must be a function')

while GetResourceState('fivem-mysql') ~= 'started' do Wait(0) end
while not exports['fivem-mysql']:isReady() do Citizen.Wait(0) end

cb()
end)
end
2 changes: 1 addition & 1 deletion source/fivem/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
➤ Author: Thymon Arens <ThymonA>
➤ Name: FiveM MySQL
➤ Version: 1.0.0
➤ Version: 1.0.1
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
2 changes: 1 addition & 1 deletion source/mysql/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
➤ Author: Thymon Arens <ThymonA>
➤ Name: FiveM MySQL
➤ Version: 1.0.0
➤ Version: 1.0.1
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
2 changes: 1 addition & 1 deletion source/mysql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
➤ Author: Thymon Arens <ThymonA>
➤ Name: FiveM MySQL
➤ Version: 1.0.0
➤ Version: 1.0.1
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
2 changes: 1 addition & 1 deletion source/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
➤ Author: Thymon Arens <ThymonA>
➤ Name: FiveM MySQL
➤ Version: 1.0.0
➤ Version: 1.0.1
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
2 changes: 1 addition & 1 deletion source/mysql/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
➤ Author: Thymon Arens <ThymonA>
➤ Name: FiveM MySQL
➤ Version: 1.0.0
➤ Version: 1.0.1
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
Loading

0 comments on commit c069bcc

Please sign in to comment.