Skip to content

Commit

Permalink
fix(global) Fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ThymonA committed Feb 12, 2021
1 parent e79eb81 commit 41c8a0a
Show file tree
Hide file tree
Showing 13 changed files with 1,297 additions and 1,318 deletions.
29 changes: 28 additions & 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.1
-- ➤ Version: 1.0.2
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down Expand Up @@ -69,6 +69,21 @@ function mysql:safeParams(params)
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 {}

Expand Down Expand Up @@ -129,6 +144,18 @@ function mysql:fetchFirst(query, params)
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 {}

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.1
-- ➤ Version: 1.0.2
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
4 changes: 2 additions & 2 deletions 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.1
-- ➤ Version: 1.0.2
-- ➤ Description: MySQL library made for FiveM
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- 𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down Expand Up @@ -37,7 +37,7 @@ game 'gta5'
-- ┃ Information about the project
--
name 'FiveM-MySQL'
version '1.0.0'
version '1.0.2'
description 'MySQL library made for FiveM'
url 'https://github.com/ThymonA/fivem-mysql/'

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fivem-mysql",
"version": "1.0.0",
"version": "1.0.2",
"description": "MySQL library made for FiveM",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,7 @@
"@types/deasync": "^0.1.1",
"@types/node": "^14.14.16",
"@types/sqlstring": "^2.3.0",
"cardinal": "^2.1.1",
"mysql2": "^2.2.5",
"pg-connection-string": "^2.4.0",
"qs": "^6.9.4",
Expand Down
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.1
➤ Version: 1.0.2
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
42 changes: 41 additions & 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.1
➤ Version: 1.0.2
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand All @@ -27,10 +27,21 @@
*/

import { format } from 'sqlstring';
import { parse } from 'qs';
import { parse as ConnectionString } from 'pg-connection-string';
import { ConnectionOptions } from 'mysql2';

function fixString(query: string): string {
if (query.startsWith("'") && query.endsWith("'")) {
query = query.substr(1, (query.length - 1));

return fixString(query);
}

return query;
}

function getConnectionFromString(rawConnectionString: string): ConnectionOptions {
let connection = {} as ConnectionOptions;

Expand Down Expand Up @@ -61,6 +72,35 @@ function getConnectionFromString(rawConnectionString: string): ConnectionOptions
}
}

connection.typeCast = true;
connection.charset = 'UTF8_GENERAL_CI'
connection.supportBigNumbers = true;
connection.stringifyObjects = false;
connection.insecureAuth = true;
connection.dateStrings = false;
connection.trace = true;
connection.multipleStatements = true;
connection.queryFormat = (q, v) => {
let sql = q.replace(/[@]/g, ':')
.replace(/`'/g, '`')
.replace(/'`/g, '`')
.replace(/`"/g, '`')
.replace(/"`/g, '`')
.replace(/``/g, '`');

sql = format(sql, v, false, 'local');
sql = fixString(sql);

sql = sql.replace(/[@]/g, ':')
.replace(/`'/g, '`')
.replace(/'`/g, '`')
.replace(/`"/g, '`')
.replace(/"`/g, '`')
.replace(/``/g, '`');

return sql;
}

return connection;
}

Expand Down
69 changes: 0 additions & 69 deletions source/mysql/helpers.ts

This file was deleted.

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.1
➤ Version: 1.0.2
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
19 changes: 8 additions & 11 deletions 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.1
➤ Version: 1.0.2
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand All @@ -28,9 +28,8 @@
*/

import { CFXCallback, OkPacket, RowDataPacket, ResultSetHeader } from '../fivem/callback';
import { fixParameters, fixQuery } from './helpers';
import { Tracer } from 'tracer';
import { Pool, PoolOptions, ConnectionOptions, QueryError, createPool } from 'mysql2';
import { Pool, PoolOptions, ConnectionOptions, QueryError, createPool, QueryOptions } from 'mysql2';

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

Expand Down Expand Up @@ -86,14 +85,12 @@ class MySQLServer {
}

execute(query: string, parameters: keyValue, callback: CFXCallback, resource: string) {
const config = this.pool?.config;

parameters = fixParameters(parameters, config?.stringifyObjects, config?.timezone);
query = fixQuery(query);

const sql = this.pool?.format(query, parameters);

return this.pool?.query(sql, parameters, (err, result) => {
return this.pool?.query({
sql: this.pool.format(query, parameters),
values: parameters,
nestTables: false,
typeCast: true
} as QueryOptions, parameters, (err, result) => {
err ? this.errorCallback(err, callback, resource, query) : callback(result, query);
});
}
Expand Down
13 changes: 10 additions & 3 deletions source/server.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.1
➤ Version: 1.0.2
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand All @@ -27,8 +27,7 @@
*/

import { console } from 'tracer';
import { warnIfNeeded } from './mysql/helpers';
import { console, Tracer } from 'tracer';
import { GetLoggerConfig, GetSlowQueryWarning } from './tracer';
import { MySQLServer, CFXCallback, OkPacket, ConnectionString, keyValue } from './mysql';

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

function warnIfNeeded(time: [number, number], logger: Tracer.Logger, sql: string, resource: string, interval: number) {
const queryTime = time[0] * 1e3 + time[1] * 1e-6;

if (interval <= 0 || interval > queryTime) { return; }

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`);
}

global.exports('executeAsync', (query: string, parameters?: keyValue, callback?: CFXCallback, resource?: string): void => {
const startTime = process.hrtime();

Expand Down
2 changes: 1 addition & 1 deletion source/tracer/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.1
➤ Version: 1.0.2
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
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.1
➤ Version: 1.0.2
➤ Description: MySQL library made for FiveM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
𝗚𝗡𝗨 𝗚𝗲𝗻𝗲𝗿𝗮𝗹 𝗣𝘂𝗯𝗹𝗶𝗰 𝗟𝗶𝗰𝗲𝗻𝘀𝗲 𝘃𝟯.𝟬
Expand Down
Loading

0 comments on commit 41c8a0a

Please sign in to comment.