1
0

SQLiteEstateData.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. if (Util.IsWindows())
  65. Util.LoadArchSpecificWindowsDll("sqlite3.dll");
  66. m_connectionString = connectionString;
  67. m_log.Info("[ESTATE DB]: Sqlite - connecting: "+m_connectionString);
  68. m_connection = new SqliteConnection(m_connectionString);
  69. m_connection.Open();
  70. Migration m = new Migration(m_connection, Assembly, "EstateStore");
  71. m.Update();
  72. //m_connection.Close();
  73. // m_connection.Open();
  74. Type t = typeof(EstateSettings);
  75. m_Fields = t.GetFields(BindingFlags.NonPublic |
  76. BindingFlags.Instance |
  77. BindingFlags.DeclaredOnly);
  78. foreach (FieldInfo f in m_Fields)
  79. if (f.Name.Substring(0, 2) == "m_")
  80. m_FieldMap[f.Name.Substring(2)] = f;
  81. }
  82. private string[] FieldList
  83. {
  84. get { return new List<string>(m_FieldMap.Keys).ToArray(); }
  85. }
  86. public EstateSettings LoadEstateSettings(UUID regionID, bool create)
  87. {
  88. 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";
  89. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  90. {
  91. cmd.CommandText = sql;
  92. cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
  93. return DoLoad(cmd, regionID, create);
  94. }
  95. }
  96. private EstateSettings DoLoad(SqliteCommand cmd, UUID regionID, bool create)
  97. {
  98. EstateSettings es = new EstateSettings();
  99. es.OnSave += StoreEstateSettings;
  100. IDataReader r = null;
  101. try
  102. {
  103. r = cmd.ExecuteReader();
  104. }
  105. catch (SqliteException)
  106. {
  107. 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.");
  108. }
  109. if (r != null && r.Read())
  110. {
  111. foreach (string name in FieldList)
  112. {
  113. if (m_FieldMap[name].GetValue(es) is bool)
  114. {
  115. int v = Convert.ToInt32(r[name]);
  116. if (v != 0)
  117. m_FieldMap[name].SetValue(es, true);
  118. else
  119. m_FieldMap[name].SetValue(es, false);
  120. }
  121. else if (m_FieldMap[name].GetValue(es) is UUID)
  122. {
  123. UUID uuid = UUID.Zero;
  124. UUID.TryParse(r[name].ToString(), out uuid);
  125. m_FieldMap[name].SetValue(es, uuid);
  126. }
  127. else
  128. {
  129. m_FieldMap[name].SetValue(es, Convert.ChangeType(r[name], m_FieldMap[name].FieldType));
  130. }
  131. }
  132. r.Close();
  133. }
  134. else if (create)
  135. {
  136. DoCreate(es);
  137. LinkRegion(regionID, (int)es.EstateID);
  138. }
  139. LoadBanList(es);
  140. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  141. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  142. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  143. return es;
  144. }
  145. public EstateSettings CreateNewEstate(int estateID)
  146. {
  147. EstateSettings es = new EstateSettings();
  148. es.OnSave += StoreEstateSettings;
  149. es.EstateID = Convert.ToUInt32(estateID);
  150. DoCreate(es);
  151. LoadBanList(es);
  152. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  153. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  154. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  155. return es;
  156. }
  157. private void DoCreate(EstateSettings es)
  158. {
  159. List<string> names = new List<string>(FieldList);
  160. // Remove EstateID and use AutoIncrement
  161. if (es.EstateID < 100)
  162. names.Remove("EstateID");
  163. using (SqliteCommand cmd = m_connection.CreateCommand())
  164. {
  165. string sql = "insert into estate_settings ("+String.Join(",", names.ToArray())+") values ( :"+String.Join(", :", names.ToArray())+")";
  166. cmd.CommandText = sql;
  167. cmd.Parameters.Clear();
  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.AddWithValue(":"+name, "1");
  174. else
  175. cmd.Parameters.AddWithValue(":"+name, "0");
  176. }
  177. else
  178. {
  179. cmd.Parameters.AddWithValue(":"+name, m_FieldMap[name].GetValue(es).ToString());
  180. }
  181. }
  182. cmd.ExecuteNonQuery();
  183. // Only get Auto ID if we actually used it else we just get 0
  184. if (es.EstateID < 100)
  185. {
  186. cmd.CommandText = "select LAST_INSERT_ROWID() as id";
  187. cmd.Parameters.Clear();
  188. using (IDataReader r = cmd.ExecuteReader())
  189. {
  190. r.Read();
  191. es.EstateID = Convert.ToUInt32(r["id"]);
  192. }
  193. }
  194. }
  195. }
  196. public void StoreEstateSettings(EstateSettings es)
  197. {
  198. List<string> fields = new List<string>(FieldList);
  199. fields.Remove("EstateID");
  200. List<string> terms = new List<string>();
  201. foreach (string f in fields)
  202. terms.Add(f+" = :"+f);
  203. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  204. {
  205. cmd.CommandText = "update estate_settings set " + String.Join(", ", terms.ToArray()) + " where EstateID = :EstateID"; ;
  206. cmd.Parameters.AddWithValue(":EstateID", es.EstateID);
  207. foreach (string name in FieldList)
  208. {
  209. if (m_FieldMap[name].GetValue(es) is bool)
  210. {
  211. if ((bool)m_FieldMap[name].GetValue(es))
  212. cmd.Parameters.AddWithValue(":"+name, "1");
  213. else
  214. cmd.Parameters.AddWithValue(":"+name, "0");
  215. }
  216. else
  217. {
  218. cmd.Parameters.AddWithValue(":"+name, m_FieldMap[name].GetValue(es).ToString());
  219. }
  220. }
  221. cmd.ExecuteNonQuery();
  222. }
  223. SaveBanList(es);
  224. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  225. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  226. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  227. }
  228. private void LoadBanList(EstateSettings es)
  229. {
  230. es.ClearBans();
  231. IDataReader r;
  232. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  233. {
  234. cmd.CommandText = "select * from estateban where EstateID = :EstateID";
  235. cmd.Parameters.AddWithValue(":EstateID", es.EstateID);
  236. r = cmd.ExecuteReader();
  237. }
  238. while (r.Read())
  239. {
  240. EstateBan eb = new EstateBan();
  241. eb.BannedUserID = DBGuid.FromDB(r["bannedUUID"]); ;
  242. eb.BannedHostAddress = "0.0.0.0";
  243. eb.BannedHostIPMask = "0.0.0.0";
  244. eb.BanningUserID = DBGuid.FromDB(r["banningUUID"]);
  245. eb.BanTime = Convert.ToInt32(r["banTime"]);
  246. es.AddBan(eb);
  247. }
  248. r.Close();
  249. }
  250. private void SaveBanList(EstateSettings es)
  251. {
  252. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  253. {
  254. cmd.CommandText = "delete from estateban where EstateID = :EstateID";
  255. cmd.Parameters.AddWithValue(":EstateID", es.EstateID.ToString());
  256. cmd.ExecuteNonQuery();
  257. cmd.Parameters.Clear();
  258. cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask, banningUUID, banTime) values ( :EstateID, :bannedUUID, '', '', '', :banningUUID, :banTime )";
  259. foreach (EstateBan b in es.EstateBans)
  260. {
  261. cmd.Parameters.AddWithValue(":EstateID", es.EstateID.ToString());
  262. cmd.Parameters.AddWithValue(":bannedUUID", b.BannedUserID.ToString());
  263. cmd.Parameters.AddWithValue(":banningUUID", b.BanningUserID.ToString());
  264. cmd.Parameters.AddWithValue(":banTime", b.BanTime);
  265. cmd.ExecuteNonQuery();
  266. cmd.Parameters.Clear();
  267. }
  268. }
  269. }
  270. void SaveUUIDList(uint EstateID, string table, UUID[] data)
  271. {
  272. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  273. {
  274. cmd.CommandText = "delete from "+table+" where EstateID = :EstateID";
  275. cmd.Parameters.AddWithValue(":EstateID", EstateID.ToString());
  276. cmd.ExecuteNonQuery();
  277. cmd.Parameters.Clear();
  278. cmd.CommandText = "insert into "+table+" (EstateID, uuid) values ( :EstateID, :uuid )";
  279. foreach (UUID uuid in data)
  280. {
  281. cmd.Parameters.AddWithValue(":EstateID", EstateID.ToString());
  282. cmd.Parameters.AddWithValue(":uuid", uuid.ToString());
  283. cmd.ExecuteNonQuery();
  284. cmd.Parameters.Clear();
  285. }
  286. }
  287. }
  288. UUID[] LoadUUIDList(uint EstateID, string table)
  289. {
  290. List<UUID> uuids = new List<UUID>();
  291. IDataReader r;
  292. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  293. {
  294. cmd.CommandText = "select uuid from "+table+" where EstateID = :EstateID";
  295. cmd.Parameters.AddWithValue(":EstateID", EstateID);
  296. r = cmd.ExecuteReader();
  297. }
  298. while (r.Read())
  299. {
  300. // EstateBan eb = new EstateBan();
  301. UUID uuid = new UUID();
  302. UUID.TryParse(r["uuid"].ToString(), out uuid);
  303. uuids.Add(uuid);
  304. }
  305. r.Close();
  306. return uuids.ToArray();
  307. }
  308. public EstateSettings LoadEstateSettings(int estateID)
  309. {
  310. string sql = "select estate_settings."+String.Join(",estate_settings.", FieldList)+" from estate_settings where estate_settings.EstateID = :EstateID";
  311. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  312. {
  313. cmd.CommandText = sql;
  314. cmd.Parameters.AddWithValue(":EstateID", estateID.ToString());
  315. return DoLoad(cmd, UUID.Zero, false);
  316. }
  317. }
  318. public List<EstateSettings> LoadEstateSettingsAll()
  319. {
  320. List<EstateSettings> estateSettings = new List<EstateSettings>();
  321. List<int> estateIds = GetEstatesAll();
  322. foreach (int estateId in estateIds)
  323. estateSettings.Add(LoadEstateSettings(estateId));
  324. return estateSettings;
  325. }
  326. public List<int> GetEstates(string search)
  327. {
  328. List<int> result = new List<int>();
  329. string sql = "select EstateID from estate_settings where estate_settings.EstateName = :EstateName";
  330. IDataReader r;
  331. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  332. {
  333. cmd.CommandText = sql;
  334. cmd.Parameters.AddWithValue(":EstateName", search);
  335. r = cmd.ExecuteReader();
  336. }
  337. while (r.Read())
  338. {
  339. result.Add(Convert.ToInt32(r["EstateID"]));
  340. }
  341. r.Close();
  342. return result;
  343. }
  344. public List<int> GetEstatesAll()
  345. {
  346. List<int> result = new List<int>();
  347. string sql = "select EstateID from estate_settings";
  348. IDataReader r;
  349. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  350. {
  351. cmd.CommandText = sql;
  352. r = cmd.ExecuteReader();
  353. }
  354. while (r.Read())
  355. {
  356. result.Add(Convert.ToInt32(r["EstateID"]));
  357. }
  358. r.Close();
  359. return result;
  360. }
  361. public List<int> GetEstatesByOwner(UUID ownerID)
  362. {
  363. List<int> result = new List<int>();
  364. string sql = "select EstateID from estate_settings where estate_settings.EstateOwner = :EstateOwner";
  365. IDataReader r;
  366. using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  367. {
  368. cmd.CommandText = sql;
  369. cmd.Parameters.AddWithValue(":EstateOwner", ownerID);
  370. r = cmd.ExecuteReader();
  371. }
  372. while (r.Read())
  373. {
  374. result.Add(Convert.ToInt32(r["EstateID"]));
  375. }
  376. r.Close();
  377. return result;
  378. }
  379. public bool LinkRegion(UUID regionID, int estateID)
  380. {
  381. using(SqliteTransaction transaction = m_connection.BeginTransaction())
  382. {
  383. // Delete any existing estate mapping for this region.
  384. using(SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  385. {
  386. cmd.CommandText = "delete from estate_map where RegionID = :RegionID";
  387. cmd.Transaction = transaction;
  388. cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
  389. cmd.ExecuteNonQuery();
  390. }
  391. using(SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
  392. {
  393. cmd.CommandText = "insert into estate_map values (:RegionID, :EstateID)";
  394. cmd.Transaction = transaction;
  395. cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
  396. cmd.Parameters.AddWithValue(":EstateID", estateID.ToString());
  397. if (cmd.ExecuteNonQuery() == 0)
  398. {
  399. transaction.Rollback();
  400. return false;
  401. }
  402. else
  403. {
  404. transaction.Commit();
  405. return true;
  406. }
  407. }
  408. }
  409. }
  410. public List<UUID> GetRegions(int estateID)
  411. {
  412. return new List<UUID>();
  413. }
  414. public bool DeleteEstate(int estateID)
  415. {
  416. return false;
  417. }
  418. }
  419. }