Skip to content

Commit

Permalink
test: revert recent changes
Browse files Browse the repository at this point in the history
People are experiencing errors that I've been unable to reproduce, but 1.8.3 apparently works fine.
Revert changes that were made.

This disables slow query warnings for now.
  • Loading branch information
thelindat committed Dec 7, 2021
1 parent b27662d commit 3928415
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 174 deletions.
5 changes: 4 additions & 1 deletion lua/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ author 'overextended'
use_fxv2_oal 'yes'
lua54 'yes'

server_scripts {
--dependencies {
-- '/server:4837', Disabled until the heat death of the universe (or recommended artifact updates)
--}

server_scripts {
'oxmysql.js',
'wrapper.lua'
}
Expand Down
185 changes: 90 additions & 95 deletions lua/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,105 +28,100 @@ CreateThread(function()
end, 'GET')
end)

-- Check for the existance of a native from FXServer 4837 to disable Lua exports
-- Shame dependency constraints were added immediately after the recommended build
if not MumbleSetPlayerMuted then

---@param query string
---@param parameters? table
---@return integer result
---returns array of matching rows or result data
exports('executeSync', function(query, parameters)
local p = promise.new()
oxmysql:execute(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return integer result
---returns array of matching rows or result data
exports('executeSync', function(query, parameters)
local p = promise.new()
oxmysql:execute(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return table result
---returns array of matching rows or result data
exports('fetchSync', function(query, parameters)
local p = promise.new()
oxmysql:fetch(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return table result
---returns array of matching rows or result data
exports('fetchSync', function(query, parameters)
local p = promise.new()
oxmysql:fetch(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return table result
---returns table containing key value pairs
exports('singleSync', function(query, parameters)
local p = promise.new()
oxmysql:single(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return table result
---returns table containing key value pairs
exports('singleSync', function(query, parameters)
local p = promise.new()
oxmysql:single(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return integer|string
---returns value of the first column of a single row
exports('scalarSync', function(query, parameters)
local p = promise.new()
oxmysql:scalar(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return integer|string
---returns value of the first column of a single row
exports('scalarSync', function(query, parameters)
local p = promise.new()
oxmysql:scalar(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return table result
---returns the last inserted id
exports('insertSync', function(query, parameters)
local p = promise.new()
oxmysql:insert(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return table result
---returns the last inserted id
exports('insertSync', function(query, parameters)
local p = promise.new()
oxmysql:insert(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters? table
---@return integer result
---returns number of rows updated by the executed query
exports('updateSync', function(query, parameters)
local p = promise.new()
oxmysql:update(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param query string
---@param parameters? table
---@return integer result
---returns number of rows updated by the executed query
exports('updateSync', function(query, parameters)
local p = promise.new()
oxmysql:update(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param queries table
---@param parameters? table
---@return boolean result
---returns true when the transaction has succeeded
exports('transactionSync', function(queries, parameters)
local p = promise.new()
oxmysql:transaction(queries, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
---@param queries table
---@param parameters? table
---@return boolean result
---returns true when the transaction has succeeded
exports('transactionSync', function(queries, parameters)
local p = promise.new()
oxmysql:transaction(queries, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)

---@param query string
---@param parameters table
---@return any result
---supports SELECT, INSERT, UPDATE, and DELETE
---
---return value is based on the number of results and the type of query used
exports('prepareSync', function(query, parameters)
local p = promise.new()
oxmysql:prepare(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
end
---@param query string
---@param parameters table
---@return any result
---supports SELECT, INSERT, UPDATE, and DELETE
---
---return value is based on the number of results and the type of query used
exports('prepareSync', function(query, parameters)
local p = promise.new()
oxmysql:prepare(query, parameters, function(result)
p:resolve(result)
end, GetInvokingResource())
return Citizen.Await(p)
end)
40 changes: 18 additions & 22 deletions src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ import { slowQueryWarning, debug, resourceName } from './config';
import { FormatError } from './errors';

const execute = async (query, parameters, resource) => {
ScheduleResourceTick(resourceName);
const connection = await pool.getConnection();
try {
[query, parameters] = parseParameters(query, parameters);

const startTime = process.hrtime();
const [rows] = await connection.query(query, parameters);
const executionTime = process.hrtime(startTime)[1] / 1000000; // nanosecond to millisecond
ScheduleResourceTick(resourceName);
// 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)
console.log(
`^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${executionTime}ms to execute a query!
${query} ${JSON.stringify(parameters)}^0`
);
// if (executionTime >= slowQueryWarning || debug)
// console.log(
// `^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${executionTime}ms to execute a query!
// ${query} ${JSON.stringify(parameters)}^0`
// );

connection.release();
return rows;
} catch (error) {
console.log(
Expand All @@ -28,8 +26,6 @@ const execute = async (query, parameters, resource) => {
${error.sql || `${query} ${JSON.stringify(parameters)}`}^0`
);
debug && console.trace(error);
} finally {
connection.release();
}
};

Expand Down Expand Up @@ -62,21 +58,21 @@ const preparedStatement = async (query, parameters, resource) => {

const results = [];
let queryCount = parameters.length;
const startTime = process.hrtime();
// const startTime = process.hrtime();

for (let i = 0; i < queryCount; i++) {
const [rows] = await connection.execute(query, parameters[i]);
results[i] = rows && (type === 3 ? rows.affectedRows : type === 2 ? rows.insertId : rows);
}

const executionTime = process.hrtime(startTime)[1] / 1000000; // nanosecond to millisecond
if (executionTime >= slowQueryWarning || debug)
console.log(
`^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${executionTime}ms to execute ${
queryCount > 1 ? queryCount + ' queries' : 'a query'
}!
${query} ${JSON.stringify(parameters)}^0`
);
// const executionTime = process.hrtime(startTime)[1] / 1000000; // nanosecond to millisecond
// if (executionTime >= slowQueryWarning || debug)
// console.log(
// `^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${executionTime}ms to execute ${
// queryCount > 1 ? queryCount + ' queries' : 'a query'
// }!
// ${query} ${JSON.stringify(parameters)}^0`
// );

if (results.length === 1) {
if (type === 1) {
Expand Down
89 changes: 41 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,51 +52,44 @@ global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource
preparedStatement(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
});

try {
// Check for the existance of a native from FXServer 4837 to enable JS exports
if (MumbleSetPlayerMuted || !GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
global.exports('prepareSync', async (query, parameters) => {
const result = await preparedStatement(query, parameters, GetInvokingResource());
return result;
});

global.exports('executeSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('insertSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.insertId;
});

global.exports('updateSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.affectedRows;
});

global.exports('fetchSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('singleSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0];
});

global.exports('scalarSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0] && Object.values(result[0])[0];
});

global.exports('transactionSync', async (queries, parameters) => {
const result = await transaction(queries, parameters, GetInvokingResource());
return result;
});
}
} catch (e) {
setTimeout(() => {
console.log(`^3Unable to load enhanced sync exports (download FXServer 4837+)^0`);
}, 1000);
}
if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
global.exports('prepareSync', async (query, parameters) => {
const result = await preparedStatement(query, parameters, GetInvokingResource());
return result;
});

global.exports('executeSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('insertSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.insertId;
});

global.exports('updateSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result.affectedRows;
});

global.exports('fetchSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result;
});

global.exports('singleSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0];
});

global.exports('scalarSync', async (query, parameters) => {
const result = await execute(query, parameters, GetInvokingResource());
return result && result[0] && Object.values(result[0])[0];
});

global.exports('transactionSync', async (queries, parameters) => {
const result = await transaction(queries, parameters, GetInvokingResource());
return result;
});
}
Loading

0 comments on commit 3928415

Please sign in to comment.