MySQLEstateData.cs 22 KB

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