1
0

PGSQLXInventoryData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1");
  151. UUID princID = UUID.Zero;
  152. UUID.TryParse(principalID, out princID);
  153. cmd.Parameters.Add(m_database.CreateParameter("uuid", principalID));
  154. cmd.Parameters.Add(m_database.CreateParameter("type", (int)AssetType.Gesture));
  155. cmd.Connection = conn;
  156. conn.Open();
  157. return DoQuery(cmd);
  158. }
  159. }
  160. }
  161. public int GetAssetPermissions(UUID principalID, UUID assetID)
  162. {
  163. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  164. {
  165. using (NpgsqlCommand cmd = new NpgsqlCommand())
  166. {
  167. /*
  168. cmd.CommandText = String.Format(@"select bit_or(""inventoryCurrentPermissions"") as ""inventoryCurrentPermissions""
  169. from inventoryitems
  170. where ""avatarID"" = :PrincipalID
  171. and ""assetID"" = :AssetID
  172. group by ""assetID"" ", m_Realm);
  173. */
  174. cmd.CommandText = String.Format(@"select bit_or(""inventoryCurrentPermissions"") as ""inventoryCurrentPermissions""
  175. from inventoryitems
  176. where ""avatarID""::uuid = :PrincipalID
  177. and ""assetID""::uuid = :AssetID
  178. group by ""assetID"" ");
  179. cmd.Parameters.Add(m_database.CreateParameter("PrincipalID", principalID));
  180. cmd.Parameters.Add(m_database.CreateParameter("AssetID", assetID));
  181. cmd.Connection = conn;
  182. conn.Open();
  183. using (NpgsqlDataReader reader = cmd.ExecuteReader())
  184. {
  185. int perms = 0;
  186. if (reader.Read())
  187. {
  188. perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
  189. }
  190. return perms;
  191. }
  192. }
  193. }
  194. }
  195. public override bool Store(XInventoryItem item)
  196. {
  197. if (!base.Store(item))
  198. return false;
  199. IncrementFolderVersion(item.parentFolderID);
  200. return true;
  201. }
  202. }
  203. public class PGSQLFolderHandler : PGSQLInventoryHandler<XInventoryFolder>
  204. {
  205. public PGSQLFolderHandler(string c, string t, string m) :
  206. base(c, t, m)
  207. {
  208. }
  209. public bool MoveFolder(string id, string newParentFolderID)
  210. {
  211. XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id });
  212. if (folders.Length == 0)
  213. return false;
  214. UUID oldParentFolderUUID = folders[0].parentFolderID;
  215. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  216. {
  217. using (NpgsqlCommand cmd = new NpgsqlCommand())
  218. {
  219. UUID foldID = UUID.Zero;
  220. UUID.TryParse(id, out foldID);
  221. UUID newPar = UUID.Zero;
  222. UUID.TryParse(newParentFolderID, out newPar);
  223. cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""folderID"" = :folderID", m_Realm);
  224. cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newPar));
  225. cmd.Parameters.Add(m_database.CreateParameter("folderID", foldID));
  226. cmd.Connection = conn;
  227. conn.Open();
  228. if (cmd.ExecuteNonQuery() == 0)
  229. return false;
  230. }
  231. }
  232. IncrementFolderVersion(oldParentFolderUUID);
  233. IncrementFolderVersion(newParentFolderID);
  234. return true;
  235. }
  236. public override bool Store(XInventoryFolder folder)
  237. {
  238. if (!base.Store(folder))
  239. return false;
  240. IncrementFolderVersion(folder.parentFolderID);
  241. return true;
  242. }
  243. }
  244. public class PGSQLInventoryHandler<T> : PGSQLGenericTableHandler<T> where T: class, new()
  245. {
  246. public PGSQLInventoryHandler(string c, string t, string m) : base(c, t, m) {}
  247. protected bool IncrementFolderVersion(UUID folderID)
  248. {
  249. return IncrementFolderVersion(folderID.ToString());
  250. }
  251. protected bool IncrementFolderVersion(string folderID)
  252. {
  253. // m_log.DebugFormat("[PGSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID);
  254. // Util.PrintCallStack();
  255. string sql = @"update inventoryfolders set version=version+1 where ""folderID"" = :folderID";
  256. using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
  257. {
  258. using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
  259. {
  260. UUID foldID = UUID.Zero;
  261. UUID.TryParse(folderID, out foldID);
  262. conn.Open();
  263. cmd.Parameters.Add( m_database.CreateParameter("folderID", foldID) );
  264. try
  265. {
  266. cmd.ExecuteNonQuery();
  267. }
  268. catch (Exception)
  269. {
  270. return false;
  271. }
  272. }
  273. }
  274. return true;
  275. }
  276. }
  277. }