SQLiteEstateData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 libsecondlife;
  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.MySQL
  40. {
  41. public class MySQLEstateStore : 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(LLUUID regionID)
  74. {
  75. EstateSettings es = new EstateSettings();
  76. 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";
  77. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  78. cmd.CommandText = sql;
  79. cmd.Parameters.Add("@RegionID", regionID.ToString());
  80. IDataReader r = cmd.ExecuteReader();
  81. if(r.Read())
  82. {
  83. foreach (string name in FieldList)
  84. {
  85. if(m_FieldMap[name].GetValue(es) is bool)
  86. {
  87. int v = Convert.ToInt32(r[name]);
  88. if(v != 0)
  89. m_FieldMap[name].SetValue(es, true);
  90. else
  91. m_FieldMap[name].SetValue(es, false);
  92. }
  93. else
  94. {
  95. m_FieldMap[name].SetValue(es, Convert.ChangeType(r[name], m_FieldMap[name].FieldType));
  96. }
  97. }
  98. r.Close();
  99. }
  100. else
  101. {
  102. // Migration case
  103. //
  104. r.Close();
  105. List<string> names = new List<string>(FieldList);
  106. names.Remove("EstateID");
  107. sql = "insert into estate_settings ("+String.Join(",", names.ToArray())+") values ( @"+String.Join(", @", names.ToArray())+")";
  108. cmd.CommandText = sql;
  109. cmd.Parameters.Clear();
  110. foreach (string name in FieldList)
  111. {
  112. if(m_FieldMap[name].GetValue(es) is bool)
  113. {
  114. if((bool)m_FieldMap[name].GetValue(es))
  115. cmd.Parameters.Add("@"+name, "1");
  116. else
  117. cmd.Parameters.Add("@"+name, "0");
  118. }
  119. else
  120. {
  121. cmd.Parameters.Add("@"+name, m_FieldMap[name].GetValue(es).ToString());
  122. }
  123. }
  124. cmd.ExecuteNonQuery();
  125. cmd.CommandText = "select LAST_INSERT_ROWID() as id";
  126. cmd.Parameters.Clear();
  127. r = cmd.ExecuteReader();
  128. r.Read();
  129. es.EstateID = Convert.ToUInt32(r["id"]);
  130. r.Close();
  131. cmd.CommandText = "insert into estate_map values (@RegionID, @EstateID)";
  132. cmd.Parameters.Add("@RegionID", regionID.ToString());
  133. cmd.Parameters.Add("@EstateID", es.EstateID.ToString());
  134. // This will throw on dupe key
  135. try
  136. {
  137. cmd.ExecuteNonQuery();
  138. }
  139. catch (Exception)
  140. {
  141. }
  142. // Munge and transfer the ban list
  143. //
  144. cmd.Parameters.Clear();
  145. cmd.CommandText = "insert into estateban select "+es.EstateID.ToString()+", bannedUUID, bannedIp, bannedIpHostMask, '' from regionban where regionban.regionUUID = @UUID";
  146. cmd.Parameters.Add("@UUID", regionID.ToString());
  147. try
  148. {
  149. cmd.ExecuteNonQuery();
  150. }
  151. catch (Exception)
  152. {
  153. }
  154. }
  155. LoadBanList(es);
  156. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  157. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  158. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  159. return es;
  160. }
  161. public void StoreEstateSettings(EstateSettings es)
  162. {
  163. List<string> fields = new List<string>(FieldList);
  164. fields.Remove("EstateID");
  165. List<string> terms = new List<string>();
  166. foreach (string f in fields)
  167. terms.Add(f+" = @"+f);
  168. string sql = "update estate_settings set "+String.Join(", ", terms.ToArray())+" where EstateID = @EstateID";
  169. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  170. cmd.CommandText = sql;
  171. foreach (string name in FieldList)
  172. {
  173. if(m_FieldMap[name].GetValue(es) is bool)
  174. {
  175. if((bool)m_FieldMap[name].GetValue(es))
  176. cmd.Parameters.Add("@"+name, "1");
  177. else
  178. cmd.Parameters.Add("@"+name, "0");
  179. }
  180. else
  181. {
  182. cmd.Parameters.Add("@"+name, m_FieldMap[name].GetValue(es).ToString());
  183. }
  184. }
  185. cmd.ExecuteNonQuery();
  186. SaveBanList(es);
  187. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  188. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  189. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  190. }
  191. private void LoadBanList(EstateSettings es)
  192. {
  193. es.ClearBans();
  194. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  195. cmd.CommandText = "select bannedUUID from estateban where EstateID = @EstateID";
  196. cmd.Parameters.Add("@EstateID", es.EstateID);
  197. IDataReader r = cmd.ExecuteReader();
  198. while(r.Read())
  199. {
  200. EstateBan eb = new EstateBan();
  201. LLUUID uuid = new LLUUID();
  202. LLUUID.TryParse(r["bannedUUID"].ToString(), out uuid);
  203. eb.bannedUUID = uuid;
  204. eb.bannedIP = "0.0.0.0";
  205. eb.bannedIPHostMask = "0.0.0.0";
  206. es.AddBan(eb);
  207. }
  208. r.Close();
  209. }
  210. private void SaveBanList(EstateSettings es)
  211. {
  212. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  213. cmd.CommandText = "delete from estateban where EstateID = @EstateID";
  214. cmd.Parameters.Add("@EstateID", es.EstateID.ToString());
  215. cmd.ExecuteNonQuery();
  216. cmd.Parameters.Clear();
  217. cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( @EstateID, @bannedUUID, '', '', '' )";
  218. foreach(EstateBan b in es.EstateBans)
  219. {
  220. cmd.Parameters.Add("@EstateID", es.EstateID.ToString());
  221. cmd.Parameters.Add("@bannedUUID", b.bannedUUID.ToString());
  222. cmd.ExecuteNonQuery();
  223. cmd.Parameters.Clear();
  224. }
  225. }
  226. void SaveUUIDList(uint EstateID, string table, LLUUID[] data)
  227. {
  228. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  229. cmd.CommandText = "delete from "+table+" where EstateID = @EstateID";
  230. cmd.Parameters.Add("@EstateID", EstateID.ToString());
  231. cmd.ExecuteNonQuery();
  232. cmd.Parameters.Clear();
  233. cmd.CommandText = "insert into "+table+" (EstateID, uuid) values ( @EstateID, @uuid )";
  234. foreach(LLUUID uuid in data)
  235. {
  236. cmd.Parameters.Add("@EstateID", EstateID.ToString());
  237. cmd.Parameters.Add("@uuid", uuid.ToString());
  238. cmd.ExecuteNonQuery();
  239. cmd.Parameters.Clear();
  240. }
  241. }
  242. LLUUID[] LoadUUIDList(uint EstateID, string table)
  243. {
  244. List<LLUUID> uuids = new List<LLUUID>();
  245. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  246. cmd.CommandText = "select uuid from "+table+" where EstateID = @EstateID";
  247. cmd.Parameters.Add("@EstateID", EstateID);
  248. IDataReader r = cmd.ExecuteReader();
  249. while(r.Read())
  250. {
  251. // EstateBan eb = new EstateBan();
  252. LLUUID uuid = new LLUUID();
  253. LLUUID.TryParse(r["uuid"].ToString(), out uuid);
  254. uuids.Add(uuid);
  255. }
  256. r.Close();
  257. return uuids.ToArray();
  258. }
  259. }
  260. }