MySQLRegionData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. dbcon.Close();
  59. }
  60. }
  61. public List<RegionData> Get(string regionName, UUID scopeID)
  62. {
  63. string command = "select * from `"+m_Realm+"` where regionName like ?regionName";
  64. if (scopeID.IsNotZero())
  65. command += " and ScopeID = ?scopeID";
  66. command += " order by regionName";
  67. using (MySqlCommand cmd = new MySqlCommand(command))
  68. {
  69. cmd.Parameters.AddWithValue("?regionName", regionName);
  70. if (scopeID.IsNotZero())
  71. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  72. return RunCommand(cmd);
  73. }
  74. }
  75. public RegionData GetSpecific(string regionName, UUID scopeID)
  76. {
  77. string command = "select * from `" + m_Realm + "` where regionName = ?regionName";
  78. if (scopeID.IsNotZero())
  79. command += " and ScopeID = ?scopeID";
  80. using (MySqlCommand cmd = new MySqlCommand(command))
  81. {
  82. cmd.Parameters.AddWithValue("?regionName", regionName);
  83. if (scopeID.IsNotZero())
  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 RegionData Get(int posX, int posY, 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.IsNotZero())
  95. command += " and ScopeID = ?scopeID";
  96. int startX = posX - (int)Constants.MaximumRegionSize;
  97. int startY = posY - (int)Constants.MaximumRegionSize;
  98. int endX = posX;
  99. int endY = posY;
  100. List<RegionData> ret;
  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. if (scopeID.IsNotZero())
  108. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  109. ret = RunCommand(cmd);
  110. }
  111. if (ret.Count == 0)
  112. return null;
  113. // find the first that contains pos
  114. RegionData rg = null;
  115. foreach (RegionData r in ret)
  116. {
  117. if (posX >= r.posX && posX < r.posX + r.sizeX
  118. && posY >= r.posY && posY < r.posY + r.sizeY)
  119. {
  120. rg = r;
  121. break;
  122. }
  123. }
  124. return rg;
  125. }
  126. public RegionData Get(UUID regionID, UUID scopeID)
  127. {
  128. string command = "select * from `"+m_Realm+"` where uuid = ?regionID";
  129. if (!scopeID.IsZero())
  130. command += " and ScopeID = ?scopeID";
  131. using (MySqlCommand cmd = new MySqlCommand(command))
  132. {
  133. cmd.Parameters.AddWithValue("?regionID", regionID.ToString());
  134. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  135. List<RegionData> ret = RunCommand(cmd);
  136. if (ret.Count == 0)
  137. return null;
  138. return ret[0];
  139. }
  140. }
  141. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  142. {
  143. /* fix size regions
  144. string command = "select * from `"+m_Realm+"` where locX between ?startX and ?endX and locY between ?startY and ?endY";
  145. if (scopeID != UUID.Zero)
  146. command += " and ScopeID = ?scopeID";
  147. using (MySqlCommand cmd = new MySqlCommand(command))
  148. {
  149. cmd.Parameters.AddWithValue("?startX", startX.ToString());
  150. cmd.Parameters.AddWithValue("?startY", startY.ToString());
  151. cmd.Parameters.AddWithValue("?endX", endX.ToString());
  152. cmd.Parameters.AddWithValue("?endY", endY.ToString());
  153. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  154. return RunCommand(cmd);
  155. }
  156. */
  157. string command = "select * from `" + m_Realm + "` where locX between ?startX and ?endX and locY between ?startY and ?endY";
  158. if (scopeID != UUID.Zero)
  159. command += " and ScopeID = ?scopeID";
  160. int qstartX = startX - (int)Constants.MaximumRegionSize;
  161. int qstartY = startY - (int)Constants.MaximumRegionSize;
  162. List<RegionData> dbret;
  163. using (MySqlCommand cmd = new MySqlCommand(command))
  164. {
  165. cmd.Parameters.AddWithValue("?startX", qstartX.ToString());
  166. cmd.Parameters.AddWithValue("?startY", qstartY.ToString());
  167. cmd.Parameters.AddWithValue("?endX", endX.ToString());
  168. cmd.Parameters.AddWithValue("?endY", endY.ToString());
  169. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  170. dbret = RunCommand(cmd);
  171. }
  172. List<RegionData> ret = new List<RegionData>();
  173. if (dbret.Count == 0)
  174. return ret;
  175. foreach (RegionData r in dbret)
  176. {
  177. if (r.posX + r.sizeX > startX && r.posX <= endX
  178. && r.posY + r.sizeY > startY && r.posY <= endY)
  179. ret.Add(r);
  180. }
  181. return ret;
  182. }
  183. public List<RegionData> RunCommand(MySqlCommand cmd)
  184. {
  185. List<RegionData> retList = new List<RegionData>();
  186. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  187. {
  188. dbcon.Open();
  189. cmd.Connection = dbcon;
  190. using (IDataReader result = cmd.ExecuteReader())
  191. {
  192. while (result.Read())
  193. {
  194. RegionData ret = new RegionData();
  195. ret.Data = new Dictionary<string, object>();
  196. ret.RegionID = DBGuid.FromDB(result["uuid"]);
  197. ret.ScopeID = DBGuid.FromDB(result["ScopeID"]);
  198. ret.RegionName = result["regionName"].ToString();
  199. ret.posX = Convert.ToInt32(result["locX"]);
  200. ret.posY = Convert.ToInt32(result["locY"]);
  201. ret.sizeX = Convert.ToInt32(result["sizeX"]);
  202. ret.sizeY = Convert.ToInt32(result["sizeY"]);
  203. CheckColumnNames(result);
  204. foreach (string s in m_ColumnNames)
  205. {
  206. if (s == "uuid")
  207. continue;
  208. if (s == "ScopeID")
  209. continue;
  210. if (s == "regionName")
  211. continue;
  212. if (s == "locX")
  213. continue;
  214. if (s == "locY")
  215. continue;
  216. object value = result[s];
  217. if (value is DBNull)
  218. ret.Data[s] = null;
  219. else
  220. ret.Data[s] = result[s].ToString();
  221. }
  222. retList.Add(ret);
  223. }
  224. }
  225. cmd.Connection = null;
  226. dbcon.Close();
  227. }
  228. return retList;
  229. }
  230. private void CheckColumnNames(IDataReader result)
  231. {
  232. if (m_ColumnNames != null)
  233. return;
  234. List<string> columnNames = new List<string>();
  235. DataTable schemaTable = result.GetSchemaTable();
  236. foreach (DataRow row in schemaTable.Rows)
  237. {
  238. if (row["ColumnName"] != null)
  239. columnNames.Add(row["ColumnName"].ToString());
  240. }
  241. m_ColumnNames = columnNames;
  242. }
  243. public bool Store(RegionData data)
  244. {
  245. data.Data.Remove("uuid");
  246. data.Data.Remove("ScopeID");
  247. data.Data.Remove("regionName");
  248. data.Data.Remove("posX");
  249. data.Data.Remove("posY");
  250. data.Data.Remove("sizeX");
  251. data.Data.Remove("sizeY");
  252. data.Data.Remove("locX");
  253. data.Data.Remove("locY");
  254. if (data.RegionName.Length > 128)
  255. data.RegionName = data.RegionName.Substring(0, 128);
  256. string[] fields = new List<string>(data.Data.Keys).ToArray();
  257. using (MySqlCommand cmd = new MySqlCommand())
  258. {
  259. string update = "update `" + m_Realm + "` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY";
  260. foreach (string field in fields)
  261. {
  262. update += ", ";
  263. update += "`" + field + "` = ?" + field;
  264. cmd.Parameters.AddWithValue("?" + field, data.Data[field]);
  265. }
  266. update += " where uuid = ?regionID";
  267. if (!data.ScopeID.IsZero())
  268. update += " and ScopeID = ?scopeID";
  269. cmd.CommandText = update;
  270. cmd.Parameters.AddWithValue("?regionID", data.RegionID.ToString());
  271. cmd.Parameters.AddWithValue("?regionName", data.RegionName);
  272. cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString());
  273. cmd.Parameters.AddWithValue("?posX", data.posX.ToString());
  274. cmd.Parameters.AddWithValue("?posY", data.posY.ToString());
  275. cmd.Parameters.AddWithValue("?sizeX", data.sizeX.ToString());
  276. cmd.Parameters.AddWithValue("?sizeY", data.sizeY.ToString());
  277. if (ExecuteNonQuery(cmd) < 1)
  278. {
  279. string insert = "insert into `" + m_Realm + "` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" +
  280. String.Join("`, `", fields) +
  281. "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(", ?", fields) + ")";
  282. cmd.CommandText = insert;
  283. if (ExecuteNonQuery(cmd) < 1)
  284. {
  285. return false;
  286. }
  287. }
  288. }
  289. return true;
  290. }
  291. public bool SetDataItem(UUID regionID, string item, string value)
  292. {
  293. using (MySqlCommand cmd = new MySqlCommand("update `" + m_Realm + "` set `" + item + "` = ?" + item + " where uuid = ?UUID"))
  294. {
  295. cmd.Parameters.AddWithValue("?" + item, value);
  296. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  297. if (ExecuteNonQuery(cmd) > 0)
  298. return true;
  299. }
  300. return false;
  301. }
  302. public bool Delete(UUID regionID)
  303. {
  304. using (MySqlCommand cmd = new MySqlCommand("delete from `" + m_Realm + "` where uuid = ?UUID"))
  305. {
  306. cmd.Parameters.AddWithValue("?UUID", regionID.ToString());
  307. if (ExecuteNonQuery(cmd) > 0)
  308. return true;
  309. }
  310. return false;
  311. }
  312. public List<RegionData> GetDefaultRegions(UUID scopeID)
  313. {
  314. return Get((int)RegionFlags.DefaultRegion, scopeID);
  315. }
  316. public List<RegionData> GetDefaultHypergridRegions(UUID scopeID)
  317. {
  318. return Get((int)RegionFlags.DefaultHGRegion, scopeID);
  319. }
  320. public List<RegionData> GetFallbackRegions(UUID scopeID)
  321. {
  322. return Get((int)RegionFlags.FallbackRegion, scopeID);
  323. }
  324. public List<RegionData> GetHyperlinks(UUID scopeID)
  325. {
  326. return Get((int)RegionFlags.Hyperlink, scopeID);
  327. }
  328. public List<RegionData> GetOnlineRegions(UUID scopeID)
  329. {
  330. return Get((int)RegionFlags.RegionOnline, scopeID);
  331. }
  332. private List<RegionData> Get(int regionFlags, UUID scopeID)
  333. {
  334. string command = "select * from `" + m_Realm + "` where (flags & " + regionFlags.ToString() + ") <> 0";
  335. if (!scopeID.IsZero())
  336. command += " and ScopeID = ?scopeID";
  337. using (MySqlCommand cmd = new MySqlCommand(command))
  338. {
  339. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  340. return RunCommand(cmd);
  341. }
  342. }
  343. }
  344. }