OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
MySQLXInventoryData.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Data;
31 using System.Linq;
32 using System.Reflection;
33 using log4net;
34 using MySql.Data.MySqlClient;
35 using OpenMetaverse;
36 using OpenSim.Framework;
37 
38 namespace OpenSim.Data.MySQL
39 {
44  {
45  private MySqlFolderHandler m_Folders;
46  private MySqlItemHandler m_Items;
47 
48  public MySQLXInventoryData(string conn, string realm)
49  {
50  m_Folders = new MySqlFolderHandler(
51  conn, "inventoryfolders", "InventoryStore");
52  m_Items = new MySqlItemHandler(
53  conn, "inventoryitems", String.Empty);
54  }
55 
56  public XInventoryFolder[] GetFolders(string[] fields, string[] vals)
57  {
58  return m_Folders.Get(fields, vals);
59  }
60 
61  public XInventoryItem[] GetItems(string[] fields, string[] vals)
62  {
63  return m_Items.Get(fields, vals);
64  }
65 
66  public bool StoreFolder(XInventoryFolder folder)
67  {
68  if (folder.folderName.Length > 64)
69  folder.folderName = folder.folderName.Substring(0, 64);
70 
71  return m_Folders.Store(folder);
72  }
73 
74  public bool StoreItem(XInventoryItem item)
75  {
76  if (item.inventoryName.Length > 64)
77  item.inventoryName = item.inventoryName.Substring(0, 64);
78  if (item.inventoryDescription.Length > 128)
79  item.inventoryDescription = item.inventoryDescription.Substring(0, 128);
80 
81  return m_Items.Store(item);
82  }
83 
84  public bool DeleteFolders(string field, string val)
85  {
86  return m_Folders.Delete(field, val);
87  }
88 
89  public bool DeleteFolders(string[] fields, string[] vals)
90  {
91  return m_Folders.Delete(fields, vals);
92  }
93 
94  public bool DeleteItems(string field, string val)
95  {
96  return m_Items.Delete(field, val);
97  }
98 
99  public bool DeleteItems(string[] fields, string[] vals)
100  {
101  return m_Items.Delete(fields, vals);
102  }
103 
104  public bool MoveItem(string id, string newParent)
105  {
106  return m_Items.MoveItem(id, newParent);
107  }
108 
109  public bool MoveFolder(string id, string newParent)
110  {
111  return m_Folders.MoveFolder(id, newParent);
112  }
113 
114  public XInventoryItem[] GetActiveGestures(UUID principalID)
115  {
116  return m_Items.GetActiveGestures(principalID);
117  }
118 
119  public int GetAssetPermissions(UUID principalID, UUID assetID)
120  {
121  return m_Items.GetAssetPermissions(principalID, assetID);
122  }
123  }
124 
125  public class MySqlItemHandler : MySqlInventoryHandler<XInventoryItem>
126  {
127 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
128 
129  public MySqlItemHandler(string c, string t, string m) :
130  base(c, t, m)
131  {
132  }
133 
134  public override bool Delete(string field, string val)
135  {
136  XInventoryItem[] retrievedItems = Get(new string[] { field }, new string[] { val });
137  if (retrievedItems.Length == 0)
138  return false;
139 
140  if (!base.Delete(field, val))
141  return false;
142 
143  // Don't increment folder version here since Delete(string, string) calls Delete(string[], string[])
144 // IncrementFolderVersion(retrievedItems[0].parentFolderID);
145 
146  return true;
147  }
148 
149  public override bool Delete(string[] fields, string[] vals)
150  {
151  XInventoryItem[] retrievedItems = Get(fields, vals);
152  if (retrievedItems.Length == 0)
153  return false;
154 
155  if (!base.Delete(fields, vals))
156  return false;
157 
158  HashSet<UUID> deletedItemFolderUUIDs = new HashSet<UUID>();
159 
160  Array.ForEach<XInventoryItem>(retrievedItems, i => deletedItemFolderUUIDs.Add(i.parentFolderID));
161 
162  foreach (UUID deletedItemFolderUUID in deletedItemFolderUUIDs)
163  IncrementFolderVersion(deletedItemFolderUUID);
164 
165  return true;
166  }
167 
168  public bool MoveItem(string id, string newParent)
169  {
170  XInventoryItem[] retrievedItems = Get(new string[] { "inventoryID" }, new string[] { id });
171  if (retrievedItems.Length == 0)
172  return false;
173 
174  UUID oldParent = retrievedItems[0].parentFolderID;
175 
176  using (MySqlCommand cmd = new MySqlCommand())
177  {
178  cmd.CommandText = String.Format("update {0} set parentFolderID = ?ParentFolderID where inventoryID = ?InventoryID", m_Realm);
179  cmd.Parameters.AddWithValue("?ParentFolderID", newParent);
180  cmd.Parameters.AddWithValue("?InventoryID", id);
181 
182  if (ExecuteNonQuery(cmd) == 0)
183  return false;
184  }
185 
186  IncrementFolderVersion(oldParent);
187  IncrementFolderVersion(newParent);
188 
189  return true;
190  }
191 
192  public XInventoryItem[] GetActiveGestures(UUID principalID)
193  {
194  using (MySqlCommand cmd = new MySqlCommand())
195  {
196  cmd.CommandText = String.Format("select * from inventoryitems where avatarId = ?uuid and assetType = ?type and flags & 1", m_Realm);
197 
198  cmd.Parameters.AddWithValue("?uuid", principalID.ToString());
199  cmd.Parameters.AddWithValue("?type", (int)AssetType.Gesture);
200 
201  return DoQuery(cmd);
202  }
203  }
204 
205  public int GetAssetPermissions(UUID principalID, UUID assetID)
206  {
207  using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
208  {
209  dbcon.Open();
210 
211  using (MySqlCommand cmd = new MySqlCommand())
212  {
213  cmd.Connection = dbcon;
214 
215  cmd.CommandText = String.Format("select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions from inventoryitems where avatarID = ?PrincipalID and assetID = ?AssetID group by assetID", m_Realm);
216  cmd.Parameters.AddWithValue("?PrincipalID", principalID.ToString());
217  cmd.Parameters.AddWithValue("?AssetID", assetID.ToString());
218 
219  using (IDataReader reader = cmd.ExecuteReader())
220  {
221 
222  int perms = 0;
223 
224  if (reader.Read())
225  {
226  perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
227  }
228 
229  return perms;
230  }
231  }
232  }
233  }
234 
235  public override bool Store(XInventoryItem item)
236  {
237  if (!base.Store(item))
238  return false;
239 
240  IncrementFolderVersion(item.parentFolderID);
241 
242  return true;
243  }
244  }
245 
246  public class MySqlFolderHandler : MySqlInventoryHandler<XInventoryFolder>
247  {
248 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
249 
250  public MySqlFolderHandler(string c, string t, string m) :
251  base(c, t, m)
252  {
253  }
254 
255  public bool MoveFolder(string id, string newParentFolderID)
256  {
257  XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id });
258 
259  if (folders.Length == 0)
260  return false;
261 
262  UUID oldParentFolderUUID = folders[0].parentFolderID;
263 
264  using (MySqlCommand cmd = new MySqlCommand())
265  {
266  cmd.CommandText
267  = String.Format(
268  "update {0} set parentFolderID = ?ParentFolderID where folderID = ?folderID", m_Realm);
269  cmd.Parameters.AddWithValue("?ParentFolderID", newParentFolderID);
270  cmd.Parameters.AddWithValue("?folderID", id);
271 
272  if (ExecuteNonQuery(cmd) == 0)
273  return false;
274  }
275 
276  IncrementFolderVersion(oldParentFolderUUID);
277  IncrementFolderVersion(newParentFolderID);
278 
279  return true;
280  }
281 
282  public override bool Store(XInventoryFolder folder)
283  {
284  if (!base.Store(folder))
285  return false;
286 
287  IncrementFolderVersion(folder.parentFolderID);
288 
289  return true;
290  }
291  }
292 
293  public class MySqlInventoryHandler<T> : MySQLGenericTableHandler<T> where T: class, new()
294  {
295  public MySqlInventoryHandler(string c, string t, string m) : base(c, t, m) {}
296 
297  protected bool IncrementFolderVersion(UUID folderID)
298  {
299  return IncrementFolderVersion(folderID.ToString());
300  }
301 
302  protected bool IncrementFolderVersion(string folderID)
303  {
304 // m_log.DebugFormat("[MYSQL FOLDER HANDLER]: Incrementing version on folder {0}", folderID);
305 // Util.PrintCallStack();
306 
307  using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
308  {
309  dbcon.Open();
310 
311  using (MySqlCommand cmd = new MySqlCommand())
312  {
313  cmd.Connection = dbcon;
314 
315  cmd.CommandText = String.Format("update inventoryfolders set version=version+1 where folderID = ?folderID");
316  cmd.Parameters.AddWithValue("?folderID", folderID);
317 
318  try
319  {
320  cmd.ExecuteNonQuery();
321  }
322  catch (Exception)
323  {
324  return false;
325  }
326  cmd.Dispose();
327  }
328 
329  dbcon.Close();
330  }
331 
332  return true;
333  }
334  }
335 }
XInventoryItem[] GetItems(string[] fields, string[] vals)
override bool Store(XInventoryFolder folder)
override bool Store(XInventoryItem item)
bool DeleteItems(string field, string val)
Delete items where field == val
bool DeleteFolders(string[] fields, string[] vals)
Delete folders where field1 == val1, field2 == val2...
XInventoryItem[] GetActiveGestures(UUID principalID)
XInventoryItem[] GetActiveGestures(UUID principalID)
bool StoreFolder(XInventoryFolder folder)
MySQLXInventoryData(string conn, string realm)
bool MoveItem(string id, string newParent)
Move an item to another folder.
override bool Delete(string[] fields, string[] vals)
A MySQL Interface for the Asset Server
int GetAssetPermissions(UUID principalID, UUID assetID)
MySqlFolderHandler(string c, string t, string m)
bool DeleteItems(string[] fields, string[] vals)
Delete items where field1 == val1, field2 == val2...
bool MoveItem(string id, string newParent)
bool MoveFolder(string id, string newParent)
Move a folder to another folder.
override bool Delete(string field, string val)
bool DeleteFolders(string field, string val)
Delete folders where field == val
MySqlItemHandler(string c, string t, string m)
XInventoryFolder[] GetFolders(string[] fields, string[] vals)
MySqlInventoryHandler(string c, string t, string m)
bool MoveFolder(string id, string newParentFolderID)
int GetAssetPermissions(UUID principalID, UUID assetID)