MySQLEstateData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. namespace OpenSim.Data.MySQL
  37. {
  38. public class MySQLEstateStore : IEstateDataStore
  39. {
  40. private static readonly ILog m_log =
  41. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private const string m_waitTimeoutSelect = "select @@wait_timeout";
  43. private MySqlConnection m_connection;
  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. public void Initialise(string connectionString)
  52. {
  53. m_connectionString = connectionString;
  54. try
  55. {
  56. m_log.Info("[REGION DB]: MySql - connecting: " + Util.GetDisplayConnectionString(m_connectionString));
  57. }
  58. catch (Exception e)
  59. {
  60. m_log.Debug("Exception: password not found in connection string\n" + e.ToString());
  61. }
  62. m_connection = new MySqlConnection(m_connectionString);
  63. m_connection.Open();
  64. GetWaitTimeout();
  65. Assembly assem = GetType().Assembly;
  66. Migration m = new Migration(m_connection, assem, "EstateStore");
  67. m.Update();
  68. Type t = typeof(EstateSettings);
  69. m_Fields = t.GetFields(BindingFlags.NonPublic |
  70. BindingFlags.Instance |
  71. BindingFlags.DeclaredOnly);
  72. foreach (FieldInfo f in m_Fields)
  73. {
  74. if (f.Name.Substring(0, 2) == "m_")
  75. m_FieldMap[f.Name.Substring(2)] = f;
  76. }
  77. }
  78. private string[] FieldList
  79. {
  80. get { return new List<string>(m_FieldMap.Keys).ToArray(); }
  81. }
  82. protected void GetWaitTimeout()
  83. {
  84. using (MySqlCommand cmd = new MySqlCommand(m_waitTimeoutSelect, m_connection))
  85. {
  86. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  87. {
  88. if (dbReader.Read())
  89. {
  90. m_waitTimeout
  91. = Convert.ToInt32(dbReader["@@wait_timeout"]) *
  92. TimeSpan.TicksPerSecond + m_waitTimeoutLeeway;
  93. }
  94. }
  95. }
  96. m_lastConnectionUse = DateTime.Now.Ticks;
  97. m_log.DebugFormat(
  98. "[REGION DB]: Connection wait timeout {0} seconds",
  99. m_waitTimeout / TimeSpan.TicksPerSecond);
  100. }
  101. protected void CheckConnection()
  102. {
  103. long timeNow = DateTime.Now.Ticks;
  104. if (timeNow - m_lastConnectionUse > m_waitTimeout ||
  105. m_connection.State != ConnectionState.Open)
  106. {
  107. m_log.DebugFormat("[REGION DB]: Database connection has gone away - reconnecting");
  108. lock (m_connection)
  109. {
  110. m_connection.Close();
  111. m_connection = new MySqlConnection(m_connectionString);
  112. m_connection.Open();
  113. }
  114. }
  115. m_lastConnectionUse = timeNow;
  116. }
  117. public EstateSettings LoadEstateSettings(UUID regionID)
  118. {
  119. EstateSettings es = new EstateSettings();
  120. es.OnSave += StoreEstateSettings;
  121. 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";
  122. CheckConnection();
  123. bool migration = true;
  124. using (MySqlCommand cmd = m_connection.CreateCommand())
  125. {
  126. cmd.CommandText = sql;
  127. cmd.Parameters.AddWithValue("?RegionID", regionID.ToString());
  128. using (IDataReader r = cmd.ExecuteReader())
  129. {
  130. if (r.Read())
  131. {
  132. migration = false;
  133. foreach (string name in FieldList)
  134. {
  135. if (m_FieldMap[name].GetValue(es) is bool)
  136. {
  137. int v = Convert.ToInt32(r[name]);
  138. if (v != 0)
  139. m_FieldMap[name].SetValue(es, true);
  140. else
  141. m_FieldMap[name].SetValue(es, false);
  142. }
  143. else if (m_FieldMap[name].GetValue(es) is UUID)
  144. {
  145. UUID uuid = UUID.Zero;
  146. UUID.TryParse(r[name].ToString(), out uuid);
  147. m_FieldMap[name].SetValue(es, uuid);
  148. }
  149. else
  150. {
  151. m_FieldMap[name].SetValue(es, r[name]);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. if (migration)
  158. {
  159. // Migration case
  160. List<string> names = new List<string>(FieldList);
  161. names.Remove("EstateID");
  162. sql = "insert into estate_settings (" + String.Join(",", names.ToArray()) + ") values ( ?" + String.Join(", ?", names.ToArray()) + ")";
  163. using (MySqlCommand cmd = m_connection.CreateCommand())
  164. {
  165. cmd.CommandText = sql;
  166. cmd.Parameters.Clear();
  167. foreach (string name in FieldList)
  168. {
  169. if (m_FieldMap[name].GetValue(es) is bool)
  170. {
  171. if ((bool)m_FieldMap[name].GetValue(es))
  172. cmd.Parameters.AddWithValue("?" + name, "1");
  173. else
  174. cmd.Parameters.AddWithValue("?" + name, "0");
  175. }
  176. else
  177. {
  178. cmd.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  179. }
  180. }
  181. cmd.ExecuteNonQuery();
  182. cmd.CommandText = "select LAST_INSERT_ID() as id";
  183. cmd.Parameters.Clear();
  184. using (IDataReader r = cmd.ExecuteReader())
  185. {
  186. r.Read();
  187. es.EstateID = Convert.ToUInt32(r["id"]);
  188. }
  189. cmd.CommandText = "insert into estate_map values (?RegionID, ?EstateID)";
  190. cmd.Parameters.AddWithValue("?RegionID", regionID.ToString());
  191. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  192. // This will throw on dupe key
  193. try { cmd.ExecuteNonQuery(); }
  194. catch (Exception) { }
  195. // Munge and transfer the ban list
  196. cmd.Parameters.Clear();
  197. cmd.CommandText = "insert into estateban select " + es.EstateID.ToString() + ", bannedUUID, bannedIp, bannedIpHostMask, '' from regionban where regionban.regionUUID = ?UUID";
  198. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  199. try { cmd.ExecuteNonQuery(); }
  200. catch (Exception) { }
  201. es.Save();
  202. }
  203. }
  204. LoadBanList(es);
  205. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  206. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  207. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  208. return es;
  209. }
  210. public void StoreEstateSettings(EstateSettings es)
  211. {
  212. string sql = "replace into estate_settings (" + String.Join(",", FieldList) + ") values ( ?" + String.Join(", ?", FieldList) + ")";
  213. CheckConnection();
  214. using (MySqlCommand cmd = m_connection.CreateCommand())
  215. {
  216. cmd.CommandText = sql;
  217. foreach (string name in FieldList)
  218. {
  219. if (m_FieldMap[name].GetValue(es) is bool)
  220. {
  221. if ((bool)m_FieldMap[name].GetValue(es))
  222. cmd.Parameters.AddWithValue("?" + name, "1");
  223. else
  224. cmd.Parameters.AddWithValue("?" + name, "0");
  225. }
  226. else
  227. {
  228. cmd.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  229. }
  230. }
  231. cmd.ExecuteNonQuery();
  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. CheckConnection();
  242. using (MySqlCommand cmd = m_connection.CreateCommand())
  243. {
  244. cmd.CommandText = "select bannedUUID from estateban where EstateID = ?EstateID";
  245. cmd.Parameters.AddWithValue("?EstateID", es.EstateID);
  246. using (IDataReader r = cmd.ExecuteReader())
  247. {
  248. while (r.Read())
  249. {
  250. EstateBan eb = new EstateBan();
  251. UUID uuid = new UUID();
  252. UUID.TryParse(r["bannedUUID"].ToString(), out uuid);
  253. eb.BannedUserID = uuid;
  254. eb.BannedHostAddress = "0.0.0.0";
  255. eb.BannedHostIPMask = "0.0.0.0";
  256. es.AddBan(eb);
  257. }
  258. }
  259. }
  260. }
  261. private void SaveBanList(EstateSettings es)
  262. {
  263. CheckConnection();
  264. using (MySqlCommand cmd = m_connection.CreateCommand())
  265. {
  266. cmd.CommandText = "delete from estateban where EstateID = ?EstateID";
  267. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  268. cmd.ExecuteNonQuery();
  269. cmd.Parameters.Clear();
  270. cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( ?EstateID, ?bannedUUID, '', '', '' )";
  271. foreach (EstateBan b in es.EstateBans)
  272. {
  273. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  274. cmd.Parameters.AddWithValue("?bannedUUID", b.BannedUserID.ToString());
  275. cmd.ExecuteNonQuery();
  276. cmd.Parameters.Clear();
  277. }
  278. }
  279. }
  280. void SaveUUIDList(uint EstateID, string table, UUID[] data)
  281. {
  282. CheckConnection();
  283. using (MySqlCommand cmd = m_connection.CreateCommand())
  284. {
  285. cmd.CommandText = "delete from " + table + " where EstateID = ?EstateID";
  286. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  287. cmd.ExecuteNonQuery();
  288. cmd.Parameters.Clear();
  289. cmd.CommandText = "insert into " + table + " (EstateID, uuid) values ( ?EstateID, ?uuid )";
  290. foreach (UUID uuid in data)
  291. {
  292. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  293. cmd.Parameters.AddWithValue("?uuid", uuid.ToString());
  294. cmd.ExecuteNonQuery();
  295. cmd.Parameters.Clear();
  296. }
  297. }
  298. }
  299. UUID[] LoadUUIDList(uint EstateID, string table)
  300. {
  301. List<UUID> uuids = new List<UUID>();
  302. CheckConnection();
  303. using (MySqlCommand cmd = m_connection.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. UUID uuid = new UUID();
  313. UUID.TryParse(r["uuid"].ToString(), out uuid);
  314. uuids.Add(uuid);
  315. }
  316. }
  317. }
  318. return uuids.ToArray();
  319. }
  320. }
  321. }