1
0

PGSQLXInventoryData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 System.Reflection;
  34. using System.Text;
  35. using log4net;
  36. using Npgsql;
  37. using NpgsqlTypes;
  38. namespace OpenSim.Data.PGSQL
  39. {
  40. public class PGSQLXInventoryData : IXInventoryData
  41. {
  42. // private static readonly ILog m_log = LogManager.GetLogger(
  43. // MethodBase.GetCurrentMethod().DeclaringType);
  44. private PGSQLFolderHandler m_Folders;
  45. private PGSQLItemHandler m_Items;
  46. public PGSQLXInventoryData(string conn, string realm)
  47. {
  48. m_Folders = new PGSQLFolderHandler(
  49. conn, "inventoryfolders", "InventoryStore");
  50. m_Items = new PGSQLItemHandler(
  51. conn, "inventoryitems", String.Empty);
  52. }
  53. public static UUID str2UUID(string strUUID)
  54. {
  55. UUID newUUID = UUID.Zero;
  56. UUID.TryParse(strUUID, out newUUID);
  57. return newUUID;
  58. }
  59. public XInventoryFolder[] GetFolders(string[] fields, string[] vals)
  60. {
  61. return m_Folders.Get(fields, vals);
  62. }
  63. public XInventoryItem[] GetItems(string[] fields, string[] vals)
  64. {
  65. return m_Items.Get(fields, vals);
  66. }
  67. public bool StoreFolder(XInventoryFolder folder)
  68. {
  69. if (folder.folderName.Length > 64)
  70. folder.folderName = folder.folderName.Substring(0, 64);
  71. return m_Folders.Store(folder);
  72. }
  73. public bool StoreItem(XInventoryItem item)
  74. {
  75. if (item.inventoryName.Length > 64)
  76. item.inventoryName = item.inventoryName.Substring(0, 64);
  77. if (item.inventoryDescription.Length > 128)
  78. item.inventoryDescription = item.inventoryDescription.Substring(0, 128);
  79. return m_Items.Store(item);
  80. }
  81. public bool DeleteFolders(string field, string val)
  82. {
  83. return m_Folders.Delete(field, val);
  84. }
  85. public bool DeleteFolders(string[] fields, string[] vals)
  86. {
  87. return m_Folders.Delete(fields, vals);
  88. }
  89. public bool DeleteItems(string field, string val)
  90. {
  91. return m_Items.Delete(field, val);
  92. }
  93. public bool DeleteItems(string[] fields, string[] vals)
  94. {
  95. return m_Items.Delete(fields, vals);
  96. }
  97. public bool MoveItem(string id, string newParent)
  98. {
  99. return m_Items.MoveItem(id, newParent);
  100. }
  101. public bool MoveFolder(string id, string newParent)
  102. {
  103. return m_Folders.MoveFolder(id, newParent);
  104. }
  105. public XInventoryItem[] GetActiveGestures(UUID principalID)
  106. {
  107. return m_Items.GetActiveGestures(principalID.ToString());
  108. }
  109. public int GetAssetPermissions(UUID principalID, UUID assetID)
  110. {
  111. return m_Items.GetAssetPermissions(principalID, assetID);
  112. }
  113. }
  114. public class PGSQLItemHandler : PGSQLInventoryHandler<XInventoryItem>
  115. {
  116. public PGSQLItemHandler(string c, string t, string m) :
  117. base(c, t, m)
  118. {
  119. }
  120. public bool MoveItem(string id, string newParent)
  121. {
  122. XInventoryItem[] retrievedItems = Get(new string[] { "inventoryID" }, new string[] { id });
  123. if (retrievedItems.Length == 0)
  124. return false;
  125. UUID oldParent = retrievedItems[0].parentFolderID;
  126. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  127. {
  128. using (NpgsqlCommand cmd = new NpgsqlCommand())
  129. {
  130. cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""inventoryID"" = :InventoryID", m_Realm);
  131. cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newParent));
  132. cmd.Parameters.Add(m_database.CreateParameter("InventoryID", id ));
  133. cmd.Connection = conn;
  134. conn.Open();
  135. if (cmd.ExecuteNonQuery() == 0)
  136. return false;
  137. }
  138. }
  139. IncrementFolderVersion(oldParent);
  140. IncrementFolderVersion(newParent);
  141. return true;
  142. }
  143. public XInventoryItem[] GetActiveGestures(string principalID)
  144. {
  145. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  146. {
  147. using (NpgsqlCommand cmd = new NpgsqlCommand())
  148. {
  149. cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1", m_Realm);
  150. UUID princID = UUID.Zero;
  151. UUID.TryParse(principalID, out princID);
  152. cmd.Parameters.Add(m_database.CreateParameter("uuid", principalID));
  153. cmd.Parameters.Add(m_database.CreateParameter("type", (int)AssetType.Gesture));
  154. cmd.Connection = conn;
  155. conn.Open();
  156. return DoQuery(cmd);
  157. }
  158. }
  159. }
  160. public int GetAssetPermissions(UUID principalID, UUID assetID)
  161. {
  162. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  163. {
  164. using (NpgsqlCommand cmd = new NpgsqlCommand())
  165. {
  166. cmd.CommandText = String.Format(@"select bit_or(""inventoryCurrentPermissions"") as ""inventoryCurrentPermissions""
  167. from inventoryitems
  168. where ""avatarID"" = :PrincipalID
  169. and ""assetID"" = :AssetID
  170. group by ""assetID"" ", m_Realm);
  171. cmd.Parameters.Add(m_database.CreateParameter("PrincipalID", principalID));
  172. cmd.Parameters.Add(m_database.CreateParameter("AssetID", assetID));
  173. cmd.Connection = conn;
  174. conn.Open();
  175. using (NpgsqlDataReader reader = cmd.ExecuteReader())
  176. {
  177. int perms = 0;
  178. if (reader.Read())
  179. {
  180. perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
  181. }
  182. return perms;
  183. }
  184. }
  185. }
  186. }
  187. public override bool Store(XInventoryItem item)
  188. {
  189. if (!base.Store(item))
  190. return false;
  191. IncrementFolderVersion(item.parentFolderID);
  192. return true;
  193. }
  194. }
  195. public class PGSQLFolderHandler : PGSQLInventoryHandler<XInventoryFolder>
  196. {
  197. public PGSQLFolderHandler(string c, string t, string m) :
  198. base(c, t, m)
  199. {
  200. }
  201. public bool MoveFolder(string id, string newParentFolderID)
  202. {
  203. XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id });
  204. if (folders.Length == 0)
  205. return false;
  206. UUID oldParentFolderUUID = folders[0].parentFolderID;
  207. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  208. {
  209. using (NpgsqlCommand cmd = new NpgsqlCommand())
  210. {
  211. UUID foldID = UUID.Zero;
  212. UUID.TryParse(id, out foldID);
  213. UUID newPar = UUID.Zero;
  214. UUID.TryParse(newParentFolderID, out newPar);
  215. cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""folderID"" = :folderID", m_Realm);
  216. cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newPar));
  217. cmd.Parameters.Add(m_database.CreateParameter("folderID", foldID));
  218. cmd.Connection = conn;
  219. conn.Open();
  220. if (cmd.ExecuteNonQuery() == 0)
  221. return false;
  222. }
  223. }
  224. IncrementFolderVersion(oldParentFolderUUID);
  225. IncrementFolderVersion(newParentFolderID);
  226. return true;
  227. }
  228. public override bool Store(XInventoryFolder folder)
  229. {
  230. if (!base.Store(folder))
  231. return false;
  232. IncrementFolderVersion(folder.parentFolderID);
  233. return true;
  234. }
  235. }
  236. public class PGSQLInventoryHandler<T> : PGSQLGenericTableHandler<T> where T: class, new()
  237. {
  238. public PGSQLInventoryHandler(string c, string t, string m) : base(c, t, m) {}
  239. protected bool IncrementFolderVersion(UUID folderID)
  240. {
  241. return IncrementFolderVersion(folderID.ToString());
  242. }
  243. protected bool IncrementFolderVersion(string folderID)
  244. {
  245. // m_log.DebugFormat("[PGSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID);
  246. // Util.PrintCallStack();
  247. string sql = @"update inventoryfolders set version=version+1 where ""folderID"" = :folderID";
  248. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  249. {
  250. using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
  251. {
  252. UUID foldID = UUID.Zero;
  253. UUID.TryParse(folderID, out foldID);
  254. conn.Open();
  255. cmd.Parameters.Add( m_database.CreateParameter("folderID", foldID) );
  256. try
  257. {
  258. cmd.ExecuteNonQuery();
  259. }
  260. catch (Exception)
  261. {
  262. return false;
  263. }
  264. }
  265. }
  266. return true;
  267. }
  268. }
  269. }