diff --git a/code/controllers/news_controller.dm b/code/controllers/news_controller.dm index 1598fc8a226..f8d9ce3c808 100644 --- a/code/controllers/news_controller.dm +++ b/code/controllers/news_controller.dm @@ -27,7 +27,7 @@ var/global/datum/news_controller/news_controller //Stores a new article for publishing. /datum/news_controller/proc/update() - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) error("SQL database connection failed. News_controller failed to retreive information.") fails++ @@ -52,7 +52,7 @@ var/global/datum/news_controller/news_controller //Publish the stored article. /datum/news_controller/proc/publish() - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) error("SQL database connection failed. News_controller failed to retreive information.") fails++ diff --git a/code/defines/procs/dbcore.dm b/code/defines/procs/dbcore.dm index c1915051633..88335bd0aa4 100644 --- a/code/defines/procs/dbcore.dm +++ b/code/defines/procs/dbcore.dm @@ -40,6 +40,7 @@ DBConnection var/con_server = "" var/con_port = 3306 var/con_database = "" + var/failed_connections = 0 DBConnection/New(server, port = 3306, database, username, password_handler, cursor_handler = Default_Cursor, dbi_handler) con_user = username diff --git a/code/defines/procs/statistics.dm b/code/defines/procs/statistics.dm index 01c97f4fcfc..8eaf7a90a27 100644 --- a/code/defines/procs/statistics.dm +++ b/code/defines/procs/statistics.dm @@ -6,7 +6,7 @@ proc/sql_poll_population() for(var/mob/M in player_list) if(M.client) playercount += 1 - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) log_game("SQL ERROR during population polling. Failed to connect.") else @@ -49,7 +49,7 @@ proc/sql_report_death(var/mob/living/carbon/human/H) var/sqltime = time2text(world.realtime, "YYYY-MM-DD hh:mm:ss") var/coord = "[H.x], [H.y], [H.z]" //world << "INSERT INTO death (name, byondkey, job, special, pod, tod, laname, lakey, gender, bruteloss, fireloss, brainloss, oxyloss) VALUES ('[sqlname]', '[sqlkey]', '[sqljob]', '[sqlspecial]', '[sqlpod]', '[sqltime]', '[laname]', '[lakey]', '[H.gender]', [H.bruteloss], [H.getFireLoss()], [H.brainloss], [H.getOxyLoss()])" - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) log_game("SQL ERROR during death reporting. Failed to connect.") else @@ -83,7 +83,7 @@ proc/sql_report_cyborg_death(var/mob/living/silicon/robot/H) var/sqltime = time2text(world.realtime, "YYYY-MM-DD hh:mm:ss") var/coord = "[H.x], [H.y], [H.z]" //world << "INSERT INTO death (name, byondkey, job, special, pod, tod, laname, lakey, gender, bruteloss, fireloss, brainloss, oxyloss) VALUES ('[sqlname]', '[sqlkey]', '[sqljob]', '[sqlspecial]', '[sqlpod]', '[sqltime]', '[laname]', '[lakey]', '[H.gender]', [H.bruteloss], [H.getFireLoss()], [H.brainloss], [H.getOxyLoss()])" - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) log_game("SQL ERROR during death reporting. Failed to connect.") else @@ -113,7 +113,7 @@ proc/sql_commit_feedback() log_game("Round ended without any feedback being generated. No feedback was sent to the database.") return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) log_game("SQL ERROR during feedback reporting. Failed to connect.") else diff --git a/code/game/jobs/whitelist.dm b/code/game/jobs/whitelist.dm index b6f16388be7..28b216654d3 100644 --- a/code/game/jobs/whitelist.dm +++ b/code/game/jobs/whitelist.dm @@ -9,7 +9,7 @@ var/list/whitelist = list() /proc/load_whitelist() if (config.sql_whitelists) - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) //Continue with the old code if we have no database. @@ -43,7 +43,7 @@ var/list/whitelist = list() /proc/load_alienwhitelist() if (config.sql_whitelists) - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) //Continue with the old code if we have no database. diff --git a/code/game/machinery/requests_console.dm b/code/game/machinery/requests_console.dm index 7f24da000a9..7fc553fdf81 100644 --- a/code/game/machinery/requests_console.dm +++ b/code/game/machinery/requests_console.dm @@ -211,7 +211,7 @@ var/list/obj/machinery/requests_console/allConsoles = list() if(11) //form database dat += text("NanoTrasen Corporate Forms

") - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) dat += text("ERROR: Unable to contact external database. Please contact your system administrator for assistance.") log_game("SQL database connection failed. Attempted to fetch form information.") @@ -377,7 +377,7 @@ var/list/obj/machinery/requests_console/allConsoles = list() if(href_list["print"]) var/printid = sanitizeSQL(href_list["print"]) - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) alert("Connection to the database lost. Aborting.") @@ -405,7 +405,7 @@ var/list/obj/machinery/requests_console/allConsoles = list() if(href_list["whatis"]) var/whatisid = sanitizeSQL(href_list["whatis"]) - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) alert("Connection to the database lost. Aborting.") if(!whatisid) diff --git a/code/modules/admin/DB ban/ban_mirroring.dm b/code/modules/admin/DB ban/ban_mirroring.dm index 588acbdfccb..d6af7bafbba 100644 --- a/code/modules/admin/DB ban/ban_mirroring.dm +++ b/code/modules/admin/DB ban/ban_mirroring.dm @@ -6,7 +6,7 @@ if (!ckey || !address || !computer_id || !ban_id) return - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) error("Ban database connection failure while attempting to mirror. Key passed for mirror handling: [ckey].") diff --git a/code/modules/admin/DB ban/functions.dm b/code/modules/admin/DB ban/functions.dm index e36fc4ff21f..d266eaf7295 100644 --- a/code/modules/admin/DB ban/functions.dm +++ b/code/modules/admin/DB ban/functions.dm @@ -4,7 +4,7 @@ datum/admins/proc/DB_ban_record(var/bantype, var/mob/banned_mob, var/duration = if(!check_rights(R_MOD,0) && !check_rights(R_BAN)) return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return @@ -122,7 +122,7 @@ datum/admins/proc/DB_ban_unban(var/ckey, var/bantype, var/job = "") if(job) sql += " AND job = '[job]'" - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return @@ -217,7 +217,7 @@ datum/admins/proc/DB_ban_unban_by_id(var/id) var/sql = "SELECT ckey FROM ss13_ban WHERE id = [id]" - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return @@ -269,7 +269,7 @@ datum/admins/proc/DB_ban_unban_by_id(var/id) if(!check_rights(R_BAN)) return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) usr << "\red Failed to establish database connection" return diff --git a/code/modules/admin/IsBanned.dm b/code/modules/admin/IsBanned.dm index da4f2af534b..37eb7cc691b 100644 --- a/code/modules/admin/IsBanned.dm +++ b/code/modules/admin/IsBanned.dm @@ -34,7 +34,7 @@ world/IsBanned(key,address,computer_id) var/ckeytext = ckey(key) - if(!establish_db_connection()) + if(!establish_db_connection(dbcon)) error("Ban database connection failure. Key [ckeytext] not checked") log_misc("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 27eb8b6d3f3..3049976dfad 100644 --- a/code/modules/admin/admin_ranks.dm +++ b/code/modules/admin/admin_ranks.dm @@ -102,7 +102,7 @@ var/list/admin_ranks = list() //list of all ranks with associated rights else //The current admin system uses SQL - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) error("Failed to connect to database in load_admins(). Reverting to legacy system.") log_misc("Failed to connect to database in load_admins(). Reverting to legacy system.") diff --git a/code/modules/admin/banjob.dm b/code/modules/admin/banjob.dm index ae04799354c..96899fc6d9c 100644 --- a/code/modules/admin/banjob.dm +++ b/code/modules/admin/banjob.dm @@ -65,7 +65,7 @@ DEBUG jobban_keylist=list() log_admin("jobban_keylist was empty") else - if(!establish_db_connection()) + if(!establish_db_connection(dbcon)) error("Database connection failed. Reverting to the legacy ban system.") log_misc("Database connection failed. Reverting to the legacy ban system.") config.ban_legacy_system = 1 diff --git a/code/modules/admin/permissionverbs/permissionedit.dm b/code/modules/admin/permissionverbs/permissionedit.dm index 0969fa35d88..1af56750311 100644 --- a/code/modules/admin/permissionverbs/permissionedit.dm +++ b/code/modules/admin/permissionverbs/permissionedit.dm @@ -54,7 +54,7 @@ usr << "\red You do not have permission to do this!" return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) usr << "\red Failed to establish database connection" @@ -104,7 +104,7 @@ usr << "\red You do not have permission to do this!" return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) usr << "\red Failed to establish database connection" return diff --git a/code/modules/admin/player_notes_sql.dm b/code/modules/admin/player_notes_sql.dm index 1d447110771..cfd42df4b7c 100644 --- a/code/modules/admin/player_notes_sql.dm +++ b/code/modules/admin/player_notes_sql.dm @@ -12,7 +12,7 @@ else query_details[":a_ckey"] = user.ckey - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) alert("SQL connection failed while trying to add a note!") return @@ -36,7 +36,7 @@ if (!note_id || !note_edit) return - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) error("SQL connection failed while attempting to delete a note!") return @@ -93,7 +93,7 @@ player_ckey = ckey(player_ckey) admin_ckey = ckey(admin_ckey) - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) error("SQL connection failed while attempting to view a player's notes!") return @@ -187,7 +187,7 @@ if (!ckey) return "No ckey given!" - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) return "Unable to establish database connection! Aborting!" @@ -231,7 +231,7 @@ note_list >> note_keys msg_scopes("Establishing DB connection!") - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) msg_scopes("No DB connection!") return diff --git a/code/modules/admin/verbs/check_customitem_activity.dm b/code/modules/admin/verbs/check_customitem_activity.dm index 8d701e26734..e9bbaded60b 100644 --- a/code/modules/admin/verbs/check_customitem_activity.dm +++ b/code/modules/admin/verbs/check_customitem_activity.dm @@ -29,7 +29,7 @@ var/inactive_keys = "None
" if(checked_for_inactives) return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return diff --git a/code/modules/admin/verbs/warning.dm b/code/modules/admin/verbs/warning.dm index b19454d04de..3ad7a81bfff 100644 --- a/code/modules/admin/verbs/warning.dm +++ b/code/modules/admin/verbs/warning.dm @@ -9,7 +9,7 @@ if (!warned_ckey || !istext(warned_ckey)) return - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) usr << "Error: warn(): Database Connection failed, reverting to legacy systems." usr.client.warn_legacy(warned_ckey) @@ -110,7 +110,7 @@ var/dcolor = "#ffaaaa" //dark colour, severity = 1 var/ecolor = "#e3e3e3" //gray colour, expired = 1 - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) alert("Connection to the SQL database lost. Aborting. Please alert an Administrator or a member of staff.") return @@ -172,7 +172,7 @@ if (!warning_id) return - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) alert("Connection to SQL database failed while attempting to update your warning's status!") return @@ -191,7 +191,7 @@ var/count = 0 var/count_expire = 0 - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) return @@ -239,7 +239,7 @@ var/dcolor = "#ffdddd" //dark colour, severity = 1 var/ecolor = "#e3e3e3" //gray colour, expired = 1 - establish_db_connection() + establish_db_connection(dbcon) if (!dbcon.IsConnected()) alert("Connection to the SQL database lost. Aborting. Please alert the database admin!") return @@ -342,7 +342,7 @@ if(!warning_id || !warning_edit) return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) alert("Connection to the SQL database lost. Aborting. Please alert the database admin!") return diff --git a/code/modules/client/client procs.dm b/code/modules/client/client procs.dm index 09127b10095..672c15387fe 100644 --- a/code/modules/client/client procs.dm +++ b/code/modules/client/client procs.dm @@ -203,7 +203,7 @@ // Returns null if no DB connection can be established, or -1 if the requested key was not found in the database /proc/get_player_age(key) - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return null @@ -223,7 +223,7 @@ if ( IsGuestKey(src.key) ) return - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return diff --git a/code/modules/library/lib_machines.dm b/code/modules/library/lib_machines.dm index fd86f484ac9..7bbdaed4749 100644 --- a/code/modules/library/lib_machines.dm +++ b/code/modules/library/lib_machines.dm @@ -43,7 +43,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f Filter by Author: [author]
\[Start Search\]
"} if(1) - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) dat += "ERROR: Unable to contact External Archive. Please contact your system administrator for assistance.
" else if(!SQLquery) @@ -189,7 +189,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f (Return to main menu)
"} if(4) dat += "

