-
Notifications
You must be signed in to change notification settings - Fork 6
/
MySQL.lua
222 lines (163 loc) · 6.61 KB
/
MySQL.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
-- 𝗙𝗶𝘃𝗲𝗠 𝗠𝘆𝗦𝗤𝗟 - 𝗠𝘆𝗦𝗤𝗟 𝗹𝗶𝗯𝗿𝗮𝗿𝘆 𝗳𝗼𝗿 𝗙𝗶𝘃𝗲𝗠
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- ➤ License: https://choosealicense.com/licenses/gpl-3.0/
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
-- ➤ Author: Thymon Arens <ThymonA>
-- ➤ Name: FiveM MySQL
-- ➤ Version: 1.0.2
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
-- ┳
-- ┃ Copyright (C) 2020 Thymon Arens <ThymonA>
-- ┃
-- ┃ This program is free software: you can redistribute it and/or modify
-- ┃ it under the terms of the GNU General Public License as published by
-- ┃ the Free Software Foundation, either version 3 of the License, or
-- ┃ (at your option) any later version.
-- ┃
-- ┃ This program is distributed in the hope that it will be useful,
-- ┃ but WITHOUT ANY WARRANTY; without even the implied warranty of
-- ┃ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- ┃ GNU General Public License for more details.
-- ┃
-- ┃ You should have received a copy of the GNU General Public License
-- ┃ al
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
local mysql = setmetatable({
resource_name = 'fivem-mysql',
current_resource_name = GetCurrentResourceName()
}, {})
function mysql: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
function mysql:safeParams(params)
if (self:typeof(params) == 'nil') then params = {} end
if (self:typeof(params) ~= 'table') then params = {} end
if (next(params) == nil) then params = {} end
return params
end
function mysql:execute(query, params)
params = params or {}
local res, finished = nil, false
self:executeAsync(query, params, function(result)
res = result
finished = true
end)
repeat Citizen.Wait(0) until finished == true
return res
end
function mysql:insert(query, params)
params = params or {}
local res, finished = nil, false
self:insertAsync(query, params, function(result)
res = result
finished = true
end)
repeat Citizen.Wait(0) until finished == true
return res
end
function mysql:fetchAll(query, params)
params = params or {}
local res, finished = nil, false
self:fetchAllAsync(query, params, function(result)
res = result
finished = true
end)
repeat Citizen.Wait(0) until finished == true
return res
end
function mysql:fetchScalar(query, params)
params = params or {}
local res, finished = nil, false
self:fetchScalarAsync(query, params, function(result)
res = result
finished = true
end)
repeat Citizen.Wait(0) until finished == true
return res
end
function mysql:fetchFirst(query, params)
params = params or {}
local res, finished = nil, false
self:fetchFirstAsync(query, params, function(result)
res = result
finished = true
end)
repeat Citizen.Wait(0) until finished == true
return res
end
function mysql:executeAsync(query, params, callback)
params = params or {}
assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')
params = self:safeParams(params)
exports[self.resource_name]:executeAsync(query, params, callback, self.current_resource_name)
end
function mysql:insertAsync(query, params, callback)
params = params or {}
assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')
params = self:safeParams(params)
exports[self.resource_name]:insertAsync(query, params, callback, self.current_resource_name)
end
function mysql:fetchAllAsync(query, params, callback)
params = params or {}
assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')
params = self:safeParams(params)
exports[self.resource_name]:fetchAllAsync(query, params, callback, self.current_resource_name)
end
function mysql:fetchScalarAsync(query, params, callback)
params = params or {}
assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')
params = self:safeParams(params)
exports[self.resource_name]:fetchScalarAsync(query, params, callback, self.current_resource_name)
end
function mysql:fetchFirstAsync(query, params, callback)
params = params or {}
assert(self:typeof(query) == 'string', 'SQL query must be a string')
assert(self:typeof(params) == 'table', 'Parameters must be a table')
assert(self:typeof(callback) == 'function', 'Callback must be a function')
params = self:safeParams(params)
exports[self.resource_name]:fetchFirstAsync(query, params, callback, self.current_resource_name)
end
function mysql:ready(callback)
CreateThread(function()
local cb = callback or function() end
assert(self:typeof(cb) == 'function', 'Callback must be a function')
while GetResourceState(self.resource_name) ~= 'started' do Wait(0) end
while not exports[self.resource_name]:isReady() do Wait(0) end
cb()
end)
end
--- Register `mysql` as global variable
_G.mysql = mysql
_ENV.mysql = mysql