MySQLManager.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. // MySQL Native
  6. using MySql;
  7. using MySql.Data;
  8. using MySql.Data.Types;
  9. using MySql.Data.MySqlClient;
  10. using OpenGrid.Framework.Data;
  11. namespace OpenGrid.Framework.Data.MySQL
  12. {
  13. class MySQLManager
  14. {
  15. IDbConnection dbcon;
  16. /// <summary>
  17. /// Initialises and creates a new MySQL connection and maintains it.
  18. /// </summary>
  19. /// <param name="hostname">The MySQL server being connected to</param>
  20. /// <param name="database">The name of the MySQL database being used</param>
  21. /// <param name="username">The username logging into the database</param>
  22. /// <param name="password">The password for the user logging in</param>
  23. /// <param name="cpooling">Whether to use connection pooling or not, can be one of the following: 'yes', 'true', 'no' or 'false', if unsure use 'false'.</param>
  24. public MySQLManager(string hostname, string database, string username, string password, string cpooling)
  25. {
  26. try
  27. {
  28. string connectionString = "Server=" + hostname + ";Database=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=" + cpooling + ";";
  29. dbcon = new MySqlConnection(connectionString);
  30. dbcon.Open();
  31. }
  32. catch (Exception e)
  33. {
  34. throw new Exception("Error initialising MySql Database: " + e.ToString());
  35. }
  36. }
  37. /// <summary>
  38. /// Shuts down the database connection
  39. /// </summary>
  40. public void Close()
  41. {
  42. dbcon.Close();
  43. dbcon = null;
  44. }
  45. /// <summary>
  46. /// Runs a query with protection against SQL Injection by using parameterised input.
  47. /// </summary>
  48. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  49. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  50. /// <returns>A MySQL DB Command</returns>
  51. public IDbCommand Query(string sql, Dictionary<string, string> parameters)
  52. {
  53. MySqlCommand dbcommand = (MySqlCommand)dbcon.CreateCommand();
  54. dbcommand.CommandText = sql;
  55. foreach (KeyValuePair<string, string> param in parameters)
  56. {
  57. dbcommand.Parameters.Add(param.Key, param.Value);
  58. }
  59. return (IDbCommand)dbcommand;
  60. }
  61. public SimProfileData getRow(IDataReader reader)
  62. {
  63. SimProfileData retval = new SimProfileData();
  64. if (reader.Read())
  65. {
  66. // Region Main
  67. retval.regionHandle = (ulong)reader["regionHandle"];
  68. retval.regionName = (string)reader["regionName"];
  69. retval.UUID = new libsecondlife.LLUUID((string)reader["uuid"]);
  70. // Secrets
  71. retval.regionRecvKey = (string)reader["regionRecvKey"];
  72. retval.regionSecret = (string)reader["regionSecret"];
  73. retval.regionSendKey = (string)reader["regionSendKey"];
  74. // Region Server
  75. retval.regionDataURI = (string)reader["regionDataURI"];
  76. retval.regionOnline = false; // Needs to be pinged before this can be set.
  77. retval.serverIP = (string)reader["serverIP"];
  78. retval.serverPort = (uint)reader["serverPort"];
  79. retval.serverURI = (string)reader["serverURI"];
  80. // Location
  81. retval.regionLocX = (uint)((int)reader["locX"]);
  82. retval.regionLocY = (uint)((int)reader["locY"]);
  83. retval.regionLocZ = (uint)((int)reader["locZ"]);
  84. // Neighbours - 0 = No Override
  85. retval.regionEastOverrideHandle = (ulong)reader["eastOverrideHandle"];
  86. retval.regionWestOverrideHandle = (ulong)reader["westOverrideHandle"];
  87. retval.regionSouthOverrideHandle = (ulong)reader["southOverrideHandle"];
  88. retval.regionNorthOverrideHandle = (ulong)reader["northOverrideHandle"];
  89. // Assets
  90. retval.regionAssetURI = (string)reader["regionAssetURI"];
  91. retval.regionAssetRecvKey = (string)reader["regionAssetRecvKey"];
  92. retval.regionAssetSendKey = (string)reader["regionAssetSendKey"];
  93. // Userserver
  94. retval.regionUserURI = (string)reader["regionUserURI"];
  95. retval.regionUserRecvKey = (string)reader["regionUserRecvKey"];
  96. retval.regionUserSendKey = (string)reader["regionUserSendKey"];
  97. }
  98. else
  99. {
  100. throw new Exception("No rows to return");
  101. }
  102. return retval;
  103. }
  104. public bool insertRow(SimProfileData profile) {
  105. string sql = "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  106. sql += "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  107. sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES ";
  108. sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, ";
  109. sql += "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, ";
  110. sql += "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey);";
  111. Dictionary<string, string> parameters = new Dictionary<string,string>();
  112. parameters["regionHandle"] = profile.regionHandle.ToString();
  113. parameters["regionName"] = profile.regionName;
  114. parameters["uuid"] = profile.UUID.ToString();
  115. parameters["regionRecvKey"] = profile.regionRecvKey;
  116. parameters["regionSendKey"] = profile.regionSendKey;
  117. parameters["regionDataURI"] = profile.regionDataURI;
  118. parameters["serverIP"] = profile.serverIP;
  119. parameters["serverPort"] = profile.serverPort.ToString();
  120. parameters["serverURI"] = profile.serverURI;
  121. parameters["locX"] = profile.regionLocX.ToString();
  122. parameters["locY"] = profile.regionLocY.ToString();
  123. parameters["locZ"] = profile.regionLocZ.ToString();
  124. parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString();
  125. parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString();
  126. parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString();
  127. parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString();
  128. parameters["regionAssetURI"] = profile.regionAssetURI;
  129. parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey;
  130. parameters["regionAssetSendKey"] = profile.regionAssetSendKey;
  131. parameters["regionUserURI"] = profile.regionUserURI;
  132. parameters["regionUserRecvKey"] = profile.regionUserRecvKey;
  133. parameters["regionUserSendKey"] = profile.regionUserSendKey;
  134. bool returnval = false;
  135. try
  136. {
  137. IDbCommand result = Query(sql, parameters);
  138. if (result.ExecuteNonQuery() == 1)
  139. returnval = true;
  140. result.Dispose();
  141. }
  142. catch (Exception e)
  143. {
  144. return false;
  145. }
  146. return returnval;
  147. }
  148. }
  149. }