Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add an export to call prepared statements #54

Merged
merged 4 commits into from
Oct 22, 2021
Merged

feat: Add an export to call prepared statements #54

merged 4 commits into from
Oct 22, 2021

Conversation

thelindat
Copy link
Member

Mostly intended for bulk queries to reduce overhead from calling multiple function references, as well as serve as a far better and safer alternative to hideous case and join queries utilised in ESX Status and ESX Legacy.

Needs testing and improvements. Adding another argument to state the number of queries expected to run would also be beneficial as we can use that to clean up some of the results and control the data being sent back. If we expect to run a single SELECT query, so we can either return results[0] or results[0][0] depending on the number of columns returned to reduce memory use; essentially giving us the benefits of both the single and scalar functions.

Status save function

function SaveData()
	local parameters = {}
	for _, xPlayer in pairs(ESX.GetExtendedPlayers()) do
		local status = ESX.Players[xPlayer.source]
		if status and next(status) then
			parameters[#parameters+1] = {json.encode(status), xPlayer.identifier}
		end
	end
	if #parameters > 0 then
		exports.oxmysql:prepared('UPDATE users SET status = ? WHERE identifier = ?', parameters)
	end
end

ESX (Ox) save function

Core.SavePlayers = function(cb)
	local xPlayers = ESX.GetExtendedPlayers()
	local count = #xPlayers
	if count > 0 then
		local parameters = {}
		local time = os.nanotime()
		for _, xPlayer in pairs(xPlayers) do
			parameters[#parameters+1] = {
				json.encode(xPlayer.getAccounts(true)),
				xPlayer.job.name,
				xPlayer.job.grade,
				xPlayer.group,
				json.encode(xPlayer.getCoords()),
				json.encode(xPlayer.getInventory(true)),
				xPlayer.identifier
			}
		end
		exports.oxmysql:prepared("UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ? WHERE `identifier` = ?", parameters,
		function(results)
			if results then
				if type(cb) == 'function' then cb() else print(('[^2INFO^7] Saved %s %s over %s ms'):format(count, count > 1 and 'players' or 'player', (os.nanotime() - time) / 1000000)) end
			end
		end)
	end
end

- Utilises pool.execute (rather than query) to actually prepare parameters through mysql.

This is intended for appropriately built queries utilising  placeholders and parameters, and should only be used on frequently repeated queries.
@thelindat thelindat changed the title feat: Add an export to call prepared queries feat: Add an export to call prepared statements Oct 18, 2021
@thelindat thelindat marked this pull request as ready for review October 22, 2021 21:58
@thelindat thelindat merged commit 2a8f4b1 into main Oct 22, 2021
@thelindat thelindat deleted the prepare branch October 24, 2021 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants