MySQLEstateData.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 MySql.Data.MySqlClient;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Data;
  37. namespace OpenSim.Data.MySQL
  38. {
  39. public class MySQLEstateStore : IEstateDataStore
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private string m_connectionString;
  44. private FieldInfo[] m_Fields;
  45. private Dictionary<string, FieldInfo> m_FieldMap = new();
  46. protected virtual Assembly Assembly
  47. {
  48. get { return GetType().Assembly; }
  49. }
  50. public MySQLEstateStore()
  51. {
  52. }
  53. public MySQLEstateStore(string connectionString)
  54. {
  55. Initialise(connectionString);
  56. }
  57. public void Initialise(string connectionString)
  58. {
  59. m_connectionString = connectionString;
  60. try
  61. {
  62. m_log.Info("[REGION DB]: MySql - connecting: " + Util.GetDisplayConnectionString(m_connectionString));
  63. }
  64. catch (Exception e)
  65. {
  66. m_log.Debug("Exception: password not found in connection string\n" + e.ToString());
  67. }
  68. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  69. {
  70. dbcon.Open();
  71. Migration m = new Migration(dbcon, Assembly, "EstateStore");
  72. m.Update();
  73. dbcon.Close();
  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. {
  80. if (f.Name.Substring(0, 2) == "m_")
  81. m_FieldMap[f.Name.Substring(2)] = f;
  82. }
  83. }
  84. }
  85. private string[] FieldList
  86. {
  87. get { return new List<string>(m_FieldMap.Keys).ToArray(); }
  88. }
  89. public EstateSettings LoadEstateSettings(UUID regionID, bool create)
  90. {
  91. string sql = "select estate_settings." + String.Join(",estate_settings.", FieldList) +
  92. " from estate_map left join estate_settings on estate_map.EstateID = estate_settings.EstateID where estate_settings.EstateID is not null and RegionID = ?RegionID";
  93. using (MySqlCommand cmd = new MySqlCommand())
  94. {
  95. cmd.CommandText = sql;
  96. cmd.Parameters.AddWithValue("?RegionID", regionID.ToString());
  97. EstateSettings e = DoLoad(cmd, regionID, create);
  98. if (!create && e.EstateID == 0) // Not found
  99. return null;
  100. return e;
  101. }
  102. }
  103. public EstateSettings CreateNewEstate(int estateID)
  104. {
  105. EstateSettings es = new EstateSettings();
  106. es.OnSave += StoreEstateSettings;
  107. es.EstateID = Convert.ToUInt32(estateID);
  108. DoCreate(es);
  109. LoadBanList(es);
  110. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  111. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  112. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  113. return es;
  114. }
  115. private EstateSettings DoLoad(MySqlCommand cmd, UUID regionID, bool create)
  116. {
  117. EstateSettings es = new EstateSettings();
  118. es.OnSave += StoreEstateSettings;
  119. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  120. {
  121. dbcon.Open();
  122. cmd.Connection = dbcon;
  123. bool found = false;
  124. using (IDataReader r = cmd.ExecuteReader())
  125. {
  126. if (r.Read())
  127. {
  128. found = true;
  129. foreach (string name in FieldList)
  130. {
  131. if (m_FieldMap[name].FieldType == typeof(bool))
  132. {
  133. m_FieldMap[name].SetValue(es, Convert.ToInt32(r[name]) != 0);
  134. }
  135. else if (m_FieldMap[name].FieldType == typeof(UUID))
  136. {
  137. m_FieldMap[name].SetValue(es, DBGuid.FromDB(r[name]));
  138. }
  139. else
  140. {
  141. m_FieldMap[name].SetValue(es, r[name]);
  142. }
  143. }
  144. }
  145. }
  146. dbcon.Close();
  147. cmd.Connection = null;
  148. if (!found && create)
  149. {
  150. DoCreate(es);
  151. LinkRegion(regionID, (int)es.EstateID);
  152. }
  153. }
  154. LoadBanList(es);
  155. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  156. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  157. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  158. return es;
  159. }
  160. private void DoCreate(EstateSettings es)
  161. {
  162. // Migration case
  163. List<string> names = new List<string>(FieldList);
  164. // Remove EstateID and use AutoIncrement
  165. if (es.EstateID < 100)
  166. names.Remove("EstateID");
  167. string sql = "insert into estate_settings (" + String.Join(",", names.ToArray()) + ") values ( ?" + String.Join(", ?", names.ToArray()) + ")";
  168. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  169. {
  170. dbcon.Open();
  171. using (MySqlCommand cmd2 = dbcon.CreateCommand())
  172. {
  173. cmd2.CommandText = sql;
  174. cmd2.Parameters.Clear();
  175. foreach (string name in FieldList)
  176. {
  177. if (m_FieldMap[name].GetValue(es) is bool)
  178. {
  179. if ((bool)m_FieldMap[name].GetValue(es))
  180. cmd2.Parameters.AddWithValue("?" + name, "1");
  181. else
  182. cmd2.Parameters.AddWithValue("?" + name, "0");
  183. }
  184. else
  185. {
  186. cmd2.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  187. }
  188. }
  189. cmd2.ExecuteNonQuery();
  190. // Only get Auto ID if we actually used it else we just get 0
  191. if (es.EstateID < 100)
  192. {
  193. cmd2.CommandText = "select LAST_INSERT_ID() as id";
  194. cmd2.Parameters.Clear();
  195. using (IDataReader r = cmd2.ExecuteReader())
  196. {
  197. r.Read();
  198. es.EstateID = Convert.ToUInt32(r["id"]);
  199. }
  200. es.Save();
  201. }
  202. }
  203. dbcon.Close();
  204. }
  205. }
  206. public void StoreEstateSettings(EstateSettings es)
  207. {
  208. string sql = "replace into estate_settings (" + String.Join(",", FieldList) + ") values ( ?" + String.Join(", ?", FieldList) + ")";
  209. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  210. {
  211. dbcon.Open();
  212. using (MySqlCommand cmd = dbcon.CreateCommand())
  213. {
  214. cmd.CommandText = sql;
  215. foreach (string name in FieldList)
  216. {
  217. if (m_FieldMap[name].GetValue(es) is bool)
  218. {
  219. if ((bool)m_FieldMap[name].GetValue(es))
  220. cmd.Parameters.AddWithValue("?" + name, "1");
  221. else
  222. cmd.Parameters.AddWithValue("?" + name, "0");
  223. }
  224. else
  225. {
  226. cmd.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  227. }
  228. }
  229. cmd.ExecuteNonQuery();
  230. }
  231. dbcon.Close();
  232. }
  233. SaveBanList(es);
  234. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  235. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  236. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  237. }
  238. private void LoadBanList(EstateSettings es)
  239. {
  240. es.ClearBans();
  241. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  242. {
  243. dbcon.Open();
  244. using (MySqlCommand cmd = dbcon.CreateCommand())
  245. {
  246. cmd.CommandText = "select * from estateban where EstateID = ?EstateID";
  247. cmd.Parameters.AddWithValue("?EstateID", es.EstateID);
  248. using (IDataReader r = cmd.ExecuteReader())
  249. {
  250. while (r.Read())
  251. {
  252. EstateBan eb = new EstateBan();
  253. eb.BannedUserID = DBGuid.FromDB(r["bannedUUID"]);
  254. eb.BannedHostAddress = "0.0.0.0";
  255. eb.BannedHostIPMask = "0.0.0.0";
  256. eb.BanningUserID = DBGuid.FromDB(r["banningUUID"]);
  257. eb.BanTime = Convert.ToInt32(r["banTime"]);
  258. es.AddBan(eb);
  259. }
  260. }
  261. }
  262. dbcon.Close();
  263. }
  264. }
  265. private void SaveBanList(EstateSettings es)
  266. {
  267. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  268. {
  269. dbcon.Open();
  270. using (MySqlCommand cmd = dbcon.CreateCommand())
  271. {
  272. cmd.CommandText = "delete from estateban where EstateID = ?EstateID";
  273. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  274. cmd.ExecuteNonQuery();
  275. cmd.Parameters.Clear();
  276. cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask, banningUUID, banTime) values ( ?EstateID, ?bannedUUID, '', '', '', ?banningUUID, ?banTime)";
  277. foreach (EstateBan b in es.EstateBans)
  278. {
  279. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  280. cmd.Parameters.AddWithValue("?bannedUUID", b.BannedUserID.ToString());
  281. cmd.Parameters.AddWithValue("?banningUUID", b.BanningUserID.ToString());
  282. cmd.Parameters.AddWithValue("?banTime", b.BanTime);
  283. cmd.ExecuteNonQuery();
  284. cmd.Parameters.Clear();
  285. }
  286. }
  287. dbcon.Close();
  288. }
  289. }
  290. void SaveUUIDList(uint EstateID, string table, UUID[] data)
  291. {
  292. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  293. {
  294. dbcon.Open();
  295. using (MySqlCommand cmd = dbcon.CreateCommand())
  296. {
  297. cmd.CommandText = "delete from " + table + " where EstateID = ?EstateID";
  298. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  299. cmd.ExecuteNonQuery();
  300. cmd.Parameters.Clear();
  301. cmd.CommandText = "insert into " + table + " (EstateID, uuid) values ( ?EstateID, ?uuid )";
  302. foreach (UUID uuid in data)
  303. {
  304. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  305. cmd.Parameters.AddWithValue("?uuid", uuid.ToString());
  306. cmd.ExecuteNonQuery();
  307. cmd.Parameters.Clear();
  308. }
  309. }
  310. dbcon.Close();
  311. }
  312. }
  313. UUID[] LoadUUIDList(uint EstateID, string table)
  314. {
  315. List<UUID> uuids = new List<UUID>();
  316. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  317. {
  318. dbcon.Open();
  319. using (MySqlCommand cmd = dbcon.CreateCommand())
  320. {
  321. cmd.CommandText = "select uuid from " + table + " where EstateID = ?EstateID";
  322. cmd.Parameters.AddWithValue("?EstateID", EstateID);
  323. using (IDataReader r = cmd.ExecuteReader())
  324. {
  325. while (r.Read())
  326. {
  327. // EstateBan eb = new EstateBan();
  328. uuids.Add(DBGuid.FromDB(r["uuid"]));
  329. }
  330. }
  331. }
  332. dbcon.Close();
  333. }
  334. return uuids.ToArray();
  335. }
  336. public EstateSettings LoadEstateSettings(int estateID)
  337. {
  338. using (MySqlCommand cmd = new MySqlCommand())
  339. {
  340. string sql = "select estate_settings." + String.Join(",estate_settings.", FieldList) + " from estate_settings where EstateID = ?EstateID";
  341. cmd.CommandText = sql;
  342. cmd.Parameters.AddWithValue("?EstateID", estateID);
  343. EstateSettings e = DoLoad(cmd, UUID.Zero, false);
  344. if (e.EstateID != estateID)
  345. return null;
  346. return e;
  347. }
  348. }
  349. public List<EstateSettings> LoadEstateSettingsAll()
  350. {
  351. List<EstateSettings> allEstateSettings = new List<EstateSettings>();
  352. List<int> allEstateIds = GetEstatesAll();
  353. foreach (int estateId in allEstateIds)
  354. allEstateSettings.Add(LoadEstateSettings(estateId));
  355. return allEstateSettings;
  356. }
  357. public List<int> GetEstatesAll()
  358. {
  359. List<int> result = new List<int>();
  360. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  361. {
  362. dbcon.Open();
  363. using (MySqlCommand cmd = dbcon.CreateCommand())
  364. {
  365. cmd.CommandText = "select estateID from estate_settings";
  366. using (IDataReader reader = cmd.ExecuteReader())
  367. {
  368. while (reader.Read())
  369. {
  370. result.Add(Convert.ToInt32(reader["EstateID"]));
  371. }
  372. reader.Close();
  373. }
  374. }
  375. dbcon.Close();
  376. }
  377. return result;
  378. }
  379. public List<int> GetEstates(string search)
  380. {
  381. List<int> result = new List<int>();
  382. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  383. {
  384. dbcon.Open();
  385. using (MySqlCommand cmd = dbcon.CreateCommand())
  386. {
  387. cmd.CommandText = "select estateID from estate_settings where EstateName = ?EstateName";
  388. cmd.Parameters.AddWithValue("?EstateName", search);
  389. using (IDataReader reader = cmd.ExecuteReader())
  390. {
  391. while (reader.Read())
  392. {
  393. result.Add(Convert.ToInt32(reader["EstateID"]));
  394. }
  395. reader.Close();
  396. }
  397. }
  398. dbcon.Close();
  399. }
  400. return result;
  401. }
  402. public List<int> GetEstatesByOwner(UUID ownerID)
  403. {
  404. List<int> result = new List<int>();
  405. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  406. {
  407. dbcon.Open();
  408. using (MySqlCommand cmd = dbcon.CreateCommand())
  409. {
  410. cmd.CommandText = "select estateID from estate_settings where EstateOwner = ?EstateOwner";
  411. cmd.Parameters.AddWithValue("?EstateOwner", ownerID.ToString());
  412. using (IDataReader reader = cmd.ExecuteReader())
  413. {
  414. while (reader.Read())
  415. {
  416. result.Add(Convert.ToInt32(reader["EstateID"]));
  417. }
  418. reader.Close();
  419. }
  420. }
  421. dbcon.Close();
  422. }
  423. return result;
  424. }
  425. public bool LinkRegion(UUID regionID, int estateID)
  426. {
  427. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  428. {
  429. dbcon.Open();
  430. MySqlTransaction transaction = dbcon.BeginTransaction();
  431. try
  432. {
  433. // Delete any existing association of this region with an estate.
  434. using (MySqlCommand cmd = dbcon.CreateCommand())
  435. {
  436. cmd.Transaction = transaction;
  437. cmd.CommandText = "delete from estate_map where RegionID = ?RegionID";
  438. cmd.Parameters.AddWithValue("?RegionID", regionID.ToString());
  439. cmd.ExecuteNonQuery();
  440. }
  441. using (MySqlCommand cmd = dbcon.CreateCommand())
  442. {
  443. cmd.Transaction = transaction;
  444. cmd.CommandText = "insert into estate_map values (?RegionID, ?EstateID)";
  445. cmd.Parameters.AddWithValue("?RegionID", regionID.ToString());
  446. cmd.Parameters.AddWithValue("?EstateID", estateID);
  447. int ret = cmd.ExecuteNonQuery();
  448. if (ret != 0)
  449. transaction.Commit();
  450. else
  451. transaction.Rollback();
  452. dbcon.Close();
  453. return (ret != 0);
  454. }
  455. }
  456. catch (MySqlException ex)
  457. {
  458. m_log.Error("[REGION DB]: LinkRegion failed: " + ex.Message);
  459. transaction.Rollback();
  460. }
  461. dbcon.Close();
  462. }
  463. return false;
  464. }
  465. public List<UUID> GetRegions(int estateID)
  466. {
  467. List<UUID> result = new List<UUID>();
  468. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  469. {
  470. dbcon.Open();
  471. try
  472. {
  473. using (MySqlCommand cmd = dbcon.CreateCommand())
  474. {
  475. cmd.CommandText = "select RegionID from estate_map where EstateID = ?EstateID";
  476. cmd.Parameters.AddWithValue("?EstateID", estateID.ToString());
  477. using (IDataReader reader = cmd.ExecuteReader())
  478. {
  479. while(reader.Read())
  480. result.Add(DBGuid.FromDB(reader["RegionID"]));
  481. reader.Close();
  482. }
  483. }
  484. }
  485. catch (Exception e)
  486. {
  487. m_log.Error("[REGION DB]: Error reading estate map. " + e.ToString());
  488. return result;
  489. }
  490. dbcon.Close();
  491. }
  492. return result;
  493. }
  494. public bool DeleteEstate(int estateID)
  495. {
  496. return false;
  497. }
  498. }
  499. }