mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 06:28:54 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7763141b7 | ||
|
|
bf2d729a39 | ||
|
|
a352273b08 | ||
|
|
e685ff5f4c | ||
|
|
dbe107e669 | ||
|
|
3e266ec952 | ||
|
|
cd2b5db342 | ||
|
|
f3fc016f31 | ||
|
|
381627266f | ||
|
|
f779ba4228 | ||
|
|
b004ec943d | ||
|
|
647243f1cf | ||
|
|
12e9ccbc8a | ||
|
|
2a9b14ce54 | ||
|
|
f7949db0d3 | ||
|
|
2a14ab1e21 | ||
|
|
9de50f684f | ||
|
|
2c90290c84 | ||
|
|
9fc06c2132 | ||
|
|
def891268b | ||
|
|
8a3fa20408 | ||
|
|
5d6ca06f49 | ||
|
|
4913d6e50a | ||
|
|
ca8cef4e26 | ||
|
|
2a8f4b1ea9 | ||
|
|
1d0a5bcfd9 | ||
|
|
5965442e48 | ||
|
|
f7ed2d5b60 | ||
|
|
ec9d787be4 |
No files matched your search
@@ -0,0 +1,38 @@
|
|||||||
|
---
|
||||||
|
name: Bug report
|
||||||
|
about: Create a report to help us improve
|
||||||
|
title: ''
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Issue checklist
|
||||||
|
> Please put `x` inside of the box that matches your issue.
|
||||||
|
|
||||||
|
- [ ] I am using the latest release
|
||||||
|
- [ ] I have referenced previously reported issues
|
||||||
|
- [ ] I have referenced available documentation and cannot resolve the issue
|
||||||
|
- [ ] I'm certain this is an issue with oxymysql, and not with my resource
|
||||||
|
|
||||||
|
**Describe the bug**
|
||||||
|
A clear and concise description of what the bug is.
|
||||||
|
|
||||||
|
**Screenshots**
|
||||||
|
If applicable, add screenshots to help explain your problem.
|
||||||
|
|
||||||
|
**Code**
|
||||||
|
```lua
|
||||||
|
If applicable, include the query and relevant code
|
||||||
|
```
|
||||||
|
|
||||||
|
**Expected behavior**
|
||||||
|
A clear and concise description of what you expected to happen.
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context about the problem here.
|
||||||
|
|
||||||
|
**Server details**
|
||||||
|
- FX Server artifact
|
||||||
|
- Operating system
|
||||||
|
- Framework
|
||||||
@@ -32,8 +32,6 @@ jobs:
|
|||||||
- name: Bundle files
|
- name: Bundle files
|
||||||
run: |
|
run: |
|
||||||
mv dist oxmysql
|
mv dist oxmysql
|
||||||
mkdir oxmysql/lib
|
|
||||||
mv lib/MySQL.lua oxmysql/lib/MySQL.lua
|
|
||||||
zip -r oxmysql-${{ env.RELEASE_VERSION }}.zip oxmysql
|
zip -r oxmysql-${{ env.RELEASE_VERSION }}.zip oxmysql
|
||||||
|
|
||||||
- name: Create Release
|
- name: Create Release
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
-194
@@ -1,194 +0,0 @@
|
|||||||
-- This file allows simple conversion from MySQL-Async to Oxmysql
|
|
||||||
-- Replace any references to '@mysql-async' in your resource manifests to use '@oxmysql' instead
|
|
||||||
-- Store is used for compatibility only
|
|
||||||
|
|
||||||
local Ox = exports.oxmysql
|
|
||||||
local Resource = GetCurrentResourceName()
|
|
||||||
local Store = {}
|
|
||||||
|
|
||||||
local function safeArgs(query, parameters, cb, transaction)
|
|
||||||
if type(query) == 'number' then query = Store[query] end
|
|
||||||
if transaction then
|
|
||||||
assert(type(query) == 'table', ('A table was expected for the transaction, but instead received %s'):format(query))
|
|
||||||
else
|
|
||||||
assert(type(query) == 'string', ('A string was expected for the query, but instead received %s'):format(query))
|
|
||||||
end
|
|
||||||
if cb then
|
|
||||||
assert(type(cb) == 'function', ('A callback function was expected, but instead received %s'):format(cb))
|
|
||||||
end
|
|
||||||
local type = parameters and type(parameters)
|
|
||||||
if type and type ~= 'table' and type ~= 'function' then
|
|
||||||
assert(nil, ('A %s was expected, but instead received %s'):format(cb and 'table' or 'function', parameters))
|
|
||||||
end
|
|
||||||
return query, parameters, cb
|
|
||||||
end
|
|
||||||
|
|
||||||
MySQL = {
|
|
||||||
--- Result is returned to a callback function, without interupting the execution of code
|
|
||||||
Async = {
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@param cb? function
|
|
||||||
---@return integer result
|
|
||||||
---returns number of rows updated by the executed query
|
|
||||||
execute = function(query, parameters, cb)
|
|
||||||
Ox:update(safeArgs(query, parameters, cb))
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@param cb? function
|
|
||||||
---@return table result
|
|
||||||
---returns array of matching rows or result data
|
|
||||||
fetchAll = function(query, parameters, cb)
|
|
||||||
Ox:execute(safeArgs(query, parameters, cb))
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@param cb? function
|
|
||||||
---@return integer|string
|
|
||||||
---returns value of the first column of a single row
|
|
||||||
fetchScalar = function(query, parameters, cb)
|
|
||||||
Ox:scalar(safeArgs(query, parameters, cb))
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@param cb? function
|
|
||||||
---@return table result
|
|
||||||
---returns table containing key value pairs
|
|
||||||
fetchSingle = function(query, parameters, cb)
|
|
||||||
Ox:single(safeArgs(query, parameters, cb))
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@param cb? function
|
|
||||||
---@return table result
|
|
||||||
---returns the last inserted id
|
|
||||||
insert = function(query, parameters, cb)
|
|
||||||
Ox:insert(safeArgs(query, parameters, cb))
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param queries table
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@param cb? function
|
|
||||||
---@return boolean result
|
|
||||||
---returns true when the transaction has succeeded
|
|
||||||
transaction = function(queries, parameters, cb)
|
|
||||||
Ox:transaction(safeArgs(queries, parameters, cb, true))
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param cb? function
|
|
||||||
---@return integer result
|
|
||||||
---returns the id used to reference a stored query string
|
|
||||||
store = function(query, cb)
|
|
||||||
assert(type(query) == 'string', 'The SQL Query must be a string')
|
|
||||||
local store = #Store+1
|
|
||||||
Store[store] = query
|
|
||||||
cb(store)
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
|
|
||||||
--- Result is assigned in-line to a variable, causing the current thread to await the result
|
|
||||||
Sync = {
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return integer result
|
|
||||||
---returns number of rows updated by the executed query
|
|
||||||
execute = function(query, parameters)
|
|
||||||
query, parameters = safeArgs(query, parameters)
|
|
||||||
local promise = promise.new()
|
|
||||||
Ox:update(query, parameters, function(result)
|
|
||||||
promise:resolve(result)
|
|
||||||
end, Resource)
|
|
||||||
return Citizen.Await(promise)
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return table result
|
|
||||||
---returns array of matching rows or result data
|
|
||||||
fetchAll = function(query, parameters)
|
|
||||||
query, parameters = safeArgs(query, parameters)
|
|
||||||
local promise = promise.new()
|
|
||||||
Ox:execute(query, parameters, function(result)
|
|
||||||
promise:resolve(result)
|
|
||||||
end, Resource)
|
|
||||||
return Citizen.Await(promise)
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return integer|string
|
|
||||||
---returns value of the first column of a single row
|
|
||||||
fetchScalar = function(query, parameters)
|
|
||||||
query, parameters = safeArgs(query, parameters)
|
|
||||||
local promise = promise.new()
|
|
||||||
Ox:scalar(query, parameters, function(result)
|
|
||||||
promise:resolve(result)
|
|
||||||
end, Resource)
|
|
||||||
return Citizen.Await(promise)
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return table result
|
|
||||||
---returns table containing key value pairs
|
|
||||||
fetchSingle = function(query, parameters)
|
|
||||||
query, parameters = safeArgs(query, parameters)
|
|
||||||
local promise = promise.new()
|
|
||||||
Ox:single(query, parameters, function(result)
|
|
||||||
promise:resolve(result)
|
|
||||||
end, Resource)
|
|
||||||
return Citizen.Await(promise)
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return table result
|
|
||||||
---returns the last inserted id
|
|
||||||
insert = function(query, parameters)
|
|
||||||
query, parameters = safeArgs(query, parameters)
|
|
||||||
local promise = promise.new()
|
|
||||||
Ox:insert(query, parameters, function(result)
|
|
||||||
promise:resolve(result)
|
|
||||||
end, Resource)
|
|
||||||
return Citizen.Await(promise)
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param queries table
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return boolean result
|
|
||||||
---returns true when the transaction has succeeded
|
|
||||||
transaction = function(queries, parameters)
|
|
||||||
queries, parameters = safeArgs(queries, parameters, true)
|
|
||||||
local promise = promise.new()
|
|
||||||
Ox:transaction(queries, parameters, function(result)
|
|
||||||
promise:resolve(result)
|
|
||||||
end, Resource)
|
|
||||||
return Citizen.Await(promise)
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@return integer result
|
|
||||||
---returns the id used to reference a stored query string
|
|
||||||
store = function(query)
|
|
||||||
assert(type(query) == 'string', 'The SQL Query must be a string')
|
|
||||||
local store = #Store+1
|
|
||||||
Store[store] = query
|
|
||||||
return store
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
|
|
||||||
ready = function(cb)
|
|
||||||
CreateThread(function()
|
|
||||||
repeat
|
|
||||||
Wait(50)
|
|
||||||
until GetResourceState('oxmysql') == 'started'
|
|
||||||
cb()
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,7 @@ 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.6.2'
|
version '1.8.2'
|
||||||
url 'https://github.com/overextended/oxmysql'
|
url 'https://github.com/overextended/oxmysql'
|
||||||
author 'overextended'
|
author 'overextended'
|
||||||
use_fxv2_oal 'yes'
|
use_fxv2_oal 'yes'
|
||||||
@@ -0,0 +1,187 @@
|
|||||||
|
-- This file allows simple conversion from MySQL-Async to Oxmysql
|
||||||
|
-- Replace any references to '@mysql-async' in your resource manifests to use '@oxmysql' instead
|
||||||
|
-- Store is used for compatibility only
|
||||||
|
|
||||||
|
local Ox = exports.oxmysql
|
||||||
|
local Store = {}
|
||||||
|
|
||||||
|
local function safeArgs(query, parameters, cb, transaction)
|
||||||
|
if type(query) == 'number' then query = Store[query] end
|
||||||
|
if transaction then
|
||||||
|
assert(type(query) == 'table', ('A table was expected for the transaction, but instead received %s'):format(query))
|
||||||
|
else
|
||||||
|
assert(type(query) == 'string', ('A string was expected for the query, but instead received %s'):format(query))
|
||||||
|
end
|
||||||
|
if cb then
|
||||||
|
assert(type(cb) == 'function', ('A callback function was expected, but instead received %s'):format(cb))
|
||||||
|
end
|
||||||
|
local type = parameters and type(parameters)
|
||||||
|
if type and type ~= 'table' and type ~= 'function' then
|
||||||
|
assert(nil, ('A %s was expected, but instead received %s'):format(cb and 'table' or 'function', parameters))
|
||||||
|
end
|
||||||
|
return query, parameters, cb
|
||||||
|
end
|
||||||
|
|
||||||
|
MySQL = { Async = {}, Sync = {} }
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@param cb? function
|
||||||
|
---@return integer result
|
||||||
|
---returns number of rows updated by the executed query
|
||||||
|
MySQL.Async.execute = function(query, parameters, cb)
|
||||||
|
Ox:update(safeArgs(query, parameters, cb))
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@param cb? function
|
||||||
|
---@return table result
|
||||||
|
---returns array of matching rows or result data
|
||||||
|
MySQL.Async.fetchAll = function(query, parameters, cb)
|
||||||
|
Ox:execute(safeArgs(query, parameters, cb))
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@param cb? function
|
||||||
|
---@return integer|string
|
||||||
|
---returns value of the first column of a single row
|
||||||
|
MySQL.Async.fetchScalar = function(query, parameters, cb)
|
||||||
|
Ox:scalar(safeArgs(query, parameters, cb))
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@param cb? function
|
||||||
|
---@return table result
|
||||||
|
---returns table containing key value pairs
|
||||||
|
MySQL.Async.fetchSingle = function(query, parameters, cb)
|
||||||
|
Ox:single(safeArgs(query, parameters, cb))
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@param cb? function
|
||||||
|
---@return table result
|
||||||
|
---returns the last inserted id
|
||||||
|
MySQL.Async.insert = function(query, parameters, cb)
|
||||||
|
Ox:insert(safeArgs(query, parameters, cb))
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param queries table
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@param cb? function
|
||||||
|
---@return boolean result
|
||||||
|
---returns true when the transaction has succeeded
|
||||||
|
MySQL.Async.transaction = function(queries, parameters, cb)
|
||||||
|
Ox:transaction(safeArgs(queries, parameters, cb, true))
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param cb? function
|
||||||
|
---@return integer result
|
||||||
|
---returns the id used to reference a stored query string
|
||||||
|
MySQL.Async.store = function(query, cb)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local store = #Store+1
|
||||||
|
Store[store] = query
|
||||||
|
cb(store)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@return integer result
|
||||||
|
---returns number of rows updated by the executed query
|
||||||
|
MySQL.Sync.execute = function(query, parameters)
|
||||||
|
query, parameters = safeArgs(query, parameters)
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:update(query, parameters, function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@return table result
|
||||||
|
---returns array of matching rows or result data
|
||||||
|
MySQL.Sync.fetchAll = function(query, parameters)
|
||||||
|
query, parameters = safeArgs(query, parameters)
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:execute(query, parameters, function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@return integer|string
|
||||||
|
---returns value of the first column of a single row
|
||||||
|
MySQL.Sync.fetchScalar = function(query, parameters)
|
||||||
|
query, parameters = safeArgs(query, parameters)
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:scalar(query, parameters, function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@return table result
|
||||||
|
---returns table containing key value pairs
|
||||||
|
MySQL.Sync.fetchSingle = function(query, parameters)
|
||||||
|
query, parameters = safeArgs(query, parameters)
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:single(query, parameters, function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@return table result
|
||||||
|
---returns the last inserted id
|
||||||
|
MySQL.Sync.insert = function(query, parameters)
|
||||||
|
query, parameters = safeArgs(query, parameters)
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:insert(query, parameters, function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param queries table
|
||||||
|
---@param parameters? table|function
|
||||||
|
---@return boolean result
|
||||||
|
---returns true when the transaction has succeeded
|
||||||
|
MySQL.Sync.transaction = function(queries, parameters)
|
||||||
|
queries, parameters = safeArgs(queries, parameters, false, true)
|
||||||
|
local promise = promise.new()
|
||||||
|
Ox:transaction(queries, parameters, function(result)
|
||||||
|
promise:resolve(result)
|
||||||
|
end)
|
||||||
|
return Citizen.Await(promise)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query string
|
||||||
|
---@return integer result
|
||||||
|
---returns the id used to reference a stored query string
|
||||||
|
MySQL.Sync.store = function(query)
|
||||||
|
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||||
|
local store = #Store+1
|
||||||
|
Store[store] = query
|
||||||
|
return store
|
||||||
|
end
|
||||||
|
|
||||||
|
MySQL.ready = function(cb)
|
||||||
|
CreateThread(function()
|
||||||
|
repeat
|
||||||
|
Wait(50)
|
||||||
|
until GetResourceState('oxmysql') == 'started'
|
||||||
|
cb()
|
||||||
|
end)
|
||||||
|
end
|
||||||
+131
@@ -0,0 +1,131 @@
|
|||||||
|
local oxmysql = exports.oxmysql
|
||||||
|
|
||||||
|
CreateThread(function()
|
||||||
|
Wait(1000)
|
||||||
|
local resource = GetCurrentResourceName()
|
||||||
|
local url = GetResourceMetadata(resource, 'url', 0)
|
||||||
|
local version = GetResourceMetadata(resource, 'version', 0)
|
||||||
|
|
||||||
|
PerformHttpRequest(('%s/main/lua/fxmanifest.lua'):format(url:gsub('github.com', 'raw.githubusercontent.com')), function(error, response)
|
||||||
|
if error == 200 then
|
||||||
|
local latest = response:match('%d%.%d+%.%d+')
|
||||||
|
if version ~= latest then
|
||||||
|
local curMajor, curMinor = string.strsplit('.', version)
|
||||||
|
local newMajor, newMinor = string.strsplit('.', response:match('%d%.%d+%.%d+'))
|
||||||
|
local link = ('%s/releases/download/v%s/oxmysql-v%s.zip'):format(url, latest, latest)
|
||||||
|
|
||||||
|
if tonumber(curMajor) < tonumber(newMajor) then
|
||||||
|
latest = 'A major update'
|
||||||
|
elseif tonumber(curMinor) < tonumber(newMinor) then
|
||||||
|
latest = 'An update'
|
||||||
|
else
|
||||||
|
latest = 'A patch'
|
||||||
|
end
|
||||||
|
|
||||||
|
print(('^3%s is available for oxmysql - please update to the latest release (current version: %s)\n ^3- %s^0'):format(latest, version, link))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end, 'GET')
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Check for a recent native since the recommended artifact doesn't support JS async_retval or dependency constraints
|
||||||
|
if not GetEntityAttachedTo 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 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 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 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 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
|
||||||
+7
-9
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"version": "1.6.2",
|
"version": "1.0.0",
|
||||||
"main": "oxmysql.js",
|
"main": "oxmysql.js",
|
||||||
"description": "node-mysql2 resource for Cfx",
|
"description": "node-mysql2 resource for Cfx",
|
||||||
"repository": "[email protected]:overextended/oxmysql.git",
|
"repository": "[email protected]:overextended/oxmysql.git",
|
||||||
@@ -12,17 +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",
|
||||||
"github-version-checker": "^2.2.0",
|
|
||||||
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
diff --git a/node_modules/github-version-checker/lib/check.js b/node_modules/github-version-checker/lib/check.js
|
|
||||||
index 7f00060..cf0e231 100644
|
|
||||||
--- a/node_modules/github-version-checker/lib/check.js
|
|
||||||
+++ b/node_modules/github-version-checker/lib/check.js
|
|
||||||
@@ -88,7 +88,6 @@ const rest = (options, callback) => {
|
|
||||||
|
|
||||||
if (found) {
|
|
||||||
const mapped = schemeMapper[options.fetchTags ? 'tag' : 'release'](version);
|
|
||||||
- console.log(mapped);
|
|
||||||
callback(null, mapped);
|
|
||||||
} else {
|
|
||||||
callback(null, null);
|
|
||||||
@@ -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
-24
@@ -1,6 +1,3 @@
|
|||||||
import versionCheck from 'github-version-checker';
|
|
||||||
import currentVersion from '../package.json';
|
|
||||||
|
|
||||||
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 150);
|
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 150);
|
||||||
const debug = GetConvar('mysql_debug', 'false') === 'true';
|
const debug = GetConvar('mysql_debug', 'false') === 'true';
|
||||||
|
|
||||||
@@ -19,26 +16,6 @@ const isolationLevel = (() => {
|
|||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const resourceName = GetCurrentResourceName() || 'oxmysql';
|
const resourceName = GetCurrentResourceName();
|
||||||
|
|
||||||
export { slowQueryWarning, debug, isolationLevel, resourceName };
|
export { slowQueryWarning, debug, isolationLevel, resourceName };
|
||||||
|
|
||||||
setImmediate(async () => {
|
|
||||||
const versionCheckConfig = {
|
|
||||||
repo: 'oxmysql',
|
|
||||||
owner: 'overextended',
|
|
||||||
currentVersion: currentVersion.version,
|
|
||||||
latest: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const update = await versionCheck(versionCheckConfig);
|
|
||||||
if (update) {
|
|
||||||
console.log(
|
|
||||||
`^3Your version of oxmysql is outdated (v${versionCheckConfig.currentVersion})! Please update to the latest version (${update.name}) here: \nhttps://github.com/overextended/oxmysql/releases/latest^0`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.log(`^3Could not fetch updates for oxmysql!^0\n${e}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
+76
-4
@@ -1,14 +1,17 @@
|
|||||||
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 } from './config';
|
||||||
|
import { FormatError } from './errors';
|
||||||
|
|
||||||
const execute = async (query, parameters, resource) => {
|
const execute = async (query, parameters, resource) => {
|
||||||
ScheduleResourceTick(resourceName);
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
[query, parameters] = parseParameters(query, parameters);
|
[query, parameters] = parseParameters(query, parameters);
|
||||||
|
const connection = await pool.getConnection();
|
||||||
|
ScheduleResourceTick(resourceName);
|
||||||
|
|
||||||
const [rows, _, executionTime] = await pool.query(query, parameters);
|
const startTime = process.hrtime();
|
||||||
|
const [rows] = await connection.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(
|
||||||
@@ -16,6 +19,7 @@ const execute = async (query, parameters, resource) => {
|
|||||||
${query} ${JSON.stringify(parameters)}^0`
|
${query} ${JSON.stringify(parameters)}^0`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
connection.release();
|
||||||
return rows;
|
return rows;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(
|
console.log(
|
||||||
@@ -27,4 +31,72 @@ const execute = async (query, parameters, resource) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { execute };
|
const queryType = (query) => {
|
||||||
|
switch (query.replace(/\s.*/, '')) {
|
||||||
|
case 'SELECT':
|
||||||
|
return 1;
|
||||||
|
case 'INSERT':
|
||||||
|
return 2;
|
||||||
|
case 'UPDATE':
|
||||||
|
return 3;
|
||||||
|
case 'DELETE':
|
||||||
|
return 3;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const preparedStatement = async (query, parameters, resource) => {
|
||||||
|
try {
|
||||||
|
if (!Array.isArray(parameters))
|
||||||
|
throw new FormatError(`Placeholders were defined, but query received no parameters!`, query);
|
||||||
|
|
||||||
|
if (typeof query !== 'string') throw new FormatError(`Prepared statements must utilise a single query`);
|
||||||
|
|
||||||
|
const type = queryType(query);
|
||||||
|
if (!type) throw new FormatError(`Prepared statements only accept SELECT, INSERT, UPDATE, and DELETE methods!`);
|
||||||
|
|
||||||
|
const connection = await pool.getConnection();
|
||||||
|
ScheduleResourceTick(resourceName);
|
||||||
|
|
||||||
|
const results = [];
|
||||||
|
let queryCount = parameters.length;
|
||||||
|
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 ${totalTime}ms to execute ${
|
||||||
|
queryCount > 1 ? queryCount + ' queries' : 'a query'
|
||||||
|
}!
|
||||||
|
${query} ${JSON.stringify(parameters)}^0`
|
||||||
|
);
|
||||||
|
|
||||||
|
connection.release();
|
||||||
|
|
||||||
|
if (results.length === 1) {
|
||||||
|
if (type === 1) {
|
||||||
|
if (results[0][0] && Object.keys(results[0][0]).length === 1) {
|
||||||
|
return Object.values(results[0][0])[0];
|
||||||
|
}
|
||||||
|
return results[0][0];
|
||||||
|
}
|
||||||
|
return results[0];
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(
|
||||||
|
`^1[ERROR] ${resource} was unable to execute a query!
|
||||||
|
${error.message}
|
||||||
|
${error.sql || `${query} ${JSON.stringify(parameters)}`}^0`
|
||||||
|
);
|
||||||
|
debug && console.trace(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { execute, preparedStatement };
|
||||||
+20
-18
@@ -1,5 +1,5 @@
|
|||||||
import { pool } from './pool';
|
import { pool } from './pool';
|
||||||
import { execute } from './execute';
|
import { execute, preparedStatement } from './execute';
|
||||||
import { transaction } from './transaction';
|
import { transaction } from './transaction';
|
||||||
import { debug, isolationLevel } from './config';
|
import { debug, isolationLevel } from './config';
|
||||||
|
|
||||||
@@ -12,41 +12,34 @@ setImmediate(async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const safeCallback = (callback, result, resource, query) => {
|
const safeCallback = (callback, result) => {
|
||||||
if (typeof callback === 'function')
|
if (typeof callback === 'function') callback(result);
|
||||||
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()) => {
|
||||||
@@ -55,7 +48,16 @@ global.exports('transaction', (queries, parameters, cb, resource = GetInvokingRe
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!GetResourceMetadata(GetCurrentResourceName(), 'server_script', 1)) {
|
global.exports('prepare', (query, parameters, cb, resource = GetInvokingResource()) => {
|
||||||
|
preparedStatement(query, parameters, resource).then((result) => safeCallback(cb || parameters, result));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (GetEntityAttachedTo || !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) => {
|
global.exports('executeSync', async (query, parameters) => {
|
||||||
const result = await execute(query, parameters, GetInvokingResource());
|
const result = await execute(query, parameters, GetInvokingResource());
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
+21
-16
@@ -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:
|
||||||
@@ -26,23 +26,28 @@ const parseTypes = (field, next) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const parseParameters = (query, parameters) => {
|
const parseParameters = (query, parameters) => {
|
||||||
if (query === undefined) throw new FormatError(`Undefined query passed`);
|
if (typeof query !== 'string')
|
||||||
|
throw new Error(
|
||||||
|
`Non-string query passed. Make sure you use "oxmysql:execute" instead of "oxmysql.execute" when calling exports.`
|
||||||
|
);
|
||||||
|
|
||||||
if (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);
|
||||||
|
|
||||||
|
|||||||
+19
-54
@@ -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,59 +7,25 @@ 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(
|
: (options = 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;
|
||||||
|
}, {}));
|
||||||
|
|
||||||
const createConnection = () => {
|
options.namedPlaceholders = true;
|
||||||
if (connectionString.includes('database=')) {
|
options.typeCast = parseTypes;
|
||||||
const options = parseSemiColons();
|
|
||||||
|
|
||||||
return createPool({
|
return options;
|
||||||
host: options.host || 'localhost',
|
})();
|
||||||
port: options.port || 3306,
|
|
||||||
user: options.username || options.user || options.userid || 'root',
|
|
||||||
password: options.password || options.pass || '',
|
|
||||||
database: options.endpoint || options.database || 'es_extended',
|
|
||||||
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) {
|
export const pool = createPool(dbOptions);
|
||||||
throw new Error(`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return createPool({
|
|
||||||
host: options.hosts[0].host || 'localhost',
|
|
||||||
port: options.hosts[0].port || 3306,
|
|
||||||
user: options.username || 'root',
|
|
||||||
password: options.password || '',
|
|
||||||
database: options.endpoint || 'es_extended',
|
|
||||||
charset: 'utf8mb4_unicode_ci',
|
|
||||||
connectTimeout: 30000,
|
|
||||||
...options.options,
|
|
||||||
namedPlaceholders: true,
|
|
||||||
typeCast: parseTypes,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const pool = createConnection();
|
|
||||||
+2
-2
@@ -13,7 +13,7 @@ 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 startTime = process.hrtime();
|
||||||
|
|
||||||
const fullQuery = parseTransaction(queries, parameters);
|
const fullQuery = parseTransaction(queries, parameters);
|
||||||
const transactionAmount = fullQuery.length;
|
const transactionAmount = fullQuery.length;
|
||||||
@@ -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(
|
||||||
|
|||||||
+4
-1
@@ -15,12 +15,15 @@ module.exports = {
|
|||||||
contextRegExp: /./,
|
contextRegExp: /./,
|
||||||
}),
|
}),
|
||||||
new CopyPlugin({
|
new CopyPlugin({
|
||||||
patterns: ["fxmanifest.lua", "wrapper.lua", "README.md", "LICENSE.md"],
|
patterns: ["./lua", "./lua", "README.md", "LICENSE.md"],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
output: {
|
output: {
|
||||||
filename: "oxmysql.js",
|
filename: "oxmysql.js",
|
||||||
path: path.resolve(__dirname, "dist"),
|
path: path.resolve(__dirname, "dist"),
|
||||||
},
|
},
|
||||||
|
optimization: {
|
||||||
|
minimize: false,
|
||||||
|
},
|
||||||
target: "node",
|
target: "node",
|
||||||
};
|
};
|
||||||
-84
@@ -1,84 +0,0 @@
|
|||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return integer result
|
|
||||||
---returns number of rows updated by the executed query
|
|
||||||
exports('executeSync', function(query, parameters)
|
|
||||||
local p = promise.new()
|
|
||||||
exports['oxmysql']:execute(query, parameters, function(result)
|
|
||||||
p:resolve(result)
|
|
||||||
end, GetInvokingResource())
|
|
||||||
return Citizen.Await(p)
|
|
||||||
end)
|
|
||||||
|
|
||||||
---@deprecated true
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return table result
|
|
||||||
---returns array of matching rows or result data
|
|
||||||
exports('fetchSync', function(query, parameters)
|
|
||||||
local p = promise.new()
|
|
||||||
exports['oxmysql']:fetch(query, parameters, function(result)
|
|
||||||
p:resolve(result)
|
|
||||||
end, GetInvokingResource())
|
|
||||||
return Citizen.Await(p)
|
|
||||||
end)
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return table result
|
|
||||||
---returns table containing key value pairs
|
|
||||||
exports('singleSync', function(query, parameters)
|
|
||||||
local p = promise.new()
|
|
||||||
exports['oxmysql']:single(query, parameters, function(result)
|
|
||||||
p:resolve(result)
|
|
||||||
end, GetInvokingResource())
|
|
||||||
return Citizen.Await(p)
|
|
||||||
end)
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return integer|string
|
|
||||||
---returns value of the first column of a single row
|
|
||||||
exports('scalarSync', function(query, parameters)
|
|
||||||
local p = promise.new()
|
|
||||||
exports['oxmysql']:scalar(query, parameters, function(result)
|
|
||||||
p:resolve(result)
|
|
||||||
end, GetInvokingResource())
|
|
||||||
return Citizen.Await(p)
|
|
||||||
end)
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return table result
|
|
||||||
---returns the last inserted id
|
|
||||||
exports('insertSync', function(query, parameters)
|
|
||||||
local p = promise.new()
|
|
||||||
exports['oxmysql']:insert(query, parameters, function(result)
|
|
||||||
p:resolve(result)
|
|
||||||
end, GetInvokingResource())
|
|
||||||
return Citizen.Await(p)
|
|
||||||
end)
|
|
||||||
|
|
||||||
---@param query string
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return integer result
|
|
||||||
---returns number of rows updated by the executed query
|
|
||||||
exports('updateSync', function(query, parameters)
|
|
||||||
local p = promise.new()
|
|
||||||
exports['oxmysql']:update(query, parameters, function(result)
|
|
||||||
p:resolve(result)
|
|
||||||
end, GetInvokingResource())
|
|
||||||
return Citizen.Await(p)
|
|
||||||
end)
|
|
||||||
|
|
||||||
---@param queries table
|
|
||||||
---@param parameters? table|function
|
|
||||||
---@return boolean result
|
|
||||||
---returns true when the transaction has succeeded
|
|
||||||
exports('transactionSync', function(queries, parameters)
|
|
||||||
local p = promise.new()
|
|
||||||
exports['oxmysql']:transaction(queries, parameters, function(result)
|
|
||||||
p:resolve(result)
|
|
||||||
end, GetInvokingResource())
|
|
||||||
return Citizen.Await(p)
|
|
||||||
end)
|
|
||||||
Reference in new issue
Block a user