Advertisement
Sshisbachza

Untitled

Feb 8th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. using DarkRift;
  2. using DarkRift.ConfigTools;
  3. using DarkRift.Storage;
  4. using MySql.Data.MySqlClient;
  5. using System;
  6. using System.Collections.Generic;
  7.  
  8. namespace MySQLConnector
  9. {
  10. internal class Connector : Database
  11. {
  12. public override string name { get { return "MySQLConnector"; } }
  13. public override string version { get { return "1.0"; } }
  14. public override Command[] commands { get { return new Command[0]; } }
  15. public override string author { get { return "Jamie Read"; } }
  16. public override string supportEmail { get { return "jamie.read@outlook.com"; } }
  17. public override string databaseName { get { return "MySQL"; } }
  18.  
  19. private string connectionString = "";
  20.  
  21. public Connector()
  22. {
  23. InstallSubdirectory(
  24. new Dictionary<string, byte[]>
  25. {
  26. {"settings.cnf", System.Text.Encoding.ASCII.GetBytes("ConnectionString:\t\tSERVER=localhost;DATABASE=DarkRift;USERNAME=username;PASSWORD=password")}
  27. }
  28. );
  29.  
  30. try
  31. {
  32. ConfigReader reader = new ConfigReader(GetSubdirectory() + @"\settings.cnf");
  33.  
  34. if (reader["ConnectionString"] == null)
  35. {
  36. Interface.LogFatal("ConnectionString was not defined in the MySQLConnector's settings.cnf file!");
  37. DarkRiftServer.Close(true);
  38. }
  39. connectionString = reader["ConnectionString"];
  40. }
  41. catch (System.IO.FileNotFoundException)
  42. {
  43. Interface.LogFatal("MySQLConnector's settings file is missing!");
  44. DarkRiftServer.Close(true);
  45. }
  46. }
  47.  
  48. /// <summary>
  49. /// Execute a query on the server with no return values.
  50. /// </summary>
  51. /// <param name="query">The query.</param>
  52. /// <param name="paramaters">The paramaters to be added to this query.</param>
  53. public override void ExecuteNonQuery(string query, params QueryParameter[] parameters)
  54. {
  55. try
  56. {
  57. using (MySqlConnection connection = new MySqlConnection(connectionString))
  58. {
  59. using (MySqlCommand command = new MySqlCommand(query, connection))
  60. {
  61. foreach (QueryParameter parameter in parameters)
  62. command.Parameters.AddWithValue(parameter.name, parameter.obj);
  63.  
  64. connection.Open();
  65. command.ExecuteNonQuery();
  66. }
  67. }
  68. }
  69. catch (MySqlException e)
  70. {
  71. throw new DatabaseException(e.Message, e);
  72. }
  73. }
  74.  
  75. /// <summary>
  76. /// Executes a query on the database with a scalar return.
  77. /// </summary>
  78. /// <returns>The object returned from the database.</returns>
  79. /// <param name="query">The query.</param>
  80. /// <param name="paramaters">The paramaters to be added to this query.</param>
  81. public override object ExecuteScalar(string query, params QueryParameter[] parameters)
  82. {
  83. try
  84. {
  85. using (MySqlConnection connection = new MySqlConnection(connectionString))
  86. {
  87. using (MySqlCommand command = new MySqlCommand(query, connection))
  88. {
  89. foreach (QueryParameter parameter in parameters)
  90. command.Parameters.AddWithValue(parameter.name, parameter.obj);
  91.  
  92. connection.Open();
  93. return command.ExecuteScalar();
  94. }
  95. }
  96. }
  97. catch (MySqlException e)
  98. {
  99. throw new DatabaseException(e.Message, e);
  100. }
  101. }
  102.  
  103. /// <summary>
  104. /// Executes a query on the database returning an array of rows.
  105. /// </summary>
  106. /// <returns>The rows of the database selected.</returns>
  107. /// <param name="query">The query.</param>
  108. /// <param name="paramaters">The paramaters to be added to this query.</param>
  109. public override DatabaseRow[] ExecuteQuery(string query, params QueryParameter[] parameters)
  110. {
  111. try
  112. {
  113. using (MySqlConnection connection = new MySqlConnection(connectionString))
  114. {
  115. using (MySqlCommand command = new MySqlCommand(query, connection))
  116. {
  117. foreach (QueryParameter parameter in parameters)
  118. command.Parameters.AddWithValue(parameter.name, parameter.obj);
  119.  
  120. connection.Open();
  121. using (MySqlDataReader reader = command.ExecuteReader())
  122. {
  123. int fieldCount = reader.FieldCount;
  124. List<DatabaseRow> rows = new List<DatabaseRow>();
  125.  
  126. while (reader.Read())
  127. {
  128. //For each row create a DatabaseRow
  129. DatabaseRow row = new DatabaseRow();
  130.  
  131. //And add each field to it
  132. for (int i = 0; i < fieldCount; i++)
  133. {
  134. row.Add(
  135. reader.GetName(i),
  136. reader.GetValue(i)
  137. );
  138. }
  139.  
  140. //Add it to the rows
  141. rows.Add(row);
  142. }
  143.  
  144. return rows.ToArray();
  145. }
  146. }
  147. }
  148. }
  149. catch (MySqlException e)
  150. {
  151. throw new DatabaseException(e.Message, e);
  152. }
  153. }
  154.  
  155. /// <summary>
  156. /// Removes any characters that could allow SQL injection.
  157. /// </summary>
  158. /// <param name="c">The string to escape</param>
  159. /// <param name="query">Query.</param>
  160. public override string EscapeString(string s)
  161. {
  162. return MySql.Data.MySqlClient.MySqlHelper.EscapeString(s);
  163. }
  164.  
  165. /// <summary>
  166. /// Releases all resource used by the <see cref="MySQLConnector.Connector"/> object.
  167. /// </summary>
  168. /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="MySQLConnector.Connector"/>. The
  169. /// <see cref="Dispose"/> method leaves the <see cref="MySQLConnector.Connector"/> in an unusable state. After calling
  170. /// <see cref="Dispose"/>, you must release all references to the <see cref="MySQLConnector.Connector"/> so the
  171. /// garbage collector can reclaim the memory that the <see cref="MySQLConnector.Connector"/> was occupying.</remarks>
  172. public override void Dispose()
  173. {
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement