-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
498 lines (434 loc) · 16.9 KB
/
server.js
File metadata and controls
498 lines (434 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
* ====================================================================================
* ingenium.sql - MySQL Query Implementation
* Main server-side implementation using mysql2 connection pool
* Based on oxmysql architecture adapted for ingenium framework
* ====================================================================================
*/
// Reference the pool from global scope (created by _pool.js)
// In FiveM, server_scripts share the same global scope
// Prepared queries storage
let preparedQueries = new Map();
let queryIdCounter = 0;
// Cache for compiled regex patterns (performance optimization)
const regexCache = new Map();
/**
* Initialize message
*/
setImmediate(() => {
console.log('^2[ig.sql] Query handler initialized^7');
});
/**
* Get or create a cached regex pattern for a parameter name
* @param {string} paramName - The parameter name (e.g., "@name")
* @returns {RegExp} Cached or newly created regex pattern (non-global for replace)
*/
function getCachedRegex(paramName) {
if (!regexCache.has(paramName)) {
const escaped = paramName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Use non-global regex since replace() replaces all occurrences anyway
// and global regex maintains state between calls
regexCache.set(paramName, new RegExp(escaped, 'g'));
}
return regexCache.get(paramName);
}
/**
* Helper function to process parameters (OPTIMIZED)
* Converts named parameters (@param) to positional (?) and returns array
*/
function processParameters(query, parameters) {
if (!parameters || typeof parameters !== 'object') {
return { query, params: [] };
}
// If parameters is an array, it's already positional
if (Array.isArray(parameters)) {
return { query, params: parameters };
}
// Quick check: if no @ symbol, return early
if (query.indexOf('@') === -1) {
return { query, params: [] };
}
// Convert named parameters to positional
const params = [];
let processedQuery = query;
// Find all unique @paramName in query and replace with ?
const paramNames = query.match(/@\w+/g);
if (paramNames) {
// Use a Set to track unique parameter names for efficiency
const seen = new Set();
for (const paramName of paramNames) {
// Skip if already processed
if (seen.has(paramName)) continue;
seen.add(paramName);
const key = paramName.substring(1); // Remove @
// Skip if parameter value not provided
if (!parameters.hasOwnProperty(key)) continue;
// Get cached regex pattern
const regex = getCachedRegex(paramName);
// Count occurrences - match() doesn't modify lastIndex but we create
// a fresh match each time to avoid any state issues
const matches = processedQuery.match(regex);
const count = matches ? matches.length : 0;
// Add the parameter value for each occurrence
for (let i = 0; i < count; i++) {
params.push(parameters[key]);
}
// Replace all occurrences with ? (replace with 'g' flag replaces all)
processedQuery = processedQuery.replace(regex, '?');
}
}
return { query: processedQuery, params };
}
/**
* Helper function to execute callback if provided
* @param {*} result - The result to return
* @param {Function} callback - Optional callback function
* @returns {*} The result
*/
function executeCallback(result, callback) {
if (callback && typeof callback === 'function') {
callback(result);
}
return result;
}
/**
* Helper function to wrap query execution with pool readiness check and error handling
* @param {Function} asyncFn - The async function to execute
* @param {string} errorContext - Context string for error messages
* @param {*} defaultReturn - Default value to return on error
* @param {Function} callback - Optional callback function
* @returns {Promise<*>} Result or default value
*/
async function withPoolCheck(asyncFn, errorContext, defaultReturn, callback) {
try {
if (!global.pool || !global.pool.ready()) {
throw new Error('Connection pool is not ready');
}
const result = await asyncFn();
return executeCallback(result, callback);
} catch (error) {
console.error(`^1[ig.sql ERROR] ${errorContext}: ${error.message}^7`);
return executeCallback(defaultReturn, callback);
}
}
/**
* Execute a SELECT query that returns multiple rows
*/
async function query(query, parameters, callback) {
return withPoolCheck(
async () => {
const { query: processedQuery, params } = processParameters(query, parameters);
return await global.pool.execute(processedQuery, params);
},
'Query failed',
[],
callback
);
}
/**
* Execute a SELECT query that returns a single row
*/
async function fetchSingle(query, parameters, callback) {
return withPoolCheck(
async () => {
const { query: processedQuery, params } = processParameters(query, parameters);
const results = await global.pool.execute(processedQuery, params);
return results.length > 0 ? results[0] : null;
},
'FetchSingle failed',
null,
callback
);
}
/**
* Execute a SELECT query that returns a single value (scalar)
*/
async function fetchScalar(query, parameters, callback) {
return withPoolCheck(
async () => {
const { query: processedQuery, params } = processParameters(query, parameters);
const results = await global.pool.execute(processedQuery, params);
let value = null;
if (results.length > 0) {
const firstRow = results[0];
const firstKey = Object.keys(firstRow)[0];
value = firstRow[firstKey];
}
return value;
},
'FetchScalar failed',
null,
callback
);
}
/**
* Execute an INSERT query and return the insert ID
*/
async function insert(query, parameters, callback) {
return withPoolCheck(
async () => {
const { query: processedQuery, params } = processParameters(query, parameters);
const results = await global.pool.execute(processedQuery, params);
return results.insertId || 0;
},
'Insert failed',
0,
callback
);
}
/**
* Execute an UPDATE or DELETE query and return affected rows
*/
async function update(query, parameters, callback) {
return withPoolCheck(
async () => {
const { query: processedQuery, params } = processParameters(query, parameters);
const results = await global.pool.execute(processedQuery, params);
return results.affectedRows || 0;
},
'Update failed',
0,
callback
);
}
/**
* Execute multiple queries in a transaction
*/
async function transaction(queries, callback) {
let connection = null;
try {
if (!global.pool || !global.pool.ready()) {
throw new Error('Connection pool is not ready');
}
connection = await global.pool.getConnection();
await connection.beginTransaction();
const results = [];
for (const queryData of queries) {
const { query: processedQuery, params } = processParameters(
queryData.query || queryData[0],
queryData.parameters || queryData[1] || []
);
const [result] = await connection.execute(processedQuery, params);
results.push(result);
}
await connection.commit();
if (callback && typeof callback === 'function') {
callback(true, results);
}
return { success: true, results };
} catch (error) {
if (connection) {
await connection.rollback();
}
console.error(`^1[ig.sql ERROR] Transaction failed: ${error.message}^7`);
if (callback && typeof callback === 'function') {
callback(false, []);
}
return { success: false, results: [] };
} finally {
if (connection) {
connection.release();
}
}
}
/**
* Execute multiple queries as a batch (without transaction)
*/
async function batch(queries, callback) {
return withPoolCheck(
async () => {
const results = [];
for (const queryData of queries) {
const { query: processedQuery, params } = processParameters(
queryData.query || queryData[0],
queryData.parameters || queryData[1] || []
);
const result = await global.pool.execute(processedQuery, params);
results.push(result);
}
return results;
},
'Batch failed',
[],
callback
);
}
// Cache for query type detection (performance optimization)
const queryTypeCache = new Map();
const QUERY_TYPE_CACHE_MAX_SIZE = 1000; // Increased limit for better caching
/**
* Normalize query for caching by removing parameter values
* This allows caching based on query pattern rather than exact query text
* @param {string} sqlQuery - The SQL query string
* @returns {string} Normalized query pattern
*/
function normalizeQueryForCache(sqlQuery) {
// Extract just the first 100 characters to create a pattern key
// This balances memory usage with cache effectiveness
return sqlQuery.substring(0, 100).trim();
}
/**
* Detect SQL query type from query string (OPTIMIZED with caching)
* @param {string} sqlQuery - The SQL query string
* @returns {string} Query type (SELECT, INSERT, UPDATE, DELETE, or empty string)
*/
function detectQueryType(sqlQuery) {
// Create a normalized cache key to handle queries with different parameters
const cacheKey = normalizeQueryForCache(sqlQuery);
// For very common queries, check cache first
if (queryTypeCache.has(cacheKey)) {
return queryTypeCache.get(cacheKey);
}
// Extract first SQL keyword more robustly (handles leading whitespace and comments)
const match = sqlQuery.match(/^\s*(\w+)/i);
const queryType = match ? match[1].toUpperCase() : '';
// Cache the result (with size limit to prevent unbounded growth)
if (queryTypeCache.size < QUERY_TYPE_CACHE_MAX_SIZE) {
queryTypeCache.set(cacheKey, queryType);
}
return queryType;
}
/**
* Prepare a query for later execution
* Returns a query ID that can be used with executePrepared
*/
function prepareQuery(query) {
try {
const queryId = `prepared_${++queryIdCounter}`;
preparedQueries.set(queryId, query);
return queryId;
} catch (error) {
console.error(`^1[ig.sql ERROR] PrepareQuery failed: ${error.message}^7`);
return null;
}
}
/**
* Execute a prepared query
*/
async function executePrepared(queryId, parameters, callback) {
try {
if (!preparedQueries.has(queryId)) {
throw new Error(`Prepared query not found: ${queryId}`);
}
const storedQuery = preparedQueries.get(queryId);
return await execute(storedQuery, parameters, callback);
} catch (error) {
console.error(`^1[ig.sql ERROR] ExecutePrepared failed: ${error.message}^7`);
return executeCallback(null, callback);
}
}
/**
* Execute function - compatibility wrapper for oxmysql and mysql-async
* Automatically routes to the appropriate function based on query type
* Note: Callbacks are handled by this function's withPoolCheck wrapper, not passed
* to the underlying query functions, to avoid double-callback invocation.
*/
async function execute(sqlQuery, parameters, callback) {
return withPoolCheck(
async () => {
const queryType = detectQueryType(sqlQuery);
switch (queryType) {
case 'SELECT':
// Call without callback - result will bubble up to our withPoolCheck
return await query(sqlQuery, parameters);
case 'INSERT':
return await insert(sqlQuery, parameters);
case 'UPDATE':
case 'DELETE':
return await update(sqlQuery, parameters);
default:
console.warn(`^3[ig.sql WARNING] Unknown query type '${queryType}', defaulting to query handler^7`);
return await query(sqlQuery, parameters);
}
},
'Execute failed',
null,
callback
);
}
// ====================================================================================
// Export all functions for use by other resources
// ====================================================================================
// Primary exports (ingenium.sql native API)
global.exports('query', query);
global.exports('fetchSingle', fetchSingle);
global.exports('fetchScalar', fetchScalar);
global.exports('insert', insert);
global.exports('update', update);
global.exports('transaction', transaction);
global.exports('batch', batch);
global.exports('prepareQuery', prepareQuery);
global.exports('executePrepared', executePrepared);
global.exports('isReady', () => global.pool ? global.pool.ready() : false);
global.exports('getStats', () => global.pool ? global.pool.getStats() : null);
// ====================================================================================
// Compatibility exports for oxmysql and mysql-async
// ====================================================================================
global.exports('single', fetchSingle); // oxmysql: single = fetchSingle
global.exports('scalar', fetchScalar); // oxmysql: scalar = fetchScalar
global.exports('prepare', prepareQuery); // oxmysql: prepare = prepareQuery
global.exports('execute', execute); // oxmysql/mysql-async: smart execute function
global.exports('fetchAll', query); // mysql-async: fetchAll = query
console.log('^2[ig.sql] Server exports registered (with oxmysql/mysql-async compatibility)^7');
// ====================================================================================
// Test Command - Check SQL Connection
// ====================================================================================
/**
* Register /sqlcheck command to test database connection
* Checks if the 'db' database exists
*/
RegisterCommand('sqlcheck', async (source, args, rawCommand) => {
try {
const playerId = source;
// Check if pool is ready
if (!global.pool || !global.pool.ready()) {
const message = '^1[SQL Check] Database connection pool is not ready^7';
console.log(message);
if (playerId > 0) {
emitNet('chat:addMessage', playerId, {
color: [255, 0, 0],
multiline: true,
args: ['SQL Check', 'Database connection pool is not ready']
});
}
return;
}
// Test query to check if database 'db' exists
const testQuery = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'db'";
console.log('^3[SQL Check] Testing database connection...^7');
const result = await query(testQuery, []);
if (result && result.length > 0) {
const successMessage = `^2[SQL Check] SUCCESS: Database 'db' exists and is accessible^7`;
console.log(successMessage);
console.log(`^2[SQL Check] Pool Stats: ${JSON.stringify(global.pool.getStats())}^7`);
if (playerId > 0) {
emitNet('chat:addMessage', playerId, {
color: [0, 255, 0],
multiline: true,
args: ['SQL Check', `✓ SUCCESS: Database 'db' exists and is accessible`]
});
}
} else {
const notFoundMessage = `^3[SQL Check] WARNING: Database 'db' does not exist^7`;
console.log(notFoundMessage);
if (playerId > 0) {
emitNet('chat:addMessage', playerId, {
color: [255, 165, 0],
multiline: true,
args: ['SQL Check', `⚠ WARNING: Database 'db' does not exist`]
});
}
}
} catch (error) {
const errorMessage = `^1[SQL Check] ERROR: ${error.message}^7`;
console.error(errorMessage);
if (source > 0) {
emitNet('chat:addMessage', source, {
color: [255, 0, 0],
multiline: true,
args: ['SQL Check', `✗ ERROR: ${error.message}`]
});
}
}
}, false); // false = can be run by anyone (not restricted to admins)
console.log('^2[ingenium.sql] /sqlcheck command registered^7');