MySQLRegionData.cs 12 KB

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