Applied patch 485, inventory patch from tleiades (thanks again).

afrisby
MW 2007-10-08 10:30:15 +00:00
parent f2f173a00e
commit 4c25008850
11 changed files with 409 additions and 254 deletions

View File

@ -128,7 +128,7 @@ namespace OpenSim.Framework.Communications
{ {
foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins) foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
{ {
plugin.Value.deleteInventoryItem(item); plugin.Value.deleteInventoryItem(item.inventoryID);
} }
} }

View File

@ -32,6 +32,8 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
using libsecondlife; using libsecondlife;
using OpenSim.Framework.Console;
namespace OpenSim.Framework.Data.MySQL namespace OpenSim.Framework.Data.MySQL
{ {
/// <summary> /// <summary>
@ -127,7 +129,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -159,7 +161,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -191,7 +193,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -277,7 +279,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }

View File

@ -26,22 +26,26 @@
* *
*/ */
using System; using System;
using System.Collections.Generic; using System.IO;
using System.Data; using System.Data;
using System.Reflection;
using System.Collections.Generic;
using libsecondlife; using libsecondlife;
using OpenSim.Framework.Types; using OpenSim.Framework.Types;
using OpenSim.Framework.Console;
using MySql.Data.MySqlClient;
namespace OpenSim.Framework.Data.MySQL namespace OpenSim.Framework.Data.MySQL
{ {
/// <summary> /// <summary>
/// A MySQL interface for the inventory server /// A MySQL interface for the inventory server
/// </summary> /// </summary>
class MySQLInventoryData : IInventoryData public class MySQLInventoryData : IInventoryData
{ {
/// <summary> /// <summary>
/// The database manager /// The database manager
/// </summary> /// </summary>
public MySQLManager database; private MySQLManager database;
/// <summary> /// <summary>
/// Loads and initialises this database plugin /// Loads and initialises this database plugin
@ -57,8 +61,101 @@ namespace OpenSim.Framework.Data.MySQL
string settingPort = GridDataMySqlFile.ParseFileReadValue("port"); string settingPort = GridDataMySqlFile.ParseFileReadValue("port");
database = new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling, settingPort); database = new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling, settingPort);
TestTables(database.Connection);
} }
#region Test and initialization code
/// <summary>
/// Extract a named string resource from the embedded resources
/// </summary>
/// <param name="name">name of embedded resource</param>
/// <returns>string contained within the embedded resource</returns>
private string getResourceString(string name)
{
Assembly assem = this.GetType().Assembly;
string[] names = assem.GetManifestResourceNames();
foreach(string s in names)
if(s.EndsWith(name))
using (Stream resource = assem.GetManifestResourceStream(s))
{
using (StreamReader resourceReader = new StreamReader(resource))
{
string resourceString = resourceReader.ReadToEnd();
return resourceString;
}
}
throw new Exception(string.Format("Resource '{0}' was not found", name));
}
private void ExecuteResourceSql(MySqlConnection conn, string name)
{
MySqlCommand cmd = new MySqlCommand(getResourceString(name), conn);
cmd.ExecuteNonQuery();
}
private void UpgradeFoldersTable(MySqlConnection conn, string oldVersion)
{
// null as the version, indicates that the table didn't exist
if (oldVersion == null)
{
ExecuteResourceSql(conn, "CreateFoldersTable.sql");
return;
}
// if the table is already at the current version, then we can exit immediately
if (oldVersion == "Rev. 2")
return;
ExecuteResourceSql(conn, "UpgradeFoldersTableToVersion2.sql");
}
private void UpgradeItemsTable(MySqlConnection conn, string oldVersion)
{
// null as the version, indicates that the table didn't exist
if (oldVersion == null)
{
ExecuteResourceSql(conn, "CreateItemsTable.sql");
return;
}
// if the table is already at the current version, then we can exit immediately
if (oldVersion == "Rev. 2")
return;
ExecuteResourceSql(conn, "UpgradeItemsTableToVersion2.sql");
}
private void TestTables(MySqlConnection conn)
{
Dictionary<string, string> tableList = new Dictionary<string, string>();
tableList["inventoryfolders"] = null;
tableList["inventoryitems"] = null;
MySqlCommand tablesCmd = new MySqlCommand("SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='opensim'", conn);
MySqlDataReader tables = tablesCmd.ExecuteReader();
while (tables.Read())
{
try
{
string tableName = (string)tables["TABLE_NAME"];
string comment = (string)tables["TABLE_COMMENT"];
tableList[tableName] = comment;
}
catch (Exception e)
{
MainLog.Instance.Error(e.ToString());
}
}
tables.Close();
UpgradeFoldersTable(conn, tableList["inventoryfolders"]);
UpgradeItemsTable(conn, tableList["inventoryitems"]);
}
#endregion
/// <summary> /// <summary>
/// The name of this DB provider /// The name of this DB provider
/// </summary> /// </summary>
@ -82,7 +179,12 @@ namespace OpenSim.Framework.Data.MySQL
/// <returns>A string containing the DB provider</returns> /// <returns>A string containing the DB provider</returns>
public string getVersion() public string getVersion()
{ {
return "0.1"; System.Reflection.Module module = this.GetType().Module;
string dllName = module.Assembly.ManifestModule.Name;
Version dllVersion = module.Assembly.GetName().Version;
return string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, dllVersion.Revision);
} }
/// <summary> /// <summary>
@ -96,13 +198,14 @@ namespace OpenSim.Framework.Data.MySQL
{ {
lock (database) lock (database)
{ {
Dictionary<string, string> param = new Dictionary<string, string>(); List<InventoryItemBase> items = new List<InventoryItemBase>();
param["?uuid"] = folderID.ToStringHyphenated();
IDbCommand result = database.Query("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", param); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", database.Connection);
IDataReader reader = result.ExecuteReader(); result.Parameters.Add("?uuid", folderID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
List<InventoryItemBase> items = database.readInventoryItems(reader); while(reader.Read())
items.Add(readInventoryItem(reader));
reader.Close(); reader.Close();
result.Dispose(); result.Dispose();
@ -113,7 +216,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -129,14 +232,15 @@ namespace OpenSim.Framework.Data.MySQL
{ {
lock (database) lock (database)
{ {
Dictionary<string, string> param = new Dictionary<string, string>(); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection);
param["?uuid"] = user.ToStringHyphenated(); result.Parameters.Add("?uuid", user.ToStringHyphenated());
param["?zero"] = LLUUID.Zero.ToStringHyphenated(); result.Parameters.Add("?zero", LLUUID.Zero.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", param); List<InventoryFolderBase> items = new List<InventoryFolderBase>();
IDataReader reader = result.ExecuteReader(); while(reader.Read())
items.Add(readInventoryFolder(reader));
List<InventoryFolderBase> items = database.readInventoryFolders(reader);
reader.Close(); reader.Close();
result.Dispose(); result.Dispose();
@ -147,7 +251,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -167,10 +271,16 @@ namespace OpenSim.Framework.Data.MySQL
param["?uuid"] = user.ToStringHyphenated(); param["?uuid"] = user.ToStringHyphenated();
param["?zero"] = LLUUID.Zero.ToStringHyphenated(); param["?zero"] = LLUUID.Zero.ToStringHyphenated();
IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", param); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection);
IDataReader reader = result.ExecuteReader(); result.Parameters.Add("?uuid", user.ToStringHyphenated());
result.Parameters.Add("?zero", LLUUID.Zero.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
List<InventoryFolderBase> items = new List<InventoryFolderBase>();
while(reader.Read())
items.Add(readInventoryFolder(reader));
List<InventoryFolderBase> items = database.readInventoryFolders(reader);
InventoryFolderBase rootFolder = items[0]; //should only be one folder with parent set to zero (the root one). InventoryFolderBase rootFolder = items[0]; //should only be one folder with parent set to zero (the root one).
reader.Close(); reader.Close();
result.Dispose(); result.Dispose();
@ -181,7 +291,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -197,13 +307,14 @@ namespace OpenSim.Framework.Data.MySQL
{ {
lock (database) lock (database)
{ {
Dictionary<string, string> param = new Dictionary<string, string>(); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", database.Connection);
param["?uuid"] = parentID.ToStringHyphenated(); result.Parameters.Add("?uuid", parentID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", param); List<InventoryFolderBase> items = new List<InventoryFolderBase>();
IDataReader reader = result.ExecuteReader();
List<InventoryFolderBase> items = database.readInventoryFolders(reader); while(reader.Read())
items.Add(readInventoryFolder(reader));
reader.Close(); reader.Close();
result.Dispose(); result.Dispose();
@ -214,87 +325,134 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; 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> /// <summary>
/// Returns a specified inventory item /// Returns a specified inventory item
/// </summary> /// </summary>
/// <param name="item">The item to return</param> /// <param name="item">The item to return</param>
/// <returns>An inventory item</returns> /// <returns>An inventory item</returns>
public InventoryItemBase getInventoryItem(LLUUID item) public InventoryItemBase getInventoryItem(LLUUID itemID)
{ {
try try
{ {
lock (database) lock (database)
{ {
Dictionary<string, string> param = new Dictionary<string, string>(); Dictionary<string, string> param = new Dictionary<string, string>();
param["?uuid"] = item.ToStringHyphenated();
IDbCommand result = database.Query("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", param); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", database.Connection);
IDataReader reader = result.ExecuteReader(); result.Parameters.Add("?uuid", itemID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
List<InventoryItemBase> items = database.readInventoryItems(reader); InventoryItemBase item = null;
if(reader.Read())
item = readInventoryItem(reader);
reader.Close(); reader.Close();
result.Dispose(); result.Dispose();
if (items.Count > 0) return item;
{
return items[0];
}
else
{
return null;
}
} }
} }
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null;
} }
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> /// <summary>
/// Returns a specified inventory folder /// Returns a specified inventory folder
/// </summary> /// </summary>
/// <param name="folder">The folder to return</param> /// <param name="folder">The folder to return</param>
/// <returns>A folder class</returns> /// <returns>A folder class</returns>
public InventoryFolderBase getInventoryFolder(LLUUID folder) public InventoryFolderBase getInventoryFolder(LLUUID folderID)
{ {
try try
{ {
lock (database) lock (database)
{ {
Dictionary<string, string> param = new Dictionary<string, string>(); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", database.Connection);
param["?uuid"] = folder.ToStringHyphenated(); result.Parameters.Add("?uuid", folderID.ToStringHyphenated());
MySqlDataReader reader = result.ExecuteReader();
IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", param);
IDataReader reader = result.ExecuteReader();
List<InventoryFolderBase> items = database.readInventoryFolders(reader);
reader.Read();
InventoryFolderBase folder = readInventoryFolder(reader);
reader.Close(); reader.Close();
result.Dispose(); result.Dispose();
if (items.Count > 0) return folder;
{
return items[0];
}
else
{
return null;
}
} }
} }
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -305,9 +463,31 @@ namespace OpenSim.Framework.Data.MySQL
/// <param name="item">The inventory item</param> /// <param name="item">The inventory item</param>
public void addInventoryItem(InventoryItemBase item) public void addInventoryItem(InventoryItemBase item)
{ {
lock (database) 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
{ {
database.insertItem(item); MySqlCommand result = new MySqlCommand(sql, database.Connection);
result.Parameters.Add("?inventoryID", item.inventoryID.ToStringHyphenated());
result.Parameters.Add("?assetID", item.assetID.ToStringHyphenated());
result.Parameters.Add("?assetType", item.assetType.ToString());
result.Parameters.Add("?parentFolderID", item.parentFolderID.ToStringHyphenated());
result.Parameters.Add("?avatarID", item.avatarID.ToStringHyphenated());
result.Parameters.Add("?inventoryName", item.inventoryName);
result.Parameters.Add("?inventoryDescription", item.inventoryDescription);
result.Parameters.Add("?inventoryNextPermissions", item.inventoryNextPermissions.ToString());
result.Parameters.Add("?inventoryCurrentPermissions", item.inventoryCurrentPermissions.ToString());
result.Parameters.Add("?invType", item.invType);
result.Parameters.Add("?creatorID", item.creatorsID.ToStringHyphenated());
result.Parameters.Add("?inventoryBasePermissions", item.inventoryBasePermissions);
result.Parameters.Add("?inventoryEveryOnePermissions", item.inventoryEveryOnePermissions);
result.ExecuteNonQuery();
result.Dispose();
}
catch (MySqlException e)
{
MainLog.Instance.Error(e.ToString());
} }
} }
@ -324,9 +504,19 @@ namespace OpenSim.Framework.Data.MySQL
/// ///
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
public void deleteInventoryItem(InventoryItemBase item) public void deleteInventoryItem(LLUUID itemID)
{ {
try
{
MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", database.Connection);
cmd.Parameters.Add("?uuid", itemID.ToStringHyphenated());
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
}
} }
/// <summary> /// <summary>
@ -335,9 +525,24 @@ namespace OpenSim.Framework.Data.MySQL
/// <param name="folder">Folder to create</param> /// <param name="folder">Folder to create</param>
public void addInventoryFolder(InventoryFolderBase folder) public void addInventoryFolder(InventoryFolderBase folder)
{ {
lock (database) 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.Add("?folderID", folder.folderID.ToStringHyphenated());
cmd.Parameters.Add("?agentID", folder.agentID.ToStringHyphenated());
cmd.Parameters.Add("?parentFolderID", folder.parentID.ToStringHyphenated());
cmd.Parameters.Add("?folderName", folder.name);
cmd.Parameters.Add("?type", (short)folder.type);
cmd.Parameters.Add("?version", folder.version);
try
{ {
database.insertFolder(folder); cmd.ExecuteNonQuery();
}
catch (Exception e)
{
MainLog.Instance.Error(e.ToString());
} }
} }
@ -351,12 +556,87 @@ namespace OpenSim.Framework.Data.MySQL
} }
/// <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.Add("?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.Add("?uuid", folderID.ToStringHyphenated());
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
database.Reconnect();
MainLog.Instance.Error(e.ToString());
}
}
/// <summary> /// <summary>
/// Delete an inventory folder /// Delete an inventory folder
/// </summary> /// </summary>
/// <param name="folderId">Id of folder to delete</param> /// <param name="folderId">Id of folder to delete</param>
public void deleteInventoryFolder(LLUUID folderID) 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);
}
} }
} }
} }

View File

@ -31,6 +31,7 @@ using System.Data;
using libsecondlife; using libsecondlife;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using OpenSim.Framework.Types; using OpenSim.Framework.Types;
using OpenSim.Framework.Console;
namespace OpenSim.Framework.Data.MySQL namespace OpenSim.Framework.Data.MySQL
{ {
@ -42,7 +43,7 @@ namespace OpenSim.Framework.Data.MySQL
/// <summary> /// <summary>
/// The database connection object /// The database connection object
/// </summary> /// </summary>
IDbConnection dbcon; MySqlConnection dbcon;
/// <summary> /// <summary>
/// Connection string for ADO.net /// Connection string for ADO.net
/// </summary> /// </summary>
@ -65,7 +66,7 @@ namespace OpenSim.Framework.Data.MySQL
dbcon.Open(); dbcon.Open();
Console.WriteLine("MySQL connection established"); MainLog.Instance.Verbose("MySQL connection established");
} }
catch (Exception e) catch (Exception e)
{ {
@ -73,6 +74,14 @@ namespace OpenSim.Framework.Data.MySQL
} }
} }
/// <summary>
/// Get the connection being used
/// </summary>
public MySqlConnection Connection
{
get { return dbcon; }
}
/// <summary> /// <summary>
/// Shuts down the database connection /// Shuts down the database connection
/// </summary> /// </summary>
@ -99,7 +108,7 @@ namespace OpenSim.Framework.Data.MySQL
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("Unable to reconnect to database " + e.ToString()); MainLog.Instance.Error("Unable to reconnect to database " + e.ToString());
} }
} }
} }
@ -142,7 +151,7 @@ namespace OpenSim.Framework.Data.MySQL
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("Unable to reconnect to database " + e.ToString()); MainLog.Instance.Error("Unable to reconnect to database " + e.ToString());
} }
// Run the query again // Run the query again
@ -160,7 +169,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
// Return null if it fails. // Return null if it fails.
Console.WriteLine("Failed during Query generation: " + e.ToString()); MainLog.Instance.Error("Failed during Query generation: " + e.ToString());
return null; return null;
} }
} }
@ -352,72 +361,7 @@ namespace OpenSim.Framework.Data.MySQL
return retval; return retval;
} }
/// <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>
public List<InventoryFolderBase> readInventoryFolders(IDataReader reader)
{
List<InventoryFolderBase> rows = new List<InventoryFolderBase>();
while(reader.Read())
{
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"];
rows.Add(folder);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
return rows;
}
/// <summary>
/// Reads a collection of items from an SQL result
/// </summary>
/// <param name="reader">The SQL Result</param>
/// <returns>A List containing Inventory Items</returns>
public List<InventoryItemBase> readInventoryItems(IDataReader reader)
{
List<InventoryItemBase> rows = new List<InventoryItemBase>();
while (reader.Read())
{
try
{
InventoryItemBase item = new InventoryItemBase();
item.assetID = new LLUUID((string)reader["assetID"]);
item.avatarID = new LLUUID((string)reader["avatarID"]);
item.inventoryCurrentPermissions = Convert.ToUInt32(reader["inventoryCurrentPermissions"].ToString());
item.inventoryDescription = (string)reader["inventoryDescription"];
item.inventoryID = new LLUUID((string)reader["inventoryID"]);
item.inventoryName = (string)reader["inventoryName"];
item.inventoryNextPermissions = Convert.ToUInt32(reader["inventoryNextPermissions"].ToString());
item.parentFolderID = new LLUUID((string)reader["parentFolderID"]);
item.assetType = Convert.ToInt32(reader["type"].ToString());
rows.Add(item);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
return rows;
}
/// <summary> /// <summary>
/// Inserts a new row into the log database /// Inserts a new row into the log database
@ -455,87 +399,13 @@ namespace OpenSim.Framework.Data.MySQL
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return false; return false;
} }
return returnval; return returnval;
} }
/// <summary>
/// Inserts a new item into the database
/// </summary>
/// <param name="item">The item</param>
/// <returns>Success?</returns>
public bool insertItem(InventoryItemBase item)
{
string sql = "REPLACE INTO inventoryitems (inventoryID, assetID, type, parentFolderID, avatarID, inventoryName, inventoryDescription, inventoryNextPermissions, inventoryCurrentPermissions) VALUES ";
sql += "(?inventoryID, ?assetID, ?type, ?parentFolderID, ?avatarID, ?inventoryName, ?inventoryDescription, ?inventoryNextPermissions, ?inventoryCurrentPermissions)";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters["?inventoryID"] = item.inventoryID.ToStringHyphenated();
parameters["?assetID"] = item.assetID.ToStringHyphenated();
parameters["?type"] = item.assetType.ToString();
parameters["?parentFolderID"] = item.parentFolderID.ToStringHyphenated();
parameters["?avatarID"] = item.avatarID.ToStringHyphenated();
parameters["?inventoryName"] = item.inventoryName;
parameters["?inventoryDescription"] = item.inventoryDescription;
parameters["?inventoryNextPermissions"] = item.inventoryNextPermissions.ToString();
parameters["?inventoryCurrentPermissions"] = item.inventoryCurrentPermissions.ToString();
bool returnval = false;
try
{
IDbCommand result = Query(sql, parameters);
if (result.ExecuteNonQuery() == 1)
returnval = true;
result.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
return returnval;
}
/// <summary>
/// Inserts a new folder into the database
/// </summary>
/// <param name="folder">The folder</param>
/// <returns>Success?</returns>
public bool insertFolder(InventoryFolderBase folder)
{
string sql = "REPLACE INTO inventoryfolders (folderID, agentID, parentFolderID, folderName) VALUES ";
sql += "(?folderID, ?agentID, ?parentFolderID, ?folderName)";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters["?folderID"] = folder.folderID.ToStringHyphenated();
parameters["?agentID"] = folder.agentID.ToStringHyphenated();
parameters["?parentFolderID"] = folder.parentID.ToStringHyphenated();
parameters["?folderName"] = folder.name;
bool returnval = false;
try
{
IDbCommand result = Query(sql, parameters);
if (result.ExecuteNonQuery() == 1)
returnval = true;
result.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
return returnval;
}
/// <summary> /// <summary>
/// Creates a new user and inserts it into the database /// Creates a new user and inserts it into the database
@ -614,7 +484,7 @@ namespace OpenSim.Framework.Data.MySQL
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return false; return false;
} }
@ -683,7 +553,7 @@ namespace OpenSim.Framework.Data.MySQL
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return false; return false;
} }

