Skip to content

Commit 41c8a0a

Browse files
committed
fix(global) Fix some bugs
1 parent e79eb81 commit 41c8a0a

File tree

13 files changed

+1297
-1318
lines changed

13 files changed

+1297
-1318
lines changed

MySQL.lua

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
55
-- ➤ Author: Thymon Arens <ThymonA>
66
-- ➤ Name: FiveM MySQL
7-
-- ➤ Version: 1.0.1
7+
-- ➤ Version: 1.0.2
88
-- ➤ Description: MySQL library made for FiveM
99
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1010
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
@@ -69,6 +69,21 @@ function mysql:safeParams(params)
6969
return params
7070
end
7171

72+
function mysql:execute(query, params)
73+
params = params or {}
74+
75+
local res, finished = nil, false
76+
77+
self:executeAsync(query, params, function(result)
78+
res = result
79+
finished = true
80+
end)
81+
82+
repeat Citizen.Wait(0) until finished == true
83+
84+
return res
85+
end
86+
7287
function mysql:insert(query, params)
7388
params = params or {}
7489

@@ -129,6 +144,18 @@ function mysql:fetchFirst(query, params)
129144
return res
130145
end
131146

147+
function mysql:executeAsync(query, params, callback)
148+
params = params or {}
149+
150+
assert(self:typeof(query) == 'string', 'SQL query must be a string')
151+
assert(self:typeof(params) == 'table', 'Parameters must be a table')
152+
assert(self:typeof(callback) == 'function', 'Callback must be a function')
153+
154+
params = self:safeParams(params)
155+
156+
exports[self.resource_name]:executeAsync(query, params, callback, self.current_resource_name)
157+
end
158+
132159
function mysql:insertAsync(query, params, callback)
133160
params = params or {}
134161

example.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
55
-- ➤ Author: Thymon Arens <ThymonA>
66
-- ➤ Name: FiveM MySQL
7-
-- ➤ Version: 1.0.1
7+
-- ➤ Version: 1.0.2
88
-- ➤ Description: MySQL library made for FiveM
99
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1010
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬

fxmanifest.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-- ➤ GitHub: https://github.com/ThymonA/fivem-mysql/
55
-- ➤ Author: Thymon Arens <ThymonA>
66
-- ➤ Name: FiveM MySQL
7-
-- ➤ Version: 1.0.1
7+
-- ➤ Version: 1.0.2
88
-- ➤ Description: MySQL library made for FiveM
99
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1010
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
@@ -37,7 +37,7 @@ game 'gta5'
3737
-- ┃ Information about the project
3838
--
3939
name 'FiveM-MySQL'
40-
version '1.0.0'
40+
version '1.0.2'
4141
description 'MySQL library made for FiveM'
4242
url 'https://github.com/ThymonA/fivem-mysql/'
4343

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fivem-mysql",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "MySQL library made for FiveM",
55
"main": "index.js",
66
"scripts": {
@@ -28,6 +28,7 @@
2828
"@types/deasync": "^0.1.1",
2929
"@types/node": "^14.14.16",
3030
"@types/sqlstring": "^2.3.0",
31+
"cardinal": "^2.1.1",
3132
"mysql2": "^2.2.5",
3233
"pg-connection-string": "^2.4.0",
3334
"qs": "^6.9.4",

source/fivem/callback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
66
➤ Author: Thymon Arens <ThymonA>
77
➤ Name: FiveM MySQL
8-
➤ Version: 1.0.1
8+
➤ Version: 1.0.2
99
➤ Description: MySQL library made for FiveM
1010
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1111
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬

source/mysql/config.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
66
➤ Author: Thymon Arens <ThymonA>
77
➤ Name: FiveM MySQL
8-
➤ Version: 1.0.1
8+
➤ Version: 1.0.2
99
➤ Description: MySQL library made for FiveM
1010
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1111
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
@@ -27,10 +27,21 @@
2727
2828
*/
2929

30+
import { format } from 'sqlstring';
3031
import { parse } from 'qs';
3132
import { parse as ConnectionString } from 'pg-connection-string';
3233
import { ConnectionOptions } from 'mysql2';
3334

35+
function fixString(query: string): string {
36+
if (query.startsWith("'") && query.endsWith("'")) {
37+
query = query.substr(1, (query.length - 1));
38+
39+
return fixString(query);
40+
}
41+
42+
return query;
43+
}
44+
3445
function getConnectionFromString(rawConnectionString: string): ConnectionOptions {
3546
let connection = {} as ConnectionOptions;
3647

@@ -61,6 +72,35 @@ function getConnectionFromString(rawConnectionString: string): ConnectionOptions
6172
}
6273
}
6374

