mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e29905179 | ||
|
|
78ce45f1de | ||
|
|
193da3e6c7 | ||
|
|
cd2e73953f | ||
|
|
3928415930 | ||
|
|
b27662d138 | ||
|
|
45b90c4ad3 | ||
|
|
87a5123117 | ||
|
|
94fe76340e | ||
|
|
b7763141b7 | ||
|
|
bf2d729a39 | ||
|
|
a352273b08 | ||
|
|
e685ff5f4c | ||
|
|
dbe107e669 | ||
|
|
3e266ec952 | ||
|
|
cd2b5db342 | ||
|
|
f3fc016f31 |
No files matched your search
@@ -2,7 +2,7 @@
|
|||||||
name: Bug report
|
name: Bug report
|
||||||
about: Create a report to help us improve
|
about: Create a report to help us improve
|
||||||
title: ''
|
title: ''
|
||||||
labels: ''
|
labels: bug
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -23,7 +23,7 @@ If applicable, add screenshots to help explain your problem.
|
|||||||
|
|
||||||
**Code**
|
**Code**
|
||||||
```lua
|
```lua
|
||||||
If applicable, include the query and relevant code
|
-- If applicable, include the query and relevant code
|
||||||
```
|
```
|
||||||
|
|
||||||
**Expected behavior**
|
**Expected behavior**
|
||||||
@@ -33,6 +33,9 @@ A clear and concise description of what you expected to happen.
|
|||||||
Add any other context about the problem here.
|
Add any other context about the problem here.
|
||||||
|
|
||||||
**Server details**
|
**Server details**
|
||||||
- FX Server artifact
|
- FXServer artifact
|
||||||
- Operating system
|
- Operating system
|
||||||
- Framework
|
|
||||||
|
**Database details**
|
||||||
|
- MariaDB or MySQL
|
||||||
|
- Version
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
name: Feature request
|
||||||
|
about: Suggest an idea for this project
|
||||||
|
title: ''
|
||||||
|
labels: enhancement
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Is your feature request related to a problem? Please describe.**
|
||||||
|
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||||
|
|
||||||
|
**Describe the solution you'd like**
|
||||||
|
A clear and concise description of what you want to happen.
|
||||||
|
|
||||||
|
**Describe alternatives you've considered**
|
||||||
|
A clear and concise description of any alternative solutions or features you've considered.
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context or screenshots about the feature request here.
|
||||||
@@ -111,6 +111,7 @@ dist
|
|||||||
.vscode-test
|
.vscode-test
|
||||||
|
|
||||||
# yarn v2
|
# yarn v2
|
||||||
|
yarn.lock
|
||||||
.yarn/cache
|
.yarn/cache
|
||||||
.yarn/unplugged
|
.yarn/unplugged
|
||||||
.yarn/build-state.yml
|
.yarn/build-state.yml
|
||||||
|
|||||||
+5
-1
@@ -3,12 +3,16 @@ game 'common'
|
|||||||
|
|
||||||
name 'oxmysql'
|
name 'oxmysql'
|
||||||
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
||||||
version '1.8.0'
|
version '1.8.7'
|
||||||
url 'https://github.com/overextended/oxmysql'
|
url 'https://github.com/overextended/oxmysql'
|
||||||
author 'overextended'
|
author 'overextended'
|
||||||
use_fxv2_oal 'yes'
|
use_fxv2_oal 'yes'
|
||||||
lua54 'yes'
|
lua54 'yes'
|
||||||
|
|
||||||
|
--dependencies {
|
||||||
|
-- '/server:4837', Disabled until the heat death of the universe (or recommended artifact updates)
|
||||||
|
--}
|
||||||
|
|
||||||
server_scripts {
|
server_scripts {
|
||||||
'oxmysql.js',
|
'oxmysql.js',
|
||||||
'wrapper.lua'
|
'wrapper.lua'
|
||||||
|
|||||||
+6
-7
@@ -12,16 +12,15 @@
|
|||||||
"postinstall": "patch-package"
|
"postinstall": "patch-package"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@citizenfx/server": "^2.0.4793-1",
|
"@citizenfx/server": "latest",
|
||||||
"connection-string-parser": "^1.0.4",
|
"mysql2": "^2.3.3",
|
||||||
"mysql2": "https://github.com/overextended/node-mysql2.git",
|
|
||||||
"postinstall-postinstall": "^2.1.0"
|
"postinstall-postinstall": "^2.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"copy-webpack-plugin": "^9.0.1",
|
"copy-webpack-plugin": "^10.0.0",
|
||||||
"patch-package": "^6.4.7",
|
"patch-package": "^6.4.7",
|
||||||
"typescript": "^4.4.3",
|
"typescript": "^4.5.2",
|
||||||
"webpack": "^5.52.1",
|
"webpack": "^5.64.4",
|
||||||
"webpack-cli": "^4.8.0"
|
"webpack-cli": "^4.9.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
diff --git a/node_modules/mysql2/lib/connection_config.js b/node_modules/mysql2/lib/connection_config.js
|
||||||
|
index 11ad01b..6fab74e 100644
|
||||||
|
--- a/node_modules/mysql2/lib/connection_config.js
|
||||||
|
+++ b/node_modules/mysql2/lib/connection_config.js
|
||||||
|
@@ -187,7 +187,6 @@ class ConnectionConfig {
|
||||||
|
'LONG_PASSWORD',
|
||||||
|
'FOUND_ROWS',
|
||||||
|
'LONG_FLAG',
|
||||||
|
- 'CONNECT_WITH_DB',
|
||||||
|
'ODBC',
|
||||||
|
'LOCAL_FILES',
|
||||||
|
'IGNORE_SPACE',
|
||||||
|
@@ -200,15 +199,14 @@ class ConnectionConfig {
|
||||||
|
'TRANSACTIONS',
|
||||||
|
'SESSION_TRACK'
|
||||||
|
];
|
||||||
|
- if (options && options.multipleStatements) {
|
||||||
|
- defaultFlags.push('MULTI_STATEMENTS');
|
||||||
|
+ if (options) {
|
||||||
|
+ if (options.multipleStatements) defaultFlags.push('MULTI_STATEMENTS');
|
||||||
|
+ if (options.database) defaultFlags.push('CONNECT_WITH_DB');
|
||||||
|
+ if (options.connectAttributes) defaultFlags.push('CONNECT_ATTRS')
|
||||||
|
}
|
||||||
|
defaultFlags.push('PLUGIN_AUTH');
|
||||||
|
defaultFlags.push('PLUGIN_AUTH_LENENC_CLIENT_DATA');
|
||||||
|
|
||||||
|
- if (options && options.connectAttributes) {
|
||||||
|
- defaultFlags.push('CONNECT_ATTRS');
|
||||||
|
- }
|
||||||
|
return defaultFlags;
|
||||||
|
}
|
||||||
|
|
||||||
|
diff --git a/node_modules/mysql2/lib/packets/execute.js b/node_modules/mysql2/lib/packets/execute.js
|
||||||
|
index c612f66..02dbe22 100644
|
||||||
|
--- a/node_modules/mysql2/lib/packets/execute.js
|
||||||
|
+++ b/node_modules/mysql2/lib/packets/execute.js
|
||||||
|
@@ -28,7 +28,9 @@ function toParameter(value, encoding, timezone) {
|
||||||
|
if (value !== null) {
|
||||||
|
switch (typeof value) {
|
||||||
|
case 'undefined':
|
||||||
|
- throw new TypeError('Bind parameters must not contain undefined');
|
||||||
|
+ value = '';
|
||||||
|
+ type = Types.NULL;
|
||||||
|
+ break
|
||||||
|
|
||||||
|
case 'number':
|
||||||
|
type = Types.DOUBLE;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
diff --git a/node_modules/named-placeholders/index.js b/node_modules/named-placeholders/index.js
|
diff --git a/node_modules/named-placeholders/index.js b/node_modules/named-placeholders/index.js
|
||||||
index 527d723..f63ce28 100644
|
index 527d723..728188b 100644
|
||||||
--- a/node_modules/named-placeholders/index.js
|
--- a/node_modules/named-placeholders/index.js
|
||||||
+++ b/node_modules/named-placeholders/index.js
|
+++ b/node_modules/named-placeholders/index.js
|
||||||
@@ -3,7 +3,7 @@
|
@@ -3,7 +3,7 @@
|
||||||
@@ -11,12 +11,13 @@ index 527d723..f63ce28 100644
|
|||||||
DQUOTE = 34,
|
DQUOTE = 34,
|
||||||
SQUOTE = 39,
|
SQUOTE = 39,
|
||||||
BSLASH = 92;
|
BSLASH = 92;
|
||||||
@@ -94,15 +94,22 @@ function createCompiler(config) {
|
@@ -94,6 +94,14 @@ function createCompiler(config) {
|
||||||
if (typeof params == 'undefined')
|
if (typeof params == 'undefined')
|
||||||
throw new Error('Named query contains placeholders, but parameters object is undefined');
|
throw new Error('Named query contains placeholders, but parameters object is undefined');
|
||||||
|
|
||||||
+ for(const key in params) {
|
+ for(const key in params) {
|
||||||
+ if(key.charAt(0) === '@' || key.charAt(0) === ':') {
|
+ const char = key[0]
|
||||||
|
+ if(char == '@' || char == ':') {
|
||||||
+ params[key.substring(1)] = params[key];
|
+ params[key.substring(1)] = params[key];
|
||||||
+ delete params[key];
|
+ delete params[key];
|
||||||
+ }
|
+ }
|
||||||
@@ -24,33 +25,34 @@ index 527d723..f63ce28 100644
|
|||||||
+
|
+
|
||||||
const tokens = tree[1];
|
const tokens = tree[1];
|
||||||
for (let i=0; i < tokens.length; ++i) {
|
for (let i=0; i < tokens.length; ++i) {
|
||||||
- arr.push(params[tokens[i]]);
|
arr.push(params[tokens[i]]);
|
||||||
+ arr.push(params[tokens[i]] === undefined ? null : params[tokens[i]]);
|
@@ -102,7 +110,8 @@ function createCompiler(config) {
|
||||||
}
|
|
||||||
return [tree[0], arr];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function noTailingSemicolon(s) {
|
function noTailingSemicolon(s) {
|
||||||
- if (s.slice(-1) == ':') {
|
- if (s.slice(-1) == ':') {
|
||||||
+ if (s.slice(-1) == ':' || s.slice(-1) == '@') {
|
+ const char = s.slice(-1)
|
||||||
|
+ if (char == ':' || char == '@') {
|
||||||
return s.slice(0, -1);
|
return s.slice(0, -1);
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
@@ -115,7 +122,7 @@ function createCompiler(config) {
|
@@ -115,7 +124,8 @@ function createCompiler(config) {
|
||||||
|
|
||||||
let unnamed = noTailingSemicolon(tree[0][0]);
|
let unnamed = noTailingSemicolon(tree[0][0]);
|
||||||
for (let i=1; i < tree[0].length; ++i) {
|
for (let i=1; i < tree[0].length; ++i) {
|
||||||
- if (tree[0][i-1].slice(-1) == ':') {
|
- if (tree[0][i-1].slice(-1) == ':') {
|
||||||
+ if (tree[0][i-1].slice(-1) == ':' || tree[0][i-1].slice(-1) == '@') {
|
+ const char = tree[0][i-1].slice(-1)
|
||||||
|
+ if (char == ':' || char == '@') {
|
||||||
unnamed += config.placeholder;
|
unnamed += config.placeholder;
|
||||||
}
|
}
|
||||||
unnamed += config.placeholder;
|
unnamed += config.placeholder;
|
||||||
@@ -124,7 +131,7 @@ function createCompiler(config) {
|
@@ -124,7 +134,8 @@ function createCompiler(config) {
|
||||||
|
|
||||||
const last = tree[0][tree[0].length -1];
|
const last = tree[0][tree[0].length -1];
|
||||||
if (tree[0].length == tree[1].length) {
|
if (tree[0].length == tree[1].length) {
|
||||||
- if (last.slice(-1) == ':') {
|
- if (last.slice(-1) == ':') {
|
||||||
+ if (last.slice(-1) == ':' || last.slice(-1) == '@') {
|
+ const char = last.slice(-1)
|
||||||
|
+ if (char == ':' || char == '@') {
|
||||||
unnamed += config.placeholder;
|
unnamed += config.placeholder;
|
||||||
}
|
}
|
||||||
unnamed += config.placeholder;
|
unnamed += config.placeholder;
|
||||||
+1
-1
@@ -16,6 +16,6 @@ const isolationLevel = (() => {
|
|||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const resourceName = GetCurrentResourceName() || 'oxmysql';
|
const resourceName = GetCurrentResourceName();
|
||||||
|
|
||||||
export { slowQueryWarning, debug, isolationLevel, resourceName };
|
export { slowQueryWarning, debug, isolationLevel, resourceName };
|
||||||
+38
-9
@@ -1,13 +1,39 @@
|
|||||||
import { pool } from './pool';
|
import { pool } from './pool';
|
||||||
import { parseParameters } from './parser';
|
import { parseParameters } from './parser';
|
||||||
import { slowQueryWarning, debug, resourceName } from './config';
|
import { slowQueryWarning, debug, resourceName, isolationLevel } from './config';
|
||||||
import { FormatError } from './errors';
|
import { FormatError } from './errors';
|
||||||
|
|
||||||
|
let isReady = false;
|
||||||
|
|
||||||
|
const serverReady = async () => {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const id = setInterval(() => {
|
||||||
|
if (GetResourceState(resourceName) == 'started') resolve(id);
|
||||||
|
}, 50);
|
||||||
|
}).then((id) => {
|
||||||
|
clearInterval(id);
|
||||||
|
isReady = true;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
setImmediate(async () => {
|
||||||
|
try {
|
||||||
|
await pool.query(isolationLevel);
|
||||||
|
console.log(`^2Database server connection established!^0`);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`^3Unable to establish a connection to the database! [${error.code}]\n${error.message}^0`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const execute = async (query, parameters, resource) => {
|
const execute = async (query, parameters, resource) => {
|
||||||
|
if (!isReady) await serverReady();
|
||||||
try {
|
try {
|
||||||
[query, parameters] = parseParameters(query, parameters);
|
[query, parameters] = parseParameters(query, parameters);
|
||||||
|
|
||||||
ScheduleResourceTick(resourceName);
|
ScheduleResourceTick(resourceName);
|
||||||
const [executionTime, rows] = await pool.query(query, parameters);
|
const startTime = process.hrtime();
|
||||||
|
const [rows] = await pool.query(query, parameters);
|
||||||
|
const executionTime = process.hrtime(startTime)[1] / 1000000; // nanosecond to millisecond
|
||||||
|
|
||||||
if (executionTime >= slowQueryWarning || debug)
|
if (executionTime >= slowQueryWarning || debug)
|
||||||
console.log(
|
console.log(
|
||||||
@@ -42,6 +68,9 @@ const queryType = (query) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const preparedStatement = async (query, parameters, resource) => {
|
const preparedStatement = async (query, parameters, resource) => {
|
||||||
|
if (!isReady) await serverReady();
|
||||||
|
ScheduleResourceTick(resourceName);
|
||||||
|
const connection = await pool.getConnection();
|
||||||
try {
|
try {
|
||||||
if (!Array.isArray(parameters))
|
if (!Array.isArray(parameters))
|
||||||
throw new FormatError(`Placeholders were defined, but query received no parameters!`, query);
|
throw new FormatError(`Placeholders were defined, but query received no parameters!`, query);
|
||||||
@@ -51,21 +80,19 @@ const preparedStatement = async (query, parameters, resource) => {
|
|||||||
const type = queryType(query);
|
const type = queryType(query);
|
||||||
if (!type) throw new FormatError(`Prepared statements only accept SELECT, INSERT, UPDATE, and DELETE methods!`);
|
if (!type) throw new FormatError(`Prepared statements only accept SELECT, INSERT, UPDATE, and DELETE methods!`);
|
||||||
|
|
||||||
ScheduleResourceTick(resourceName);
|
|
||||||
const results = [];
|
const results = [];
|
||||||
let totalTime = 0;
|
|
||||||
let queryCount = parameters.length;
|
let queryCount = parameters.length;
|
||||||
|
const startTime = process.hrtime();
|
||||||
|
|
||||||
for (let i = 0; i < queryCount; i++) {
|
for (let i = 0; i < queryCount; i++) {
|
||||||
const [executionTime, rows] = await pool.execute(query, parameters[i]);
|
const [rows] = await connection.execute(query, parameters[i]);
|
||||||
totalTime + executionTime;
|
|
||||||
results[i] = rows && (type === 3 ? rows.affectedRows : type === 2 ? rows.insertId : rows);
|
results[i] = rows && (type === 3 ? rows.affectedRows : type === 2 ? rows.insertId : rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
totalTime = totalTime / queryCount;
|
const executionTime = process.hrtime(startTime)[1] / 1000000; // nanosecond to millisecond
|
||||||
if (totalTime >= slowQueryWarning || debug)
|
if (executionTime >= slowQueryWarning || debug)
|
||||||
console.log(
|
console.log(
|
||||||
`^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${totalTime}ms to execute ${
|
`^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${executionTime}ms to execute ${
|
||||||
queryCount > 1 ? queryCount + ' queries' : 'a query'
|
queryCount > 1 ? queryCount + ' queries' : 'a query'
|
||||||
}!
|
}!
|
||||||
${query} ${JSON.stringify(parameters)}^0`
|
${query} ${JSON.stringify(parameters)}^0`
|
||||||
@@ -88,6 +115,8 @@ const preparedStatement = async (query, parameters, resource) => {
|
|||||||
${error.sql || `${query} ${JSON.stringify(parameters)}`}^0`
|
${error.sql || `${query} ${JSON.stringify(parameters)}`}^0`
|
||||||
);
|
);
|
||||||
debug && console.trace(error);
|
debug && console.trace(error);
|
||||||
|
} finally {
|
||||||
|
connection.release();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+11
-29
@@ -1,52 +1,35 @@
|
|||||||
import { pool } from './pool';
|
|
||||||
import { execute, preparedStatement } from './execute';
|
import { execute, preparedStatement } from './execute';
|
||||||
import { transaction } from './transaction';
|
import { transaction } from './transaction';
|
||||||
import { debug, isolationLevel } from './config';
|
import { debug } from './config';
|
||||||
|
|
||||||
setImmediate(async () => {
|
const safeCallback = (callback, result) => {
|
||||||
try {
|
if (typeof callback === 'function') callback(result);
|
||||||
await pool.query(isolationLevel);
|
|
||||||
console.log(`^2Database server connection established!^0`);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`^3Unable to establish a connection to the database! [${error.code}]\n${error.message}^0`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const safeCallback = (callback, result, resource, query) => {
|
|
||||||
if (typeof callback === 'function')
|
|
||||||
return callback(result);
|
|
||||||
else if (debug)
|
|
||||||
return console.log(`^3[WARNING] ${resource} executed a query, but no callback function was defined!\n ^3 ${query}^0`);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
global.exports('execute', (query, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('execute', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
execute(query, parameters, resource).then((result) =>
|
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
|
||||||
safeCallback(cb || parameters, result, resource, query));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports('insert', (query, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('insert', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
execute(query, parameters, resource).then((result) =>
|
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result.insertId));
|
||||||
safeCallback(cb || parameters, result && result.insertId, resource, query));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports('update', (query, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('update', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
execute(query, parameters, resource).then((result) =>
|
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result.affectedRows));
|
||||||
safeCallback(cb || parameters, result && result.affectedRows, resource, query));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports('fetch', (query, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('fetch', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
execute(query, parameters, resource).then((result) =>
|
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
|
||||||
safeCallback(cb || parameters, result, resource, query));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports('single', (query, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('single', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
execute(query, parameters, resource).then((result) =>
|
execute(query, parameters, resource).then((result) => safeCallback(cb || parameters, result && result[0]));
|
||||||
safeCallback(cb || parameters, result && result[0], resource, query));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports('scalar', (query, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('scalar', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
execute(query, parameters, resource).then((result) =>
|
execute(query, parameters, resource).then((result) =>
|
||||||
safeCallback(cb || parameters, result && result[0] && Object.values(result[0])[0], resource, query));
|
safeCallback(cb || parameters, result && result[0] && Object.values(result[0])[0])
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
global.exports('transaction', (queries, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('transaction', (queries, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
@@ -56,8 +39,7 @@ global.exports('transaction', (queries, parameters, cb, resource = GetInvokingRe
|
|||||||
});
|
});
|
||||||
|
|
||||||
global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource()) => {
|
global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
preparedStatement(query, parameters, resource).then((result) =>
|
preparedStatement(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
|
||||||
safeCallback(cb || parameters, result, resource, query))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
|
if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
|
||||||
|
|||||||
+19
-15
@@ -3,8 +3,9 @@ import * as createCompiler from 'named-placeholders';
|
|||||||
|
|
||||||
const convertNamedPlaceholders = createCompiler();
|
const convertNamedPlaceholders = createCompiler();
|
||||||
|
|
||||||
|
// DATE compatibility with mysql-async
|
||||||
|
// https://github.com/brouznouf/fivem-mysql-async/blob/master/mysql-async.js#L15499-L15536
|
||||||
const parseTypes = (field, next) => {
|
const parseTypes = (field, next) => {
|
||||||
//https://github.com/GHMatti/ghmattimysql/blob/37f1d2ae5c53f91782d168fe81fba80512d3c46d/packages/ghmattimysql/src/server/utility/typeCast.ts#L3
|
|
||||||
switch (field.type) {
|
switch (field.type) {
|
||||||
case 'DATETIME':
|
case 'DATETIME':
|
||||||
case 'DATETIME2':
|
case 'DATETIME2':
|
||||||
@@ -16,8 +17,7 @@ const parseTypes = (field, next) => {
|
|||||||
? new Date(field.string() + ' 00:00:00').getTime()
|
? new Date(field.string() + ' 00:00:00').getTime()
|
||||||
: new Date(field.string()).getTime();
|
: new Date(field.string()).getTime();
|
||||||
case 'TINY':
|
case 'TINY':
|
||||||
if (field.length == 1) return field.string() === '1';
|
return field.length === 1 ? field.string() === '1' : next();
|
||||||
else return next();
|
|
||||||
case 'BIT':
|
case 'BIT':
|
||||||
return field.buffer()[0] === 1;
|
return field.buffer()[0] === 1;
|
||||||
default:
|
default:
|
||||||
@@ -27,23 +27,27 @@ const parseTypes = (field, next) => {
|
|||||||
|
|
||||||
const parseParameters = (query, parameters) => {
|
const parseParameters = (query, parameters) => {
|
||||||
if (typeof query !== 'string')
|
if (typeof query !== 'string')
|
||||||
throw new Error(`Non-string query passed. Make sure you use "oxmysql:execute" instead of "oxmysql.execute" when calling exports.`);
|
throw new Error(
|
||||||
|
`Non-string query passed. Make sure you use "oxmysql:execute" instead of "oxmysql.execute" when calling exports.`
|
||||||
|
);
|
||||||
|
|
||||||
if (!parameters || typeof parameters === 'function') return [query, []];
|
if (!parameters || typeof parameters === 'function') return [query, []];
|
||||||
|
|
||||||
if (query.includes('@') || query.includes(':')) {
|
if (query.includes('@') || query.includes(':')) {
|
||||||
const obj = parameters.length !== 0 ? parameters : (() => {
|
const obj =
|
||||||
let obj = {};
|
parameters.length !== 0
|
||||||
const [_, paramNames] = convertNamedPlaceholders.parse(query);
|
? parameters
|
||||||
if (paramNames) {
|
: (() => {
|
||||||
for (let i = 0; i < paramNames.length; i++)
|
let obj = {};
|
||||||
obj[paramNames[i]] = null;
|
const [_, paramNames] = convertNamedPlaceholders.parse(query);
|
||||||
}
|
if (paramNames) {
|
||||||
return obj;
|
for (let i = 0; i < paramNames.length; i++) obj[paramNames[i]] = null;
|
||||||
})();
|
}
|
||||||
|
return obj;
|
||||||
|
})();
|
||||||
|
|
||||||
return [query, obj]
|
return [query, obj];
|
||||||
};
|
}
|
||||||
|
|
||||||
const queryParams = query.match(/\?(?!\?)/g);
|
const queryParams = query.match(/\?(?!\?)/g);
|
||||||
|
|
||||||
|
|||||||
+23
-53
@@ -1,6 +1,5 @@
|
|||||||
import { createPool } from 'mysql2/promise';
|
import { createPool } from 'mysql2/promise';
|
||||||
import { parseTypes } from './parser';
|
import { parseTypes } from './parser';
|
||||||
import { ConnectionStringParser } from 'connection-string-parser';
|
|
||||||
|
|
||||||
const connectionString = GetConvar('mysql_connection_string', '');
|
const connectionString = GetConvar('mysql_connection_string', '');
|
||||||
|
|
||||||
@@ -8,60 +7,31 @@ if (connectionString === '') {
|
|||||||
throw new Error(`Set up mysql_connection_string in server.cfg`);
|
throw new Error(`Set up mysql_connection_string in server.cfg`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const parseSemiColons = () => {
|
const dbOptions = (() => {
|
||||||
const parts = connectionString.split(';');
|
const options = connectionString.includes('mysql://')
|
||||||
if (parseTypes.length === 1) {
|
? { uri: connectionString }
|
||||||
throw new Error(
|
: connectionString
|
||||||
`Set up mysql_connection_string in correct format - 'username=root;password=password;database=db;host=127.0.0.1'`
|
.replace(/(?:host(?:name)|ip|server|data\s?source|addr(?:ess)?)=/gi, 'host=')
|
||||||
);
|
.replace(/(?:user\s?(?:id|name)?|uid)=/gi, 'user=')
|
||||||
}
|
.replace(/(?:pwd|pass)=/gi, 'password=')
|
||||||
return parts.reduce((connectionInfo, parameter) => {
|
.replace(/(?:db)=/gi, 'database=')
|
||||||
const [key, value] = parameter.split('=');
|
.split(';')
|
||||||
connectionInfo[key] = value;
|
.reduce((connectionInfo, parameter) => {
|
||||||
return connectionInfo;
|
const [key, value] = parameter.split('=');
|
||||||
}, {});
|
connectionInfo[key] = value;
|
||||||
};
|
return connectionInfo;
|
||||||
|
}, {});
|
||||||
|
|
||||||
// @TODO: refactor acceptable options when using semicolon connection; this or stuff is getting old
|
options.typeCast = parseTypes;
|
||||||
const createConnection = () => {
|
options.namedPlaceholders = true;
|
||||||
if (connectionString.includes('host=')) {
|
|
||||||
const options = parseSemiColons();
|
|
||||||
|
|
||||||
return createPool({
|
// increase the default timeout for slow servers
|
||||||
host: options.host || 'localhost',
|
options.connectTimeout = options.connectTimeout || 60000;
|
||||||
port: options.port || 3306,
|
|
||||||
user: options.username || options.user || options.userid || options.uid || 'root',
|
|
||||||
password: options.password || options.pass || options.pwd || '',
|
|
||||||
database: options.endpoint || options.database,
|
|
||||||
charset: 'utf8mb4_unicode_ci',
|
|
||||||
connectTimeout: 30000,
|
|
||||||
...options.options,
|
|
||||||
namedPlaceholders: true,
|
|
||||||
typeCast: parseTypes,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
const options = new ConnectionStringParser({
|
|
||||||
scheme: 'mysql',
|
|
||||||
hosts: [],
|
|
||||||
}).parse(connectionString);
|
|
||||||
|
|
||||||
if (Object.keys(options).length === 0) {
|
// disabled to prevent SQL injection
|
||||||
throw new Error(`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`);
|
options.multipleStatements = options.multipleStatements || false;
|
||||||
}
|
|
||||||
|
|
||||||
return createPool({
|
return options;
|
||||||
host: options.hosts[0].host || 'localhost',
|
})();
|
||||||
port: options.hosts[0].port || 3306,
|
|
||||||
user: options.username || 'root',
|
|
||||||
password: options.password || '',
|
|
||||||
database: options.endpoint,
|
|
||||||
charset: 'utf8mb4_unicode_ci',
|
|
||||||
connectTimeout: 30000,
|
|
||||||
...options.options,
|
|
||||||
namedPlaceholders: true,
|
|
||||||
typeCast: parseTypes,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const pool = createConnection();
|
export const pool = createPool(dbOptions);
|
||||||
+2
-2
@@ -13,10 +13,10 @@ const transaction = async (queries, parameters, resource) => {
|
|||||||
ScheduleResourceTick(resourceName);
|
ScheduleResourceTick(resourceName);
|
||||||
const connection = await pool.getConnection();
|
const connection = await pool.getConnection();
|
||||||
try {
|
try {
|
||||||
const time = debug ? process.hrtime.bigint() : Date.now();
|
|
||||||
|
|
||||||
const fullQuery = parseTransaction(queries, parameters);
|
const fullQuery = parseTransaction(queries, parameters);
|
||||||
const transactionAmount = fullQuery.length;
|
const transactionAmount = fullQuery.length;
|
||||||
|
const startTime = process.hrtime();
|
||||||
|
|
||||||
await connection.beginTransaction();
|
await connection.beginTransaction();
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ const transaction = async (queries, parameters, resource) => {
|
|||||||
|
|
||||||
await connection.commit();
|
await connection.commit();
|
||||||
|
|
||||||
const executionTime = debug ? Number(process.hrtime.bigint() - time) / 1e6 : Date.now() - time;
|
const executionTime = process.hrtime(startTime)[1] / 1000000;
|
||||||
|
|
||||||
if (executionTime >= slowQueryWarning * transactionAmount || debug)
|
if (executionTime >= slowQueryWarning * transactionAmount || debug)
|
||||||
console.log(
|
console.log(
|
||||||
|
|||||||
Reference in new issue
Block a user