SQLiteEstateData.cs 13 KB

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