SQLiteEstateData.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. #if CSharpSqlite
  33. using Community.CsharpSqlite.Sqlite;
  34. #else
  35. using Mono.Data.Sqlite;
  36. #endif
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. namespace OpenSim.Data.SQLite
  41. {
  42. public class SQLiteEstateStore : IEstateDataStore
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private SqliteConnection m_connection;
  47. private string m_connectionString;
  48. private FieldInfo[] m_Fields;
  49. private Dictionary<string, FieldInfo> m_FieldMap =
  50. new Dictionary<string, FieldInfo>();
  51. protected virtual Assembly Assembly
  52. {
  53. get { return GetType().Assembly; }
  54. }
  55. public SQLiteEstateStore()
  56. {
  57. }
  58. public SQLiteEstateStore(string connectionString)
  59. {
  60. Initialise(connectionString);
  61. }
  62. public void Initialise(string connectionString)
  63. {
  64. m_connectionString = connectionString;
  65. m_log.Info("[ESTATE DB]: Sqlite - connecting: "+m_connectionString);
  66. m_connection = new SqliteConnection(m_connectionString);
  67. m_connection.Open();
  68. Migration m = new Migration(m_connection, Assembly, "EstateStore");
  69. m.Update();
  70. //m_connection.Close();
  71. // m_connection.Open();
  72. Type t = typeof(EstateSettings);
  73. m_Fields = t.GetFields(BindingFlags.NonPublic |
  74. BindingFlags.Instance |
  75. BindingFlags.DeclaredOnly);
  76. foreach (FieldInfo f in m_Fields)
  77. if (f.Name.Substring(0, 2) == "m_")
  78. m_FieldMap[f.Name.Substring(2)] = f;
  79. }
  80. private string[] FieldList
  81. {
  82. get { return new List<string>(m_FieldMap.Keys).ToArray(); }
  83. }
  84. public EstateSettings LoadEstateSettings(UUID regionID, bool create)
  85. {
  86. 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";
  87. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  88. cmd.CommandText = sql;
  89. cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
  90. return DoLoad(cmd, regionID, create);
  91. }
  92. private EstateSettings DoLoad(SqliteCommand cmd, UUID regionID, bool create)
  93. {
  94. EstateSettings es = new EstateSettings();
  95. es.OnSave += StoreEstateSettings;
  96. IDataReader r = null;
  97. try
  98. {
  99. r = cmd.ExecuteReader();
  100. }
  101. catch (SqliteException)
  102. {
  103. m_log.Error("[SQLITE]: There was an issue loading the estate settings. This can happen the first time running OpenSimulator with CSharpSqlite the first time. OpenSimulator will probably crash, restart it and it should be good to go.");
  104. }
  105. if (r != null && r.Read())
  106. {
  107. foreach (string name in FieldList)
  108. {
  109. if (m_FieldMap[name].GetValue(es) is bool)
  110. {
  111. int v = Convert.ToInt32(r[name]);
  112. if (v != 0)
  113. m_FieldMap[name].SetValue(es, true);
  114. else
  115. m_FieldMap[name].SetValue(es, false);
  116. }
  117. else if (m_FieldMap[name].GetValue(es) is UUID)
  118. {
  119. UUID uuid = UUID.Zero;
  120. UUID.TryParse(r[name].ToString(), out uuid);
  121. m_FieldMap[name].SetValue(es, uuid);
  122. }
  123. else
  124. {
  125. m_FieldMap[name].SetValue(es, Convert.ChangeType(r[name], m_FieldMap[name].FieldType));
  126. }
  127. }
  128. r.Close();
  129. }
  130. else if (create)
  131. {
  132. DoCreate(es);
  133. LinkRegion(regionID, (int)es.EstateID);
  134. }
  135. LoadBanList(es);
  136. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  137. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  138. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  139. return es;
  140. }
  141. public EstateSettings CreateNewEstate()
  142. {
  143. EstateSettings es = new EstateSettings();
  144. es.OnSave += StoreEstateSettings;
  145. DoCreate(es);
  146. LoadBanList(es);
  147. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  148. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  149. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  150. return es;
  151. }
  152. private void DoCreate(EstateSettings es)
  153. {
  154. List<string> names = new List<string>(FieldList);
  155. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  156. IDataReader r = null;
  157. names.Remove("EstateID");
  158. string sql = "insert into estate_settings ("+String.Join(",", names.ToArray())+") values ( :"+String.Join(", :", names.ToArray())+")";
  159. cmd.CommandText = sql;
  160. cmd.Parameters.Clear();
  161. foreach (string name in FieldList)
  162. {
  163. if (m_FieldMap[name].GetValue(es) is bool)
  164. {
  165. if ((bool)m_FieldMap[name].GetValue(es))
  166. cmd.Parameters.AddWithValue(":"+name, "1");
  167. else
  168. cmd.Parameters.AddWithValue(":"+name, "0");
  169. }
  170. else
  171. {
  172. cmd.Parameters.AddWithValue(":"+name, m_FieldMap[name].GetValue(es).ToString());
  173. }
  174. }
  175. cmd.ExecuteNonQuery();
  176. cmd.CommandText = "select LAST_INSERT_ROWID() as id";
  177. cmd.Parameters.Clear();
  178. r = cmd.ExecuteReader();
  179. r.Read();
  180. es.EstateID = Convert.ToUInt32(r["id"]);
  181. r.Close();
  182. es.Save();
  183. }
  184. public void StoreEstateSettings(EstateSettings es)
  185. {
  186. List<string> fields = new List<string>(FieldList);
  187. fields.Remove("EstateID");
  188. List<string> terms = new List<string>();
  189. foreach (string f in fields)
  190. terms.Add(f+" = :"+f);
  191. string sql = "update estate_settings set "+String.Join(", ", terms.ToArray())+" where EstateID = :EstateID";
  192. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  193. cmd.CommandText = sql;
  194. foreach (string name in FieldList)
  195. {
  196. if (m_FieldMap[name].GetValue(es) is bool)
  197. {
  198. if ((bool)m_FieldMap[name].GetValue(es))
  199. cmd.Parameters.AddWithValue(":"+name, "1");
  200. else
  201. cmd.Parameters.AddWithValue(":"+name, "0");
  202. }
  203. else
  204. {
  205. cmd.Parameters.AddWithValue(":"+name, m_FieldMap[name].GetValue(es).ToString());
  206. }
  207. }
  208. cmd.ExecuteNonQuery();
  209. SaveBanList(es);
  210. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  211. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  212. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  213. }
  214. private void LoadBanList(EstateSettings es)
  215. {
  216. es.ClearBans();
  217. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  218. cmd.CommandText = "select bannedUUID from estateban where EstateID = :EstateID";
  219. cmd.Parameters.AddWithValue(":EstateID", es.EstateID);
  220. IDataReader r = cmd.ExecuteReader();
  221. while (r.Read())
  222. {
  223. EstateBan eb = new EstateBan();
  224. UUID uuid = new UUID();
  225. UUID.TryParse(r["bannedUUID"].ToString(), out uuid);
  226. eb.BannedUserID = uuid;
  227. eb.BannedHostAddress = "0.0.0.0";
  228. eb.BannedHostIPMask = "0.0.0.0";
  229. es.AddBan(eb);
  230. }
  231. r.Close();
  232. }
  233. private void SaveBanList(EstateSettings es)
  234. {
  235. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  236. cmd.CommandText = "delete from estateban where EstateID = :EstateID";
  237. cmd.Parameters.AddWithValue(":EstateID", es.EstateID.ToString());
  238. cmd.ExecuteNonQuery();
  239. cmd.Parameters.Clear();
  240. cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( :EstateID, :bannedUUID, '', '', '' )";
  241. foreach (EstateBan b in es.EstateBans)
  242. {
  243. cmd.Parameters.AddWithValue(":EstateID", es.EstateID.ToString());
  244. cmd.Parameters.AddWithValue(":bannedUUID", b.BannedUserID.ToString());
  245. cmd.ExecuteNonQuery();
  246. cmd.Parameters.Clear();
  247. }
  248. }
  249. void SaveUUIDList(uint EstateID, string table, UUID[] data)
  250. {
  251. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  252. cmd.CommandText = "delete from "+table+" where EstateID = :EstateID";
  253. cmd.Parameters.AddWithValue(":EstateID", EstateID.ToString());
  254. cmd.ExecuteNonQuery();
  255. cmd.Parameters.Clear();
  256. cmd.CommandText = "insert into "+table+" (EstateID, uuid) values ( :EstateID, :uuid )";
  257. foreach (UUID uuid in data)
  258. {
  259. cmd.Parameters.AddWithValue(":EstateID", EstateID.ToString());
  260. cmd.Parameters.AddWithValue(":uuid", uuid.ToString());
  261. cmd.ExecuteNonQuery();
  262. cmd.Parameters.Clear();
  263. }
  264. }
  265. UUID[] LoadUUIDList(uint EstateID, string table)
  266. {
  267. List<UUID> uuids = new List<UUID>();
  268. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  269. cmd.CommandText = "select uuid from "+table+" where EstateID = :EstateID";
  270. cmd.Parameters.AddWithValue(":EstateID", EstateID);
  271. IDataReader r = cmd.ExecuteReader();
  272. while (r.Read())
  273. {
  274. // EstateBan eb = new EstateBan();
  275. UUID uuid = new UUID();
  276. UUID.TryParse(r["uuid"].ToString(), out uuid);
  277. uuids.Add(uuid);
  278. }
  279. r.Close();
  280. return uuids.ToArray();
  281. }
  282. public EstateSettings LoadEstateSettings(int estateID)
  283. {
  284. string sql = "select estate_settings."+String.Join(",estate_settings.", FieldList)+" from estate_settings where estate_settings.EstateID = :EstateID";
  285. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  286. cmd.CommandText = sql;
  287. cmd.Parameters.AddWithValue(":EstateID", estateID.ToString());
  288. return DoLoad(cmd, UUID.Zero, false);
  289. }
  290. public List<EstateSettings> LoadEstateSettingsAll()
  291. {
  292. List<EstateSettings> estateSettings = new List<EstateSettings>();
  293. List<int> estateIds = GetEstatesAll();
  294. foreach (int estateId in estateIds)
  295. estateSettings.Add(LoadEstateSettings(estateId));
  296. return estateSettings;
  297. }
  298. public List<int> GetEstates(string search)
  299. {
  300. List<int> result = new List<int>();
  301. string sql = "select EstateID from estate_settings where estate_settings.EstateName = :EstateName";
  302. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  303. cmd.CommandText = sql;
  304. cmd.Parameters.AddWithValue(":EstateName", search);
  305. IDataReader r = cmd.ExecuteReader();
  306. while (r.Read())
  307. {
  308. result.Add(Convert.ToInt32(r["EstateID"]));
  309. }
  310. r.Close();
  311. return result;
  312. }
  313. public List<int> GetEstatesAll()
  314. {
  315. List<int> result = new List<int>();
  316. string sql = "select EstateID from estate_settings";
  317. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  318. cmd.CommandText = sql;
  319. IDataReader r = cmd.ExecuteReader();
  320. while (r.Read())
  321. {
  322. result.Add(Convert.ToInt32(r["EstateID"]));
  323. }
  324. r.Close();
  325. return result;
  326. }
  327. public List<int> GetEstatesByOwner(UUID ownerID)
  328. {
  329. List<int> result = new List<int>();
  330. string sql = "select EstateID from estate_settings where estate_settings.EstateOwner = :EstateOwner";
  331. SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
  332. cmd.CommandText = sql;
  333. cmd.Parameters.AddWithValue(":EstateOwner", ownerID);
  334. IDataReader r = cmd.ExecuteReader();
  335. while (r.Read())
  336. {
  337. result.Add(Convert.ToInt32(r["EstateID"]));
  338. }
  339. r.Close();
  340. return result;
  341. }
  342. public bool LinkRegion(UUID regionID, int estateID)
  343. {
  344. SqliteTransaction transaction = m_connection.BeginTransaction();
  345. // Delete any existing estate mapping for this region.
  346. using(SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  347. {
  348. cmd.CommandText = "delete from estate_map where RegionID = :RegionID";
  349. cmd.Transaction = transaction;
  350. cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
  351. cmd.ExecuteNonQuery();
  352. }
  353. using(SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  354. {
  355. cmd.CommandText = "insert into estate_map values (:RegionID, :EstateID)";
  356. cmd.Transaction = transaction;
  357. cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
  358. cmd.Parameters.AddWithValue(":EstateID", estateID.ToString());
  359. if (cmd.ExecuteNonQuery() == 0)
  360. {
  361. transaction.Rollback();
  362. return false;
  363. }
  364. else
  365. {
  366. transaction.Commit();
  367. return true;
  368. }
  369. }
  370. }
  371. public List<UUID> GetRegions(int estateID)
  372. {
  373. return new List<UUID>();
  374. }
  375. public bool DeleteEstate(int estateID)
  376. {
  377. return false;
  378. }
  379. }
  380. }