SQLiteManager.cs 9.7 KB

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