diff options
Diffstat (limited to 'OpenSim/Framework/Data.MSSQL/MSSQLInventoryData.cs')
-rw-r--r-- | OpenSim/Framework/Data.MSSQL/MSSQLInventoryData.cs | 697 |
1 files changed, 697 insertions, 0 deletions
diff --git a/OpenSim/Framework/Data.MSSQL/MSSQLInventoryData.cs b/OpenSim/Framework/Data.MSSQL/MSSQLInventoryData.cs new file mode 100644 index 0000000..364e0f2 --- /dev/null +++ b/OpenSim/Framework/Data.MSSQL/MSSQLInventoryData.cs | |||
@@ -0,0 +1,697 @@ | |||
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 OpenSim 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.IO; | ||
30 | using System.Data; | ||
31 | using System.Data.SqlClient; | ||
32 | using System.Collections.Generic; | ||
33 | using libsecondlife; | ||
34 | using OpenSim.Framework.Console; | ||
35 | |||
36 | namespace OpenSim.Framework.Data.MSSQL | ||
37 | { | ||
38 | /// <summary> | ||
39 | /// A MySQL interface for the inventory server | ||
40 | /// </summary> | ||
41 | public class MSSQLInventoryData : IInventoryData | ||
42 | { | ||
43 | /// <summary> | ||
44 | /// The database manager | ||
45 | /// </summary> | ||
46 | private MSSQLManager database; | ||
47 | |||
48 | /// <summary> | ||
49 | /// Loads and initialises this database plugin | ||
50 | /// </summary> | ||
51 | public void Initialise() | ||
52 | { | ||
53 | IniFile GridDataMySqlFile = new IniFile("mssql_connection.ini"); | ||
54 | string settingDataSource = GridDataMySqlFile.ParseFileReadValue("data_source"); | ||
55 | string settingInitialCatalog = GridDataMySqlFile.ParseFileReadValue("initial_catalog"); | ||
56 | string settingPersistSecurityInfo = GridDataMySqlFile.ParseFileReadValue("persist_security_info"); | ||
57 | string settingUserId = GridDataMySqlFile.ParseFileReadValue("user_id"); | ||
58 | string settingPassword = GridDataMySqlFile.ParseFileReadValue("password"); | ||
59 | |||
60 | database = new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId, settingPassword); | ||
61 | TestTables(); | ||
62 | } | ||
63 | |||
64 | #region Test and initialization code | ||
65 | |||
66 | private void UpgradeFoldersTable(string tableName) | ||
67 | { | ||
68 | // null as the version, indicates that the table didn't exist | ||
69 | if (tableName == null) | ||
70 | { | ||
71 | database.ExecuteResourceSql("CreateFoldersTable.sql"); | ||
72 | //database.ExecuteResourceSql("UpgradeFoldersTableToVersion2.sql"); | ||
73 | return; | ||
74 | } | ||
75 | |||
76 | } | ||
77 | |||
78 | private void UpgradeItemsTable(string tableName) | ||
79 | { | ||
80 | // null as the version, indicates that the table didn't exist | ||
81 | if (tableName == null) | ||
82 | { | ||
83 | database.ExecuteResourceSql("CreateItemsTable.sql"); | ||
84 | //database.ExecuteResourceSql("UpgradeItemsTableToVersion2.sql"); | ||
85 | return; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | private void TestTables() | ||
90 | { | ||
91 | |||
92 | Dictionary<string, string> tableList = new Dictionary<string, string>(); | ||
93 | |||
94 | tableList["inventoryfolders"] = null; | ||
95 | tableList["inventoryitems"] = null; | ||
96 | |||
97 | database.GetTableVersion(tableList); | ||
98 | |||
99 | UpgradeFoldersTable(tableList["inventoryfolders"]); | ||
100 | UpgradeItemsTable(tableList["inventoryitems"]); | ||
101 | } | ||
102 | #endregion | ||
103 | |||
104 | /// <summary> | ||
105 | /// The name of this DB provider | ||
106 | /// </summary> | ||
107 | /// <returns>Name of DB provider</returns> | ||
108 | public string getName() | ||
109 | { | ||
110 | return "MSSQL Inventory Data Interface"; | ||
111 | } | ||
112 | |||
113 | /// <summary> | ||
114 | /// Closes this DB provider | ||
115 | /// </summary> | ||
116 | public void Close() | ||
117 | { | ||
118 | // Do nothing. | ||
119 | } | ||
120 | |||
121 | /// <summary> | ||
122 | /// Returns the version of this DB provider | ||
123 | /// </summary> | ||
124 | /// <returns>A string containing the DB provider</returns> | ||
125 | public string getVersion() | ||
126 | { | ||
127 | return database.getVersion(); | ||
128 | } | ||
129 | |||
130 | /// <summary> | ||
131 | /// Returns a list of items in a specified folder | ||
132 | /// </summary> | ||
133 | /// <param name="folderID">The folder to search</param> | ||
134 | /// <returns>A list containing inventory items</returns> | ||
135 | public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID) | ||
136 | { | ||
137 | try | ||
138 | { | ||
139 | lock (database) | ||
140 | { | ||
141 | List<InventoryItemBase> items = new List<InventoryItemBase>(); | ||
142 | |||
143 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
144 | param["parentFolderID"] = folderID.ToStringHyphenated(); | ||
145 | |||
146 | IDbCommand result = database.Query("SELECT * FROM inventoryitems WHERE parentFolderID = @parentFolderID", param); | ||
147 | IDataReader reader = result.ExecuteReader(); | ||
148 | |||
149 | while(reader.Read()) | ||
150 | items.Add(readInventoryItem(reader)); | ||
151 | |||
152 | reader.Close(); | ||
153 | result.Dispose(); | ||
154 | |||
155 | return items; | ||
156 | } | ||
157 | } | ||
158 | catch (Exception e) | ||
159 | { | ||
160 | database.Reconnect(); | ||
161 | MainLog.Instance.Error(e.ToString()); | ||
162 | return null; | ||
163 | } | ||
164 | } | ||
165 | |||
166 | /// <summary> | ||
167 | /// Returns a list of the root folders within a users inventory | ||
168 | /// </summary> | ||
169 | /// <param name="user">The user whos inventory is to be searched</param> | ||
170 | /// <returns>A list of folder objects</returns> | ||
171 | public List<InventoryFolderBase> getUserRootFolders(LLUUID user) | ||
172 | { | ||
173 | try | ||
174 | { | ||
175 | lock (database) | ||
176 | { | ||
177 | |||
178 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
179 | param["uuid"] = user.ToStringHyphenated(); | ||
180 | param["zero"] = LLUUID.Zero.ToStringHyphenated(); | ||
181 | |||
182 | IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = @zero AND agentID = @uuid", param); | ||
183 | IDataReader reader = result.ExecuteReader(); | ||
184 | |||
185 | List<InventoryFolderBase> items = new List<InventoryFolderBase>(); | ||
186 | while(reader.Read()) | ||
187 | items.Add(readInventoryFolder(reader)); | ||
188 | |||
189 | |||
190 | reader.Close(); | ||
191 | result.Dispose(); | ||
192 | |||
193 | return items; | ||
194 | } | ||
195 | } | ||
196 | catch (Exception e) | ||
197 | { | ||
198 | database.Reconnect(); | ||
199 | MainLog.Instance.Error(e.ToString()); | ||
200 | return null; | ||
201 | } | ||
202 | } | ||
203 | |||
204 | /// <summary> | ||
205 | /// Returns the users inventory root folder. | ||
206 | /// </summary> | ||
207 | /// <param name="user"></param> | ||
208 | /// <returns></returns> | ||
209 | public InventoryFolderBase getUserRootFolder(LLUUID user) | ||
210 | { | ||
211 | try | ||
212 | { | ||
213 | lock (database) | ||
214 | { | ||
215 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
216 | param["uuid"] = user.ToStringHyphenated(); | ||
217 | param["zero"] = LLUUID.Zero.ToStringHyphenated(); | ||
218 | |||
219 | IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = @zero AND agentID = @uuid", param); | ||
220 | IDataReader reader = result.ExecuteReader(); | ||
221 | |||
222 | List<InventoryFolderBase> items = new List<InventoryFolderBase>(); | ||
223 | while(reader.Read()) | ||
224 | items.Add(readInventoryFolder(reader)); | ||
225 | |||
226 | InventoryFolderBase rootFolder = null; | ||
227 | if (items.Count > 0) { | ||
228 | rootFolder = items[0]; //should only be one folder with parent set to zero (the root one). | ||
229 | } | ||
230 | |||
231 | reader.Close(); | ||
232 | result.Dispose(); | ||
233 | |||
234 | return rootFolder; | ||
235 | } | ||
236 | } | ||
237 | catch (Exception e) | ||
238 | { | ||
239 | database.Reconnect(); | ||
240 | MainLog.Instance.Error(e.ToString()); | ||
241 | return null; | ||
242 | } | ||
243 | } | ||
244 | |||
245 | /// <summary> | ||
246 | /// Returns a list of folders in a users inventory contained within the specified folder | ||
247 | /// </summary> | ||
248 | /// <param name="parentID">The folder to search</param> | ||
249 | /// <returns>A list of inventory folders</returns> | ||
250 | public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID) | ||
251 | { | ||
252 | try | ||
253 | { | ||
254 | lock (database) | ||
255 | { | ||
256 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
257 | param["parentFolderID"] = parentID.ToStringHyphenated(); | ||
258 | |||
259 | |||
260 | IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = @parentFolderID", param); | ||
261 | IDataReader reader = result.ExecuteReader(); | ||
262 | |||
263 | List<InventoryFolderBase> items = new List<InventoryFolderBase>(); | ||
264 | |||
265 | while(reader.Read()) | ||
266 | items.Add(readInventoryFolder(reader)); | ||
267 | |||
268 | reader.Close(); | ||
269 | result.Dispose(); | ||
270 | |||
271 | return items; | ||
272 | } | ||
273 | } | ||
274 | catch (Exception e) | ||
275 | { | ||
276 | database.Reconnect(); | ||
277 | MainLog.Instance.Error(e.ToString()); | ||
278 | return null; | ||
279 | } | ||
280 | } | ||
281 | |||
282 | /// <summary> | ||
283 | /// Reads a one item from an SQL result | ||
284 | /// </summary> | ||
285 | /// <param name="reader">The SQL Result</param> | ||
286 | /// <returns>the item read</returns> | ||
287 | public InventoryItemBase readInventoryItem(IDataReader reader) | ||
288 | { | ||
289 | try | ||
290 | { | ||
291 | InventoryItemBase item = new InventoryItemBase(); | ||
292 | |||
293 | item.inventoryID = new LLUUID((string)reader["inventoryID"]); | ||
294 | item.assetID = new LLUUID((string)reader["assetID"]); | ||
295 | item.assetType = (int)reader["assetType"]; | ||
296 | item.parentFolderID = new LLUUID((string)reader["parentFolderID"]); | ||
297 | item.avatarID = new LLUUID((string)reader["avatarID"]); | ||
298 | item.inventoryName = (string)reader["inventoryName"]; | ||
299 | item.inventoryDescription = (string)reader["inventoryDescription"]; | ||
300 | item.inventoryNextPermissions = Convert.ToUInt32(reader["inventoryNextPermissions"]); | ||
301 | item.inventoryCurrentPermissions = Convert.ToUInt32(reader["inventoryCurrentPermissions"]); | ||
302 | item.invType = (int)reader["invType"]; | ||
303 | item.creatorsID = new LLUUID((string)reader["creatorID"]); | ||
304 | item.inventoryBasePermissions = Convert.ToUInt32(reader["inventoryBasePermissions"]); | ||
305 | item.inventoryEveryOnePermissions = Convert.ToUInt32(reader["inventoryEveryOnePermissions"]); | ||
306 | return item; | ||
307 | } | ||
308 | catch (SqlException e) | ||
309 | { | ||
310 | MainLog.Instance.Error(e.ToString()); | ||
311 | } | ||
312 | |||
313 | return null; | ||
314 | } | ||
315 | |||
316 | /// <summary> | ||
317 | /// Returns a specified inventory item | ||
318 | /// </summary> | ||
319 | /// <param name="item">The item to return</param> | ||
320 | /// <returns>An inventory item</returns> | ||
321 | public InventoryItemBase getInventoryItem(LLUUID itemID) | ||
322 | { | ||
323 | |||
324 | try | ||
325 | { | ||
326 | lock (database) | ||
327 | { | ||
328 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
329 | param["inventoryID"] = itemID.ToStringHyphenated(); | ||
330 | |||
331 | IDbCommand result = database.Query("SELECT * FROM inventoryitems WHERE inventoryID = @inventoryID", param); | ||
332 | IDataReader reader = result.ExecuteReader(); | ||
333 | |||
334 | InventoryItemBase item = null; | ||
335 | if(reader.Read()) | ||
336 | item = readInventoryItem(reader); | ||
337 | |||
338 | reader.Close(); | ||
339 | result.Dispose(); | ||
340 | |||
341 | return item; | ||
342 | } | ||
343 | } | ||
344 | catch (Exception e) | ||
345 | { | ||
346 | database.Reconnect(); | ||
347 | MainLog.Instance.Error(e.ToString()); | ||
348 | } | ||
349 | return null; | ||
350 | } | ||
351 | |||
352 | /// <summary> | ||
353 | /// Reads a list of inventory folders returned by a query. | ||
354 | /// </summary> | ||
355 | /// <param name="reader">A MySQL Data Reader</param> | ||
356 | /// <returns>A List containing inventory folders</returns> | ||
357 | protected InventoryFolderBase readInventoryFolder(IDataReader reader) | ||
358 | { | ||
359 | try | ||
360 | { | ||
361 | InventoryFolderBase folder = new InventoryFolderBase(); | ||
362 | folder.agentID = new LLUUID((string)reader["agentID"]); | ||
363 | folder.parentID = new LLUUID((string)reader["parentFolderID"]); | ||
364 | folder.folderID = new LLUUID((string)reader["folderID"]); | ||
365 | folder.name = (string)reader["folderName"]; | ||
366 | folder.type = (short)reader["type"]; | ||
367 | folder.version = (ushort)((int)reader["version"]); | ||
368 | return folder; | ||
369 | } | ||
370 | catch (Exception e) | ||
371 | { | ||
372 | MainLog.Instance.Error(e.ToString()); | ||
373 | } | ||
374 | |||
375 | return null; | ||
376 | } | ||
377 | |||
378 | |||
379 | /// <summary> | ||
380 | /// Returns a specified inventory folder | ||
381 | /// </summary> | ||
382 | /// <param name="folder">The folder to return</param> | ||
383 | /// <returns>A folder class</returns> | ||
384 | public InventoryFolderBase getInventoryFolder(LLUUID folderID) | ||
385 | { | ||
386 | try | ||
387 | { | ||
388 | lock (database) | ||
389 | { | ||
390 | Dictionary<string, string> param = new Dictionary<string,string>(); | ||
391 | param["uuid"] = folderID.ToStringHyphenated(); | ||
392 | |||
393 | IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE folderID = @uuid", param); | ||
394 | IDataReader reader = result.ExecuteReader(); | ||
395 | |||
396 | reader.Read(); | ||
397 | InventoryFolderBase folder = readInventoryFolder(reader); | ||
398 | reader.Close(); | ||
399 | result.Dispose(); | ||
400 | |||
401 | return folder; | ||
402 | } | ||
403 | } | ||
404 | catch (Exception e) | ||
405 | { | ||
406 | database.Reconnect(); | ||
407 | MainLog.Instance.Error(e.ToString()); | ||
408 | return null; | ||
409 | } | ||
410 | } | ||
411 | |||
412 | /// <summary> | ||
413 | /// Adds a specified item to the database | ||
414 | /// </summary> | ||
415 | /// <param name="item">The inventory item</param> | ||
416 | public void addInventoryItem(InventoryItemBase item) | ||
417 | { | ||
418 | string sql = "INSERT INTO inventoryitems"; | ||
419 | sql += "([inventoryID], [assetID], [assetType], [parentFolderID], [avatarID], [inventoryName], [inventoryDescription], [inventoryNextPermissions], [inventoryCurrentPermissions], [invType], [creatorID], [inventoryBasePermissions], [inventoryEveryOnePermissions]) VALUES "; | ||
420 | sql += "(@inventoryID, @assetID, @assetType, @parentFolderID, @avatarID, @inventoryName, @inventoryDescription, @inventoryNextPermissions, @inventoryCurrentPermissions, @invType, @creatorID, @inventoryBasePermissions, @inventoryEveryOnePermissions);"; | ||
421 | |||
422 | try | ||
423 | { | ||
424 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
425 | param["inventoryID"] = item.inventoryID.ToStringHyphenated(); | ||
426 | param["assetID"] = item.assetID.ToStringHyphenated(); | ||
427 | param["assetType"] = item.assetType.ToString(); | ||
428 | param["parentFolderID"] = item.parentFolderID.ToStringHyphenated(); | ||
429 | param["avatarID"] = item.avatarID.ToStringHyphenated(); | ||
430 | param["inventoryName"] = item.inventoryName; | ||
431 | param["inventoryDescription"] = item.inventoryDescription; | ||
432 | param["inventoryNextPermissions"] = item.inventoryNextPermissions.ToString(); | ||
433 | param["inventoryCurrentPermissions"] = item.inventoryCurrentPermissions.ToString(); | ||
434 | param["invType"] = Convert.ToString(item.invType); | ||
435 | param["creatorID"] = item.creatorsID.ToStringHyphenated(); | ||
436 | param["inventoryBasePermissions"] = Convert.ToString(item.inventoryBasePermissions); | ||
437 | param["inventoryEveryOnePermissions"] = Convert.ToString(item.inventoryEveryOnePermissions); | ||
438 | |||
439 | IDbCommand result = database.Query(sql, param); | ||
440 | result.ExecuteNonQuery(); | ||
441 | result.Dispose(); | ||
442 | |||
443 | } | ||
444 | catch (SqlException e) | ||
445 | { | ||
446 | MainLog.Instance.Error(e.ToString()); | ||
447 | } | ||
448 | } | ||
449 | |||
450 | /// <summary> | ||
451 | /// Updates the specified inventory item | ||
452 | /// </summary> | ||
453 | /// <param name="item">Inventory item to update</param> | ||
454 | public void updateInventoryItem(InventoryItemBase item) | ||
455 | { | ||
456 | SqlCommand command = new SqlCommand("UPDATE inventoryitems set inventoryID = @inventoryID, " + | ||
457 | "assetID = @assetID, " + | ||
458 | "assetType = @assetType" + | ||
459 | "parentFolderID = @parentFolderID" + | ||
460 | "avatarID = @avatarID" + | ||
461 | "inventoryName = @inventoryName"+ | ||
462 | "inventoryDescription = @inventoryDescription" + | ||
463 | "inventoryNextPermissions = @inventoryNextPermissions" + | ||
464 | "inventoryCurrentPermissions = @inventoryCurrentPermissions" + | ||
465 | "invType = @invType" + | ||
466 | "creatorID = @creatorID" + | ||
467 | "inventoryBasePermissions = @inventoryBasePermissions" + | ||
468 | "inventoryEveryOnePermissions = @inventoryEveryOnePermissions) where " + | ||
469 | "invenoryID = @keyInventoryID;", database.getConnection()); | ||
470 | SqlParameter param1 = new SqlParameter("@inventoryID", item.inventoryID.ToStringHyphenated()); | ||
471 | SqlParameter param2 = new SqlParameter("@assetID", item.assetID); | ||
472 | SqlParameter param3 = new SqlParameter("@assetType", item.assetType); | ||
473 | SqlParameter param4 = new SqlParameter("@parentFolderID", item.parentFolderID); | ||
474 | SqlParameter param5 = new SqlParameter("@avatarID", item.avatarID); | ||
475 | SqlParameter param6 = new SqlParameter("@inventoryName", item.inventoryName); | ||
476 | SqlParameter param7 = new SqlParameter("@inventoryDescription", item.inventoryDescription); | ||
477 | SqlParameter param8 = new SqlParameter("@inventoryNextPermissions", item.inventoryNextPermissions); | ||
478 | SqlParameter param9 = new SqlParameter("@inventoryCurrentPermissions", item.inventoryCurrentPermissions); | ||
479 | SqlParameter param10 = new SqlParameter("@invType", item.invType); | ||
480 | SqlParameter param11 = new SqlParameter("@creatorID", item.creatorsID); | ||
481 | SqlParameter param12 = new SqlParameter("@inventoryBasePermissions", item.inventoryBasePermissions); | ||
482 | SqlParameter param13 = new SqlParameter("@inventoryEveryOnePermissions", item.inventoryEveryOnePermissions); | ||
483 | SqlParameter param14 = new SqlParameter("@keyInventoryID", item.inventoryID.ToStringHyphenated()); | ||
484 | command.Parameters.Add(param1); | ||
485 | command.Parameters.Add(param2); | ||
486 | command.Parameters.Add(param3); | ||
487 | command.Parameters.Add(param4); | ||
488 | command.Parameters.Add(param5); | ||
489 | command.Parameters.Add(param6); | ||
490 | command.Parameters.Add(param7); | ||
491 | command.Parameters.Add(param8); | ||
492 | command.Parameters.Add(param9); | ||
493 | command.Parameters.Add(param10); | ||
494 | command.Parameters.Add(param11); | ||
495 | command.Parameters.Add(param12); | ||
496 | command.Parameters.Add(param13); | ||
497 | command.Parameters.Add(param14); | ||
498 | |||
499 | try | ||
500 | { | ||
501 | command.ExecuteNonQuery(); | ||
502 | } | ||
503 | catch (Exception e) | ||
504 | { | ||
505 | MainLog.Instance.Error(e.ToString()); | ||
506 | } | ||
507 | |||
508 | } | ||
509 | |||
510 | /// <summary> | ||
511 | /// | ||
512 | /// </summary> | ||
513 | /// <param name="item"></param> | ||
514 | public void deleteInventoryItem(LLUUID itemID) | ||
515 | { | ||
516 | try | ||
517 | { | ||
518 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
519 | param["uuid"] = itemID.ToStringHyphenated(); | ||
520 | |||
521 | IDbCommand cmd = database.Query("DELETE FROM inventoryitems WHERE inventoryID=@uuid", param); | ||
522 | cmd.ExecuteNonQuery(); | ||
523 | cmd.Dispose(); | ||
524 | |||
525 | |||
526 | } | ||
527 | catch (SqlException e) | ||
528 | { | ||
529 | database.Reconnect(); | ||
530 | MainLog.Instance.Error(e.ToString()); | ||
531 | } | ||
532 | } | ||
533 | |||
534 | /// <summary> | ||
535 | /// Creates a new inventory folder | ||
536 | /// </summary> | ||
537 | /// <param name="folder">Folder to create</param> | ||
538 | public void addInventoryFolder(InventoryFolderBase folder) | ||
539 | { | ||
540 | string sql = "INSERT INTO inventoryfolders ([folderID], [agentID], [parentFolderID], [folderName], [type], [version]) VALUES "; | ||
541 | sql += "(@folderID, @agentID, @parentFolderID, @folderName, @type, @version);"; | ||
542 | |||
543 | |||
544 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
545 | param["folderID"] = folder.folderID.ToStringHyphenated(); | ||
546 | param["agentID"] = folder.agentID.ToStringHyphenated(); | ||
547 | param["parentFolderID"] = folder.parentID.ToStringHyphenated(); | ||
548 | param["folderName"] = folder.name; | ||
549 | param["type"] = Convert.ToString(folder.type); | ||
550 | param["version"] = Convert.ToString(folder.version); | ||
551 | |||
552 | try | ||
553 | { | ||
554 | IDbCommand result = database.Query(sql, param); | ||
555 | result.ExecuteNonQuery(); | ||
556 | result.Dispose(); | ||
557 | } | ||
558 | catch (Exception e) | ||
559 | { | ||
560 | MainLog.Instance.Error(e.ToString()); | ||
561 | } | ||
562 | } | ||
563 | |||
564 | /// <summary> | ||
565 | /// Updates an inventory folder | ||
566 | /// </summary> | ||
567 | /// <param name="folder">Folder to update</param> | ||
568 | |||
569 | public void updateInventoryFolder(InventoryFolderBase folder) | ||
570 | { | ||
571 | SqlCommand command = new SqlCommand("UPDATE inventoryfolders set folderID = @folderID, " + | ||
572 | "agentID = @agentID, " + | ||
573 | "parentFolderID = @parentFolderID," + | ||
574 | "folderName = @folderName," + | ||
575 | "type = @type," + | ||
576 | "version = @version where " + | ||
577 | "folderID = @keyFolderID;", database.getConnection()); | ||
578 | SqlParameter param1 = new SqlParameter("@folderID", folder.folderID.ToStringHyphenated()); | ||
579 | SqlParameter param2 = new SqlParameter("@agentID", folder.agentID.ToStringHyphenated()); | ||
580 | SqlParameter param3 = new SqlParameter("@parentFolderID", folder.parentID.ToStringHyphenated()); | ||
581 | SqlParameter param4 = new SqlParameter("@folderName", folder.name); | ||
582 | SqlParameter param5 = new SqlParameter("@type", folder.type); | ||
583 | SqlParameter param6 = new SqlParameter("@version", folder.version); | ||
584 | SqlParameter param7 = new SqlParameter("@keyFolderID", folder.folderID.ToStringHyphenated()); | ||
585 | command.Parameters.Add(param1); | ||
586 | command.Parameters.Add(param2); | ||
587 | command.Parameters.Add(param3); | ||
588 | command.Parameters.Add(param4); | ||
589 | command.Parameters.Add(param5); | ||
590 | command.Parameters.Add(param6); | ||
591 | command.Parameters.Add(param7); | ||
592 | |||
593 | try | ||
594 | { | ||
595 | command.ExecuteNonQuery(); | ||
596 | } | ||
597 | catch (Exception e) | ||
598 | { | ||
599 | MainLog.Instance.Error(e.ToString()); | ||
600 | } | ||
601 | |||
602 | |||
603 | } | ||
604 | |||
605 | |||
606 | /// <summary> | ||
607 | /// Append a list of all the child folders of a parent folder | ||
608 | /// </summary> | ||
609 | /// <param name="folders">list where folders will be appended</param> | ||
610 | /// <param name="parentID">ID of parent</param> | ||
611 | protected void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID) | ||
612 | { | ||
613 | List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID); | ||
614 | |||
615 | foreach (InventoryFolderBase f in subfolderList) | ||
616 | folders.Add(f); | ||
617 | } | ||
618 | |||
619 | /// <summary> | ||
620 | /// Returns all child folders in the hierarchy from the parent folder and down | ||
621 | /// </summary> | ||
622 | /// <param name="parentID">The folder to get subfolders for</param> | ||
623 | /// <returns>A list of inventory folders</returns> | ||
624 | protected List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID) | ||
625 | { | ||
626 | List<InventoryFolderBase> folders = new List<InventoryFolderBase>(); | ||
627 | getInventoryFolders(ref folders, parentID); | ||
628 | |||
629 | for (int i = 0; i < folders.Count; i++) | ||
630 | getInventoryFolders(ref folders, folders[i].folderID); | ||
631 | |||
632 | return folders; | ||
633 | } | ||
634 | |||
635 | protected void deleteOneFolder(LLUUID folderID) | ||
636 | { | ||
637 | try | ||
638 | { | ||
639 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
640 | param["folderID"] = folderID.ToStringHyphenated(); | ||
641 | |||
642 | IDbCommand cmd = database.Query("DELETE FROM inventoryfolders WHERE folderID=@folderID", param); | ||
643 | cmd.ExecuteNonQuery(); | ||
644 | cmd.Dispose(); | ||
645 | |||
646 | } | ||
647 | catch (SqlException e) | ||
648 | { | ||
649 | database.Reconnect(); | ||
650 | MainLog.Instance.Error(e.ToString()); | ||
651 | } | ||
652 | } | ||
653 | |||
654 | protected void deleteItemsInFolder(LLUUID folderID) | ||
655 | { | ||
656 | try | ||
657 | { | ||
658 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
659 | param["parentFolderID"] = folderID.ToStringHyphenated(); | ||
660 | |||
661 | |||
662 | IDbCommand cmd = database.Query("DELETE FROM inventoryitems WHERE parentFolderID=@parentFolderID", param); | ||
663 | cmd.ExecuteNonQuery(); | ||
664 | cmd.Dispose(); | ||
665 | } | ||
666 | catch (SqlException e) | ||
667 | { | ||
668 | database.Reconnect(); | ||
669 | MainLog.Instance.Error(e.ToString()); | ||
670 | } | ||
671 | } | ||
672 | |||
673 | |||
674 | /// <summary> | ||
675 | /// Delete an inventory folder | ||
676 | /// </summary> | ||
677 | /// <param name="folderId">Id of folder to delete</param> | ||
678 | public void deleteInventoryFolder(LLUUID folderID) | ||
679 | { | ||
680 | lock (database) | ||
681 | { | ||
682 | List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID); | ||
683 | |||
684 | //Delete all sub-folders | ||
685 | foreach (InventoryFolderBase f in subFolders) | ||
686 | { | ||
687 | deleteOneFolder(f.folderID); | ||
688 | deleteItemsInFolder(f.folderID); | ||
689 | } | ||
690 | |||
691 | //Delete the actual row | ||
692 | deleteOneFolder(folderID); | ||
693 | deleteItemsInFolder(folderID); | ||
694 | } | ||
695 | } | ||
696 | } | ||
697 | } \ No newline at end of file | ||