-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from peppy/static-db-helper
Move database connection method to static helper class
- Loading branch information
Showing
2 changed files
with
48 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using MySqlConnector; | ||
|
||
namespace osu.Server.QueueProcessor | ||
{ | ||
/// <summary> | ||
/// Provides access to a MySQL database. | ||
/// </summary> | ||
public static class DatabaseAccess | ||
{ | ||
/// <summary> | ||
/// Retrieve a fresh MySQL connection. Should be disposed after use. | ||
/// </summary> | ||
public static MySqlConnection GetConnection() | ||
{ | ||
string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") ?? String.Empty; | ||
|
||
if (string.IsNullOrEmpty(connectionString)) | ||
{ | ||
string host = (Environment.GetEnvironmentVariable("DB_HOST") ?? "localhost"); | ||
string port = (Environment.GetEnvironmentVariable("DB_PORT") ?? "3306"); | ||
string user = (Environment.GetEnvironmentVariable("DB_USER") ?? "root"); | ||
string password = (Environment.GetEnvironmentVariable("DB_PASS") ?? string.Empty); | ||
string name = (Environment.GetEnvironmentVariable("DB_NAME") ?? "osu"); | ||
|
||
string passwordString = string.IsNullOrEmpty(password) ? string.Empty : $"Password={password};"; | ||
|
||
connectionString = $"Server={host};Port={port};Database={name};User ID={user};{passwordString}ConnectionTimeout=5;ConnectionReset=false;Pooling=true;"; | ||
} | ||
|
||
var connection = new MySqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
// TODO: remove this when we have set a saner time zone server-side. | ||
using (var cmd = connection.CreateCommand()) | ||
{ | ||
cmd.CommandText = "SET time_zone = '+00:00';"; | ||
cmd.ExecuteNonQuery(); | ||
} | ||
|
||
return connection; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters