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
@@ -26,7 +26,7 @@
"args" = list("id")))
/datum/category_item/player_setup_item/general/basic/gather_load_parameters()
return list(":id" = pref.current_character)
return list("id" = pref.current_character)
/datum/category_item/player_setup_item/general/basic/gather_save_query()
return list("ss13_characters" = list("name",
@@ -38,13 +38,13 @@
"ckey" = 1))
/datum/category_item/player_setup_item/general/basic/gather_save_parameters()
return list(":name" = pref.real_name,
":gender" = pref.gender,
":age" = pref.age,
":metadata" = pref.metadata,
":spawnpoint" = pref.spawnpoint,
":id" = pref.current_character,
":ckey" = pref.client.ckey)
return list("name" = pref.real_name,
"gender" = pref.gender,
"age" = pref.age,
"metadata" = pref.metadata,
"spawnpoint" = pref.spawnpoint,
"id" = pref.current_character,
"ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/general/basic/load_special()
pref.can_edit_name = 1
@@ -55,8 +55,8 @@
// Called /after/ loading and /before/ sanitization.
// So we have pref.current_character. It's just in text format.
var/DBQuery/query = dbcon.NewQuery("SELECT DATEDIFF(NOW(), created_at) AS DiffDate FROM ss13_characters WHERE id = :id")
query.Execute(list(":id" = text2num(pref.current_character)))
var/DBQuery/query = dbcon.NewQuery("SELECT DATEDIFF(NOW(), created_at) AS DiffDate FROM ss13_characters WHERE id = :id:")
query.Execute(list("id" = text2num(pref.current_character)))
if (query.NextRow())
if (text2num(query.item[1]) > 5)
@@ -12,7 +12,7 @@
return list("ss13_characters" = list("vars" = list("language" = "alternate_languages"), "args" = list("id")))
/datum/category_item/player_setup_item/general/language/gather_load_parameters()
return list(":id" = pref.current_character)
return list("id" = pref.current_character)
/datum/category_item/player_setup_item/general/language/gather_save_query()
return list("ss13_characters" = list("id" = 1,
@@ -20,9 +20,9 @@
"language"))
/datum/category_item/player_setup_item/general/language/gather_save_parameters()
return list(":language" = list2params(pref.alternate_languages),
":id" = pref.current_character,
":ckey" = pref.client.ckey)
return list("language" = list2params(pref.alternate_languages),
"id" = pref.current_character,
"ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/general/language/sanitize_character(var/sql_load = 0)
if (sql_load)
@@ -64,7 +64,7 @@ var/global/list/valid_bloodtypes = list("A+", "A-", "B+", "B-", "AB+", "AB-", "O
"args" = list("id")))
/datum/category_item/player_setup_item/general/body/gather_load_parameters()
return list(":id" = pref.current_character)
return list("id" = pref.current_character)
/datum/category_item/player_setup_item/general/body/gather_save_query()
return list("ss13_characters" = list("species",
@@ -83,20 +83,20 @@ var/global/list/valid_bloodtypes = list("A+", "A-", "B+", "B-", "AB+", "AB-", "O
"ckey" = 1))
/datum/category_item/player_setup_item/general/body/gather_save_parameters()
return list(":species" = pref.species,
":hair_colour" = "#" + num2hex(pref.r_hair, 2) + num2hex(pref.g_hair, 2) + num2hex(pref.b_hair, 2),
":facial_colour" = "#" + num2hex(pref.r_facial, 2) + num2hex(pref.g_facial, 2) + num2hex(pref.b_facial, 2),
":skin_tone" = pref.s_tone,
":skin_colour" = "#" + num2hex(pref.r_skin, 2) + num2hex(pref.g_skin, 2) + num2hex(pref.b_skin, 2),
":hair_style" = pref.h_style,
":facial_style" = pref.f_style,
":eyes_colour" = "#" + num2hex(pref.r_eyes, 2) + num2hex(pref.g_eyes, 2) + num2hex(pref.b_eyes, 2),
":b_type" = pref.b_type,
":disabilities" = pref.disabilities,
":organs_data" = list2params(pref.organ_data),
":organs_robotic"= list2params(pref.rlimb_data),
":id" = pref.current_character,
":ckey" = pref.client.ckey)
return list("species" = pref.species,
"hair_colour" = "#" + num2hex(pref.r_hair, 2) + num2hex(pref.g_hair, 2) + num2hex(pref.b_hair, 2),
"facial_colour" = "#" + num2hex(pref.r_facial, 2) + num2hex(pref.g_facial, 2) + num2hex(pref.b_facial, 2),
"skin_tone" = pref.s_tone,
"skin_colour" = "#" + num2hex(pref.r_skin, 2) + num2hex(pref.g_skin, 2) + num2hex(pref.b_skin, 2),
"hair_style" = pref.h_style,
"facial_style" = pref.f_style,
"eyes_colour" = "#" + num2hex(pref.r_eyes, 2) + num2hex(pref.g_eyes, 2) + num2hex(pref.b_eyes, 2),
"b_type" = pref.b_type,
"disabilities" = pref.disabilities,
"organs_data" = list2params(pref.organ_data),
"organs_robotic"= list2params(pref.rlimb_data),
"id" = pref.current_character,
"ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/general/body/sanitize_character(var/sql_load = 0)
if(!pref.species || !(pref.species in playable_species))
@@ -18,13 +18,13 @@
return list("ss13_characters" = list("vars" = list("underwear", "undershirt", "socks", "backbag"), "args" = list("id")))
/datum/category_item/player_setup_item/general/equipment/gather_load_parameters()
return list(":id" = pref.current_character)
return list("id" = pref.current_character)
/datum/category_item/player_setup_item/general/equipment/gather_save_query()
return list("ss13_characters" = list("underwear", "undershirt", "socks", "backbag", "id" = 1, "ckey" = 1))
/datum/category_item/player_setup_item/general/equipment/gather_save_parameters()
return list(":underwear" = pref.underwear, ":undershirt" = pref.undershirt, ":socks" = pref.socks, ":backbag" = pref.backbag, ":id" = pref.current_character, ":ckey" = pref.client.ckey)
return list("underwear" = pref.underwear, "undershirt" = pref.undershirt, "socks" = pref.socks, "backbag" = pref.backbag, "id" = pref.current_character, "ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/general/equipment/sanitize_character(var/sql_load = 0)
if (sql_load)
@@ -31,7 +31,7 @@
"ss13_characters" = list("vars" = list("home_system", "citizenship", "faction", "religion"), "args" = list("id")))
/datum/category_item/player_setup_item/general/background/gather_load_parameters()
return list(":id" = pref.current_character, ":char_id" = pref.current_character)
return list("id" = pref.current_character, "char_id" = pref.current_character)
/datum/category_item/player_setup_item/general/background/gather_save_query()
return list("ss13_characters_flavour" = list("records_employment",
@@ -41,16 +41,16 @@
"ss13_characters" = list("home_system", "citizenship", "faction", "religion", "id" = 1, "ckey" = 1))
/datum/category_item/player_setup_item/general/background/gather_save_parameters()
return list(":records_employment" = pref.gen_record,
":records_medical" = pref.med_record,
":records_security" = pref.sec_record,
":char_id" = pref.current_character,
":home_system" = pref.home_system,
":citizenship" = pref.citizenship,
":faction" = pref.faction,
":religion" = pref.religion,
":id" = pref.current_character,
":ckey" = pref.client.ckey)
return list("records_employment" = pref.gen_record,
"records_medical" = pref.med_record,
"records_security" = pref.sec_record,
"char_id" = pref.current_character,
"home_system" = pref.home_system,
"citizenship" = pref.citizenship,
"faction" = pref.faction,
"religion" = pref.religion,
"id" = pref.current_character,
"ckey" = pref.client.ckey)
/datum/category_item/player_setup_item/general/background/sanitize_character()
if(!pref.home_system)
@@ -59,7 +59,7 @@
return list("ss13_characters_flavour" = list("vars" = var_list, "args" = list("char_id")))
/datum/category_item/player_setup_item/general/flavor/gather_load_parameters()
return list(":char_id" = pref.current_character)
return list("char_id" = pref.current_character)
/datum/category_item/player_setup_item/general/flavor/gather_save_query()
var/list/var_list = list("flavour_general",
@@ -82,22 +82,22 @@
return list("ss13_characters_flavour" = var_list)
/datum/category_item/player_setup_item/general/flavor/gather_save_parameters()
var/list/var_list = list(":char_id" = pref.current_character,
":flavour_general" = pref.flavor_texts["general"],
":flavour_head" = pref.flavor_texts["head"],
":flavour_face" = pref.flavor_texts["face"],
":flavour_eyes" = pref.flavor_texts["eyes"],
":flavour_torso" = pref.flavor_texts["torso"],
":flavour_arms" = pref.flavor_texts["arms"],
":flavour_hands" = pref.flavor_texts["hands"],
":flavour_legs" = pref.flavor_texts["legs"],
":flavour_feet" = pref.flavor_texts["feet"],
":robot_default" = pref.flavour_texts_robot["default"],
":signature_font" = pref.signfont,
":signature" = pref.signature)
var/list/var_list = list("char_id" = pref.current_character,
"flavour_general" = pref.flavor_texts["general"],
"flavour_head" = pref.flavor_texts["head"],
"flavour_face" = pref.flavor_texts["face"],
"flavour_eyes" = pref.flavor_texts["eyes"],
"flavour_torso" = pref.flavor_texts["torso"],
"flavour_arms" = pref.flavor_texts["arms"],
"flavour_hands" = pref.flavor_texts["hands"],
"flavour_legs" = pref.flavor_texts["legs"],
"flavour_feet" = pref.flavor_texts["feet"],
"robot_default" = pref.flavour_texts_robot["default"],
"signature" = pref.signature,
"signature_font" = pref.signfont)
for (var/module in robot_module_types)
var_list[":robot_[module]"] += pref.flavour_texts_robot[module]
var_list["robot_[module]"] += pref.flavour_texts_robot[module]
return var_list