View File

@ -30,6 +30,7 @@ using System.Collections.Generic;
using System.Data; using System.Data;
using libsecondlife; using libsecondlife;
using OpenSim.Framework.Types; using OpenSim.Framework.Types;
using OpenSim.Framework.Console;
namespace OpenSim.Framework.Data.MySQL namespace OpenSim.Framework.Data.MySQL
{ {
@ -101,7 +102,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -134,7 +135,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -189,7 +190,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
return null; return null;
} }
} }
@ -212,7 +213,7 @@ namespace OpenSim.Framework.Data.MySQL
catch (Exception e) catch (Exception e)
{ {
database.Reconnect(); database.Reconnect();
Console.WriteLine(e.ToString()); MainLog.Instance.Error(e.ToString());
} }
} }

View File

@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
// //
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyVersion("0.4.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Reflection;
using OpenSim.Framework.Console; using OpenSim.Framework.Console;
using OpenSim.Framework.Types; using OpenSim.Framework.Types;
@ -166,7 +167,12 @@ namespace OpenSim.Framework.Data.SQLite
/// <returns>A string containing the plugin version</returns> /// <returns>A string containing the plugin version</returns>
public string getVersion() public string getVersion()
{ {
return "0.1"; System.Reflection.Module module = this.GetType().Module;
string dllName = module.Assembly.ManifestModule.Name;
Version dllVersion = module.Assembly.GetName().Version;
return string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, dllVersion.Revision);
} }
/// <summary> /// <summary>
@ -337,11 +343,11 @@ namespace OpenSim.Framework.Data.SQLite
/// ///
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
public void deleteInventoryItem(InventoryItemBase item) public void deleteInventoryItem(LLUUID itemID)
{ {
DataTable inventoryItemTable = ds.Tables["inventoryitems"]; DataTable inventoryItemTable = ds.Tables["inventoryitems"];
DataRow inventoryRow = inventoryItemTable.Rows.Find(item.inventoryID); DataRow inventoryRow = inventoryItemTable.Rows.Find(itemID);
if (inventoryRow != null) if (inventoryRow != null)
{ {
inventoryRow.Delete(); inventoryRow.Delete();
@ -350,6 +356,20 @@ namespace OpenSim.Framework.Data.SQLite
this.invItemsDa.Update(ds, "inventoryitems"); this.invItemsDa.Update(ds, "inventoryitems");
} }
/// <summary>
/// Delete all items in the specified folder
/// </summary>
/// <param name="folderId">id of the folder, whose item content should be deleted</param>
//!TODO, this is horribly inefficient, but I don't want to ruin the overall structure of this implementatio
private void deleteItemsInFolder(LLUUID folderId)
{
List<InventoryItemBase> items = getInventoryInFolder(folderId);
foreach(InventoryItemBase i in items)
deleteInventoryItem(i.inventoryID);
}
/// <summary> /// <summary>
/// Adds a new folder specified by folder /// Adds a new folder specified by folder
/// </summary> /// </summary>
@ -389,6 +409,7 @@ namespace OpenSim.Framework.Data.SQLite
inventoryRow = inventoryFolderTable.Rows.Find(f.folderID); inventoryRow = inventoryFolderTable.Rows.Find(f.folderID);
if (inventoryRow != null) if (inventoryRow != null)
{ {
deleteItemsInFolder(f.folderID);
inventoryRow.Delete(); inventoryRow.Delete();
} }
} }
@ -397,6 +418,7 @@ namespace OpenSim.Framework.Data.SQLite
inventoryRow = inventoryFolderTable.Rows.Find(folderID); inventoryRow = inventoryFolderTable.Rows.Find(folderID);
if (inventoryRow != null) if (inventoryRow != null)
{ {
deleteItemsInFolder(folderID);
inventoryRow.Delete(); inventoryRow.Delete();
} }
@ -554,3 +576,4 @@ namespace OpenSim.Framework.Data.SQLite
} }

