2016-01-29 22:40:27 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using MySql.Data.MySqlClient;
|
|
|
|
|
|
2018-06-15 02:57:03 +02:00
|
|
|
|
namespace Teknik.MailService
|
2016-01-29 22:40:27 +01:00
|
|
|
|
{
|
|
|
|
|
public class MysqlDatabase
|
|
|
|
|
{
|
|
|
|
|
public event EventHandler<string> MysqlErrorEvent;
|
|
|
|
|
|
|
|
|
|
private bool Connected { get; set; }
|
|
|
|
|
private MySqlConnection Connection { get; set; }
|
|
|
|
|
private ReaderWriterLockSlim DatabaseLock { get; set; }
|
|
|
|
|
|
2017-01-18 09:12:22 +01:00
|
|
|
|
public MysqlDatabase(string server, string database, string username, string password, int port)
|
2016-01-29 22:40:27 +01:00
|
|
|
|
{
|
|
|
|
|
Connected = false;
|
|
|
|
|
Connection = null;
|
|
|
|
|
DatabaseLock = new ReaderWriterLockSlim();
|
2017-01-18 09:12:22 +01:00
|
|
|
|
Connect(server, database, username, password, port);
|
2016-01-29 22:40:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Dictionary<string, object>> Query(string query, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
|
|
|
|
|
if (Connected)
|
|
|
|
|
{
|
|
|
|
|
DatabaseLock.EnterWriteLock();
|
|
|
|
|
MySqlCommand cmd = PrepareQuery(query, args);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
MySqlDataReader reader = cmd.ExecuteReader();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, object> row = new Dictionary<string, object>();
|
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
|
|
|
{
|
|
|
|
|
row.Add(reader.GetName(i), reader.GetValue(i));
|
|
|
|
|
}
|
|
|
|
|
rows.Add(row);
|
|
|
|
|
}
|
|
|
|
|
reader.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (MySqlException exception)
|
|
|
|
|
{
|
|
|
|
|
if (MysqlErrorEvent != null)
|
|
|
|
|
{
|
|
|
|
|
MysqlErrorEvent(this, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
if (MysqlErrorEvent != null)
|
|
|
|
|
{
|
|
|
|
|
MysqlErrorEvent(this, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DatabaseLock.ExitWriteLock();
|
|
|
|
|
}
|
|
|
|
|
return rows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ScalarQuery(string query, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (Connected)
|
|
|
|
|
{
|
|
|
|
|
DatabaseLock.EnterWriteLock();
|
|
|
|
|
MySqlCommand cmd = PrepareQuery(query, args);
|
|
|
|
|
object result = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
result = cmd.ExecuteScalar();
|
|
|
|
|
}
|
|
|
|
|
catch (MySqlException exception)
|
|
|
|
|
{
|
|
|
|
|
if (MysqlErrorEvent != null)
|
|
|
|
|
{
|
|
|
|
|
MysqlErrorEvent(this, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
if (MysqlErrorEvent != null)
|
|
|
|
|
{
|
|
|
|
|
MysqlErrorEvent(this, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DatabaseLock.ExitWriteLock();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Execute(string query, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (Connected)
|
|
|
|
|
{
|
|
|
|
|
DatabaseLock.EnterWriteLock();
|
|
|
|
|
MySqlCommand cmd = PrepareQuery(query, args);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int result = cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (MySqlException exception)
|
|
|
|
|
{
|
|
|
|
|
if (MysqlErrorEvent != null)
|
|
|
|
|
{
|
|
|
|
|
MysqlErrorEvent(this, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
if (MysqlErrorEvent != null)
|
|
|
|
|
{
|
|
|
|
|
MysqlErrorEvent(this, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DatabaseLock.ExitWriteLock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 09:12:22 +01:00
|
|
|
|
private void Connect(string server, string database, string username, string password, int port)
|
2016-01-29 22:40:27 +01:00
|
|
|
|
{
|
|
|
|
|
if (Connection == null)
|
|
|
|
|
{
|
2017-01-18 09:12:22 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(server) && !string.IsNullOrEmpty(database) && !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
|
2016-01-29 22:40:27 +01:00
|
|
|
|
{
|
2017-01-18 09:12:22 +01:00
|
|
|
|
string strCon = string.Format("Server={0}; database={1}; user={2}; password={3}; port={4}; charset=utf8; Allow Zero Datetime=true;", server, database, username, password, port);
|
2016-01-29 22:40:27 +01:00
|
|
|
|
Connection = new MySqlConnection(strCon);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Connection.Open();
|
|
|
|
|
Connected = true;
|
|
|
|
|
}
|
|
|
|
|
catch (MySqlException ex)
|
|
|
|
|
{
|
|
|
|
|
Connected = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Disconnect()
|
|
|
|
|
{
|
|
|
|
|
if (Connection != null && Connected)
|
|
|
|
|
{
|
|
|
|
|
Connected = false;
|
|
|
|
|
Connection.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MySqlCommand PrepareQuery(string query, object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (Connected)
|
|
|
|
|
{
|
|
|
|
|
MySqlCommand cmd = new MySqlCommand();
|
|
|
|
|
cmd.Connection = Connection;
|
|
|
|
|
for (int i = 0; i < args.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string param = "{" + i + "}";
|
|
|
|
|
string paramName = "@DBVar_" + i;
|
|
|
|
|
query = query.Replace(param, paramName);
|
|
|
|
|
cmd.Parameters.AddWithValue(paramName, args[i]);
|
|
|
|
|
}
|
|
|
|
|
cmd.CommandText = query;
|
|
|
|
|
return cmd;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|