1
0

MySQLEstateData.cs 15 KB

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