The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Database.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Combot.Configurations;
  5. using MySql.Data.MySqlClient;
  6. namespace Combot.Databases
  7. {
  8. public class Database
  9. {
  10. public event EventHandler<string> MysqlErrorEvent;
  11. private bool Connected { get; set; }
  12. private MySqlConnection Connection { get; set; }
  13. private ReaderWriterLockSlim DatabaseLock { get; set; }
  14. public Database(DatabaseConfig config)
  15. {
  16. Connected = false;
  17. Connection = null;
  18. DatabaseLock = new ReaderWriterLockSlim();
  19. Connect(config);
  20. }
  21. public List<Dictionary<string, object>> Query(string query, params object[] args)
  22. {
  23. List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
  24. if (Connected)
  25. {
  26. DatabaseLock.EnterWriteLock();
  27. MySqlCommand cmd = PrepareQuery(query, args);
  28. try
  29. {
  30. MySqlDataReader reader = cmd.ExecuteReader();
  31. while (reader.Read())
  32. {
  33. Dictionary<string, object> row = new Dictionary<string, object>();
  34. for (int i = 0; i < reader.FieldCount; i++)
  35. {
  36. row.Add(reader.GetName(i), reader.GetValue(i));
  37. }
  38. rows.Add(row);
  39. }
  40. reader.Close();
  41. }
  42. catch (MySqlException exception)
  43. {
  44. if (MysqlErrorEvent != null)
  45. {
  46. MysqlErrorEvent(this, exception.Message);
  47. }
  48. }
  49. catch (Exception exception)
  50. {
  51. if (MysqlErrorEvent != null)
  52. {
  53. MysqlErrorEvent(this, exception.Message);
  54. }
  55. }
  56. DatabaseLock.ExitWriteLock();
  57. }
  58. return rows;
  59. }
  60. public object ScalarQuery(string query, params object[] args)
  61. {
  62. if (Connected)
  63. {
  64. DatabaseLock.EnterWriteLock();
  65. MySqlCommand cmd = PrepareQuery(query, args);
  66. object result = null;
  67. try
  68. {
  69. result = cmd.ExecuteScalar();
  70. }
  71. catch (MySqlException exception)
  72. {
  73. if (MysqlErrorEvent != null)
  74. {
  75. MysqlErrorEvent(this, exception.Message);
  76. }
  77. }
  78. catch (Exception exception)
  79. {
  80. if (MysqlErrorEvent != null)
  81. {
  82. MysqlErrorEvent(this, exception.Message);
  83. }
  84. }
  85. DatabaseLock.ExitWriteLock();
  86. return result;
  87. }
  88. return null;
  89. }
  90. public void Execute(string query, params object[] args)
  91. {
  92. if (Connected)
  93. {
  94. DatabaseLock.EnterWriteLock();
  95. MySqlCommand cmd = PrepareQuery(query, args);
  96. try
  97. {
  98. int result = cmd.ExecuteNonQuery();
  99. }
  100. catch (MySqlException exception)
  101. {
  102. if (MysqlErrorEvent != null)
  103. {
  104. MysqlErrorEvent(this, exception.Message);
  105. }
  106. }
  107. catch (Exception exception)
  108. {
  109. if (MysqlErrorEvent != null)
  110. {
  111. MysqlErrorEvent(this, exception.Message);
  112. }
  113. }
  114. DatabaseLock.ExitWriteLock();
  115. }
  116. }
  117. private void Connect(DatabaseConfig config)
  118. {
  119. if (Connection == null)
  120. {
  121. if (config.Server != string.Empty && config.Database != string.Empty && config.Username != string.Empty && config.Password != string.Empty)
  122. {
  123. string strCon = string.Format("Server={0}; database={1}; user={2}; password={3}; port={4}; charset=utf8", config.Server, config.Database, config.Username, config.Password, config.Port);
  124. Connection = new MySqlConnection(strCon);
  125. try
  126. {
  127. Connection.Open();
  128. Connected = true;
  129. }
  130. catch (MySqlException ex)
  131. {
  132. Connected = false;
  133. }
  134. }
  135. }
  136. }
  137. private void Disconnect()
  138. {
  139. if (Connection != null && Connected)
  140. {
  141. Connected = false;
  142. Connection.Close();
  143. }
  144. }
  145. private MySqlCommand PrepareQuery(string query, object[] args)
  146. {
  147. if (Connected)
  148. {
  149. MySqlCommand cmd = new MySqlCommand();
  150. cmd.Connection = Connection;
  151. for (int i = 0; i < args.Length; i++)
  152. {
  153. string param = "{" + i + "}";
  154. string paramName = "@DBVar_" + i;
  155. query = query.Replace(param, paramName);
  156. cmd.Parameters.AddWithValue(paramName, args[i]);
  157. }
  158. cmd.CommandText = query;
  159. return cmd;
  160. }
  161. return null;
  162. }
  163. }
  164. }