Use another way to connect to database. (#3)

* Use another way to connect to database. Actual way crash if mysql user doesn't have for example the password.

* remove tabs -> spaces
This commit is contained in:
Elle
2017-07-07 17:00:52 +02:00
committed by hondacrx
parent 9b4549874a
commit db31558ded
6 changed files with 84 additions and 13 deletions
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Framework.Database
{
public class ConnectionObject
{
public string Host { get; set; }
public string Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Database { get; set; }
}
}
+10 -8
View File
@@ -35,14 +35,16 @@ namespace Framework.Database
bool updatesEnabled = database.IsAutoUpdateEnabled(_updateFlags);
_open.Add(() =>
{
string dbString = ConfigMgr.GetDefaultValue(name + "DatabaseInfo", "");
if (string.IsNullOrEmpty(dbString))
{
Log.outError(LogFilter.ServerLoading, "Database {0} not specified in configuration file!", name);
return false;
}
ConnectionObject co = new ConnectionObject
{
Database = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Database", ""),
Host = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Host", ""),
Password = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Password", ""),
Port = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Port", ""),
Username = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Username", "")
};
var error = database.Initialize(dbString);
var error = database.Initialize(co);
if (error != MySqlErrorCode.None)
{
// Database does not exist
@@ -57,7 +59,7 @@ namespace Framework.Database
Log.outInfo(LogFilter.ServerLoading, "Creating database \"{0}\"...", name);
string sqlString = string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", name);
// Try to create the database and connect again if auto setup is enabled
if (database.Apply(sqlString) && database.Initialize(dbString) == MySqlErrorCode.None)
if (database.Apply(sqlString) && database.Initialize(co) == MySqlErrorCode.None)
error = MySqlErrorCode.None;
}
+30
View File
@@ -39,6 +39,16 @@ namespace Framework.Database
Poolsize = poolSize;
}
public MySqlConnectionInfo(ConnectionObject connectionObject, int poolSize = 10)
{
Host = connectionObject.Host;
Port = connectionObject.Port;
Username = connectionObject.Username;
Password = connectionObject.Password;
Database = connectionObject.Database;
Poolsize = poolSize;
}
public MySqlConnection GetConnection()
{
return new MySqlConnection(string.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};Allow Zero Datetime=True;Allow User Variables=True;Pooling=true;MaximumPoolSize={5};",
@@ -81,6 +91,26 @@ namespace Framework.Database
}
}
public MySqlErrorCode Initialize(ConnectionObject connectionObject)
{
_connectionInfo = new MySqlConnectionInfo(connectionObject);
_updater = new DatabaseUpdater<T>(this);
using (var connection = _connectionInfo.GetConnection())
{
try
{
connection.Open();
Log.outInfo(LogFilter.SqlDriver, "Connected to MySQL(ver: {0}) Database: {1}", connection.ServerVersion, _connectionInfo.Database);
return MySqlErrorCode.None;
}
catch (MySqlException ex)
{
return (MySqlErrorCode)((MySqlException)ex.InnerException).Number;
}
}
}
public void Execute(string sql, params object[] args)
{
Execute(new PreparedStatement(string.Format(sql, args)));
+1
View File
@@ -131,6 +131,7 @@
<Compile Include="Cryptography\SARC4.cs" />
<Compile Include="Cryptography\SessionKeyGeneration.cs" />
<Compile Include="Cryptography\ShaHmac.cs" />
<Compile Include="Database\ConnectionObject.cs" />
<Compile Include="Database\DatabaseLoader.cs" />
<Compile Include="Database\Databases\CharacterDatabase.cs" />
<Compile Include="Database\DatabaseUpdater.cs" />