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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. private bool Connected { get; set; }
  11. private MySqlConnection Connection { get; set; }
  12. private ReaderWriterLockSlim DatabaseLock { get; set; }
  13. public Database(DatabaseConfig config)
  14. {
  15. Connected = false;
  16. Connection = null;
  17. DatabaseLock = new ReaderWriterLockSlim();
  18. Connect(config);
  19. }
  20. public List<Dictionary<string, object>> Query(string query, params object[] args)
  21. {
  22. List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
  23. if (Connected)
  24. {
  25. DatabaseLock.EnterWriteLock();
  26. MySqlCommand cmd = PrepareQuery(query, args);
  27. MySqlDataReader reader = cmd.ExecuteReader();
  28. while (reader.Read())
  29. {
  30. Dictionary<string, object> row = new Dictionary<string, object>();
  31. for (int i = 0; i < reader.FieldCount; i++)
  32. {
  33. row.Add(reader.GetName(i), reader.GetValue(i));
  34. }
  35. rows.Add(row);
  36. }
  37. reader.Close();
  38. DatabaseLock.ExitWriteLock();
  39. }
  40. return rows;
  41. }
  42. public object ScalarQuery(string query, params object[] args)
  43. {
  44. if (Connected)
  45. {
  46. DatabaseLock.EnterWriteLock();
  47. MySqlCommand cmd = PrepareQuery(query, args);
  48. object result = cmd.ExecuteScalar();
  49. DatabaseLock.ExitWriteLock();
  50. return result;
  51. }
  52. return null;
  53. }
  54. public void Execute(string query, params object[] args)
  55. {
  56. if (Connected)
  57. {
  58. DatabaseLock.EnterWriteLock();
  59. MySqlCommand cmd = PrepareQuery(query, args);
  60. int result = cmd.ExecuteNonQuery();
  61. DatabaseLock.ExitWriteLock();
  62. }
  63. }
  64. private void Connect(DatabaseConfig config)
  65. {
  66. if (Connection == null)
  67. {
  68. if (config.Server != string.Empty && config.Database != string.Empty && config.Username != string.Empty && config.Password != string.Empty)
  69. {
  70. 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);
  71. Connection = new MySqlConnection(strCon);
  72. try
  73. {
  74. Connection.Open();
  75. Connected = true;
  76. }
  77. catch (MySqlException ex)
  78. {
  79. Connected = false;
  80. }
  81. }
  82. }
  83. }
  84. private void Disconnect()
  85. {
  86. if (Connection != null && Connected)
  87. {
  88. Connected = false;
  89. Connection.Close();
  90. }
  91. }
  92. private MySqlCommand PrepareQuery(string query, object[] args)
  93. {
  94. if (Connected)
  95. {
  96. MySqlCommand cmd = new MySqlCommand();
  97. cmd.Connection = Connection;
  98. for (int i = 0; i < args.Length; i++)
  99. {
  100. string param = "{" + i + "}";
  101. string paramName = "@DBVar_" + i;
  102. query = query.Replace(param, paramName);
  103. cmd.Parameters.AddWithValue(paramName, args[i]);
  104. }
  105. cmd.CommandText = query;
  106. return cmd;
  107. }
  108. return null;
  109. }
  110. }
  111. }