1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSim Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using libsecondlife;
using OpenSim.Framework;
using OpenSim.Framework.Console;
using MySql.Data.MySqlClient;
namespace OpenSim.Framework.Data.MySQL
{
/// <summary>
/// A MySQL interface for the inventory server
/// </summary>
public class MySQLInventoryData : IInventoryData
{
/// <summary>
/// The database manager
/// </summary>
private MySQLManager database;
/// <summary>
/// Loads and initialises this database plugin
/// </summary>
public void Initialise()
{
IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
string settingHostname = GridDataMySqlFile.ParseFileReadValue("hostname");
string settingDatabase = GridDataMySqlFile.ParseFileReadValue("database");
string settingUsername = GridDataMySqlFile.ParseFileReadValue("username");
string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
string settingPooling = GridDataMySqlFile.ParseFileReadValue("pooling");
string settingPort = GridDataMySqlFile.ParseFileReadValue("port");
database = new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling, settingPort);
TestTables(database.Connection);
}
#region Test and initialization code
private void UpgradeFoldersTable(string oldVersion)
{
// null as the version, indicates that the table didn't exist
if (oldVersion == null)
{
database.ExecuteResourceSql("CreateFoldersTable.sql");
return;
}
// if the table is already at the current version, then we can exit immediately
if (oldVersion == "Rev. 2")
return;
database.ExecuteResourceSql("UpgradeFoldersTableToVersion2.sql");
}
private void UpgradeItemsTable(string oldVersion)
{
// null as the version, indicates that the table didn't exist
if (oldVersion == null)
{
database.ExecuteResourceSql("CreateItemsTable.sql");
return;
}
// if the table is already at the current version, then we can exit immediately
if (oldVersion == "Rev. 2")
return;
database.ExecuteResourceSql("UpgradeItemsTableToVersion2.sql");
}
private void TestTables(MySqlConnection conn)
{
Dictionary<string, string> tableList = new Dictionary<string, string>();
tableList["inventoryfolders"] = null;
tableList["inventoryitems"] = null;
database.GetTableVersion(tableList);
UpgradeFoldersTable(tableList["inventoryfolders"]);
UpgradeItemsTable(tableList["inventoryitems"]);
}
#endregion
/// <summary>
/// The name of this DB provider
/// </summary>
/// <returns>Name of DB provider</returns>
public string getName()
{
return "MySQL Inventory Data Interface";
}
/// <summary>
/// Closes this DB provider
/// </summary>
public void Close()
{
// Do nothing.
}
/// <summary>
/// Returns the version of this DB provider
/// </summary>
/// <returns>A string containing the DB provider</returns>
public string getVersion()
{
return database.getVersion();
}
/// <summary>
/// Returns a list of items in a specified folder
/// </summary>
/// <param name="folderID">The folder to search</param>
/// <returns>A list containing inventory items</returns>
public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID)
{
try
{
lock (database)
{
List<InventoryItemBase> items = new List<InventoryItemBase>();
MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", database.Connection);
result.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
while(reader.Read())
items.Add(readInventoryItem(reader));
reader.Close();
result.Dispose();
return items;
}
}
catch (Exception e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
return null;
}
}
/// <summary>
/// Returns a list of the root folders within a users inventory
/// </summary>
/// <param name="user">The user whos inventory is to be searched</param>
/// <returns>A list of folder objects</returns>
public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
{
try
{
lock (database)
{
MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection);
result.Parameters.AddWithValue("?uuid", user.ToStringHyphenated());
result.Parameters.AddWithValue("?zero", LLUUID.Zero.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
List<InventoryFolderBase> items = new List<InventoryFolderBase>();
while(reader.Read())
items.Add(readInventoryFolder(reader));
reader.Close();
result.Dispose();
return items;
}
}
catch (Exception e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
return null;
}
}
/// <summary>
/// Returns the users inventory root folder.
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public InventoryFolderBase getUserRootFolder(LLUUID user)
{
try
{
lock (database)
{
MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection);
result.Parameters.AddWithValue("?uuid", user.ToStringHyphenated());
result.Parameters.AddWithValue("?zero", LLUUID.Zero.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
List<InventoryFolderBase> items = new List<InventoryFolderBase>();
while(reader.Read())
items.Add(readInventoryFolder(reader));
InventoryFolderBase rootFolder = items[0]; //should only be one folder with parent set to zero (the root one).
reader.Close();
result.Dispose();
return rootFolder;
}
}
catch (Exception e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
return null;
}
}
/// <summary>
/// Returns a list of folders in a users inventory contained within the specified folder
/// </summary>
/// <param name="parentID">The folder to search</param>
/// <returns>A list of inventory folders</returns>
public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID)
{
try
{
lock (database)
{
MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", database.Connection);
result.Parameters.AddWithValue("?uuid", parentID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
List<InventoryFolderBase> items = new List<InventoryFolderBase>();
while(reader.Read())
items.Add(readInventoryFolder(reader));
reader.Close();
result.Dispose();
return items;
}
}
catch (Exception e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
return null;
}
}
/// <summary>
/// Reads a one item from an SQL result
/// </summary>
/// <param name="reader">The SQL Result</param>
/// <returns>the item read</returns>
public InventoryItemBase readInventoryItem(MySqlDataReader reader)
{
try
{
InventoryItemBase item = new InventoryItemBase();
item.inventoryID = new LLUUID((string)reader["inventoryID"]);
item.assetID = new LLUUID((string)reader["assetID"]);
item.assetType = (int)reader["assetType"];
item.parentFolderID = new LLUUID((string)reader["parentFolderID"]);
item.avatarID = new LLUUID((string)reader["avatarID"]);
item.inventoryName = (string)reader["inventoryName"];
item.inventoryDescription = (string)reader["inventoryDescription"];
item.inventoryNextPermissions = (uint)reader["inventoryNextPermissions"];
item.inventoryCurrentPermissions = (uint)reader["inventoryCurrentPermissions"];
item.invType = (int)reader["invType"];
item.creatorsID = new LLUUID((string)reader["creatorID"]);
item.inventoryBasePermissions = (uint)reader["inventoryBasePermissions"];
item.inventoryEveryOnePermissions = (uint)reader["inventoryEveryOnePermissions"];
return item;
}
catch (MySqlException e)
{
MainLog.Instance.Error(e.ToString());
}
return null;
}
/// <summary>
/// Returns a specified inventory item
/// </summary>
/// <param name="item">The item to return</param>
/// <returns>An inventory item</returns>
public InventoryItemBase getInventoryItem(LLUUID itemID)
{
try
{
lock (database)
{
Dictionary<string, string> param = new Dictionary<string, string>();
MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", database.Connection);
result.Parameters.AddWithValue("?uuid", itemID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
InventoryItemBase item = null;
if(reader.Read())
item = readInventoryItem(reader);
reader.Close();
result.Dispose();
return item;
}
}
catch (Exception e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
}
return null;
}
/// <summary>
/// Reads a list of inventory folders returned by a query.
/// </summary>
/// <param name="reader">A MySQL Data Reader</param>
/// <returns>A List containing inventory folders</returns>
protected InventoryFolderBase readInventoryFolder(MySqlDataReader reader)
{
try
{
InventoryFolderBase folder = new InventoryFolderBase();
folder.agentID = new LLUUID((string)reader["agentID"]);
folder.parentID = new LLUUID((string)reader["parentFolderID"]);
folder.folderID = new LLUUID((string)reader["folderID"]);
folder.name = (string)reader["folderName"];
folder.type = (short)reader["type"];
folder.version = (ushort)((int)reader["version"]);
return folder;
}
catch (Exception e)
{
MainLog.Instance.Error(e.ToString());
}
return null;
}
/// <summary>
/// Returns a specified inventory folder
/// </summary>
/// <param name="folder">The folder to return</param>
/// <returns>A folder class</returns>
public InventoryFolderBase getInventoryFolder(LLUUID folderID)
{
try
{
lock (database)
{
MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", database.Connection);
result.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
reader.Read();
InventoryFolderBase folder = readInventoryFolder(reader);
reader.Close();
result.Dispose();
return folder;
}
}
catch (Exception e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
return null;
}
}
/// <summary>
/// Adds a specified item to the database
/// </summary>
/// <param name="item">The inventory item</param>
public void addInventoryItem(InventoryItemBase item)
{
string sql = "REPLACE INTO inventoryitems (inventoryID, assetID, assetType, parentFolderID, avatarID, inventoryName, inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions, invType, creatorID, inventoryBasePermissions, inventoryEveryOnePermissions) VALUES ";
sql += "(?inventoryID, ?assetID, ?assetType, ?parentFolderID, ?avatarID, ?inventoryName, ?inventoryDescription, ?inventoryNextPermissions, ?inventoryCurrentPermissions, ?invType, ?creatorID, ?inventoryBasePermissions, ?inventoryEveryOnePermissions)";
try
{
MySqlCommand result = new MySqlCommand(sql, database.Connection);
result.Parameters.AddWithValue("?inventoryID", item.inventoryID.ToStringHyphenated());
result.Parameters.AddWithValue("?assetID", item.assetID.ToStringHyphenated());
result.Parameters.AddWithValue("?assetType", item.assetType.ToString());
result.Parameters.AddWithValue("?parentFolderID", item.parentFolderID.ToStringHyphenated());
result.Parameters.AddWithValue("?avatarID", item.avatarID.ToStringHyphenated());
result.Parameters.AddWithValue("?inventoryName", item.inventoryName);
result.Parameters.AddWithValue("?inventoryDescription", item.inventoryDescription);
result.Parameters.AddWithValue("?inventoryNextPermissions", item.inventoryNextPermissions.ToString());
result.Parameters.AddWithValue("?inventoryCurrentPermissions", item.inventoryCurrentPermissions.ToString());
result.Parameters.AddWithValue("?invType", item.invType);
result.Parameters.AddWithValue("?creatorID", item.creatorsID.ToStringHyphenated());
result.Parameters.AddWithValue("?inventoryBasePermissions", item.inventoryBasePermissions);
result.Parameters.AddWithValue("?inventoryEveryOnePermissions", item.inventoryEveryOnePermissions);
result.ExecuteNonQuery();
result.Dispose();
}
catch (MySqlException e)
{
MainLog.Instance.Error(e.ToString());
}
}
/// <summary>
/// Updates the specified inventory item
/// </summary>
/// <param name="item">Inventory item to update</param>
public void updateInventoryItem(InventoryItemBase item)
{
addInventoryItem(item);
}
/// <summary>
///
/// </summary>
/// <param name="item"></param>
public void deleteInventoryItem(LLUUID itemID)
{
try
{
MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", database.Connection);
cmd.Parameters.AddWithValue("?uuid", itemID.ToStringHyphenated());
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
}
}
/// <summary>
/// Creates a new inventory folder
/// </summary>
/// <param name="folder">Folder to create</param>
public void addInventoryFolder(InventoryFolderBase folder)
{
string sql = "REPLACE INTO inventoryfolders (folderID, agentID, parentFolderID, folderName, type, version) VALUES ";
sql += "(?folderID, ?agentID, ?parentFolderID, ?folderName, ?type, ?version)";
MySqlCommand cmd = new MySqlCommand(sql, database.Connection);
cmd.Parameters.AddWithValue("?folderID", folder.folderID.ToStringHyphenated());
cmd.Parameters.AddWithValue("?agentID", folder.agentID.ToStringHyphenated());
cmd.Parameters.AddWithValue("?parentFolderID", folder.parentID.ToStringHyphenated());
cmd.Parameters.AddWithValue("?folderName", folder.name);
cmd.Parameters.AddWithValue("?type", (short)folder.type);
cmd.Parameters.AddWithValue("?version", folder.version);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
MainLog.Instance.Error(e.ToString());
}
}
/// <summary>
/// Updates an inventory folder
/// </summary>
/// <param name="folder">Folder to update</param>
public void updateInventoryFolder(InventoryFolderBase folder)
{
addInventoryFolder(folder);
}
/// <summary>
/// Append a list of all the child folders of a parent folder
/// </summary>
/// <param name="folders">list where folders will be appended</param>
/// <param name="parentID">ID of parent</param>
protected void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID)
{
List<InventoryFolderBase> subfolderList = getInventoryFolders(parentID);
foreach (InventoryFolderBase f in subfolderList)
folders.Add(f);
}
/// <summary>
/// Returns all child folders in the hierarchy from the parent folder and down
/// </summary>
/// <param name="parentID">The folder to get subfolders for</param>
/// <returns>A list of inventory folders</returns>
protected List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID)
{
List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
getInventoryFolders(ref folders, parentID);
for (int i = 0; i < folders.Count; i++)
getInventoryFolders(ref folders, folders[i].folderID);
return folders;
}
protected void deleteOneFolder(LLUUID folderID)
{
try
{
MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryfolders WHERE folderID=?uuid", database.Connection);
cmd.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated());
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
}
}
protected void deleteItemsInFolder(LLUUID folderID)
{
try
{
MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE parentFolderID=?uuid", database.Connection);
cmd.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated());
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
}
}
/// <summary>
/// Delete an inventory folder
/// </summary>
/// <param name="folderId">Id of folder to delete</param>
public void deleteInventoryFolder(LLUUID folderID)
{
lock (database)
{
List<InventoryFolderBase> subFolders = getFolderHierarchy(folderID);
//Delete all sub-folders
foreach (InventoryFolderBase f in subFolders)
{
deleteOneFolder(f.folderID);
deleteItemsInFolder(f.folderID);
}
//Delete the actual row
deleteOneFolder(folderID);
deleteItemsInFolder(folderID);
}
}
}
}
|