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
@@ -18,13 +18,13 @@
return list("ss13_player_preferences" = list("vars" = list("UI_style", "UI_style_color", "UI_style_alpha", "ooccolor"), "args" = list("ckey")))
/datum/category_item/player_setup_item/player_global/ui/gather_load_parameters()
return list(":ckey" = pref.client.ckey)
return list("ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/player_global/ui/gather_save_query()
return list("ss13_player_preferences" = list("UI_style", "UI_style_color", "UI_style_alpha", "ooccolor", "ckey" = 1))
/datum/category_item/player_setup_item/player_global/ui/gather_save_parameters()
return list(":ckey" = pref.client.ckey, ":UI_style_alpha" = pref.UI_style_alpha, ":UI_style_color" = pref.UI_style_color, ":UI_style" = pref.UI_style, ":ooccolor" = pref.ooccolor)
return list("ckey" = pref.client.ckey, "UI_style_alpha" = pref.UI_style_alpha, "UI_style_color" = pref.UI_style_color, "UI_style" = pref.UI_style, "ooccolor" = pref.ooccolor)
/datum/category_item/player_setup_item/player_global/ui/sanitize_preferences()
pref.UI_style = sanitize_inlist(pref.UI_style, all_ui_styles, initial(pref.UI_style))
@@ -26,21 +26,21 @@
return list("ss13_player_preferences" = list("vars" = list("lastchangelog", "current_character", "toggles", "asfx_togs", "lastmotd" = "motd_hash", "lastmemo" = "memo_hash"), "args" = list("ckey")))
/datum/category_item/player_setup_item/player_global/settings/gather_load_parameters()
return list(":ckey" = pref.client.ckey)
return list("ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/player_global/settings/gather_save_query()
return list("ss13_player_preferences" = list("lastchangelog", "current_character", "toggles", "asfx_togs", "lastmotd", "lastmemo", "ckey" = 1, "parallax_toggles", "parallax_speed"))
/datum/category_item/player_setup_item/player_global/settings/gather_save_parameters()
return list(":ckey" = pref.client.ckey,
":lastchangelog" = pref.lastchangelog,
":current_character" = pref.current_character,
":toggles" = pref.toggles,
":asfx_togs" = pref.asfx_togs,
":lastmotd" = pref.motd_hash,
":lastmemo" = pref.memo_hash,
":parallax_toggles" = pref.parallax_togs,
":parallax_speed" = pref.parallax_speed)
return list("ckey" = pref.client.ckey,
"lastchangelog" = pref.lastchangelog,
"current_character" = pref.current_character,
"toggles" = pref.toggles,
"asfx_togs" = pref.asfx_togs,
"lastmotd" = pref.motd_hash,
"lastmemo" = pref.memo_hash,
"parallax_toggles" = pref.parallax_togs,
"parallax_speed" = pref.parallax_speed)
/datum/category_item/player_setup_item/player_global/settings/sanitize_preferences(var/sql_load = 0)
if (sql_load)
@@ -12,13 +12,13 @@
return list("ss13_player_preferences" = list("vars" = list("language_prefixes"), "args" = list("ckey")))
/datum/category_item/player_setup_item/player_global/language/gather_load_parameters()
return list(":ckey" = pref.client.ckey)
return list("ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/player_global/language/gather_save_query()
return list("ss13_player_preferences" = list("language_prefixes", "ckey" = 1))
/datum/category_item/player_setup_item/player_global/language/gather_save_parameters()
return list(":ckey" = pref.client.ckey, ":language_prefixes" = list2params(pref.language_prefixes))
return list("ckey" = pref.client.ckey, "language_prefixes" = list2params(pref.language_prefixes))
/datum/category_item/player_setup_item/player_global/language/sanitize_preferences(var/sql_load = 0)
if (sql_load && pref.language_prefixes)
@@ -31,7 +31,7 @@
return list("ss13_player_pai" = list("vars" = list("name" = "pai/name", "description" = "pai/description", "role" = "pai/role", "comments" = "pai/comments"), "args" = list("ckey")))
/datum/category_item/player_setup_item/player_global/pai/gather_load_parameters()
return list(":ckey" = pref.client.ckey)
return list("ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/player_global/pai/gather_save_query()
return list("ss13_player_pai" = list("name", "description", "role", "comments", "ckey" = 1))
@@ -40,7 +40,7 @@
if (!candidate)
return list()
return list(":ckey" = pref.client.ckey, ":name" = candidate.name, ":description" = candidate.description, ":role" = candidate.role, ":comments" = candidate.comments)
return list("ckey" = pref.client.ckey, "name" = candidate.name, "description" = candidate.description, "role" = candidate.role, "comments" = candidate.comments)
/datum/category_item/player_setup_item/player_global/pai/sanitize_preferences(var/sql_load = 0)
if (sql_load && candidate && pref.pai.len)