75+
connection.typeCast = true;
76+
connection.charset = 'UTF8_GENERAL_CI'
77+
connection.supportBigNumbers = true;
78+
connection.stringifyObjects = false;
79+
connection.insecureAuth = true;
80+
connection.dateStrings = false;
81+
connection.trace = true;
82+
connection.multipleStatements = true;
83+
connection.queryFormat = (q, v) => {
84+
let sql = q.replace(/[@]/g, ':')
85+
.replace(/`'/g, '`')
86+
.replace(/'`/g, '`')
87+
.replace(/`"/g, '`')
88+
.replace(/"`/g, '`')
89+
.replace(/``/g, '`');
90+
91+
sql = format(sql, v, false, 'local');
92+
sql = fixString(sql);
93+
94+
sql = sql.replace(/[@]/g, ':')
95+
.replace(/`'/g, '`')
96+
.replace(/'`/g, '`')
97+
.replace(/`"/g, '`')
98+
.replace(/"`/g, '`')
99+
.replace(/``/g, '`');
100+
101+
return sql;
102+
}
103+
64104
return connection;
65105
}
66106

source/mysql/helpers.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

source/mysql/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
66
➤ Author: Thymon Arens <ThymonA>
77
➤ Name: FiveM MySQL
8-
➤ Version: 1.0.1
8+
➤ Version: 1.0.2
99
➤ Description: MySQL library made for FiveM
1010
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1111
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬

source/mysql/mysql.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
66
➤ Author: Thymon Arens <ThymonA>
77
➤ Name: FiveM MySQL
8-
➤ Version: 1.0.1
8+
➤ Version: 1.0.2
99
➤ Description: MySQL library made for FiveM
1010
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1111
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
@@ -28,9 +28,8 @@
2828
*/
2929

3030
import { CFXCallback, OkPacket, RowDataPacket, ResultSetHeader } from '../fivem/callback';
31-
import { fixParameters, fixQuery } from './helpers';
3231
import { Tracer } from 'tracer';
33-
import { Pool, PoolOptions, ConnectionOptions, QueryError, createPool } from 'mysql2';
32+
import { Pool, PoolOptions, ConnectionOptions, QueryError, createPool, QueryOptions } from 'mysql2';
3433

3534
declare type keyValue = { [key: string]: any };
3635

@@ -86,14 +85,12 @@ class MySQLServer {
8685
}
8786

8887
execute(query: string, parameters: keyValue, callback: CFXCallback, resource: string) {
89-
const config = this.pool?.config;
90-
91-
parameters = fixParameters(parameters, config?.stringifyObjects, config?.timezone);
92-
query = fixQuery(query);
93-
94-
const sql = this.pool?.format(query, parameters);
95-
96-
return this.pool?.query(sql, parameters, (err, result) => {
88+
return this.pool?.query({
89+
sql: this.pool.format(query, parameters),
90+
values: parameters,
91+
nestTables: false,
92+
typeCast: true
93+
} as QueryOptions, parameters, (err, result) => {
9794
err ? this.errorCallback(err, callback, resource, query) : callback(result, query);
9895
});
9996
}

source/server.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
➤ GitHub: https://github.com/ThymonA/fivem-mysql/
66
➤ Author: Thymon Arens <ThymonA>
77
➤ Name: FiveM MySQL
8-
➤ Version: 1.0.1
8+
➤ Version: 1.0.2
99
➤ Description: MySQL library made for FiveM
1010
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1111
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
@@ -27,8 +27,7 @@
2727
2828
*/
2929

30-
import { console } from 'tracer';
31-
import { warnIfNeeded } from './mysql/helpers';
30+
import { console, Tracer } from 'tracer';
3231
import { GetLoggerConfig, GetSlowQueryWarning } from './tracer';
3332
import { MySQLServer, CFXCallback, OkPacket, ConnectionString, keyValue } from './mysql';
3433

@@ -42,6 +41,14 @@ const slowQueryWarning = GetSlowQueryWarning();
4241
const logger = console(GetLoggerConfig());
4342
const server = new MySQLServer(connectionString, logger, () => { isReady = true; });
4443

44+
function warnIfNeeded(time: [number, number], logger: Tracer.Logger, sql: string, resource: string, interval: number) {
45+
const queryTime = time[0] * 1e3 + time[1] * 1e-6;
46+
47+
if (interval <= 0 || interval > queryTime) { return; }
48+
49+
logger.warn(`Resource '${resource}' executed an query that took ${queryTime.toFixed()}ms to execute\n> ^4Query: ^7${sql}\n> ^4Execution time: ^7${queryTime.toFixed()}ms`);
50+
}
51+
4552
global.exports('executeAsync', (query: string, parameters?: keyValue, callback?: CFXCallback, resource?: string): void => {
4653
const startTime = process.hrtime();
4754

0 commit comments

Comments
 (0)