MySQLXInventoryData.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Data;
  29. using System.Reflection;
  30. using System.Collections.Generic;
  31. using log4net;
  32. using MySql.Data.MySqlClient;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.MySQL
  36. {
  37. /// <summary>
  38. /// A MySQL Interface for the Asset Server
  39. /// </summary>
  40. public class MySQLXInventoryData : IXInventoryData
  41. {
  42. private MySQLGenericTableHandler<XInventoryFolder> m_Folders;
  43. private MySqlItemHandler m_Items;
  44. public MySQLXInventoryData(string conn, string realm)
  45. {
  46. m_Folders = new MySQLGenericTableHandler<XInventoryFolder>(
  47. conn, "inventoryfolders", "InventoryStore");
  48. m_Items = new MySqlItemHandler(
  49. conn, "inventoryitems", String.Empty);
  50. }
  51. public XInventoryFolder[] GetFolders(string[] fields, string[] vals)
  52. {
  53. return m_Folders.Get(fields, vals);
  54. }
  55. public XInventoryItem[] GetItems(string[] fields, string[] vals)
  56. {
  57. return m_Items.Get(fields, vals);
  58. }
  59. public bool StoreFolder(XInventoryFolder folder)
  60. {
  61. if (folder.folderName.Length > 64)
  62. folder.folderName = folder.folderName.Substring(0, 64);
  63. return m_Folders.Store(folder);
  64. }
  65. public bool StoreItem(XInventoryItem item)
  66. {
  67. if (item.inventoryName.Length > 64)
  68. item.inventoryName = item.inventoryName.Substring(0, 64);
  69. if (item.inventoryDescription.Length > 128)
  70. item.inventoryDescription = item.inventoryDescription.Substring(0, 128);
  71. return m_Items.Store(item);
  72. }
  73. public bool DeleteFolders(string field, string val)
  74. {
  75. return m_Folders.Delete(field, val);
  76. }
  77. public bool DeleteFolders(string[] fields, string[] vals)
  78. {
  79. return m_Folders.Delete(fields, vals);
  80. }
  81. public bool DeleteItems(string field, string val)
  82. {
  83. return m_Items.Delete(field, val);
  84. }
  85. public bool DeleteItems(string[] fields, string[] vals)
  86. {
  87. return m_Items.Delete(fields, vals);
  88. }
  89. public bool MoveItem(string id, string newParent)
  90. {
  91. return m_Items.MoveItem(id, newParent);
  92. }
  93. public XInventoryItem[] GetActiveGestures(UUID principalID)
  94. {
  95. return m_Items.GetActiveGestures(principalID);
  96. }
  97. public int GetAssetPermissions(UUID principalID, UUID assetID)
  98. {
  99. return m_Items.GetAssetPermissions(principalID, assetID);
  100. }
  101. }
  102. public class MySqlItemHandler : MySQLGenericTableHandler<XInventoryItem>
  103. {
  104. public MySqlItemHandler(string c, string t, string m) :
  105. base(c, t, m)
  106. {
  107. }
  108. public bool MoveItem(string id, string newParent)
  109. {
  110. using (MySqlCommand cmd = new MySqlCommand())
  111. {
  112. cmd.CommandText = String.Format("update {0} set parentFolderID = ?ParentFolderID where inventoryID = ?InventoryID", m_Realm);
  113. cmd.Parameters.AddWithValue("?ParentFolderID", newParent);
  114. cmd.Parameters.AddWithValue("?InventoryID", id);
  115. return ExecuteNonQuery(cmd) == 0 ? false : true;
  116. }
  117. }
  118. public XInventoryItem[] GetActiveGestures(UUID principalID)
  119. {
  120. using (MySqlCommand cmd = new MySqlCommand())
  121. {
  122. cmd.CommandText = String.Format("select * from inventoryitems where avatarId = ?uuid and assetType = ?type and flags & 1", m_Realm);
  123. cmd.Parameters.AddWithValue("?uuid", principalID.ToString());
  124. cmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture);
  125. return DoQuery(cmd);
  126. }
  127. }
  128. public int GetAssetPermissions(UUID principalID, UUID assetID)
  129. {
  130. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  131. {
  132. dbcon.Open();
  133. using (MySqlCommand cmd = new MySqlCommand())
  134. {
  135. cmd.Connection = dbcon;
  136. cmd.CommandText = String.Format("select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems where avatarID = ?PrincipalID and assetID = ?AssetID group by assetID", m_Realm);
  137. cmd.Parameters.AddWithValue("?PrincipalID", principalID.ToString());
  138. cmd.Parameters.AddWithValue("?AssetID", assetID.ToString());
  139. using (IDataReader reader = cmd.ExecuteReader())
  140. {
  141. int perms = 0;
  142. if (reader.Read())
  143. {
  144. perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
  145. }
  146. return perms;
  147. }
  148. }
  149. }
  150. }
  151. public override bool Store(XInventoryItem item)
  152. {
  153. if (!base.Store(item))
  154. return false;
  155. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  156. {
  157. dbcon.Open();
  158. using (MySqlCommand cmd = new MySqlCommand())
  159. {
  160. cmd.Connection = dbcon;
  161. cmd.CommandText = String.Format("update inventoryfolders set version=version+1 where folderID = ?folderID");
  162. cmd.Parameters.AddWithValue("?folderID", item.parentFolderID.ToString());
  163. try
  164. {
  165. cmd.ExecuteNonQuery();
  166. }
  167. catch (Exception)
  168. {
  169. return false;
  170. }
  171. cmd.Dispose();
  172. }
  173. dbcon.Close();
  174. }
  175. return true;
  176. }
  177. }
  178. }