1
0

PGSQLXInventoryData.cs 12 KB

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