MySQLEstateData.cs 15 KB

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