View File

@ -205,7 +205,7 @@ namespace OpenSim.Framework.Types
/// ///
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
void deleteInventoryItem(InventoryItemBase item); void deleteInventoryItem(LLUUID item);
/// <summary> /// <summary>
/// Adds a new folder specified by folder /// Adds a new folder specified by folder
@ -224,6 +224,5 @@ namespace OpenSim.Framework.Types
/// </summary> /// </summary>
/// <param name="folder">The id of the folder</param> /// <param name="folder">The id of the folder</param>
void deleteInventoryFolder(LLUUID folder); void deleteInventoryFolder(LLUUID folder);
} }
} }

View File

@ -696,8 +696,10 @@
<Reference name="OpenSim.Framework.Data"/> <Reference name="OpenSim.Framework.Data"/>
<Reference name="libsecondlife.dll"/> <Reference name="libsecondlife.dll"/>
<Reference name="MySql.Data.dll"/> <Reference name="MySql.Data.dll"/>
<Reference name="OpenSim.Framework.Console"/>
<Files> <Files>
<Match pattern="*.cs" recurse="true"/> <Match pattern="*.cs" recurse="true"/>
<Match path="Resources" pattern="*.sql" buildAction="EmbeddedResource"/>
</Files> </Files>
</Project> </Project>
@ -1044,3 +1046,4 @@
</Prebuild> </Prebuild>

View File

@ -1,9 +0,0 @@
CREATE TABLE `inventoryfolders` (
`folderID` varchar(36) NOT NULL default '',
`agentID` varchar(36) default NULL,
`parentFolderID` varchar(36) default NULL,
`folderName` varchar(64) default NULL,
PRIMARY KEY (`folderID`),
KEY `owner` (`agentID`),
KEY `parent` (`parentFolderID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -1,14 +0,0 @@
CREATE TABLE `inventoryitems` (
`inventoryID` varchar(36) NOT NULL default '',
`assetID` varchar(36) default NULL,
`type` int(11) default NULL,
`parentFolderID` varchar(36) default NULL,
`avatarID` varchar(36) default NULL,
`inventoryName` varchar(64) default NULL,
`inventoryDescription` varchar(64) default NULL,
`inventoryNextPermissions` int(10) unsigned default NULL,
`inventoryCurrentPermissions` int(10) unsigned default NULL,
PRIMARY KEY (`inventoryID`),
KEY `owner` (`avatarID`),
KEY `folder` (`parentFolderID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;