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