Skip to content

Commit 0f38986

Browse files
author
GHMatti
authored
Merge pull request #135 from brouznouf/dev
Dev
2 parents 8638da6 + f4efdb3 commit 0f38986

25 files changed

+1631
-1819
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "src/vendor/ghmattimysql"]
2+
path = src/vendor/ghmattimysql
3+
url = https://github.com/GHMatti/ghmattimysql.git
4+
branch = master

lib/MySQL.lua

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ local function safeParameters(params)
99
end
1010

1111
assert(type(params) == "table", "A table is expected")
12-
assert(params[1] == nil, "Parameters should not be an array, but a map (key / value pair) instead")
1312

1413
if next(params) == nil then
1514
return {[''] = ''}
@@ -27,7 +26,7 @@ end
2726
-- @return int Number of rows updated
2827
--
2928
function MySQL.Sync.execute(query, params)
30-
assert(type(query) == "string", "The SQL Query must be a string")
29+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
3130

3231
local res = 0
3332
local finishedQuery = false
@@ -47,7 +46,7 @@ end
4746
-- @return table Query results
4847
--
4948
function MySQL.Sync.fetchAll(query, params)
50-
assert(type(query) == "string", "The SQL Query must be a string")
49+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
5150

5251
local res = {}
5352
local finishedQuery = false
@@ -69,7 +68,7 @@ end
6968
-- @return mixed Value of the first column in the first row
7069
--
7170
function MySQL.Sync.fetchScalar(query, params)
72-
assert(type(query) == "string", "The SQL Query must be a string")
71+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
7372

7473
local res = ''
7574
local finishedQuery = false
@@ -90,7 +89,7 @@ end
9089
-- @return mixed Value of the last insert id
9190
--
9291
function MySQL.Sync.insert(query, params)
93-
assert(type(query) == "string", "The SQL Query must be a string")
92+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
9493

9594
local res = 0
9695
local finishedQuery = false
@@ -102,6 +101,24 @@ function MySQL.Sync.insert(query, params)
102101
return res
103102
end
104103

104+
---
105+
-- Stores a query for later execution
106+
--
107+
-- @param query
108+
--
109+
function MySQL.Sync.store(query)
110+
assert(type(query) == "string", "The SQL Query must be a string")
111+
112+
local res = -1
113+
local finishedQuery = false
114+
exports['mysql-async']:mysql_store(query, function (result)
115+
res = result
116+
finishedQuery = true
117+
end)
118+
repeat Citizen.Wait(0) until finishedQuery == true
119+
return res
120+
end
121+
105122
---
106123
-- Execute a List of querys and returns bool true when all are executed successfully
107124
--
@@ -129,7 +146,7 @@ end
129146
-- @param func(int)
130147
--
131148
function MySQL.Async.execute(query, params, func)
132-
assert(type(query) == "string", "The SQL Query must be a string")
149+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
133150

134151
exports['mysql-async']:mysql_execute(query, safeParameters(params), func)
135152
end
@@ -142,7 +159,7 @@ end
142159
-- @param func(table)
143160
--
144161
function MySQL.Async.fetchAll(query, params, func)
145-
assert(type(query) == "string", "The SQL Query must be a string")
162+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
146163

147164
exports['mysql-async']:mysql_fetch_all(query, safeParameters(params), func)
148165
end
@@ -156,7 +173,7 @@ end
156173
-- @param func(mixed)
157174
--
158175
function MySQL.Async.fetchScalar(query, params, func)
159-
assert(type(query) == "string", "The SQL Query must be a string")
176+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
160177

161178
exports['mysql-async']:mysql_fetch_scalar(query, safeParameters(params), func)
162179
end
@@ -169,11 +186,23 @@ end
169186
-- @param func(string)
170187
--
171188
function MySQL.Async.insert(query, params, func)
172-
assert(type(query) == "string", "The SQL Query must be a string")
189+
assert(type(query) == "string" or type(query) == "number", "The SQL Query must be a string")
173190

174191
exports['mysql-async']:mysql_insert(query, safeParameters(params), func)
175192
end
176193

194+
---
195+
-- Stores a query for later execution
196+
--
197+
-- @param query
198+
-- @param func(number)
199+
--
200+
function MySQL.Async.store(query, func)
201+
assert(type(query) == "string", "The SQL Query must be a string")
202+
203+
exports['mysql-async']:mysql_store(query, func)
204+
end
205+
177206
---
178207
-- Execute a List of querys and returns bool true when all are executed successfully
179208
--

mysql-async-client.js

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@
9191
let isNuiActive = false;
9292

9393
function NuiMessage(msg) {
94-
window.SendNuiMessage(JSON.stringify(msg));
94+
SendNuiMessage(JSON.stringify(msg));
9595
}
9696

