using MySql.Data.MySqlClient; using ServiceShared.Database.Migrations; using ServiceShared.Models.Database; using System.Reflection; namespace ServiceShared.Database { public class DbContext { /// /// Current Application Version /// private static string _currentVersion = null; /// /// ConnectionString to the mysql server /// private readonly string _connectionString = null; /// /// Constructor of DbContext /// /// ConnectionString to the mysql server public DbContext(string connectionString) { this._connectionString = connectionString; } /// /// Runs a migration /// /// Migration that should be run /// Application version /// returns true if migration was succssfully public bool RunMigration(Migration migration, string currentVersion) { _currentVersion = currentVersion + (migration != null ? "_" + migration.Version.ToString() : ""); return migration.Run(this); } /// /// Sets application version /// /// public static void setApplicationVersion(string currentVersion) { _currentVersion = currentVersion; } /// /// Checks if database exists /// /// returns true if database exists public bool DatabaseExists() { bool result = false; try { MySqlConnection dbConnection = this.CreateConnection(); using (MySqlConnection client = this.CreateConnectionWithoutSelectedDatabase()) { client.Open(); MySqlCommand cmd = client.CreateCommand(); cmd.CommandText = "show databases like '" + dbConnection.Database + "'"; MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { result = true; client.Close(); } } } catch(Exception ex) { Log.Critical(ex, "ServiceShared.Database.DbContext", "DatabaseExists"); if(ex.InnerException != null) { Log.Critical(ex.InnerException, "ServiceShared.Database.DbContext", "DatabaseExists"); } result = false; } return result; } /// /// Creates a database if not exists /// /// returns true if the creation of database was successfully public bool DatabaseCreate() { bool result = false; try { MySqlConnection dbConnection = this.CreateConnection(); using (MySqlConnection client = this.CreateConnectionWithoutSelectedDatabase()) { client.Open(); MySqlCommand cmd = client.CreateCommand(); cmd.CommandText = "create database if not exists " + dbConnection.Database + " character set utf8mb4 collate utf8mb4_general_ci"; cmd.ExecuteNonQuery(); client.Close(); result = true; } } catch (Exception ex) { Log.Error(ex, "ServiceOutside.Database.DbContext", "DatabaseCreate"); } return result; } /// /// Maintenance flag /// /// returns true if Maintenance state has been activated public bool GetMaintenance() { bool result = false; try { using (MySqlConnection client = this.CreateConnection()) { client.Open(); MySqlCommand cmd = client.CreateCommand(); cmd.CommandText = @"select maintenance from config limit 1"; MySqlDataReader reader = cmd.ExecuteReader(); if (reader != null && reader.HasRows) { while (reader.Read()) { result = (reader["maintenance"] != null && reader["maintenance"] != DBNull.Value && Convert.ToInt32(reader["maintenance"].ToString()) == 1); } } client.Close(); } } catch (Exception ex) { Log.Error(ex, "ServiceOutside.Database.DbContext", "Maintenance"); } return result; } /// /// Returns a new MySqlConnection /// /// MySqlConnection public MySqlConnection CreateConnection() { return new MySqlConnection(this._connectionString); } /// /// Returns a new MySqlConnection without selected database /// /// MySqlConnection public MySqlConnection CreateConnectionWithoutSelectedDatabase() { string connectionString = null; if (!string.IsNullOrEmpty(this._connectionString)) { string[] parts = this._connectionString.Split(';'); if (parts.Length > 0) { connectionString = ""; foreach (string part in parts) { string[] values = part.Split('='); if (values.Length > 0) { if (values[0].ToLower().Replace(" ", "") != "database") { connectionString += part + ";"; } } } connectionString = connectionString.TrimEnd(';'); } } return new MySqlConnection(connectionString); } /// /// Retursn current migration of app /// /// Migration object public static string GetCurrentVersion() { return _currentVersion; } } }