MySQLEstateData.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. MySqlCommand cmd = new MySqlCommand(m_waitTimeoutSelect,
  85. m_connection);
  86. using (MySqlDataReader dbReader =
  87. cmd.ExecuteReader(CommandBehavior.SingleRow))
  88. {
  89. if (dbReader.Read())
  90. {
  91. m_waitTimeout
  92. = Convert.ToInt32(dbReader["@@wait_timeout"]) *
  93. TimeSpan.TicksPerSecond + m_waitTimeoutLeeway;
  94. }
  95. dbReader.Close();
  96. cmd.Dispose();
  97. }
  98. m_lastConnectionUse = DateTime.Now.Ticks;
  99. m_log.DebugFormat(
  100. "[REGION DB]: Connection wait timeout {0} seconds",
  101. m_waitTimeout / TimeSpan.TicksPerSecond);
  102. }
  103. protected void CheckConnection()
  104. {
  105. long timeNow = DateTime.Now.Ticks;
  106. if (timeNow - m_lastConnectionUse > m_waitTimeout ||
  107. m_connection.State != ConnectionState.Open)
  108. {
  109. m_log.DebugFormat("[REGION DB]: Database connection has gone away - reconnecting");
  110. lock (m_connection)
  111. {
  112. m_connection.Close();
  113. m_connection = new MySqlConnection(m_connectionString);
  114. m_connection.Open();
  115. }
  116. }
  117. m_lastConnectionUse = timeNow;
  118. }
  119. public EstateSettings LoadEstateSettings(UUID regionID)
  120. {
  121. EstateSettings es = new EstateSettings();
  122. es.OnSave += StoreEstateSettings;
  123. 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";
  124. CheckConnection();
  125. MySqlCommand cmd = m_connection.CreateCommand();
  126. cmd.CommandText = sql;
  127. cmd.Parameters.AddWithValue("?RegionID", regionID.ToString());
  128. IDataReader r = cmd.ExecuteReader();
  129. if (r.Read())
  130. {
  131. foreach (string name in FieldList)
  132. {
  133. if (m_FieldMap[name].GetValue(es) is bool)
  134. {
  135. int v = Convert.ToInt32(r[name]);
  136. if (v != 0)
  137. m_FieldMap[name].SetValue(es, true);
  138. else
  139. m_FieldMap[name].SetValue(es, false);
  140. }
  141. else if (m_FieldMap[name].GetValue(es) is UUID)
  142. {
  143. UUID uuid = UUID.Zero;
  144. UUID.TryParse(r[name].ToString(), out uuid);
  145. m_FieldMap[name].SetValue(es, uuid);
  146. }
  147. else
  148. {
  149. m_FieldMap[name].SetValue(es, r[name]);
  150. }
  151. }
  152. r.Close();
  153. }
  154. else
  155. {
  156. // Migration case
  157. //
  158. r.Close();
  159. List<string> names = new List<string>(FieldList);
  160. names.Remove("EstateID");
  161. sql = "insert into estate_settings (" + String.Join(",", names.ToArray()) + ") values ( ?" + String.Join(", ?", names.ToArray()) + ")";
  162. cmd.CommandText = sql;
  163. cmd.Parameters.Clear();
  164. foreach (string name in FieldList)
  165. {
  166. if (m_FieldMap[name].GetValue(es) is bool)
  167. {
  168. if ((bool)m_FieldMap[name].GetValue(es))
  169. cmd.Parameters.AddWithValue("?" + name, "1");
  170. else
  171. cmd.Parameters.AddWithValue("?" + name, "0");
  172. }
  173. else
  174. {
  175. cmd.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  176. }
  177. }
  178. cmd.ExecuteNonQuery();
  179. cmd.CommandText = "select LAST_INSERT_ID() as id";
  180. cmd.Parameters.Clear();
  181. r = cmd.ExecuteReader();
  182. r.Read();
  183. es.EstateID = Convert.ToUInt32(r["id"]);
  184. r.Close();
  185. cmd.CommandText = "insert into estate_map values (?RegionID, ?EstateID)";
  186. cmd.Parameters.AddWithValue("?RegionID", regionID.ToString());
  187. cmd.Parameters.AddWithValue("?EstateID", es.EstateID.ToString());
  188. // This will throw on dupe key
  189. try
  190. {
  191. cmd.ExecuteNonQuery();
  192. }
  193. catch (Exception)
  194. {
  195. }
  196. // Munge and transfer the ban list
  197. //
  198. cmd.Parameters.Clear();
  199. cmd.CommandText = "insert into estateban select " + es.EstateID.ToString() + ", bannedUUID, bannedIp, bannedIpHostMask, '' from regionban where regionban.regionUUID = ?UUID";
  200. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  201. try
  202. {
  203. cmd.ExecuteNonQuery();
  204. }
  205. catch (Exception)
  206. {
  207. }
  208. es.Save();
  209. }
  210. LoadBanList(es);
  211. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  212. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  213. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  214. return es;
  215. }
  216. public void StoreEstateSettings(EstateSettings es)
  217. {
  218. string sql = "replace into estate_settings (" + String.Join(",", FieldList) + ") values ( ?" + String.Join(", ?", FieldList) + ")";
  219. CheckConnection();
  220. MySqlCommand cmd = m_connection.CreateCommand();
  221. cmd.CommandText = sql;
  222. foreach (string name in FieldList)
  223. {
  224. if (m_FieldMap[name].GetValue(es) is bool)
  225. {
  226. if ((bool)m_FieldMap[name].GetValue(es))
  227. cmd.Parameters.AddWithValue("?" + name, "1");
  228. else
  229. cmd.Parameters.AddWithValue("?" + name, "0");
  230. }
  231. else
  232. {
  233. cmd.Parameters.AddWithValue("?" + name, m_FieldMap[name].GetValue(es).ToString());
  234. }
  235. }
  236. cmd.ExecuteNonQuery();
  237. SaveBanList(es);
  238. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  239. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  240. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  241. }
  242. private void LoadBanList(EstateSettings es)
  243. {
  244. es.ClearBans();
  245. CheckConnection();
  246. MySqlCommand cmd = m_connection.CreateCommand();
  247. cmd.CommandText = "select bannedUUID from estateban where EstateID = ?EstateID";
  248. cmd.Parameters.AddWithValue("?EstateID", es.EstateID);
  249. IDataReader r = cmd.ExecuteReader();
  250. while (r.Read())
  251. {
  252. EstateBan eb = new EstateBan();
  253. UUID uuid = new UUID();
  254. UUID.TryParse(r["bannedUUID"].ToString(), out uuid);
  255. eb.BannedUserID = uuid;
  256. eb.BannedHostAddress = "0.0.0.0";
  257. eb.BannedHostIPMask = "0.0.0.0";
  258. es.AddBan(eb);
  259. }
  260. r.Close();
  261. }
  262. private void SaveBanList(EstateSettings es)
  263. {
  264. CheckConnection();
  265. MySqlCommand cmd = m_connection.CreateCommand();
  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. void SaveUUIDList(uint EstateID, string table, UUID[] data)
  280. {
  281. CheckConnection();
  282. MySqlCommand cmd = m_connection.CreateCommand();
  283. cmd.CommandText = "delete from " + table + " where EstateID = ?EstateID";
  284. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  285. cmd.ExecuteNonQuery();
  286. cmd.Parameters.Clear();
  287. cmd.CommandText = "insert into " + table + " (EstateID, uuid) values ( ?EstateID, ?uuid )";
  288. foreach (UUID uuid in data)
  289. {
  290. cmd.Parameters.AddWithValue("?EstateID", EstateID.ToString());
  291. cmd.Parameters.AddWithValue("?uuid", uuid.ToString());
  292. cmd.ExecuteNonQuery();
  293. cmd.Parameters.Clear();
  294. }
  295. }
  296. UUID[] LoadUUIDList(uint EstateID, string table)
  297. {
  298. List<UUID> uuids = new List<UUID>();
  299. CheckConnection();
  300. MySqlCommand cmd = m_connection.CreateCommand();
  301. cmd.CommandText = "select uuid from " + table + " where EstateID = ?EstateID";
  302. cmd.Parameters.AddWithValue("?EstateID", EstateID);
  303. IDataReader r = cmd.ExecuteReader();
  304. while (r.Read())
  305. {
  306. // EstateBan eb = new EstateBan();
  307. UUID uuid = new UUID();
  308. UUID.TryParse(r["uuid"].ToString(), out uuid);
  309. uuids.Add(uuid);
  310. }
  311. r.Close();
  312. return uuids.ToArray();
  313. }
  314. }
  315. }