1
0

PGSQLXInventoryData.cs 12 KB

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