MySQLEstateData.cs 22 KB

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