1
0

MySQLRegionData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 = null;
  40. // private int m_LastExpire = 0;
  41. public MySqlRegionData(string connectionString, string realm)
  42. : base(connectionString)
  43. {
  44. m_Realm = realm;
  45. Migration m = new Migration(m_Connection, GetType().Assembly, "GridStore");
  46. m.Update();
  47. }
  48. public List<RegionData> Get(string regionName, UUID scopeID)
  49. {
  50. string command = "select * from `"+m_Realm+"` where regionName like ?regionName";
  51. if (scopeID != UUID.Zero)
  52. command += " and ScopeID = ?scopeID";
  53. MySqlCommand cmd = new MySqlCommand(command);
  54. cmd.Parameters.AddWithValue("?regionName", regionName);
  55. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  56. return RunCommand(cmd);
  57. }
  58. public RegionData Get(int posX, int posY, UUID scopeID)
  59. {
  60. string command = "select * from `"+m_Realm+"` where locX = ?posX and locY = ?posY";
  61. if (scopeID != UUID.Zero)
  62. command += " and ScopeID = ?scopeID";
  63. MySqlCommand cmd = new MySqlCommand(command);
  64. cmd.Parameters.AddWithValue("?posX", posX.ToString());
  65. cmd.Parameters.AddWithValue("?posY", posY.ToString());
  66. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  67. List<RegionData> ret = RunCommand(cmd);
  68. if (ret.Count == 0)
  69. return null;
  70. return ret[0];
  71. }
  72. public RegionData Get(UUID regionID, UUID scopeID)
  73. {
  74. string command = "select * from `"+m_Realm+"` where uuid = ?regionID";
  75. if (scopeID != UUID.Zero)
  76. command += " and ScopeID = ?scopeID";
  77. MySqlCommand cmd = new MySqlCommand(command);
  78. cmd.Parameters.AddWithValue("?regionID", regionID.ToString());
  79. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  80. List<RegionData> ret = RunCommand(cmd);
  81. if (ret.Count == 0)
  82. return null;
  83. return ret[0];
  84. }
  85. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  86. {
  87. string command = "select * from `"+m_Realm+"` where locX between ?startX and ?endX and locY between ?startY and ?endY";
  88. if (scopeID != UUID.Zero)
  89. command += " and ScopeID = ?scopeID";
  90. MySqlCommand cmd = new MySqlCommand(command);
  91. cmd.Parameters.AddWithValue("?startX", startX.ToString());
  92. cmd.Parameters.AddWithValue("?startY", startY.ToString());
  93. cmd.Parameters.AddWithValue("?endX", endX.ToString());
  94. cmd.Parameters.AddWithValue("?endY", endY.ToString());
  95. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  96. return RunCommand(cmd);
  97. }
  98. public List<RegionData> RunCommand(MySqlCommand cmd)
  99. {
  100. List<RegionData> retList = new List<RegionData>();
  101. IDataReader result = ExecuteReader(cmd);
  102. while (result.Read())
  103. {
  104. RegionData ret = new RegionData();
  105. ret.Data = new Dictionary<string, object>();
  106. UUID regionID;
  107. UUID.TryParse(result["uuid"].ToString(), out regionID);
  108. ret.RegionID = regionID;
  109. UUID scope;
  110. UUID.TryParse(result["ScopeID"].ToString(), out scope);
  111. ret.ScopeID = scope;
  112. ret.RegionName = result["regionName"].ToString();
  113. ret.posX = Convert.ToInt32(result["locX"]);
  114. ret.posY = Convert.ToInt32(result["locY"]);
  115. ret.sizeX = Convert.ToInt32(result["sizeX"]);
  116. ret.sizeY = Convert.ToInt32(result["sizeY"]);
  117. if (m_ColumnNames == null)
  118. {
  119. m_ColumnNames = new List<string>();
  120. DataTable schemaTable = result.GetSchemaTable();
  121. foreach (DataRow row in schemaTable.Rows)
  122. {
  123. if (row["ColumnName"] != null)
  124. m_ColumnNames.Add(row["ColumnName"].ToString());
  125. }
  126. }
  127. foreach (string s in m_ColumnNames)
  128. {
  129. if (s == "uuid")
  130. continue;
  131. if (s == "ScopeID")
  132. continue;
  133. if (s == "regionName")
  134. continue;
  135. if (s == "locX")
  136. continue;
  137. if (s == "locY")
  138. continue;
  139. ret.Data[s] = result[s].ToString();
  140. }
  141. retList.Add(ret);
  142. }
  143. result.Close();
  144. CloseReaderCommand(cmd);
  145. return retList;
  146. }
  147. public bool Store(RegionData data)
  148. {
  149. if (data.Data.ContainsKey("uuid"))
  150. data.Data.Remove("uuid");
  151. if (data.Data.ContainsKey("ScopeID"))
  152. data.Data.Remove("ScopeID");
  153. if (data.Data.ContainsKey("regionName"))
  154. data.Data.Remove("regionName");
  155. if (data.Data.ContainsKey("posX"))
  156. data.Data.Remove("posX");
  157. if (data.Data.ContainsKey("posY"))
  158. data.Data.Remove("posY");
  159. if (data.Data.ContainsKey("sizeX"))
  160. data.Data.Remove("sizeX");
  161. if (data.Data.ContainsKey("sizeY"))
  162. data.Data.Remove("sizeY");
  163. if (data.Data.ContainsKey("locX"))
  164. data.Data.Remove("locX");
  165. if (data.Data.ContainsKey("locY"))
  166. data.Data.Remove("locY");
  167. string[] fields = new List<string>(data.Data.Keys).ToArray();
  168. MySqlCommand cmd = new MySqlCommand();
  169. string update = "update `"+m_Realm+"` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY";
  170. foreach (string field in fields)
  171. {
  172. update += ", ";
  173. update += "`" + field + "` = ?"+field;
  174. cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
  175. }
  176. update += " where uuid = ?regionID";
  177. if (data.ScopeID != UUID.Zero)
  178. update += " and ScopeID = ?scopeID";
  179. cmd.CommandText = update;
  180. cmd.Parameters.AddWithValue("?regionID", data.RegionID.ToString());
  181. cmd.Parameters.AddWithValue("?regionName", data.RegionName);
  182. cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString());
  183. cmd.Parameters.AddWithValue("?posX", data.posX.ToString());
  184. cmd.Parameters.AddWithValue("?posY", data.posY.ToString());
  185. cmd.Parameters.AddWithValue("?sizeX", data.sizeX.ToString());
  186. cmd.Parameters.AddWithValue("?sizeY", data.sizeY.ToString());
  187. if (ExecuteNonQuery(cmd) < 1)
  188. {
  189. string insert = "insert into `" + m_Realm + "` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" +
  190. String.Join("`, `", fields) +
  191. "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(", ?", fields) + ")";
  192. cmd.CommandText = insert;
  193. if (ExecuteNonQuery(cmd) < 1)
  194. {
  195. cmd.Dispose();
  196. return false;
  197. }
  198. }
  199. cmd.Dispose();
  200. return true;
  201. }
  202. public bool SetDataItem(UUID regionID, string item, string value)
  203. {
  204. MySqlCommand cmd = new MySqlCommand("update `" + m_Realm +
  205. "` set `" + item + "` = ?" + item + " where uuid = ?UUID");
  206. cmd.Parameters.AddWithValue("?"+item, value);
  207. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  208. if (ExecuteNonQuery(cmd) > 0)
  209. return true;
  210. return false;
  211. }
  212. public bool Delete(UUID regionID)
  213. {
  214. MySqlCommand cmd = new MySqlCommand("delete from `" + m_Realm +
  215. "` where uuid = ?UUID");
  216. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  217. if (ExecuteNonQuery(cmd) > 0)
  218. return true;
  219. return false;
  220. }
  221. }
  222. }