9797
function NuiCallback(name, callback) {
98-
window.RegisterNuiCallbackType(name);
99-
window.on(`__cfx_nui:${name}`, (data, cb) => {
98+
RegisterNuiCallbackType(name);
99+
on(`__cfx_nui:${name}`, (data, cb) => {
100100
callback(data);
101101
cb('ok');
102102
});
@@ -105,42 +105,44 @@ function NuiCallback(name, callback) {
105105
function setNuiActive(boolean = true) {
106106
if (boolean !== isNuiActive) {
107107
if (boolean) {
108-
window.emitNet('mysql-async:request-data');
108+
emitNet('mysql-async:request-data');
109109
}
110+
110111
isNuiActive = boolean;
111-
NuiMessage({ type: 'onToggleShow' });
112-
window.SetNuiFocus(boolean, boolean);
112+
NuiMessage({
113+
type: 'onToggleShow'
114+
});
115+
SetNuiFocus(boolean, boolean);
113116
}
114117
}
115118

116-
window.RegisterCommand('mysql', () => {
119+
RegisterCommand('mysql', () => {
117120
setNuiActive();
118121
}, true);
119-
120122
NuiCallback('close-explorer', () => {
121123
setNuiActive(false);
122124
});
123-
124-
window.setInterval(() => {
125+
setInterval(() => {
125126
if (isNuiActive) {
126-
window.emitNet('mysql-async:request-data');
127+
emitNet('mysql-async:request-data');
127128
}
128129
}, 300000);
129-
130-
window.onNet('mysql-async:update-resource-data', (resourceData) => {
130+
onNet('mysql-async:update-resource-data', resourceData => {
131131
let arrayToSortAndMap = [];
132132
const resources = Object.keys(resourceData);
133+
133134
for (let i = 0; i < resources.length; i += 1) {
134135
if (Object.prototype.hasOwnProperty.call(resourceData, resources[i])) {
135136
if (Object.prototype.hasOwnProperty.call(resourceData[resources[i]], 'totalExecutionTime')) {
136137
arrayToSortAndMap.push({
137138
resource: resources[i],
138139
queryTime: resourceData[resources[i]].totalExecutionTime,
139-
count: resourceData[resources[i]].queryCount,
140+
count: resourceData[resources[i]].queryCount
140141
});
141142
}
142143
}
143144
}
145+
144146
if (arrayToSortAndMap.length > 0) {
145147
arrayToSortAndMap.sort((a, b) => a.queryTime - b.queryTime);
146148
const len = arrayToSortAndMap.length;
@@ -149,71 +151,63 @@ window.onNet('mysql-async:update-resource-data', (resourceData) => {
149151
const resourceA = a.resource.toLowerCase();
150152
const resourceB = b.resource.toLowerCase();
151153
let result = 0;
154+
152155
if (resourceA < resourceB) {
153156
result = -1;
154157
} else if (resourceA > resourceB) {
155158
result = 1;
156159
}
160+
157161
return result;
158162
});
159163
NuiMessage({
160164
type: 'onResourceLabels',
161-
resourceLabels: arrayToSortAndMap.map((el) => el.resource),
165+
resourceLabels: arrayToSortAndMap.map(el => el.resource)
162166
});
163167
NuiMessage({
164168
type: 'onResourceData',
165-
resourceData: [
166-
{
167-
data: arrayToSortAndMap.map((el) => el.queryTime),
168-
},
169-
{
170-
data: arrayToSortAndMap.map((el) => ((el.count > 0) ? el.queryTime / el.count : 0)),
171-
},
172-
{
173-
data: arrayToSortAndMap.map((el) => el.count),
174-
},
175-
],
169+
resourceData: [{
170+
data: arrayToSortAndMap.map(el => el.queryTime)
171+
}, {
172+
data: arrayToSortAndMap.map(el => el.count > 0 ? el.queryTime / el.count : 0)
173+
}, {
174+
data: arrayToSortAndMap.map(el => el.count)
175+
}]
176176
});
177177
}
178178
});
179-
180-
window.onNet('mysql-async:update-time-data', (timeData) => {
179+
onNet('mysql-async:update-time-data', timeData => {
181180
let timeArray = [];
181+
182182
if (Array.isArray(timeData)) {
183183
const len = timeData.length;
184184
timeArray = timeData.filter((_, index) => index > len - 31);
185185
}
186+
186187
if (timeArray.length > 0) {
187188
NuiMessage({
188189
type: 'onTimeData',
189-
timeData: [
190-
{
191-
data: timeArray.map((el) => el.totalExecutionTime),
192-
},
193-
{
194-
data: timeArray.map((el) => ((el.queryCount > 0) ? el.totalExecutionTime / el.queryCount
195-
: 0)),
196-
},
197-
{
198-
data: timeArray.map((el) => el.queryCount),
199-
},
200-
],
190+
timeData: [{
191+
data: timeArray.map(el => el.totalExecutionTime)
192+
}, {
193+
data: timeArray.map(el => el.queryCount > 0 ? el.totalExecutionTime / el.queryCount : 0)
194+
}, {
195+
data: timeArray.map(el => el.queryCount)
196+
}]
201197
});
202198
}
203199
});
204-
205-
window.onNet('mysql-async:update-slow-queries', (slowQueryData) => {
206-
const slowQueries = slowQueryData.map((el) => {
200+
onNet('mysql-async:update-slow-queries', slowQueryData => {
201+
const slowQueries = slowQueryData.map(el => {
207202
const element = el;
208203
element.queryTime = Math.round(el.queryTime * 100) / 100;
209204
return element;
210205
});
211206
NuiMessage({
212207
type: 'onSlowQueryData',
213-
slowQueries,
208+
slowQueries
214209
});
215210
});
216211

217-
218212
/***/ })
219213
/******/ ]);

0 commit comments

Comments
 (0)