SQLiteXInventoryData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #if CSharpSqlite
  32. using Community.CsharpSqlite.Sqlite;
  33. #else
  34. using Mono.Data.Sqlite;
  35. #endif
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. namespace OpenSim.Data.SQLite
  40. {
  41. /// <summary>
  42. /// A SQLite Interface for the Asset Server
  43. /// </summary>
  44. public class SQLiteXInventoryData : IXInventoryData
  45. {
  46. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private SqliteFolderHandler m_Folders;
  48. private SqliteItemHandler m_Items;
  49. public SQLiteXInventoryData(string conn, string realm)
  50. {
  51. DllmapConfigHelper.RegisterAssembly(typeof(SqliteConnection).Assembly);
  52. m_Folders = new SqliteFolderHandler(
  53. conn, "inventoryfolders", "XInventoryStore");
  54. m_Items = new SqliteItemHandler(
  55. conn, "inventoryitems", String.Empty);
  56. }
  57. public XInventoryFolder[] GetFolder(string field, string val)
  58. {
  59. return m_Folders.Get(field, val);
  60. }
  61. public XInventoryFolder[] GetFolders(string[] fields, string[] vals)
  62. {
  63. return m_Folders.Get(fields, vals);
  64. }
  65. public XInventoryItem[] GetItems(string[] fields, string[] vals)
  66. {
  67. return m_Items.Get(fields, vals);
  68. }
  69. public bool StoreFolder(XInventoryFolder folder)
  70. {
  71. if (folder.folderName.Length > 64)
  72. folder.folderName = folder.folderName.Substring(0, 64);
  73. return m_Folders.Store(folder);
  74. }
  75. public bool StoreItem(XInventoryItem item)
  76. {
  77. if (item.inventoryName.Length > 64)
  78. item.inventoryName = item.inventoryName.Substring(0, 64);
  79. if (item.inventoryDescription.Length > 128)
  80. item.inventoryDescription = item.inventoryDescription.Substring(0, 128);
  81. return m_Items.Store(item);
  82. }
  83. public bool DeleteFolders(string field, string val)
  84. {
  85. return m_Folders.Delete(field, val);
  86. }
  87. public bool DeleteFolders(string[] fields, string[] vals)
  88. {
  89. return m_Folders.Delete(fields, vals);
  90. }
  91. public bool DeleteItems(string field, string val)
  92. {
  93. return m_Items.Delete(field, val);
  94. }
  95. public bool DeleteItems(string[] fields, string[] vals)
  96. {
  97. return m_Items.Delete(fields, vals);
  98. }
  99. public bool MoveItem(string id, string newParent)
  100. {
  101. return m_Items.MoveItem(id, newParent);
  102. }
  103. public bool MoveFolder(string id, string newParent)
  104. {
  105. return m_Folders.MoveFolder(id, newParent);
  106. }
  107. public XInventoryItem[] GetActiveGestures(UUID principalID)
  108. {
  109. return m_Items.GetActiveGestures(principalID);
  110. }
  111. public int GetAssetPermissions(UUID principalID, UUID assetID)
  112. {
  113. return m_Items.GetAssetPermissions(principalID, assetID);
  114. }
  115. }
  116. public class SqliteItemHandler : SqliteInventoryHandler<XInventoryItem>
  117. {
  118. public SqliteItemHandler(string c, string t, string m) :
  119. base(c, t, m)
  120. {
  121. }
  122. public override bool Store(XInventoryItem item)
  123. {
  124. if (!base.Store(item))
  125. return false;
  126. IncrementFolderVersion(item.parentFolderID);
  127. return true;
  128. }
  129. public override bool Delete(string field, string val)
  130. {
  131. XInventoryItem[] retrievedItems = Get(new string[] { field }, new string[] { val });
  132. if (retrievedItems.Length == 0)
  133. return false;
  134. if (!base.Delete(field, val))
  135. return false;
  136. // Don't increment folder version here since Delete(string, string) calls Delete(string[], string[])
  137. // IncrementFolderVersion(retrievedItems[0].parentFolderID);
  138. return true;
  139. }
  140. public override bool Delete(string[] fields, string[] vals)
  141. {
  142. XInventoryItem[] retrievedItems = Get(fields, vals);
  143. if (retrievedItems.Length == 0)
  144. return false;
  145. if (!base.Delete(fields, vals))
  146. return false;
  147. HashSet<UUID> deletedItemFolderUUIDs = new HashSet<UUID>();
  148. Array.ForEach<XInventoryItem>(retrievedItems, i => deletedItemFolderUUIDs.Add(i.parentFolderID));
  149. foreach (UUID deletedItemFolderUUID in deletedItemFolderUUIDs)
  150. IncrementFolderVersion(deletedItemFolderUUID);
  151. return true;
  152. }
  153. public bool MoveItem(string id, string newParent)
  154. {
  155. XInventoryItem[] retrievedItems = Get(new string[] { "inventoryID" }, new string[] { id });
  156. if (retrievedItems.Length == 0)
  157. return false;
  158. UUID oldParent = retrievedItems[0].parentFolderID;
  159. using (SqliteCommand cmd = new SqliteCommand())
  160. {
  161. cmd.CommandText = String.Format("update {0} set parentFolderID = :ParentFolderID where inventoryID = :InventoryID", m_Realm);
  162. cmd.Parameters.Add(new SqliteParameter(":ParentFolderID", newParent));
  163. cmd.Parameters.Add(new SqliteParameter(":InventoryID", id));
  164. if (ExecuteNonQuery(cmd, m_Connection) == 0)
  165. return false;
  166. }
  167. IncrementFolderVersion(oldParent);
  168. IncrementFolderVersion(newParent);
  169. return true;
  170. }
  171. public XInventoryItem[] GetActiveGestures(UUID principalID)
  172. {
  173. using (SqliteCommand cmd = new SqliteCommand())
  174. {
  175. cmd.CommandText = String.Format("select * from inventoryitems where avatarId = :uuid and assetType = :type and flags = 1", m_Realm);
  176. cmd.Parameters.Add(new SqliteParameter(":uuid", principalID.ToString()));
  177. cmd.Parameters.Add(new SqliteParameter(":type", (int)AssetType.Gesture));
  178. return DoQuery(cmd);
  179. }
  180. }
  181. public int GetAssetPermissions(UUID principalID, UUID assetID)
  182. {
  183. IDataReader reader;
  184. using (SqliteCommand cmd = new SqliteCommand())
  185. {
  186. cmd.CommandText = String.Format("select inventoryCurrentPermissions from inventoryitems where avatarID = :PrincipalID and assetID = :AssetID", m_Realm);
  187. cmd.Parameters.Add(new SqliteParameter(":PrincipalID", principalID.ToString()));
  188. cmd.Parameters.Add(new SqliteParameter(":AssetID", assetID.ToString()));
  189. reader = ExecuteReader(cmd, m_Connection);
  190. }
  191. int perms = 0;
  192. while (reader.Read())
  193. {
  194. perms |= Convert.ToInt32(reader["inventoryCurrentPermissions"]);
  195. }
  196. reader.Close();
  197. //CloseCommand(cmd);
  198. return perms;
  199. }
  200. }
  201. public class SqliteFolderHandler : SqliteInventoryHandler<XInventoryFolder>
  202. {
  203. public SqliteFolderHandler(string c, string t, string m) :
  204. base(c, t, m)
  205. {
  206. }
  207. public override bool Store(XInventoryFolder folder)
  208. {
  209. if (!base.Store(folder))
  210. return false;
  211. IncrementFolderVersion(folder.parentFolderID);
  212. return true;
  213. }
  214. public bool MoveFolder(string id, string newParentFolderID)
  215. {
  216. XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id });
  217. if (folders.Length == 0)
  218. return false;
  219. UUID oldParentFolderUUID = folders[0].parentFolderID;
  220. using (SqliteCommand cmd = new SqliteCommand())
  221. {
  222. cmd.CommandText = String.Format("update {0} set parentFolderID = :ParentFolderID where folderID = :FolderID", m_Realm);
  223. cmd.Parameters.Add(new SqliteParameter(":ParentFolderID", newParentFolderID));
  224. cmd.Parameters.Add(new SqliteParameter(":FolderID", id));
  225. if (ExecuteNonQuery(cmd, m_Connection) == 0)
  226. return false;
  227. }
  228. IncrementFolderVersion(oldParentFolderUUID);
  229. IncrementFolderVersion(newParentFolderID);
  230. return true;
  231. }
  232. }
  233. public class SqliteInventoryHandler<T> : SQLiteGenericTableHandler<T> where T: class, new()
  234. {
  235. public SqliteInventoryHandler(string c, string t, string m) : base(c, t, m) {}
  236. protected bool IncrementFolderVersion(UUID folderID)
  237. {
  238. return IncrementFolderVersion(folderID.ToString());
  239. }
  240. protected bool IncrementFolderVersion(string folderID)
  241. {
  242. // m_log.DebugFormat("[MYSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID);
  243. // Util.PrintCallStack();
  244. using (SqliteCommand cmd = new SqliteCommand())
  245. {
  246. cmd.CommandText = "update inventoryfolders set version=version+1 where folderID = :folderID";
  247. cmd.Parameters.Add(new SqliteParameter(":folderID", folderID));
  248. if(ExecuteNonQuery(cmd, m_Connection) == 0)
  249. return false;
  250. }
  251. return true;
  252. }
  253. }
  254. }