aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/PGSQL/PGSQLXInventoryData.cs')
-rw-r--r--OpenSim/Data/PGSQL/PGSQLXInventoryData.cs339
1 files changed, 339 insertions, 0 deletions
diff --git a/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs b/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs
new file mode 100644
index 0000000..4c10ac9
--- /dev/null
+++ b/OpenSim/Data/PGSQL/PGSQLXInventoryData.cs
@@ -0,0 +1,339 @@
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
28using System;
29using System.Collections;
30using System.Collections.Generic;
31using System.Data;
32using OpenMetaverse;
33using OpenSim.Framework;
34using System.Reflection;
35using System.Text;
36using log4net;
37using Npgsql;
38using NpgsqlTypes;
39
40namespace OpenSim.Data.PGSQL
41{
42 public class PGSQLXInventoryData : IXInventoryData
43 {
44// private static readonly ILog m_log = LogManager.GetLogger(
45// MethodBase.GetCurrentMethod().DeclaringType);
46
47 private PGSQLFolderHandler m_Folders;
48 private PGSQLItemHandler m_Items;
49
50 public PGSQLXInventoryData(string conn, string realm)
51 {
52 m_Folders = new PGSQLFolderHandler(
53 conn, "inventoryfolders", "InventoryStore");
54 m_Items = new PGSQLItemHandler(
55 conn, "inventoryitems", String.Empty);
56 }
57
58 public static UUID str2UUID(string strUUID)
59 {
60 UUID newUUID = UUID.Zero;
61
62 UUID.TryParse(strUUID, out newUUID);
63
64 return newUUID;
65 }
66
67 public XInventoryFolder[] GetFolders(string[] fields, string[] vals)
68 {
69 return m_Folders.Get(fields, vals);
70 }
71
72 public XInventoryItem[] GetItems(string[] fields, string[] vals)
73 {
74 return m_Items.Get(fields, vals);
75 }
76
77 public bool StoreFolder(XInventoryFolder folder)
78 {
79 if (folder.folderName.Length > 64)
80 folder.folderName = folder.folderName.Substring(0, 64);
81 return m_Folders.Store(folder);
82 }
83
84 public bool StoreItem(XInventoryItem item)
85 {
86 if (item.inventoryName.Length > 64)
87 item.inventoryName = item.inventoryName.Substring(0, 64);
88 if (item.inventoryDescription.Length > 128)
89 item.inventoryDescription = item.inventoryDescription.Substring(0, 128);
90
91 return m_Items.Store(item);
92 }
93
94 public bool DeleteFolders(string field, string val)
95 {
96 return m_Folders.Delete(field, val);
97 }
98
99 public bool DeleteFolders(string[] fields, string[] vals)
100 {
101 return m_Folders.Delete(fields, vals);
102 }
103
104 public bool DeleteItems(string field, string val)
105 {
106 return m_Items.Delete(field, val);
107 }
108
109 public bool DeleteItems(string[] fields, string[] vals)
110 {
111 return m_Items.Delete(fields, vals);
112 }
113
114 public bool MoveItem(string id, string newParent)
115 {
116 return m_Items.MoveItem(id, newParent);
117 }
118
119 public bool MoveFolder(string id, string newParent)
120 {
121 return m_Folders.MoveFolder(id, newParent);
122 }
123
124 public XInventoryItem[] GetActiveGestures(UUID principalID)
125 {
126 return m_Items.GetActiveGestures(principalID.ToString());
127 }
128
129 public int GetAssetPermissions(UUID principalID, UUID assetID)
130 {
131 return m_Items.GetAssetPermissions(principalID, assetID);
132 }
133 }
134
135 public class PGSQLItemHandler : PGSQLInventoryHandler<XInventoryItem>
136 {
137 public PGSQLItemHandler(string c, string t, string m) :
138 base(c, t, m)
139 {
140 }
141
142 public bool MoveItem(string id, string newParent)
143 {
144 XInventoryItem[] retrievedItems = Get(new string[] { "inventoryID" }, new string[] { id });
145 if (retrievedItems.Length == 0)
146 return false;
147
148 UUID oldParent = retrievedItems[0].parentFolderID;
149
150 using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
151 {
152 using (NpgsqlCommand cmd = new NpgsqlCommand())
153 {
154 cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""inventoryID"" = :InventoryID", m_Realm);
155 cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newParent));
156 cmd.Parameters.Add(m_database.CreateParameter("InventoryID", id ));
157 cmd.Connection = conn;
158 conn.Open();
159
160 if (cmd.ExecuteNonQuery() == 0)
161 return false;
162 }
163 }
164
165 IncrementFolderVersion(oldParent);
166 IncrementFolderVersion(newParent);
167
168 return true;
169 }
170
171 public XInventoryItem[] GetActiveGestures(string principalID)
172 {
173 using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
174 {
175 using (NpgsqlCommand cmd = new NpgsqlCommand())
176 {
177// cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1", m_Realm);
178
179 cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1");
180
181 UUID princID = UUID.Zero;
182 UUID.TryParse(principalID, out princID);
183
184 cmd.Parameters.Add(m_database.CreateParameter("uuid", principalID));
185 cmd.Parameters.Add(m_database.CreateParameter("type", (int)AssetType.Gesture));
186 cmd.Connection = conn;
187 conn.Open();
188 return DoQuery(cmd);
189 }
190 }
191 }
192
193 public int GetAssetPermissions(UUID principalID, UUID assetID)
194 {
195 using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
196 {
197 using (NpgsqlCommand cmd = new NpgsqlCommand())
198 {
199/*
200 cmd.CommandText = String.Format(@"select bit_or(""inventoryCurrentPermissions"") as ""inventoryCurrentPermissions""
201 from inventoryitems
202 where ""avatarID"" = :PrincipalID
203 and ""assetID"" = :AssetID
204 group by ""assetID"" ", m_Realm);
205*/
206 cmd.CommandText = String.Format(@"select bit_or(""inventoryCurrentPermissions"") as ""inventoryCurrentPermissions""
207 from inventoryitems
208 where ""avatarID""::uuid = :PrincipalID
209 and ""assetID""::uuid = :AssetID
210 group by ""assetID"" ");
211
212 cmd.Parameters.Add(m_database.CreateParameter("PrincipalID", principalID));
213 cmd.Parameters.Add(m_database.CreateParameter("AssetID", assetID));
214 cmd.Connection = conn;
215 conn.Open();
216 using (NpgsqlDataReader reader = cmd.ExecuteReader())
217 {
218
219 int perms = 0;
220
221 if (reader.Read())
222 {
223 perms = Convert.ToInt32(reader["inventoryCurrentPermissions"]);
224 }
225
226 return perms;
227 }
228
229 }
230 }
231 }
232
233 public override bool Store(XInventoryItem item)
234 {
235 if (!base.Store(item))
236 return false;
237
238 IncrementFolderVersion(item.parentFolderID);
239
240 return true;
241 }
242 }
243
244 public class PGSQLFolderHandler : PGSQLInventoryHandler<XInventoryFolder>
245 {
246 public PGSQLFolderHandler(string c, string t, string m) :
247 base(c, t, m)
248 {
249 }
250
251 public bool MoveFolder(string id, string newParentFolderID)
252 {
253 XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id });
254
255 if (folders.Length == 0)
256 return false;
257
258 UUID oldParentFolderUUID = folders[0].parentFolderID;
259
260 using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
261 {
262 using (NpgsqlCommand cmd = new NpgsqlCommand())
263 {
264 UUID foldID = UUID.Zero;
265 UUID.TryParse(id, out foldID);
266
267 UUID newPar = UUID.Zero;
268 UUID.TryParse(newParentFolderID, out newPar);
269
270 cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""folderID"" = :folderID", m_Realm);
271 cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newPar));
272 cmd.Parameters.Add(m_database.CreateParameter("folderID", foldID));
273 cmd.Connection = conn;
274 conn.Open();
275
276 if (cmd.ExecuteNonQuery() == 0)
277 return false;
278 }
279 }
280
281 IncrementFolderVersion(oldParentFolderUUID);
282 IncrementFolderVersion(newParentFolderID);
283
284 return true;
285 }
286
287 public override bool Store(XInventoryFolder folder)
288 {
289 if (!base.Store(folder))
290 return false;
291
292 IncrementFolderVersion(folder.parentFolderID);
293
294 return true;
295 }
296 }
297
298 public class PGSQLInventoryHandler<T> : PGSQLGenericTableHandler<T> where T: class, new()
299 {
300 public PGSQLInventoryHandler(string c, string t, string m) : base(c, t, m) {}
301
302 protected bool IncrementFolderVersion(UUID folderID)
303 {
304 return IncrementFolderVersion(folderID.ToString());
305 }
306
307 protected bool IncrementFolderVersion(string folderID)
308 {
309// m_log.DebugFormat("[PGSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID);
310// Util.PrintCallStack();
311
312 string sql = @"update inventoryfolders set version=version+1 where ""folderID"" = :folderID";
313
314 using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
315 {
316 using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
317 {
318 UUID foldID = UUID.Zero;
319 UUID.TryParse(folderID, out foldID);
320
321 conn.Open();
322
323 cmd.Parameters.Add( m_database.CreateParameter("folderID", foldID) );
324
325 try
326 {
327 cmd.ExecuteNonQuery();
328 }
329 catch (Exception)
330 {
331 return false;
332 }
333 }
334 }
335
336 return true;
337 }
338 }
339}