SQLiteManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 connect)
  49. {
  50. try
  51. {
  52. string connectionString = String.Empty;
  53. if (connect != String.Empty)
  54. {
  55. connectionString = connect;
  56. }
  57. else
  58. {
  59. m_log.Warn("[SQLITE] grid db not specified, using default");
  60. connectionString = "URI=file:GridServerSqlite.db;";
  61. }
  62. dbcon = new SQLiteConnection(connectionString);
  63. dbcon.Open();
  64. }
  65. catch (Exception e)
  66. {
  67. throw new Exception("Error initialising SQLite Database: " + e.ToString());
  68. }
  69. }
  70. /// <summary>
  71. /// Shuts down the database connection
  72. /// </summary>
  73. public void Close()
  74. {
  75. dbcon.Close();
  76. dbcon = null;
  77. }
  78. /// <summary>
  79. /// Runs a query with protection against SQL Injection by using parameterised input.
  80. /// </summary>
  81. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  82. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  83. /// <returns>A SQLite DB Command</returns>
  84. public IDbCommand Query(string sql, Dictionary<string, string> parameters)
  85. {
  86. SQLiteCommand dbcommand = (SQLiteCommand) dbcon.CreateCommand();
  87. dbcommand.CommandText = sql;
  88. foreach (KeyValuePair<string, string> param in parameters)
  89. {
  90. SQLiteParameter paramx = new SQLiteParameter(param.Key, param.Value);
  91. dbcommand.Parameters.Add(paramx);
  92. }
  93. return (IDbCommand) dbcommand;
  94. }
  95. /// <summary>
  96. /// Reads a region row from a database reader
  97. /// </summary>
  98. /// <param name="reader">An active database reader</param>
  99. /// <returns>A region profile</returns>
  100. public RegionProfileData getRow(IDataReader reader)
  101. {
  102. RegionProfileData retval = new RegionProfileData();
  103. if (reader.Read())
  104. {
  105. // Region Main
  106. retval.regionHandle = (ulong) reader["regionHandle"];
  107. retval.regionName = (string) reader["regionName"];
  108. retval.UUID = new LLUUID((string) reader["uuid"]);
  109. // Secrets
  110. retval.regionRecvKey = (string) reader["regionRecvKey"];
  111. retval.regionSecret = (string) reader["regionSecret"];
  112. retval.regionSendKey = (string) reader["regionSendKey"];
  113. // Region Server
  114. retval.regionDataURI = (string) reader["regionDataURI"];
  115. retval.regionOnline = false; // Needs to be pinged before this can be set.
  116. retval.serverIP = (string) reader["serverIP"];
  117. retval.serverPort = (uint) reader["serverPort"];
  118. retval.serverURI = (string) reader["serverURI"];
  119. // Location
  120. retval.regionLocX = (uint) ((int) reader["locX"]);
  121. retval.regionLocY = (uint) ((int) reader["locY"]);
  122. retval.regionLocZ = (uint) ((int) reader["locZ"]);
  123. // Neighbours - 0 = No Override
  124. retval.regionEastOverrideHandle = (ulong) reader["eastOverrideHandle"];
  125. retval.regionWestOverrideHandle = (ulong) reader["westOverrideHandle"];
  126. retval.regionSouthOverrideHandle = (ulong) reader["southOverrideHandle"];
  127. retval.regionNorthOverrideHandle = (ulong) reader["northOverrideHandle"];
  128. // Assets
  129. retval.regionAssetURI = (string) reader["regionAssetURI"];
  130. retval.regionAssetRecvKey = (string) reader["regionAssetRecvKey"];
  131. retval.regionAssetSendKey = (string) reader["regionAssetSendKey"];
  132. // Userserver
  133. retval.regionUserURI = (string) reader["regionUserURI"];
  134. retval.regionUserRecvKey = (string) reader["regionUserRecvKey"];
  135. retval.regionUserSendKey = (string) reader["regionUserSendKey"];
  136. }
  137. else
  138. {
  139. throw new Exception("No rows to return");
  140. }
  141. return retval;
  142. }
  143. /// <summary>
  144. /// Inserts a new region into the database
  145. /// </summary>
  146. /// <param name="profile">The region to insert</param>
  147. /// <returns>Success?</returns>
  148. public bool insertRow(RegionProfileData profile)
  149. {
  150. string sql =
  151. "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  152. sql +=
  153. "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  154. sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES ";
  155. sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, ";
  156. sql +=
  157. "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, ";
  158. sql += "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey);";
  159. Dictionary<string, string> parameters = new Dictionary<string, string>();
  160. parameters["regionHandle"] = profile.regionHandle.ToString();
  161. parameters["regionName"] = profile.regionName;
  162. parameters["uuid"] = profile.UUID.ToString();
  163. parameters["regionRecvKey"] = profile.regionRecvKey;
  164. parameters["regionSendKey"] = profile.regionSendKey;
  165. parameters["regionDataURI"] = profile.regionDataURI;
  166. parameters["serverIP"] = profile.serverIP;
  167. parameters["serverPort"] = profile.serverPort.ToString();
  168. parameters["serverURI"] = profile.serverURI;
  169. parameters["locX"] = profile.regionLocX.ToString();
  170. parameters["locY"] = profile.regionLocY.ToString();
  171. parameters["locZ"] = profile.regionLocZ.ToString();
  172. parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString();
  173. parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString();
  174. parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString();
  175. parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString();
  176. parameters["regionAssetURI"] = profile.regionAssetURI;
  177. parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey;
  178. parameters["regionAssetSendKey"] = profile.regionAssetSendKey;
  179. parameters["regionUserURI"] = profile.regionUserURI;
  180. parameters["regionUserRecvKey"] = profile.regionUserRecvKey;
  181. parameters["regionUserSendKey"] = profile.regionUserSendKey;
  182. bool returnval = false;
  183. try
  184. {
  185. IDbCommand result = Query(sql, parameters);
  186. if (result.ExecuteNonQuery() == 1)
  187. returnval = true;
  188. result.Dispose();
  189. }
  190. catch (Exception)
  191. {
  192. return false;
  193. }
  194. return returnval;
  195. }
  196. }
  197. }