MSSQLManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Data.SqlClient;
  32. using libsecondlife;
  33. namespace OpenSim.Framework.Data.MSSQL
  34. {
  35. /// <summary>
  36. /// A management class for the MS SQL Storage Engine
  37. /// </summary>
  38. class MSSqlManager
  39. {
  40. /// <summary>
  41. /// The database connection object
  42. /// </summary>
  43. IDbConnection dbcon;
  44. /// <summary>
  45. /// Initialises and creates a new Sql connection and maintains it.
  46. /// </summary>
  47. /// <param name="hostname">The Sql server being connected to</param>
  48. /// <param name="database">The name of the Sql database being used</param>
  49. /// <param name="username">The username logging into the database</param>
  50. /// <param name="password">The password for the user logging in</param>
  51. /// <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>
  52. public MSSqlManager(string hostname, string database, string username, string password, string cpooling)
  53. {
  54. try
  55. {
  56. string connectionString = "Server=" + hostname + ";Database=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=" + cpooling + ";";
  57. dbcon = new SqlConnection(connectionString);
  58. dbcon.Open();
  59. }
  60. catch (Exception e)
  61. {
  62. throw new Exception("Error initialising Sql Database: " + e.ToString());
  63. }
  64. }
  65. /// <summary>
  66. /// Shuts down the database connection
  67. /// </summary>
  68. public void Close()
  69. {
  70. dbcon.Close();
  71. dbcon = null;
  72. }
  73. /// <summary>
  74. /// Runs a query with protection against SQL Injection by using parameterised input.
  75. /// </summary>
  76. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  77. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  78. /// <returns>A Sql DB Command</returns>
  79. public IDbCommand Query(string sql, Dictionary<string, string> parameters)
  80. {
  81. SqlCommand dbcommand = (SqlCommand)dbcon.CreateCommand();
  82. dbcommand.CommandText = sql;
  83. foreach (KeyValuePair<string, string> param in parameters)
  84. {
  85. dbcommand.Parameters.AddWithValue(param.Key, param.Value);
  86. }
  87. return (IDbCommand)dbcommand;
  88. }
  89. /// <summary>
  90. /// Runs a database reader object and returns a region row
  91. /// </summary>
  92. /// <param name="reader">An active database reader</param>
  93. /// <returns>A region row</returns>
  94. public SimProfileData getRow(IDataReader reader)
  95. {
  96. SimProfileData regionprofile = new SimProfileData();
  97. if (reader.Read())
  98. {
  99. // Region Main
  100. regionprofile.regionHandle = (ulong)reader["regionHandle"];
  101. regionprofile.regionName = (string)reader["regionName"];
  102. regionprofile.UUID = new LLUUID((string)reader["uuid"]);
  103. // Secrets
  104. regionprofile.regionRecvKey = (string)reader["regionRecvKey"];
  105. regionprofile.regionSecret = (string)reader["regionSecret"];
  106. regionprofile.regionSendKey = (string)reader["regionSendKey"];
  107. // Region Server
  108. regionprofile.regionDataURI = (string)reader["regionDataURI"];
  109. regionprofile.regionOnline = false; // Needs to be pinged before this can be set.
  110. regionprofile.serverIP = (string)reader["serverIP"];
  111. regionprofile.serverPort = (uint)reader["serverPort"];
  112. regionprofile.serverURI = (string)reader["serverURI"];
  113. // Location
  114. regionprofile.regionLocX = (uint)((int)reader["locX"]);
  115. regionprofile.regionLocY = (uint)((int)reader["locY"]);
  116. regionprofile.regionLocZ = (uint)((int)reader["locZ"]);
  117. // Neighbours - 0 = No Override
  118. regionprofile.regionEastOverrideHandle = (ulong)reader["eastOverrideHandle"];
  119. regionprofile.regionWestOverrideHandle = (ulong)reader["westOverrideHandle"];
  120. regionprofile.regionSouthOverrideHandle = (ulong)reader["southOverrideHandle"];
  121. regionprofile.regionNorthOverrideHandle = (ulong)reader["northOverrideHandle"];
  122. // Assets
  123. regionprofile.regionAssetURI = (string)reader["regionAssetURI"];
  124. regionprofile.regionAssetRecvKey = (string)reader["regionAssetRecvKey"];
  125. regionprofile.regionAssetSendKey = (string)reader["regionAssetSendKey"];
  126. // Userserver
  127. regionprofile.regionUserURI = (string)reader["regionUserURI"];
  128. regionprofile.regionUserRecvKey = (string)reader["regionUserRecvKey"];
  129. regionprofile.regionUserSendKey = (string)reader["regionUserSendKey"];
  130. }
  131. else
  132. {
  133. throw new Exception("No rows to return");
  134. }
  135. return regionprofile;
  136. }
  137. /// <summary>
  138. /// Creates a new region in the database
  139. /// </summary>
  140. /// <param name="profile">The region profile to insert</param>
  141. /// <returns>Successful?</returns>
  142. public bool insertRow(SimProfileData profile)
  143. {
  144. string sql = "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  145. sql += "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  146. sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES ";
  147. sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, ";
  148. sql += "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, ";
  149. sql += "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey);";
  150. Dictionary<string, string> parameters = new Dictionary<string, string>();
  151. parameters["regionHandle"] = profile.regionHandle.ToString();
  152. parameters["regionName"] = profile.regionName;
  153. parameters["uuid"] = profile.UUID.ToString();
  154. parameters["regionRecvKey"] = profile.regionRecvKey;
  155. parameters["regionSendKey"] = profile.regionSendKey;
  156. parameters["regionDataURI"] = profile.regionDataURI;
  157. parameters["serverIP"] = profile.serverIP;
  158. parameters["serverPort"] = profile.serverPort.ToString();
  159. parameters["serverURI"] = profile.serverURI;
  160. parameters["locX"] = profile.regionLocX.ToString();
  161. parameters["locY"] = profile.regionLocY.ToString();
  162. parameters["locZ"] = profile.regionLocZ.ToString();
  163. parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString();
  164. parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString();
  165. parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString();
  166. parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString();
  167. parameters["regionAssetURI"] = profile.regionAssetURI;
  168. parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey;
  169. parameters["regionAssetSendKey"] = profile.regionAssetSendKey;
  170. parameters["regionUserURI"] = profile.regionUserURI;
  171. parameters["regionUserRecvKey"] = profile.regionUserRecvKey;
  172. parameters["regionUserSendKey"] = profile.regionUserSendKey;
  173. bool returnval = false;
  174. try
  175. {
  176. IDbCommand result = Query(sql, parameters);
  177. if (result.ExecuteNonQuery() == 1)
  178. returnval = true;
  179. result.Dispose();
  180. }
  181. catch (Exception)
  182. {
  183. return false;
  184. }
  185. return returnval;
  186. }
  187. }
  188. }