MySQLRegionData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Reflection;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Data;
  35. using MySql.Data.MySqlClient;
  36. namespace OpenSim.Data.MySQL
  37. {
  38. public class MySqlRegionData : MySqlFramework, IRegionData
  39. {
  40. private string m_Realm;
  41. private List<string> m_ColumnNames;
  42. //private string m_connectionString;
  43. protected virtual Assembly Assembly
  44. {
  45. get { return GetType().Assembly; }
  46. }
  47. public MySqlRegionData(string connectionString, string realm)
  48. : base(connectionString)
  49. {
  50. m_Realm = realm;
  51. m_connectionString = connectionString;
  52. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  53. {
  54. dbcon.Open();
  55. Migration m = new Migration(dbcon, Assembly, "GridStore");
  56. m.Update();
  57. }
  58. }
  59. public List<RegionData> Get(string regionName, UUID scopeID)
  60. {
  61. string command = "select * from `"+m_Realm+"` where regionName like ?regionName";
  62. if (scopeID != UUID.Zero)
  63. command += " and ScopeID = ?scopeID";
  64. command += " order by regionName";
  65. using (MySqlCommand cmd = new MySqlCommand(command))
  66. {
  67. cmd.Parameters.AddWithValue("?regionName", regionName);
  68. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  69. return RunCommand(cmd);
  70. }
  71. }
  72. public RegionData Get(int posX, int posY, UUID scopeID)
  73. {
  74. string command = "select * from `"+m_Realm+"` where locX = ?posX and locY = ?posY";
  75. if (scopeID != UUID.Zero)
  76. command += " and ScopeID = ?scopeID";
  77. using (MySqlCommand cmd = new MySqlCommand(command))
  78. {
  79. cmd.Parameters.AddWithValue("?posX", posX.ToString());
  80. cmd.Parameters.AddWithValue("?posY", posY.ToString());
  81. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  82. List<RegionData> ret = RunCommand(cmd);
  83. if (ret.Count == 0)
  84. return null;
  85. return ret[0];
  86. }
  87. }
  88. public RegionData Get(UUID regionID, UUID scopeID)
  89. {
  90. string command = "select * from `"+m_Realm+"` where uuid = ?regionID";
  91. if (scopeID != UUID.Zero)
  92. command += " and ScopeID = ?scopeID";
  93. using (MySqlCommand cmd = new MySqlCommand(command))
  94. {
  95. cmd.Parameters.AddWithValue("?regionID", regionID.ToString());
  96. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  97. List<RegionData> ret = RunCommand(cmd);
  98. if (ret.Count == 0)
  99. return null;
  100. return ret[0];
  101. }
  102. }
  103. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  104. {
  105. string command = "select * from `"+m_Realm+"` where locX between ?startX and ?endX and locY between ?startY and ?endY";
  106. if (scopeID != UUID.Zero)
  107. command += " and ScopeID = ?scopeID";
  108. using (MySqlCommand cmd = new MySqlCommand(command))
  109. {
  110. cmd.Parameters.AddWithValue("?startX", startX.ToString());
  111. cmd.Parameters.AddWithValue("?startY", startY.ToString());
  112. cmd.Parameters.AddWithValue("?endX", endX.ToString());
  113. cmd.Parameters.AddWithValue("?endY", endY.ToString());
  114. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  115. return RunCommand(cmd);
  116. }
  117. }
  118. public List<RegionData> RunCommand(MySqlCommand cmd)
  119. {
  120. List<RegionData> retList = new List<RegionData>();
  121. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  122. {
  123. dbcon.Open();
  124. cmd.Connection = dbcon;
  125. using (IDataReader result = cmd.ExecuteReader())
  126. {
  127. while (result.Read())
  128. {
  129. RegionData ret = new RegionData();
  130. ret.Data = new Dictionary<string, object>();
  131. ret.RegionID = DBGuid.FromDB(result["uuid"]);
  132. ret.ScopeID = DBGuid.FromDB(result["ScopeID"]);
  133. ret.RegionName = result["regionName"].ToString();
  134. ret.posX = Convert.ToInt32(result["locX"]);
  135. ret.posY = Convert.ToInt32(result["locY"]);
  136. ret.sizeX = Convert.ToInt32(result["sizeX"]);
  137. ret.sizeY = Convert.ToInt32(result["sizeY"]);
  138. if (m_ColumnNames == null)
  139. {
  140. m_ColumnNames = new List<string>();
  141. DataTable schemaTable = result.GetSchemaTable();
  142. foreach (DataRow row in schemaTable.Rows)
  143. {
  144. if (row["ColumnName"] != null)
  145. m_ColumnNames.Add(row["ColumnName"].ToString());
  146. }
  147. }
  148. foreach (string s in m_ColumnNames)
  149. {
  150. if (s == "uuid")
  151. continue;
  152. if (s == "ScopeID")
  153. continue;
  154. if (s == "regionName")
  155. continue;
  156. if (s == "locX")
  157. continue;
  158. if (s == "locY")
  159. continue;
  160. ret.Data[s] = result[s].ToString();
  161. }
  162. retList.Add(ret);
  163. }
  164. }
  165. }
  166. return retList;
  167. }
  168. public bool Store(RegionData data)
  169. {
  170. if (data.Data.ContainsKey("uuid"))
  171. data.Data.Remove("uuid");
  172. if (data.Data.ContainsKey("ScopeID"))
  173. data.Data.Remove("ScopeID");
  174. if (data.Data.ContainsKey("regionName"))
  175. data.Data.Remove("regionName");
  176. if (data.Data.ContainsKey("posX"))
  177. data.Data.Remove("posX");
  178. if (data.Data.ContainsKey("posY"))
  179. data.Data.Remove("posY");
  180. if (data.Data.ContainsKey("sizeX"))
  181. data.Data.Remove("sizeX");
  182. if (data.Data.ContainsKey("sizeY"))
  183. data.Data.Remove("sizeY");
  184. if (data.Data.ContainsKey("locX"))
  185. data.Data.Remove("locX");
  186. if (data.Data.ContainsKey("locY"))
  187. data.Data.Remove("locY");
  188. if (data.RegionName.Length > 128)
  189. data.RegionName = data.RegionName.Substring(0, 128);
  190. string[] fields = new List<string>(data.Data.Keys).ToArray();
  191. using (MySqlCommand cmd = new MySqlCommand())
  192. {
  193. string update = "update `" + m_Realm + "` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY";
  194. foreach (string field in fields)
  195. {
  196. update += ", ";
  197. update += "`" + field + "` = ?" + field;
  198. cmd.Parameters.AddWithValue("?" + field, data.Data[field]);
  199. }
  200. update += " where uuid = ?regionID";
  201. if (data.ScopeID != UUID.Zero)
  202. update += " and ScopeID = ?scopeID";
  203. cmd.CommandText = update;
  204. cmd.Parameters.AddWithValue("?regionID", data.RegionID.ToString());
  205. cmd.Parameters.AddWithValue("?regionName", data.RegionName);
  206. cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString());
  207. cmd.Parameters.AddWithValue("?posX", data.posX.ToString());
  208. cmd.Parameters.AddWithValue("?posY", data.posY.ToString());
  209. cmd.Parameters.AddWithValue("?sizeX", data.sizeX.ToString());
  210. cmd.Parameters.AddWithValue("?sizeY", data.sizeY.ToString());
  211. if (ExecuteNonQuery(cmd) < 1)
  212. {
  213. string insert = "insert into `" + m_Realm + "` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" +
  214. String.Join("`, `", fields) +
  215. "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(", ?", fields) + ")";
  216. cmd.CommandText = insert;
  217. if (ExecuteNonQuery(cmd) < 1)
  218. {
  219. return false;
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. public bool SetDataItem(UUID regionID, string item, string value)
  226. {
  227. using (MySqlCommand cmd = new MySqlCommand("update `" + m_Realm + "` set `" + item + "` = ?" + item + " where uuid = ?UUID"))
  228. {
  229. cmd.Parameters.AddWithValue("?" + item, value);
  230. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  231. if (ExecuteNonQuery(cmd) > 0)
  232. return true;
  233. }
  234. return false;
  235. }
  236. public bool Delete(UUID regionID)
  237. {
  238. using (MySqlCommand cmd = new MySqlCommand("delete from `" + m_Realm + "` where uuid = ?UUID"))
  239. {
  240. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  241. if (ExecuteNonQuery(cmd) > 0)
  242. return true;
  243. }
  244. return false;
  245. }
  246. public List<RegionData> GetDefaultRegions(UUID scopeID)
  247. {
  248. return Get((int)RegionFlags.DefaultRegion, scopeID);
  249. }
  250. public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y)
  251. {
  252. List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID);
  253. RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y);
  254. regions.Sort(distanceComparer);
  255. return regions;
  256. }
  257. public List<RegionData> GetHyperlinks(UUID scopeID)
  258. {
  259. return Get((int)RegionFlags.Hyperlink, scopeID);
  260. }
  261. private List<RegionData> Get(int regionFlags, UUID scopeID)
  262. {
  263. string command = "select * from `" + m_Realm + "` where (flags & " + regionFlags.ToString() + ") <> 0";
  264. if (scopeID != UUID.Zero)
  265. command += " and ScopeID = ?scopeID";
  266. MySqlCommand cmd = new MySqlCommand(command);
  267. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  268. return RunCommand(cmd);
  269. }
  270. }
  271. }