MySQLEstateData.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. 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()
  104. {
  105. EstateSettings es = new EstateSettings();
  106. es.OnSave += StoreEstateSettings;
  107. DoCreate(es);
  108. LoadBanList(es);
  109. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  110. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  111. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  112. return es;
  113. }
  114. private EstateSettings DoLoad(MySqlCommand cmd, UUID regionID, bool create)
  115. {
  116. EstateSettings es = new EstateSettings();
  117. es.OnSave += StoreEstateSettings;
  118. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  119. {
  120. dbcon.Open();
  121. cmd.Connection = dbcon;
  122. bool found = false;
  123. using (IDataReader r = cmd.ExecuteReader())
  124. {
  125. if (r.Read())
  126. {
  127. found = true;
  128. foreach (string name in FieldList)
  129. {
  130. if (m_FieldMap[name].FieldType == typeof(bool))
  131. {
  132. m_FieldMap[name].SetValue(es, Convert.ToInt32(r[name]) != 0);
  133. }
  134. else if (m_FieldMap[name].FieldType == typeof(UUID))
  135. {
  136. m_FieldMap[name].SetValue(es, DBGuid.FromDB(r[name]));
  137. }
  138. else
  139. {
  140. m_FieldMap[name].SetValue(es, r[name]);
  141. }
  142. }
  143. }
  144. }
  145. if (!found && create)
  146. {
  147. DoCreate(es);
  148. LinkRegion(regionID, (int)es.EstateID);
  149. }
  150. }
  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. // Migration case
  160. List<string> names = new List<string>(FieldList);
  161. names.Remove("EstateID");
  162. string sql = "insert into estate_settings (" + String.Join(",", names.ToArray()) + ") values ( ?" + String.Join(", ?", names.ToArray()) + ")";
  163. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  164. {
  165. dbcon.Open();
  166. using (MySqlCommand cmd2 = dbcon.CreateCommand())
  167. {
  168. cmd2.CommandText = sql;
  169. cmd2.Parameters.Clear();
  170. foreach (string name in FieldList)
  171. {
  172. if (m_FieldMap[name].GetValue(es) is bool)
  173. {
  174. if ((bool)m_FieldMap[name].GetValue(es))
  175. cmd2.Parameters.AddWithValue("?" + name, "1");
  176. else
  177. cmd2.Parameters.AddWithValue("?" + name, "0");
  178. }
  179. else
  180. {
  181. cmd2.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  182. }
  183. }
  184. cmd2.ExecuteNonQuery();
  185. cmd2.CommandText = "select LAST_INSERT_ID() as id";
  186. cmd2.Parameters.Clear();
  187. using (IDataReader r = cmd2.ExecuteReader())
  188. {
  189. r.Read();
  190. es.EstateID = Convert.ToUInt32(r["id"]);
  191. }
  192. es.Save();
  193. }
  194. }
  195. }
  196. public void StoreEstateSettings(EstateSettings es)
  197. {
  198. string sql = "replace into estate_settings (" + String.Join(",", FieldList) + ") values ( ?" + String.Join(", ?", FieldList) + ")";
  199. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  200. {
  201. dbcon.Open();
  202. using (MySqlCommand cmd = dbcon.CreateCommand())
  203. {
  204. cmd.CommandText = sql;
  205. foreach (string name in FieldList)
  206. {
  207. if (m_FieldMap[name].GetValue(es) is bool)
  208. {
  209. if ((bool)m_FieldMap[name].GetValue(es))
  210. cmd.Parameters.AddWithValue("?" + name, "1");
  211. else
  212. cmd.Parameters.AddWithValue("?" + name, "0");
  213. }
  214. else
  215. {
  216. cmd.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  217. }
  218. }
  219. cmd.ExecuteNonQuery();
  220. }
  221. }
  222. SaveBanList(es);
  223. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  224. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  225. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  226. }
  227. private void LoadBanList(EstateSettings es)
  228. {
  229. es.ClearBans();
  230. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  231. {
  232. dbcon.Open();
  233. using (MySqlCommand cmd = dbcon.CreateCommand())
  234. {
  235. cmd.CommandText = "select bannedUUID from estateban where EstateID = ?EstateID";
  236. cmd.Parameters.AddWithValue("?EstateID", es.EstateID);
  237. using (IDataReader r = cmd.ExecuteReader())
  238. {
  239. while (r.Read())
  240. {
  241. EstateBan eb = new EstateBan();
  242. UUID uuid = new UUID();
  243. UUID.TryParse(r["bannedUUID"].ToString(), out uuid);
  244. eb.BannedUserID = uuid;
  245. eb.BannedHostAddress = "0.0.0.0";
  246. eb.BannedHostIPMask = "0.0.0.0";
  247. es.AddBan(eb);
  248. }
  249. }
  250. }
  251. }
  252. }
  253. private void SaveBanList(EstateSettings es)
  254. {
  255. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  256. {
  257. dbcon.Open();
  258. using (MySqlCommand cmd = dbcon.CreateCommand())
  259. {
  260. cmd.CommandText = "delete from estateban where EstateID = ?EstateID";
  261. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  262. cmd.ExecuteNonQuery();
  263. cmd.Parameters.Clear();
  264. cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( ?EstateID, ?bannedUUID, '', '', '' )";
  265. foreach (EstateBan b in es.EstateBans)
  266. {
  267. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  268. cmd.Parameters.AddWithValue("?bannedUUID", b.BannedUserID.ToString());
  269. cmd.ExecuteNonQuery();
  270. cmd.Parameters.Clear();
  271. }
  272. }
  273. }
  274. }
  275. void SaveUUIDList(uint EstateID, string table, UUID[] data)
  276. {
  277. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  278. {
  279. dbcon.Open();
  280. using (MySqlCommand cmd = dbcon.CreateCommand())
  281. {
  282. cmd.CommandText = "delete from " + table + " where EstateID = ?EstateID";
  283. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  284. cmd.ExecuteNonQuery();
  285. cmd.Parameters.Clear();
  286. cmd.CommandText = "insert into " + table + " (EstateID, uuid) values ( ?EstateID, ?uuid )";
  287. foreach (UUID uuid in data)
  288. {
  289. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  290. cmd.Parameters.AddWithValue("?uuid", uuid.ToString());
  291. cmd.ExecuteNonQuery();
  292. cmd.Parameters.Clear();
  293. }
  294. }
  295. }
  296. }
  297. UUID[] LoadUUIDList(uint EstateID, string table)
  298. {
  299. List<UUID> uuids = new List<UUID>();
  300. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  301. {
  302. dbcon.Open();
  303. using (MySqlCommand cmd = dbcon.CreateCommand())
  304. {
  305. cmd.CommandText = "select uuid from " + table + " where EstateID = ?EstateID";
  306. cmd.Parameters.AddWithValue("?EstateID", EstateID);
  307. using (IDataReader r = cmd.ExecuteReader())
  308. {
  309. while (r.Read())
  310. {
  311. // EstateBan eb = new EstateBan();
  312. uuids.Add(DBGuid.FromDB(r["uuid"]));
  313. }
  314. }
  315. }
  316. }
  317. return uuids.ToArray();
  318. }
  319. public EstateSettings LoadEstateSettings(int estateID)
  320. {
  321. using (MySqlCommand cmd = new MySqlCommand())
  322. {
  323. string sql = "select estate_settings." + String.Join(",estate_settings.", FieldList) + " from estate_settings where EstateID = ?EstateID";
  324. cmd.CommandText = sql;
  325. cmd.Parameters.AddWithValue("?EstateID", estateID);
  326. EstateSettings e = DoLoad(cmd, UUID.Zero, false);
  327. if (e.EstateID != estateID)
  328. return null;
  329. return e;
  330. }
  331. }
  332. public List<EstateSettings> LoadEstateSettingsAll()
  333. {
  334. List<EstateSettings> allEstateSettings = new List<EstateSettings>();
  335. List<int> allEstateIds = GetEstatesAll();
  336. foreach (int estateId in allEstateIds)
  337. allEstateSettings.Add(LoadEstateSettings(estateId));
  338. return allEstateSettings;
  339. }
  340. public List<int> GetEstatesAll()
  341. {
  342. List<int> result = new List<int>();
  343. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  344. {
  345. dbcon.Open();
  346. using (MySqlCommand cmd = dbcon.CreateCommand())
  347. {
  348. cmd.CommandText = "select estateID from estate_settings";
  349. using (IDataReader reader = cmd.ExecuteReader())
  350. {
  351. while (reader.Read())
  352. {
  353. result.Add(Convert.ToInt32(reader["EstateID"]));
  354. }
  355. reader.Close();
  356. }
  357. }
  358. dbcon.Close();
  359. }
  360. return result;
  361. }
  362. public List<int> GetEstates(string search)
  363. {
  364. List<int> result = new List<int>();
  365. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  366. {
  367. dbcon.Open();
  368. using (MySqlCommand cmd = dbcon.CreateCommand())
  369. {
  370. cmd.CommandText = "select estateID from estate_settings where EstateName = ?EstateName";
  371. cmd.Parameters.AddWithValue("?EstateName", search);
  372. using (IDataReader reader = cmd.ExecuteReader())
  373. {
  374. while (reader.Read())
  375. {
  376. result.Add(Convert.ToInt32(reader["EstateID"]));
  377. }
  378. reader.Close();
  379. }
  380. }
  381. dbcon.Close();
  382. }
  383. return result;
  384. }
  385. public List<int> GetEstatesByOwner(UUID ownerID)
  386. {
  387. List<int> result = new List<int>();
  388. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  389. {
  390. dbcon.Open();
  391. using (MySqlCommand cmd = dbcon.CreateCommand())
  392. {
  393. cmd.CommandText = "select estateID from estate_settings where EstateOwner = ?EstateOwner";
  394. cmd.Parameters.AddWithValue("?EstateOwner", ownerID);
  395. using (IDataReader reader = cmd.ExecuteReader())
  396. {
  397. while (reader.Read())
  398. {
  399. result.Add(Convert.ToInt32(reader["EstateID"]));
  400. }
  401. reader.Close();
  402. }
  403. }
  404. dbcon.Close();
  405. }
  406. return result;
  407. }
  408. public bool LinkRegion(UUID regionID, int estateID)
  409. {
  410. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  411. {
  412. dbcon.Open();
  413. MySqlTransaction transaction = dbcon.BeginTransaction();
  414. try
  415. {
  416. // Delete any existing association of this region with an estate.
  417. using (MySqlCommand cmd = dbcon.CreateCommand())
  418. {
  419. cmd.Transaction = transaction;
  420. cmd.CommandText = "delete from estate_map where RegionID = ?RegionID";
  421. cmd.Parameters.AddWithValue("?RegionID", regionID);
  422. cmd.ExecuteNonQuery();
  423. }
  424. using (MySqlCommand cmd = dbcon.CreateCommand())
  425. {
  426. cmd.Transaction = transaction;
  427. cmd.CommandText = "insert into estate_map values (?RegionID, ?EstateID)";
  428. cmd.Parameters.AddWithValue("?RegionID", regionID);
  429. cmd.Parameters.AddWithValue("?EstateID", estateID);
  430. int ret = cmd.ExecuteNonQuery();
  431. if (ret != 0)
  432. transaction.Commit();
  433. else
  434. transaction.Rollback();
  435. dbcon.Close();
  436. return (ret != 0);
  437. }
  438. }
  439. catch (MySqlException ex)
  440. {
  441. m_log.Error("[REGION DB]: LinkRegion failed: " + ex.Message);
  442. transaction.Rollback();
  443. }
  444. dbcon.Close();
  445. }
  446. return false;
  447. }
  448. public List<UUID> GetRegions(int estateID)
  449. {
  450. List<UUID> result = new List<UUID>();
  451. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  452. {
  453. dbcon.Open();
  454. try
  455. {
  456. using (MySqlCommand cmd = dbcon.CreateCommand())
  457. {
  458. cmd.CommandText = "select RegionID from estate_map where EstateID = ?EstateID";
  459. cmd.Parameters.AddWithValue("?EstateID", estateID.ToString());
  460. using (IDataReader reader = cmd.ExecuteReader())
  461. {
  462. while(reader.Read())
  463. result.Add(DBGuid.FromDB(reader["RegionID"]));
  464. reader.Close();
  465. }
  466. }
  467. }
  468. catch (Exception e)
  469. {
  470. m_log.Error("[REGION DB]: Error reading estate map. " + e.ToString());
  471. return result;
  472. }
  473. dbcon.Close();
  474. }
  475. return result;
  476. }
  477. public bool DeleteEstate(int estateID)
  478. {
  479. return false;
  480. }
  481. }
  482. }