MySQLRegionData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. m_ColumnNames.Add(row["ColumnName"].ToString());
  123. }
  124. foreach (string s in m_ColumnNames)
  125. {
  126. if (s == "uuid")
  127. continue;
  128. if (s == "ScopeID")
  129. continue;
  130. if (s == "regionName")
  131. continue;
  132. if (s == "locX")
  133. continue;
  134. if (s == "locY")
  135. continue;
  136. ret.Data[s] = result[s].ToString();
  137. }
  138. retList.Add(ret);
  139. }
  140. result.Close();
  141. CloseReaderCommand(cmd);
  142. return retList;
  143. }
  144. public bool Store(RegionData data)
  145. {
  146. if (data.Data.ContainsKey("uuid"))
  147. data.Data.Remove("uuid");
  148. if (data.Data.ContainsKey("ScopeID"))
  149. data.Data.Remove("ScopeID");
  150. if (data.Data.ContainsKey("regionName"))
  151. data.Data.Remove("regionName");
  152. if (data.Data.ContainsKey("posX"))
  153. data.Data.Remove("posX");
  154. if (data.Data.ContainsKey("posY"))
  155. data.Data.Remove("posY");
  156. if (data.Data.ContainsKey("sizeX"))
  157. data.Data.Remove("sizeX");
  158. if (data.Data.ContainsKey("sizeY"))
  159. data.Data.Remove("sizeY");
  160. if (data.Data.ContainsKey("locX"))
  161. data.Data.Remove("locX");
  162. if (data.Data.ContainsKey("locY"))
  163. data.Data.Remove("locY");
  164. string[] fields = new List<string>(data.Data.Keys).ToArray();
  165. MySqlCommand cmd = new MySqlCommand();
  166. string update = "update `"+m_Realm+"` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY";
  167. foreach (string field in fields)
  168. {
  169. update += ", ";
  170. update += "`" + field + "` = ?"+field;
  171. cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
  172. }
  173. update += " where uuid = ?regionID";
  174. if (data.ScopeID != UUID.Zero)
  175. update += " and ScopeID = ?scopeID";
  176. cmd.CommandText = update;
  177. cmd.Parameters.AddWithValue("?regionID", data.RegionID.ToString());
  178. cmd.Parameters.AddWithValue("?regionName", data.RegionName);
  179. cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString());
  180. cmd.Parameters.AddWithValue("?posX", data.posX.ToString());
  181. cmd.Parameters.AddWithValue("?posY", data.posY.ToString());
  182. cmd.Parameters.AddWithValue("?sizeX", data.sizeX.ToString());
  183. cmd.Parameters.AddWithValue("?sizeY", data.sizeY.ToString());
  184. if (ExecuteNonQuery(cmd) < 1)
  185. {
  186. string insert = "insert into `" + m_Realm + "` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" +
  187. String.Join("`, `", fields) +
  188. "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(", ?", fields) + ")";
  189. cmd.CommandText = insert;
  190. if (ExecuteNonQuery(cmd) < 1)
  191. {
  192. cmd.Dispose();
  193. return false;
  194. }
  195. }
  196. cmd.Dispose();
  197. return true;
  198. }
  199. public bool SetDataItem(UUID regionID, string item, string value)
  200. {
  201. MySqlCommand cmd = new MySqlCommand("update `" + m_Realm +
  202. "` set `" + item + "` = ?" + item + " where uuid = ?UUID");
  203. cmd.Parameters.AddWithValue("?"+item, value);
  204. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  205. if (ExecuteNonQuery(cmd) > 0)
  206. return true;
  207. return false;
  208. }
  209. public bool Delete(UUID regionID)
  210. {
  211. MySqlCommand cmd = new MySqlCommand("delete from `" + m_Realm +
  212. "` where uuid = ?UUID");
  213. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  214. if (ExecuteNonQuery(cmd) > 0)
  215. return true;
  216. return false;
  217. }
  218. }
  219. }