Better SQL prepared statements (#2474)

The system used to be of complexity O(n^2). Essentially two for loops running per every argument. Which ended up being surprisingly slow (there were instances where I saw the argument parser as using quite a lot of CPU time).

This replaces it with a more linear algorithm. It's somewhere near O(n) where n is the length of the unparsed query. Which is more stable and faaaster. This comes with two changes, however:

Parameters inside the query now have to be delimited from both sides with : (colons). The alternative to this would be to use something like $n or just assume that space marks the end of a marker. Only the former is workable, the latter would break a few queries already.
Arguments in the argument array no longer have to be prefixed by : (colons). So, while in the query you would write :thing:, you'd initialize the array of args as: list("thing" = somevar). It could be made to work without it, but eh, I think this is fine.
Argument validation is slightly weaker. What I mean by this is that with the old system, unused keys would result in an error. This is no longer a thing. Missing keys will still result in an error, however.
One more improvement: double delimiting removes an edge case where if key A partially covers key B, depending on the order, key A would mangle key B.
Updated and tested all queries that I could find. So this should be good.
This commit is contained in:
skull132
2017-05-29 21:17:41 +03:00
committed by GitHub
parent b88d6aaadb
commit a3ec0cf45d
36 changed files with 366 additions and 333 deletions
+10 -10
View File
@@ -104,7 +104,7 @@ A list of items and costs is stored under the datum of every game mode, alongsid
// Interaction code. Gathers a list of items purchasable from the paren't uplink and displays it. It also adds a lock button.
/obj/item/device/uplink/hidden/interact(mob/user)
ui_interact(user)
/obj/item/device/uplink/hidden/CanUseTopic()
if(!active)
return STATUS_CLOSE
@@ -208,14 +208,14 @@ A list of items and costs is stored under the datum of every game mode, alongsid
switch (nanoui_data["contracts_view"])
if (1)
query_details[":status"] = "open"
query_details["status"] = "open"
if (2)
query_details[":status"] = "closed"
query_details["status"] = "closed"
else
nanoui_data["contracts_view"] = 1
query_details[":status"] = "open"
query_details["status"] = "open"
var/DBQuery/index_query = dbcon.NewQuery("SELECT count(*) as Total_Contracts FROM ss13_syndie_contracts WHERE deleted_at IS NULL AND status = :status")
var/DBQuery/index_query = dbcon.NewQuery("SELECT count(*) as Total_Contracts FROM ss13_syndie_contracts WHERE deleted_at IS NULL AND status = :status:")
index_query.Execute(query_details)
var/pages = 0
@@ -242,9 +242,9 @@ A list of items and costs is stored under the datum of every game mode, alongsid
if (nanoui_data["contracts_current_page"] > pages)
return
query_details[":offset"] = (nanoui_data["contracts_current_page"] - 1) * 10
query_details["offset"] = (nanoui_data["contracts_current_page"] - 1) * 10
var/DBQuery/list_query = dbcon.NewQuery("SELECT contract_id, contractee_name, title FROM ss13_syndie_contracts WHERE deleted_at IS NULL AND status = :status LIMIT 10 OFFSET :offset")
var/DBQuery/list_query = dbcon.NewQuery("SELECT contract_id, contractee_name, title FROM ss13_syndie_contracts WHERE deleted_at IS NULL AND status = :status: LIMIT 10 OFFSET :offset:")
list_query.Execute(query_details)
var/list/contracts = list()
@@ -272,11 +272,11 @@ A list of items and costs is stored under the datum of every game mode, alongsid
nanoui_data["contracts_view"] = 1
var/query_details[0]
query_details[":contract_id"] = exploit_id
query_details["contract_id"] = exploit_id
var/DBQuery/select_query = dbcon.NewQuery("SELECT contract_id, contractee_name, status, title, description, reward_other FROM ss13_syndie_contracts WHERE contract_id = :contract_id")
var/DBQuery/select_query = dbcon.NewQuery("SELECT contract_id, contractee_name, status, title, description, reward_other FROM ss13_syndie_contracts WHERE contract_id = :contract_id:")
select_query.Execute(query_details)
if (select_query.NextRow())
nanoui_data["contracts_found"] = 1