From 2d424cf6be18cc7ace07f491e9e6d417b4192bb4 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Sat, 18 Feb 2017 20:21:53 -0500 Subject: [PATCH] Moves some stuff that belongs in dbcore to dbcore (#24145) * Moves some stuff that belongs in dbcore to dbcore * Wew garbage * Ree * Fixed --- code/controllers/subsystem/garbage.dm | 1 - code/modules/admin/DB_ban/functions.dm | 12 ++---- code/modules/admin/IsBanned.dm | 2 +- code/modules/admin/admin_ranks.dm | 9 ++-- code/modules/admin/ipintel.dm | 4 +- .../admin/permissionverbs/permissionedit.dm | 7 +-- .../admin/verbs/reestablish_db_connection.dm | 5 +-- code/modules/client/client_procs.dm | 6 +-- code/modules/library/lib_machines.dm | 12 ++---- code/modules/library/random_books.dm | 2 +- code/modules/mob/new_player/new_player.dm | 4 +- code/modules/mob/new_player/poll.dm | 16 +++---- code/orphaned_procs/dbcore.dm | 31 +++++++++++-- code/orphaned_procs/statistics.dm | 15 +++---- code/world.dm | 43 +------------------ 15 files changed, 64 insertions(+), 105 deletions(-) diff --git a/code/controllers/subsystem/garbage.dm b/code/controllers/subsystem/garbage.dm index e88ed902656..37a8098397b 100644 --- a/code/controllers/subsystem/garbage.dm +++ b/code/controllers/subsystem/garbage.dm @@ -351,7 +351,6 @@ var/datum/subsystem/garbage_collector/SSgarbage //if find_references isn't working for some datum //update this list using tools/DMTreeToGlobalsList /datum/proc/find_references_in_globals() - SearchVar(failed_db_connections) SearchVar(nextmap) SearchVar(mapchanging) SearchVar(rebootingpendingmapchange) diff --git a/code/modules/admin/DB_ban/functions.dm b/code/modules/admin/DB_ban/functions.dm index 50a6f972afe..3e35d83715d 100644 --- a/code/modules/admin/DB_ban/functions.dm +++ b/code/modules/admin/DB_ban/functions.dm @@ -6,8 +6,7 @@ if(!check_rights(R_BAN)) return - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) src << "Failed to establish database connection." return @@ -183,8 +182,7 @@ if(job) sql += " AND job = '[job]'" - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) return var/ban_id @@ -280,8 +278,7 @@ var/sql = "SELECT ckey FROM [format_table_name("ban")] WHERE id = [id]" - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) return var/ban_number = 0 //failsafe @@ -333,8 +330,7 @@ if(!check_rights(R_BAN)) return - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) usr << "Failed to establish database connection." return diff --git a/code/modules/admin/IsBanned.dm b/code/modules/admin/IsBanned.dm index 725a3332112..7e1b8f038b6 100644 --- a/code/modules/admin/IsBanned.dm +++ b/code/modules/admin/IsBanned.dm @@ -61,7 +61,7 @@ var/ckeytext = ckey(key) - if(!establish_db_connection()) + if(!dbcon.Connect()) log_world("Ban database connection failure. Key [ckeytext] not checked") diary << "Ban database connection failure. Key [ckeytext] not checked" return diff --git a/code/modules/admin/admin_ranks.dm b/code/modules/admin/admin_ranks.dm index 4032554165a..b4816d87eab 100644 --- a/code/modules/admin/admin_ranks.dm +++ b/code/modules/admin/admin_ranks.dm @@ -127,8 +127,7 @@ var/list/admin_ranks = list() //list of all admin_rank datums previous_rights = R.rights else - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) log_world("Failed to connect to database in load_admin_ranks(). Reverting to legacy system.") diary << "Failed to connect to database in load_admin_ranks(). Reverting to legacy system." config.admin_legacy_system = 1 @@ -202,8 +201,7 @@ var/list/admin_ranks = list() //list of all admin_rank datums world.SetConfig("APP/admin", ckey, "role=admin") D.associate(directory[ckey]) //find the client for a ckey if they are connected and associate them with the new admin datum else - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) log_world("Failed to connect to database in load_admins(). Reverting to legacy system.") diary << "Failed to connect to database in load_admins(). Reverting to legacy system." config.admin_legacy_system = 1 @@ -375,8 +373,7 @@ var/list/admin_ranks = list() //list of all admin_rank datums edit_admin_permissions() /datum/admins/proc/updateranktodb(ckey,newrank) - establish_db_connection() - if (!dbcon.IsConnected()) + if(!dbcon.Connect()) return var/sql_ckey = sanitizeSQL(ckey) var/sql_admin_rank = sanitizeSQL(newrank) diff --git a/code/modules/admin/ipintel.dm b/code/modules/admin/ipintel.dm index af7296311f7..d6c02ee1e59 100644 --- a/code/modules/admin/ipintel.dm +++ b/code/modules/admin/ipintel.dm @@ -33,7 +33,7 @@ cachedintel.cache = TRUE return cachedintel - if (establish_db_connection()) + if(dbcon.Connect()) var/DBQuery/query = dbcon.NewQuery({" SELECT date, intel, TIMESTAMPDIFF(MINUTE,date,NOW()) FROM [format_table_name("ipintel")] @@ -61,7 +61,7 @@ res.intel = ip_intel_query(ip) if (updatecache && res.intel >= 0) SSipintel.cache[ip] = res - if (establish_db_connection()) + if(dbcon.Connect()) var/DBQuery/query = dbcon.NewQuery("INSERT INTO [format_table_name("ipintel")] (ip, intel) VALUES (INET_ATON('[ip]'), [res.intel]) ON DUPLICATE KEY UPDATE intel = VALUES(intel), date = NOW()") query.Execute() return diff --git a/code/modules/admin/permissionverbs/permissionedit.dm b/code/modules/admin/permissionverbs/permissionedit.dm index 63fe0ee3149..89a0a5d703e 100644 --- a/code/modules/admin/permissionverbs/permissionedit.dm +++ b/code/modules/admin/permissionverbs/permissionedit.dm @@ -60,9 +60,7 @@ if (!check_rights(R_PERMISSIONS)) return - establish_db_connection() - - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) usr << "Failed to establish database connection." return @@ -109,8 +107,7 @@ if(check_rights(R_PERMISSIONS)) return - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) usr << "Failed to establish database connection." return diff --git a/code/modules/admin/verbs/reestablish_db_connection.dm b/code/modules/admin/verbs/reestablish_db_connection.dm index 146b8481a19..d80ad68c8d0 100644 --- a/code/modules/admin/verbs/reestablish_db_connection.dm +++ b/code/modules/admin/verbs/reestablish_db_connection.dm @@ -15,7 +15,6 @@ return dbcon.Disconnect() - failed_db_connections = 0 log_admin("[key_name(usr)] has forced the database to disconnect") message_admins("[key_name_admin(usr)] has forced the database to disconnect!") feedback_add_details("admin_verb","FRDB") //If you are copy-pasting this, ensure the 2nd parameter is unique to the new proc! @@ -24,8 +23,8 @@ message_admins("[key_name_admin(usr)] is attempting to re-established the DB Connection") feedback_add_details("admin_verb","RDB") //If you are copy-pasting this, ensure the 2nd parameter is unique to the new proc! - failed_db_connections = 0 - if (!establish_db_connection()) + dbcon.failed_connections = 0 + if(!dbcon.Connect()) message_admins("Database connection failed: " + dbcon.ErrorMsg()) else message_admins("Database connection re-established") \ No newline at end of file diff --git a/code/modules/client/client_procs.dm b/code/modules/client/client_procs.dm index e333ffe225e..b7984a7fe4c 100644 --- a/code/modules/client/client_procs.dm +++ b/code/modules/client/client_procs.dm @@ -325,8 +325,7 @@ var/next_external_rsc = 0 if (IsGuestKey(src.key)) return - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) return var/sql_ckey = sanitizeSQL(src.ckey) @@ -347,8 +346,7 @@ var/next_external_rsc = 0 if (IsGuestKey(src.key)) return - establish_db_connection() - if (!dbcon.IsConnected()) + if (!dbcon.Connect()) return var/sql_ckey = sanitizeSQL(ckey) diff --git a/code/modules/library/lib_machines.dm b/code/modules/library/lib_machines.dm index e8ffdbe6368..5e628b6c952 100644 --- a/code/modules/library/lib_machines.dm +++ b/code/modules/library/lib_machines.dm @@ -43,8 +43,7 @@ dat += "Filter by Author: [author]
" dat += "\[Start Search\]
" if(1) - establish_db_connection() - if(!dbcon.IsConnected()) + if (!dbcon.Connect()) dat += "ERROR: Unable to contact External Archive. Please contact your system administrator for assistance.
" else if(!SQLquery) dat += "ERROR: Malformed search request. Please contact your system administrator for assistance.
" @@ -135,8 +134,7 @@ var/global/list/datum/cachedbook/cachedbooks // List of our cached book datums /proc/load_library_db_to_cache() if(cachedbooks) return - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) return cachedbooks = list() var/DBQuery/query = dbcon.NewQuery("SELECT id, author, title, category FROM [format_table_name("library")] WHERE isnull(deleted)") @@ -408,8 +406,7 @@ var/global/list/datum/cachedbook/cachedbooks // List of our cached book datums if(scanner.cache) var/choice = input("Are you certain you wish to upload this title to the Archive?") in list("Confirm", "Abort") if(choice == "Confirm") - establish_db_connection() - if(!dbcon.IsConnected()) + if (!dbcon.Connect()) alert("Connection to Archive has been severed. Aborting.") else @@ -446,8 +443,7 @@ var/global/list/datum/cachedbook/cachedbooks // List of our cached book datums if(href_list["targetid"]) var/sqlid = sanitizeSQL(href_list["targetid"]) - establish_db_connection() - if(!dbcon.IsConnected()) + if (!dbcon.Connect()) alert("Connection to Archive has been severed. Aborting.") if(cooldown > world.time) say("Printer unavailable. Please allow a short time before attempting to print.") diff --git a/code/modules/library/random_books.dm b/code/modules/library/random_books.dm index 1d5a3c3df6a..4685a3f2893 100644 --- a/code/modules/library/random_books.dm +++ b/code/modules/library/random_books.dm @@ -34,7 +34,7 @@ . = list() if(!isnum(amount) || amount<1) return - if(!establish_db_connection()) + if (!dbcon.Connect()) if(fail_loud || prob(5)) var/obj/item/weapon/paper/P = new(location) P.info = "There once was a book from Nantucket
But the database failed us, so f*$! it.
I tried to be good to you
Now this is an I.O.U
If you're feeling entitled, well, stuff it!

~" diff --git a/code/modules/mob/new_player/new_player.dm b/code/modules/mob/new_player/new_player.dm index 35c7030aded..e03ad3649e3 100644 --- a/code/modules/mob/new_player/new_player.dm +++ b/code/modules/mob/new_player/new_player.dm @@ -40,9 +40,7 @@ output += "

Observe

" if(!IsGuestKey(src.key)) - establish_db_connection() - - if(dbcon.IsConnected()) + if (dbcon.Connect()) var/isadmin = 0 if(src.client && src.client.holder) isadmin = 1 diff --git a/code/modules/mob/new_player/poll.dm b/code/modules/mob/new_player/poll.dm index 8297fbeb562..1083a9b3543 100644 --- a/code/modules/mob/new_player/poll.dm +++ b/code/modules/mob/new_player/poll.dm @@ -24,7 +24,7 @@ /mob/new_player/proc/poll_player(pollid) if(!pollid) return - if(!establish_db_connection()) + if (!dbcon.Connect()) usr << "Failed to establish database connection." return var/DBQuery/select_query = dbcon.NewQuery("SELECT starttime, endtime, question, polltype, multiplechoiceoptions FROM [format_table_name("poll_question")] WHERE id = [pollid]") @@ -349,7 +349,7 @@ var/table = "poll_vote" if (text) table = "poll_textreply" - if (!establish_db_connection()) + if (!dbcon.Connect()) usr << "Failed to establish database connection." return var/DBQuery/query_hasvoted = dbcon.NewQuery("SELECT id FROM `[format_table_name(table)]` WHERE pollid = [pollid] AND ckey = '[ckey]'") @@ -379,7 +379,7 @@ return 1 /mob/new_player/proc/vote_valid_check(pollid, holder, type) - if (!establish_db_connection()) + if (!dbcon.Connect()) src << "Failed to establish database connection." return 0 pollid = text2num(pollid) @@ -406,7 +406,7 @@ return 1 /mob/new_player/proc/vote_on_irv_poll(pollid, list/votelist) - if (!establish_db_connection()) + if (!dbcon.Connect()) src << "Failed to establish database connection." return 0 if (!vote_rig_check()) @@ -482,7 +482,7 @@ /mob/new_player/proc/vote_on_poll(pollid, optionid) - if(!establish_db_connection()) + if (!dbcon.Connect()) src << "Failed to establish database connection." return 0 if (!vote_rig_check()) @@ -504,7 +504,7 @@ return 1 /mob/new_player/proc/log_text_poll_reply(pollid, replytext) - if(!establish_db_connection()) + if (!dbcon.Connect()) src << "Failed to establish database connection." return 0 if (!vote_rig_check()) @@ -533,7 +533,7 @@ return 1 /mob/new_player/proc/vote_on_numval_poll(pollid, optionid, rating) - if(!establish_db_connection()) + if (!dbcon.Connect()) src << "Failed to establish database connection." return 0 if (!vote_rig_check()) @@ -563,7 +563,7 @@ return 1 /mob/new_player/proc/vote_on_multi_poll(pollid, optionid) - if(!establish_db_connection()) + if (!dbcon.Connect()) src << "Failed to establish database connection." return 0 if (!vote_rig_check()) diff --git a/code/orphaned_procs/dbcore.dm b/code/orphaned_procs/dbcore.dm index 83f7d50db3f..28d3fd5a1a0 100644 --- a/code/orphaned_procs/dbcore.dm +++ b/code/orphaned_procs/dbcore.dm @@ -1,4 +1,4 @@ - +#define FAILED_DB_CONNECTION_CUTOFF 5 //cursors #define Default_Cursor 0 @@ -47,6 +47,7 @@ DBConnection // var/server = "" var/port = 3306 + var/failed_connections = 0 DBConnection/New(dbi_handler,username,password_handler,cursor_handler) src.dbi = dbi_handler @@ -55,7 +56,26 @@ DBConnection/New(dbi_handler,username,password_handler,cursor_handler) src.default_cursor = cursor_handler _db_con = _dm_db_new_con() -DBConnection/proc/Connect(dbi_handler=src.dbi,user_handler=src.user,password_handler=src.password,cursor_handler) +DBConnection/proc/Connect() + if(IsConnected()) + return TRUE + + if(failed_connections > FAILED_DB_CONNECTION_CUTOFF) //If it failed to establish a connection more than 5 times in a row, don't bother attempting to connect anymore. + return FALSE + + var/user = sqlfdbklogin + var/pass = sqlfdbkpass + var/db = sqlfdbkdb + var/address = sqladdress + var/port = sqlport + + doConnect("dbi:mysql:[db]:[address]:[port]","[user]","[pass]") + . = IsConnected() + if (!. && config.sql_enabled) + log_world("SQL error: " + ErrorMsg()) + ++failed_connections + +DBConnection/proc/doConnect(dbi_handler=src.dbi,user_handler=src.user,password_handler=src.password,cursor_handler) if(!config.sql_enabled) return 0 if(!src) return 0 @@ -63,7 +83,9 @@ DBConnection/proc/Connect(dbi_handler=src.dbi,user_handler=src.user,password_han if(!cursor_handler) cursor_handler = Default_Cursor return _dm_db_connect(_db_con,dbi_handler,user_handler,password_handler,cursor_handler,null) -DBConnection/proc/Disconnect() return _dm_db_close(_db_con) +DBConnection/proc/Disconnect() + failed_connections = 0 + return _dm_db_close(_db_con) DBConnection/proc/IsConnected() if(!config.sql_enabled) return 0 @@ -206,3 +228,6 @@ DBColumn/proc/SqlTypeName(type_handler=src.sql_type) #undef TIME #undef STRING #undef BLOB + + +#undef FAILED_DB_CONNECTION_CUTOFF diff --git a/code/orphaned_procs/statistics.dm b/code/orphaned_procs/statistics.dm index bc6b6b2988b..58a473138d7 100644 --- a/code/orphaned_procs/statistics.dm +++ b/code/orphaned_procs/statistics.dm @@ -62,8 +62,7 @@ var/datum/feedback/blackbox = new() if (!feedback) return round_end_data_gathering() //round_end time logging and some other data processing - establish_db_connection() - if (!dbcon.IsConnected()) return + if (!dbcon.Connect()) return var/round_id var/DBQuery/query = dbcon.NewQuery("SELECT MAX(round_id) AS round_id FROM [format_table_name("feedback")]") @@ -213,8 +212,7 @@ var/datum/feedback/blackbox = new() for(var/mob/M in player_list) if(M.client) playercount += 1 - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) log_game("SQL ERROR during player polling. Failed to connect.") else var/sqltime = time2text(world.realtime, "YYYY-MM-DD hh:mm:ss") @@ -227,8 +225,7 @@ var/datum/feedback/blackbox = new() if(!config.sql_enabled) return var/admincount = admins.len - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) log_game("SQL ERROR during admin polling. Failed to connect.") else var/sqltime = time2text(world.realtime, "YYYY-MM-DD hh:mm:ss") @@ -273,8 +270,7 @@ var/datum/feedback/blackbox = new() var/coord = "[L.x], [L.y], [L.z]" var/map = MAP_NAME var/server = "[world.internet_address]:[world.port]" - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) log_game("SQL ERROR during death reporting. Failed to connect.") else var/DBQuery/query = dbcon.NewQuery("INSERT INTO [format_table_name("death")] (name, byondkey, job, special, pod, tod, laname, lakey, gender, bruteloss, fireloss, brainloss, oxyloss, coord, mapname, server) VALUES ('[sqlname]', '[sqlkey]', '[sqljob]', '[sqlspecial]', '[sqlpod]', '[sqltime]', '[laname]', '[lakey]', '[L.gender]', [L.getBruteLoss()], [L.getFireLoss()], [L.brainloss], [L.getOxyLoss()], '[coord]', '[map]', '[server]')") @@ -295,8 +291,7 @@ var/datum/feedback/blackbox = new() log_game("Round ended without any feedback being generated. No feedback was sent to the database.") return - establish_db_connection() - if(!dbcon.IsConnected()) + if(!dbcon.Connect()) log_game("SQL ERROR during feedback reporting. Failed to connect.") else diff --git a/code/world.dm b/code/world.dm index 4d7bb441d5a..d356c4e6b17 100644 --- a/code/world.dm +++ b/code/world.dm @@ -51,7 +51,7 @@ var/list/map_transition_config = MAP_TRANSITION_CONFIG timezoneOffset = text2num(time2text(0,"hh")) * 36000 if(config.sql_enabled) - if(!setup_database_connection()) + if(!dbcon.Connect()) log_world("Your server failed to establish a connection with the database.") else log_world("Database connection established.") @@ -353,47 +353,6 @@ var/list/map_transition_config = MAP_TRANSITION_CONFIG status = s -#define FAILED_DB_CONNECTION_CUTOFF 5 -var/failed_db_connections = 0 - -/proc/setup_database_connection() - - if(failed_db_connections >= FAILED_DB_CONNECTION_CUTOFF) //If it failed to establish a connection more than 5 times in a row, don't bother attempting to connect anymore. - return 0 - - if(!dbcon) - dbcon = new() - - var/user = sqlfdbklogin - var/pass = sqlfdbkpass - var/db = sqlfdbkdb - var/address = sqladdress - var/port = sqlport - - dbcon.Connect("dbi:mysql:[db]:[address]:[port]","[user]","[pass]") - . = dbcon.IsConnected() - if ( . ) - failed_db_connections = 0 //If this connection succeeded, reset the failed connections counter. - else - failed_db_connections++ //If it failed, increase the failed connections counter. - if(config.sql_enabled) - log_world("SQL error: " + dbcon.ErrorMsg()) - - return . - -//This proc ensures that the connection to the feedback database (global variable dbcon) is established -/proc/establish_db_connection() - if(failed_db_connections > FAILED_DB_CONNECTION_CUTOFF) - return 0 - - if(!dbcon || !dbcon.IsConnected()) - return setup_database_connection() - else - return 1 - -#undef FAILED_DB_CONNECTION_CUTOFF - - /proc/maprotate() if (!SERVERTOOLS) return