1
0

MSSQLEstateData.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.Data.SqlClient;
  31. using System.Reflection;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. namespace OpenSim.Data.MSSQL
  37. {
  38. public class MSSQLEstateStore : IEstateDataStore
  39. {
  40. private const string _migrationStore = "EstateStore";
  41. private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private MSSQLManager _Database;
  43. private string m_connectionString;
  44. private FieldInfo[] _Fields;
  45. private Dictionary<string, FieldInfo> _FieldMap = new Dictionary<string, FieldInfo>();
  46. #region Public methods
  47. /// <summary>
  48. /// Initialises the estatedata class.
  49. /// </summary>
  50. /// <param name="connectionString">connectionString.</param>
  51. public void Initialise(string connectionString)
  52. {
  53. if (!string.IsNullOrEmpty(connectionString))
  54. {
  55. m_connectionString = connectionString;
  56. _Database = new MSSQLManager(connectionString);
  57. }
  58. //Migration settings
  59. _Database.CheckMigration(_migrationStore);
  60. //Interesting way to get parameters! Maybe implement that also with other types
  61. Type t = typeof(EstateSettings);
  62. _Fields = t.GetFields(BindingFlags.NonPublic |
  63. BindingFlags.Instance |
  64. BindingFlags.DeclaredOnly);
  65. foreach (FieldInfo f in _Fields)
  66. {
  67. if (f.Name.Substring(0, 2) == "m_")
  68. _FieldMap[f.Name.Substring(2)] = f;
  69. }
  70. }
  71. /// <summary>
  72. /// Loads the estate settings.
  73. /// </summary>
  74. /// <param name="regionID">region ID.</param>
  75. /// <returns></returns>
  76. public EstateSettings LoadEstateSettings(UUID regionID, bool create)
  77. {
  78. EstateSettings es = new EstateSettings();
  79. 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";
  80. bool insertEstate = false;
  81. using (SqlConnection conn = new SqlConnection(m_connectionString))
  82. using (SqlCommand cmd = new SqlCommand(sql, conn))
  83. {
  84. cmd.Parameters.Add(_Database.CreateParameter("@RegionID", regionID));
  85. conn.Open();
  86. using (SqlDataReader reader = cmd.ExecuteReader())
  87. {
  88. if (reader.Read())
  89. {
  90. foreach (string name in FieldList)
  91. {
  92. FieldInfo f = _FieldMap[name];
  93. object v = reader[name];
  94. if (f.FieldType == typeof(bool) )
  95. {
  96. f.SetValue(es, Convert.ToInt32(v) != 0);
  97. }
  98. else if (f.FieldType == typeof(UUID) )
  99. {
  100. f.SetValue(es, new UUID((Guid)v)); // uuid);
  101. }
  102. else if (f.FieldType == typeof(string))
  103. {
  104. f.SetValue(es, v.ToString());
  105. }
  106. else if (f.FieldType == typeof(UInt32))
  107. {
  108. f.SetValue(es, Convert.ToUInt32(v));
  109. }
  110. else if (f.FieldType == typeof(Single))
  111. {
  112. f.SetValue(es, Convert.ToSingle(v));
  113. }
  114. else
  115. f.SetValue(es, v);
  116. }
  117. }
  118. else
  119. {
  120. insertEstate = true;
  121. }
  122. }
  123. }
  124. if (insertEstate && create)
  125. {
  126. List<string> names = new List<string>(FieldList);
  127. names.Remove("EstateID");
  128. sql = string.Format("insert into estate_settings ({0}) values ( @{1})", String.Join(",", names.ToArray()), String.Join(", @", names.ToArray()));
  129. //_Log.Debug("[DB ESTATE]: SQL: " + sql);
  130. using (SqlConnection conn = new SqlConnection(m_connectionString))
  131. using (SqlCommand insertCommand = new SqlCommand(sql, conn))
  132. {
  133. insertCommand.CommandText = sql + " SET @ID = SCOPE_IDENTITY()";
  134. foreach (string name in names)
  135. {
  136. insertCommand.Parameters.Add(_Database.CreateParameter("@" + name, _FieldMap[name].GetValue(es)));
  137. }
  138. SqlParameter idParameter = new SqlParameter("@ID", SqlDbType.Int);
  139. idParameter.Direction = ParameterDirection.Output;
  140. insertCommand.Parameters.Add(idParameter);
  141. conn.Open();
  142. insertCommand.ExecuteNonQuery();
  143. es.EstateID = Convert.ToUInt32(idParameter.Value);
  144. }
  145. sql = "INSERT INTO [estate_map] ([RegionID] ,[EstateID]) VALUES (@RegionID, @EstateID)";
  146. using (SqlConnection conn = new SqlConnection(m_connectionString))
  147. using (SqlCommand cmd = new SqlCommand(sql, conn))
  148. {
  149. cmd.Parameters.Add(_Database.CreateParameter("@RegionID", regionID));
  150. cmd.Parameters.Add(_Database.CreateParameter("@EstateID", es.EstateID));
  151. // This will throw on dupe key
  152. try
  153. {
  154. conn.Open();
  155. cmd.ExecuteNonQuery();
  156. }
  157. catch (Exception e)
  158. {
  159. _Log.DebugFormat("[ESTATE DB]: Error inserting regionID and EstateID in estate_map: {0}", e);
  160. }
  161. }
  162. //TODO check if this is needed??
  163. es.Save();
  164. }
  165. LoadBanList(es);
  166. es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
  167. es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
  168. es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
  169. //Set event
  170. es.OnSave += StoreEstateSettings;
  171. return es;
  172. }
  173. /// <summary>
  174. /// Stores the estate settings.
  175. /// </summary>
  176. /// <param name="es">estate settings</param>
  177. public void StoreEstateSettings(EstateSettings es)
  178. {
  179. List<string> names = new List<string>(FieldList);
  180. names.Remove("EstateID");
  181. string sql = string.Format("UPDATE estate_settings SET ");
  182. foreach (string name in names)
  183. {
  184. sql += name + " = @" + name + ", ";
  185. }
  186. sql = sql.Remove(sql.LastIndexOf(","));
  187. sql += " WHERE EstateID = @EstateID";
  188. using (SqlConnection conn = new SqlConnection(m_connectionString))
  189. using (SqlCommand cmd = new SqlCommand(sql, conn))
  190. {
  191. foreach (string name in names)
  192. {
  193. cmd.Parameters.Add(_Database.CreateParameter("@" + name, _FieldMap[name].GetValue(es)));
  194. }
  195. cmd.Parameters.Add(_Database.CreateParameter("@EstateID", es.EstateID));
  196. conn.Open();
  197. cmd.ExecuteNonQuery();
  198. }
  199. SaveBanList(es);
  200. SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
  201. SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
  202. SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
  203. }
  204. #endregion
  205. #region Private methods
  206. private string[] FieldList
  207. {
  208. get { return new List<string>(_FieldMap.Keys).ToArray(); }
  209. }
  210. private void LoadBanList(EstateSettings es)
  211. {
  212. es.ClearBans();
  213. string sql = "select bannedUUID from estateban where EstateID = @EstateID";
  214. using (SqlConnection conn = new SqlConnection(m_connectionString))
  215. using (SqlCommand cmd = new SqlCommand(sql, conn))
  216. {
  217. SqlParameter idParameter = new SqlParameter("@EstateID", SqlDbType.Int);
  218. idParameter.Value = es.EstateID;
  219. cmd.Parameters.Add(idParameter);
  220. conn.Open();
  221. using (SqlDataReader reader = cmd.ExecuteReader())
  222. {
  223. while (reader.Read())
  224. {
  225. EstateBan eb = new EstateBan();
  226. eb.BannedUserID = new UUID((Guid)reader["bannedUUID"]); //uuid;
  227. eb.BannedHostAddress = "0.0.0.0";
  228. eb.BannedHostIPMask = "0.0.0.0";
  229. es.AddBan(eb);
  230. }
  231. }
  232. }
  233. }
  234. private UUID[] LoadUUIDList(uint estateID, string table)
  235. {
  236. List<UUID> uuids = new List<UUID>();
  237. string sql = string.Format("select uuid from {0} where EstateID = @EstateID", table);
  238. using (SqlConnection conn = new SqlConnection(m_connectionString))
  239. using (SqlCommand cmd = new SqlCommand(sql, conn))
  240. {
  241. cmd.Parameters.Add(_Database.CreateParameter("@EstateID", estateID));
  242. conn.Open();
  243. using (SqlDataReader reader = cmd.ExecuteReader())
  244. {
  245. while (reader.Read())
  246. {
  247. uuids.Add(new UUID((Guid)reader["uuid"])); //uuid);
  248. }
  249. }
  250. }
  251. return uuids.ToArray();
  252. }
  253. private void SaveBanList(EstateSettings es)
  254. {
  255. //Delete first
  256. using (SqlConnection conn = new SqlConnection(m_connectionString))
  257. {
  258. conn.Open();
  259. using (SqlCommand cmd = conn.CreateCommand())
  260. {
  261. cmd.CommandText = "delete from estateban where EstateID = @EstateID";
  262. cmd.Parameters.AddWithValue("@EstateID", (int)es.EstateID);
  263. cmd.ExecuteNonQuery();
  264. //Insert after
  265. cmd.CommandText = "insert into estateban (EstateID, bannedUUID) values ( @EstateID, @bannedUUID )";
  266. cmd.Parameters.AddWithValue("@bannedUUID", Guid.Empty);
  267. foreach (EstateBan b in es.EstateBans)
  268. {
  269. cmd.Parameters["@bannedUUID"].Value = b.BannedUserID.Guid;
  270. cmd.ExecuteNonQuery();
  271. }
  272. }
  273. }
  274. }
  275. private void SaveUUIDList(uint estateID, string table, UUID[] data)
  276. {
  277. using (SqlConnection conn = new SqlConnection(m_connectionString))
  278. {
  279. conn.Open();
  280. using (SqlCommand cmd = conn.CreateCommand())
  281. {
  282. cmd.Parameters.AddWithValue("@EstateID", (int)estateID);
  283. cmd.CommandText = string.Format("delete from {0} where EstateID = @EstateID", table);
  284. cmd.ExecuteNonQuery();
  285. cmd.CommandText = string.Format("insert into {0} (EstateID, uuid) values ( @EstateID, @uuid )", table);
  286. cmd.Parameters.AddWithValue("@uuid", Guid.Empty);
  287. foreach (UUID uuid in data)
  288. {
  289. cmd.Parameters["@uuid"].Value = uuid.Guid; //.ToString(); //TODO check if this works
  290. cmd.ExecuteNonQuery();
  291. }
  292. }
  293. }
  294. }
  295. public EstateSettings LoadEstateSettings(int estateID)
  296. {
  297. return new EstateSettings();
  298. }
  299. public List<int> GetEstates(string search)
  300. {
  301. return new List<int>();
  302. }
  303. public bool LinkRegion(UUID regionID, int estateID)
  304. {
  305. return false;
  306. }
  307. public List<UUID> GetRegions(int estateID)
  308. {
  309. return new List<UUID>();
  310. }
  311. public bool DeleteEstate(int estateID)
  312. {
  313. return false;
  314. }
  315. #endregion
  316. }
  317. }