1
0

MySQLRegionData.cs 13 KB

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