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