SQLiteEstateData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.IO;
  31. using System.Reflection;
  32. using System.Threading;
  33. using OpenMetaverse;
  34. using Mono.Data.SqliteClient;
  35. using log4net;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Environment.Interfaces;
  38. using OpenSim.Region.Environment.Scenes;
  39. namespace OpenSim.Data.SQLite
  40. {
  41. public class SQLiteEstateStore : IEstateDataStore
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private SqliteConnection m_connection;
  46. private string m_connectionString;
  47. private FieldInfo[] m_Fields;
  48. private Dictionary<string, FieldInfo> m_FieldMap =
  49. new Dictionary<string, FieldInfo>();
  50. public void Initialise(string connectionString)
  51. {
  52. m_connectionString = connectionString;
  53. m_log.Info("[ESTATE DB]: Sqlite - connecting: "+m_connectionString);
  54. m_connection = new SqliteConnection(m_connectionString);
  55. m_connection.Open();
  56. Assembly assem = GetType().Assembly;
  57. Migration m = new Migration(m_connection, assem, "EstateStore");
  58. m.Update();
  59. m_connection.Close();
  60. m_connection.Open();
  61. Type t = typeof(EstateSettings);
  62. m_Fields = t.GetFields(BindingFlags.NonPublic |
  63. BindingFlags.Instance |
  64. BindingFlags.DeclaredOnly);
  65. foreach (FieldInfo f in m_Fields)
  66. if (f.Name.Substring(0, 2) == "m_")
  67. m_FieldMap[f.Name.Substring(2)] = f;
  68. }
  69. private string[] FieldList
  70. {
  71. get { return new List<string>(m_FieldMap.Keys).ToArray(); }
  72. }
  73. public EstateSettings LoadEstateSettings(UUID regionID)
  74. {
  75. EstateSettings es = new EstateSettings();
  76. es.OnSave += StoreEstateSettings;
  77. string sql = "select estate_settings."+String.Join(",estate_settings.", FieldList)+" from estate_map left join estate_settings on estate_map.EstateID = estate_settings.EstateID where estate_settings.EstateID is not null and RegionID = :RegionID";
  78. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  79. cmd.CommandText = sql;
  80. cmd.Parameters.Add(":RegionID", regionID.ToString());
  81. IDataReader r = cmd.ExecuteReader();
  82. if (r.Read())
  83. {
  84. foreach (string name in FieldList)
  85. {
  86. if (m_FieldMap[name].GetValue(es) is bool)
  87. {
  88. int v = Convert.ToInt32(r[name]);
  89. if (v != 0)
  90. m_FieldMap[name].SetValue(es, true);
  91. else
  92. m_FieldMap[name].SetValue(es, false);
  93. }
  94. else if (m_FieldMap[name].GetValue(es) is OpenMetaverse.UUID)
  95. {
  96. UUID uuid = UUID.Zero;
  97. UUID.TryParse(r[name].ToString(), out uuid);
  98. m_FieldMap[name].SetValue(es, uuid);
  99. }
  100. else
  101. {
  102. m_FieldMap[name].SetValue(es, Convert.ChangeType(r[name], m_FieldMap[name].FieldType));
  103. }
  104. }
  105. r.Close();
  106. }
  107. else
  108. {
  109. // Migration case
  110. //
  111. r.Close();
  112. List<string> names = new List<string>(FieldList);
  113. names.Remove("EstateID");
  114. sql = "insert into estate_settings ("+String.Join(",", names.ToArray())+") values ( :"+String.Join(", :", names.ToArray())+")";
  115. cmd.CommandText = sql;
  116. cmd.Parameters.Clear();
  117. foreach (string name in FieldList)
  118. {
  119. if (m_FieldMap[name].GetValue(es) is bool)
  120. {
  121. if ((bool)m_FieldMap[name].GetValue(es))
  122. cmd.Parameters.Add(":"+name, "1");
  123. else
  124. cmd.Parameters.Add(":"+name, "0");
  125. }
  126. else
  127. {
  128. cmd.Parameters.Add(":"+name, m_FieldMap[name].GetValue(es).ToString());
  129. }
  130. }
  131. cmd.ExecuteNonQuery();
  132. cmd.CommandText = "select LAST_INSERT_ROWID() as id";
  133. cmd.Parameters.Clear();
  134. r = cmd.ExecuteReader();
  135. r.Read();
  136. es.EstateID = Convert.ToUInt32(r["id"]);
  137. r.Close();
  138. cmd.CommandText = "insert into estate_map values (:RegionID, :EstateID)";
  139. cmd.Parameters.Add(":RegionID", regionID.ToString());
  140. cmd.Parameters.Add(":EstateID", es.EstateID.ToString());
  141. // This will throw on dupe key
  142. try
  143. {
  144. cmd.ExecuteNonQuery();
  145. }
  146. catch (Exception)
  147. {
  148. }
  149. // Munge and transfer the ban list
  150. //
  151. cmd.Parameters.Clear();
  152. cmd.CommandText = "insert into estateban select "+es.EstateID.ToString()+", bannedUUID, bannedIp, bannedIpHostMask, '' from regionban where regionban.regionUUID = :UUID";
  153. cmd.Parameters.Add(":UUID", regionID.ToString());
  154. try
  155. {
  156. cmd.ExecuteNonQuery();
  157. }
  158. catch (Exception)
  159. {
  160. }
  161. es.Save();
  162. }
  163. LoadBanList(es);
  164. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  165. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  166. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  167. return es;
  168. }
  169. public void StoreEstateSettings(EstateSettings es)
  170. {
  171. List<string> fields = new List<string>(FieldList);
  172. fields.Remove("EstateID");
  173. List<string> terms = new List<string>();
  174. foreach (string f in fields)
  175. terms.Add(f+" = :"+f);
  176. string sql = "update estate_settings set "+String.Join(", ", terms.ToArray())+" where EstateID = :EstateID";
  177. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  178. cmd.CommandText = sql;
  179. foreach (string name in FieldList)
  180. {
  181. if (m_FieldMap[name].GetValue(es) is bool)
  182. {
  183. if ((bool)m_FieldMap[name].GetValue(es))
  184. cmd.Parameters.Add(":"+name, "1");
  185. else
  186. cmd.Parameters.Add(":"+name, "0");
  187. }
  188. else
  189. {
  190. cmd.Parameters.Add(":"+name, m_FieldMap[name].GetValue(es).ToString());
  191. }
  192. }
  193. cmd.ExecuteNonQuery();
  194. SaveBanList(es);
  195. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  196. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  197. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  198. }
  199. private void LoadBanList(EstateSettings es)
  200. {
  201. es.ClearBans();
  202. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  203. cmd.CommandText = "select bannedUUID from estateban where EstateID = :EstateID";
  204. cmd.Parameters.Add(":EstateID", es.EstateID);
  205. IDataReader r = cmd.ExecuteReader();
  206. while (r.Read())
  207. {
  208. EstateBan eb = new EstateBan();
  209. UUID uuid = new UUID();
  210. UUID.TryParse(r["bannedUUID"].ToString(), out uuid);
  211. eb.bannedUUID = uuid;
  212. eb.bannedIP = "0.0.0.0";
  213. eb.bannedIPHostMask = "0.0.0.0";
  214. es.AddBan(eb);
  215. }
  216. r.Close();
  217. }
  218. private void SaveBanList(EstateSettings es)
  219. {
  220. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  221. cmd.CommandText = "delete from estateban where EstateID = :EstateID";
  222. cmd.Parameters.Add(":EstateID", es.EstateID.ToString());
  223. cmd.ExecuteNonQuery();
  224. cmd.Parameters.Clear();
  225. cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( :EstateID, :bannedUUID, '', '', '' )";
  226. foreach (EstateBan b in es.EstateBans)
  227. {
  228. cmd.Parameters.Add(":EstateID", es.EstateID.ToString());
  229. cmd.Parameters.Add(":bannedUUID", b.bannedUUID.ToString());
  230. cmd.ExecuteNonQuery();
  231. cmd.Parameters.Clear();
  232. }
  233. }
  234. void SaveUUIDList(uint EstateID, string table, UUID[] data)
  235. {
  236. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  237. cmd.CommandText = "delete from "+table+" where EstateID = :EstateID";
  238. cmd.Parameters.Add(":EstateID", EstateID.ToString());
  239. cmd.ExecuteNonQuery();
  240. cmd.Parameters.Clear();
  241. cmd.CommandText = "insert into "+table+" (EstateID, uuid) values ( :EstateID, :uuid )";
  242. foreach (UUID uuid in data)
  243. {
  244. cmd.Parameters.Add(":EstateID", EstateID.ToString());
  245. cmd.Parameters.Add(":uuid", uuid.ToString());
  246. cmd.ExecuteNonQuery();
  247. cmd.Parameters.Clear();
  248. }
  249. }
  250. UUID[] LoadUUIDList(uint EstateID, string table)
  251. {
  252. List<UUID> uuids = new List<UUID>();
  253. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  254. cmd.CommandText = "select uuid from "+table+" where EstateID = :EstateID";
  255. cmd.Parameters.Add(":EstateID", EstateID);
  256. IDataReader r = cmd.ExecuteReader();
  257. while (r.Read())
  258. {
  259. // EstateBan eb = new EstateBan();
  260. UUID uuid = new UUID();
  261. UUID.TryParse(r["uuid"].ToString(), out uuid);
  262. uuids.Add(uuid);
  263. }
  264. r.Close();
  265. return uuids.ToArray();
  266. }
  267. }
  268. }