External Archive

" - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) dat += "ERROR: Unable to contact External Archive. Please contact your system administrator for assistance." else @@ -332,7 +332,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f if(scanner.cache.unique) alert("This book has been rejected from the database. Aborting!") else - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) alert("Connection to Archive has been severed. Aborting.") else @@ -355,7 +355,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f if(href_list["targetid"]) var/sqlid = sanitizeSQL(href_list["targetid"]) - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) alert("Connection to Archive has been severed. Aborting.") if(bibledelay) diff --git a/code/modules/mob/new_player/new_player.dm b/code/modules/mob/new_player/new_player.dm index c5716172a08..ca7d753590c 100644 --- a/code/modules/mob/new_player/new_player.dm +++ b/code/modules/mob/new_player/new_player.dm @@ -41,7 +41,7 @@ output += "

Observe

" if(!IsGuestKey(src.key)) - establish_db_connection() + establish_db_connection(dbcon) if(dbcon.IsConnected()) var/isadmin = 0 @@ -181,7 +181,7 @@ return if(href_list["privacy_poll"]) - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return var/voted = 0 diff --git a/code/modules/mob/new_player/poll.dm b/code/modules/mob/new_player/poll.dm index 953119961e0..6051d178b4b 100644 --- a/code/modules/mob/new_player/poll.dm +++ b/code/modules/mob/new_player/poll.dm @@ -1,6 +1,6 @@ /mob/new_player/proc/handle_privacy_poll() - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return var/voted = 0 @@ -47,7 +47,7 @@ var/optiontext /mob/new_player/proc/handle_player_polling() - establish_db_connection() + establish_db_connection(dbcon) if(dbcon.IsConnected()) var/isadmin = 0 if(src.client && src.client.holder) @@ -81,7 +81,7 @@ /mob/new_player/proc/poll_player(var/pollid = -1) if(pollid == -1) return - establish_db_connection() + establish_db_connection(dbcon) if(dbcon.IsConnected()) var/DBQuery/select_query = dbcon.NewQuery("SELECT starttime, endtime, question, polltype, multiplechoiceoptions FROM ss13_poll_question WHERE id = [pollid]") @@ -342,7 +342,7 @@ if(!isnum(pollid) || !isnum(optionid)) return - establish_db_connection() + establish_db_connection(dbcon) if(dbcon.IsConnected()) var/DBQuery/select_query = dbcon.NewQuery("SELECT starttime, endtime, question, polltype, multiplechoiceoptions FROM ss13_poll_question WHERE id = [pollid] AND Now() BETWEEN starttime AND endtime") @@ -412,7 +412,7 @@ if(!isnum(pollid) || !istext(replytext)) return - establish_db_connection() + establish_db_connection(dbcon) if(dbcon.IsConnected()) var/DBQuery/select_query = dbcon.NewQuery("SELECT starttime, endtime, question, polltype FROM ss13_poll_question WHERE id = [pollid] AND Now() BETWEEN starttime AND endtime") @@ -470,7 +470,7 @@ if(!isnum(pollid) || !isnum(optionid)) return - establish_db_connection() + establish_db_connection(dbcon) if(dbcon.IsConnected()) var/DBQuery/select_query = dbcon.NewQuery("SELECT starttime, endtime, question, polltype FROM ss13_poll_question WHERE id = [pollid] AND Now() BETWEEN starttime AND endtime") diff --git a/code/modules/research/message_server.dm b/code/modules/research/message_server.dm index 9655f3b3dcd..9144cd11d98 100644 --- a/code/modules/research/message_server.dm +++ b/code/modules/research/message_server.dm @@ -297,7 +297,7 @@ var/obj/machinery/blackbox_recorder/blackbox if(!feedback) return round_end_data_gathering() //round_end time logging and some other data processing - establish_db_connection() + establish_db_connection(dbcon) if(!dbcon.IsConnected()) return var/round_id diff --git a/code/world.dm b/code/world.dm index f0315fde9c0..bc23a3a54ea 100644 --- a/code/world.dm +++ b/code/world.dm @@ -649,37 +649,39 @@ var/failed_old_db_connections = 0 //Construct the database object now that configs are loaded dbcon = new(sqladdress, sqlport, sqldb, sqllogin, sqlpass) - if(!setup_database_connection()) + if (!setup_database_connection(dbcon)) world.log << "Your server failed to establish a connection with the feedback database." else world.log << "Feedback database connection established." return 1 -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 conenct anymore. +/proc/setup_database_connection(var/DBConnection/con) + if (!con) return 0 - if(!dbcon) - dbcon = new() + if (con.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 conenct anymore. + return 0 - dbcon.Connect() - . = dbcon.IsConnected() + con.Connect() + . = con.IsConnected() if ( . ) - failed_db_connections = 0 //If this connection succeeded, reset the failed connections counter. + con.failed_connections = 0 //If this connection succeeded, reset the failed connections counter. else - failed_db_connections++ //If it failed, increase the failed connections counter. - world.log << dbcon.ErrorMsg() + con.failed_connections++ //If it failed, increase the failed connections counter. + world.log << con.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) +/proc/establish_db_connection(var/DBConnection/con) + if (!con) return 0 - if(!dbcon || !dbcon.IsConnected()) - return setup_database_connection() + if (con.failed_connections > FAILED_DB_CONNECTION_CUTOFF) + return 0 + + if (!con.IsConnected()) + return setup_database_connection(con) else return 1