SQLiteManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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. using System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Data.SQLite;
  31. using System.Reflection;
  32. using libsecondlife;
  33. using log4net;
  34. namespace OpenSim.Data.SQLite
  35. {
  36. internal class SQLiteManager : SQLiteUtil
  37. {
  38. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. private IDbConnection dbcon;
  40. /// <summary>
  41. /// Initialises and creates a new SQLite connection and maintains it.
  42. /// </summary>
  43. /// <param name="hostname">The SQLite server being connected to</param>
  44. /// <param name="database">The name of the SQLite database being used</param>
  45. /// <param name="username">The username logging into the database</param>
  46. /// <param name="password">The password for the user logging in</param>
  47. /// <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>
  48. public SQLiteManager(string hostname, string database, string username, string password, string cpooling)
  49. {
  50. try
  51. {
  52. string connectionString = "URI=file:GridServerSqlite.db;";
  53. dbcon = new SQLiteConnection(connectionString);
  54. dbcon.Open();
  55. }
  56. catch (Exception e)
  57. {
  58. throw new Exception("Error initialising SQLite Database: " + e.ToString());
  59. }
  60. }
  61. /// <summary>
  62. /// Shuts down the database connection
  63. /// </summary>
  64. public void Close()
  65. {
  66. dbcon.Close();
  67. dbcon = null;
  68. }
  69. /// <summary>
  70. /// Runs a query with protection against SQL Injection by using parameterised input.
  71. /// </summary>
  72. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  73. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  74. /// <returns>A SQLite DB Command</returns>
  75. public IDbCommand Query(string sql, Dictionary<string, string> parameters)
  76. {
  77. SQLiteCommand dbcommand = (SQLiteCommand) dbcon.CreateCommand();
  78. dbcommand.CommandText = sql;
  79. foreach (KeyValuePair<string, string> param in parameters)
  80. {
  81. SQLiteParameter paramx = new SQLiteParameter(param.Key, param.Value);
  82. dbcommand.Parameters.Add(paramx);
  83. }
  84. return (IDbCommand) dbcommand;
  85. }
  86. /// <summary>
  87. /// Reads a region row from a database reader
  88. /// </summary>
  89. /// <param name="reader">An active database reader</param>
  90. /// <returns>A region profile</returns>
  91. public RegionProfileData getRow(IDataReader reader)
  92. {
  93. RegionProfileData retval = new RegionProfileData();
  94. if (reader.Read())
  95. {
  96. // Region Main
  97. retval.regionHandle = (ulong) reader["regionHandle"];
  98. retval.regionName = (string) reader["regionName"];
  99. retval.UUID = new LLUUID((string) reader["uuid"]);
  100. // Secrets
  101. retval.regionRecvKey = (string) reader["regionRecvKey"];
  102. retval.regionSecret = (string) reader["regionSecret"];
  103. retval.regionSendKey = (string) reader["regionSendKey"];
  104. // Region Server
  105. retval.regionDataURI = (string) reader["regionDataURI"];
  106. retval.regionOnline = false; // Needs to be pinged before this can be set.
  107. retval.serverIP = (string) reader["serverIP"];
  108. retval.serverPort = (uint) reader["serverPort"];
  109. retval.serverURI = (string) reader["serverURI"];
  110. // Location
  111. retval.regionLocX = (uint) ((int) reader["locX"]);
  112. retval.regionLocY = (uint) ((int) reader["locY"]);
  113. retval.regionLocZ = (uint) ((int) reader["locZ"]);
  114. // Neighbours - 0 = No Override
  115. retval.regionEastOverrideHandle = (ulong) reader["eastOverrideHandle"];
  116. retval.regionWestOverrideHandle = (ulong) reader["westOverrideHandle"];
  117. retval.regionSouthOverrideHandle = (ulong) reader["southOverrideHandle"];
  118. retval.regionNorthOverrideHandle = (ulong) reader["northOverrideHandle"];
  119. // Assets
  120. retval.regionAssetURI = (string) reader["regionAssetURI"];
  121. retval.regionAssetRecvKey = (string) reader["regionAssetRecvKey"];
  122. retval.regionAssetSendKey = (string) reader["regionAssetSendKey"];
  123. // Userserver
  124. retval.regionUserURI = (string) reader["regionUserURI"];
  125. retval.regionUserRecvKey = (string) reader["regionUserRecvKey"];
  126. retval.regionUserSendKey = (string) reader["regionUserSendKey"];
  127. }
  128. else
  129. {
  130. throw new Exception("No rows to return");
  131. }
  132. return retval;
  133. }
  134. /// <summary>
  135. /// Inserts a new region into the database
  136. /// </summary>
  137. /// <param name="profile">The region to insert</param>
  138. /// <returns>Success?</returns>
  139. public bool insertRow(RegionProfileData profile)
  140. {
  141. string sql =
  142. "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  143. sql +=
  144. "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  145. sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES ";
  146. sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, ";
  147. sql +=
  148. "@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. }