From 1232eb1c587ffdc06c26a1c5b1b4fa5f22848754 Mon Sep 17 00:00:00 2001 From: Tleiades Hax Date: Sat, 13 Oct 2007 07:26:21 +0000 Subject: Asset server implementation. Again one of these "plumbing" releases, where no real functionality has been introduced, but ground work has been made, enabling the asset server, and preparing the sim server to query the asset server. Introduced an "IPlugin" interface, which plugins can inherit from. --- OpenSim/Framework/Data.MySQL/MySQLAssetData.cs | 112 +++++++++++++++++ OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs | 133 +++++++-------------- OpenSim/Framework/Data.MySQL/MySQLManager.cs | 92 +++++++++++++- .../Data.MySQL/Resources/CreateAssetsTable.sql | 11 ++ 4 files changed, 252 insertions(+), 96 deletions(-) create mode 100644 OpenSim/Framework/Data.MySQL/MySQLAssetData.cs create mode 100644 OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql (limited to 'OpenSim/Framework/Data.MySQL') diff --git a/OpenSim/Framework/Data.MySQL/MySQLAssetData.cs b/OpenSim/Framework/Data.MySQL/MySQLAssetData.cs new file mode 100644 index 0000000..70e04b6 --- /dev/null +++ b/OpenSim/Framework/Data.MySQL/MySQLAssetData.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using MySql.Data.MySqlClient; + +using libsecondlife; +using OpenSim.Framework.Console; +using OpenSim.Framework.Interfaces; +using OpenSim.Framework.Types; + +namespace OpenSim.Framework.Data.MySQL +{ + class MySQLAssetData : IAssetProvider + { + MySQLManager _dbConnection; + #region IAssetProvider Members + + private void UpgradeAssetsTable(string oldVersion) + { + // null as the version, indicates that the table didn't exist + if (oldVersion == null) + { + MainLog.Instance.Notice("ASSETS", "Creating new database tables"); + _dbConnection.ExecuteResourceSql("CreateAssetsTable.sql"); + return; + } + } + + /// + /// Ensure that the assets related tables exists and are at the latest version + /// + private void TestTables() + { + + Dictionary tableList = new Dictionary(); + + tableList["assets"] = null; + _dbConnection.GetTableVersion(tableList); + + UpgradeAssetsTable(tableList["assets"]); + + } + + public AssetBase FetchAsset(LLUUID uuid) + { + throw new Exception("The method or operation is not implemented."); + } + + public void CreateAsset(AssetBase asset) + { + MySqlCommand cmd = new MySqlCommand("REPLACE INTO assets(id, name, description, assetType, invType, local, temporary, data)" + + "VALUES(?id, ?name, ?description, ?assetType, ?invType, ?local, ?temporary, ?data)", _dbConnection.Connection); + MySqlParameter p = cmd.Parameters.Add("?id", MySqlDbType.Binary, 16); + p.Value = asset.FullID.GetBytes(); + cmd.Parameters.AddWithValue("?name", asset.Name); + cmd.Parameters.AddWithValue("?description", asset.Description); + cmd.Parameters.AddWithValue("?assetType", asset.Type); + cmd.Parameters.AddWithValue("?invType", asset.InvType); + cmd.Parameters.AddWithValue("?local", asset.Local); + cmd.Parameters.AddWithValue("?temporary", asset.Temporary); + cmd.Parameters.AddWithValue("?data", asset.Data); + cmd.ExecuteNonQuery(); + } + + public void UpdateAsset(AssetBase asset) + { + CreateAsset(asset); + } + + public bool ExistsAsset(LLUUID uuid) + { + throw new Exception("The method or operation is not implemented."); + } + + /// + /// All writes are immediately commited to the database, so this is a no-op + /// + public void CommitAssets() + { + } + + #endregion + + #region IPlugin Members + + public void Initialise() + { + IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini"); + string hostname = GridDataMySqlFile.ParseFileReadValue("hostname"); + string database = GridDataMySqlFile.ParseFileReadValue("database"); + string username = GridDataMySqlFile.ParseFileReadValue("username"); + string password = GridDataMySqlFile.ParseFileReadValue("password"); + string pooling = GridDataMySqlFile.ParseFileReadValue("pooling"); + string port = GridDataMySqlFile.ParseFileReadValue("port"); + + _dbConnection = new MySQLManager(hostname, database, username, password, pooling, port); + + TestTables(); + } + + public string Version + { + get { return _dbConnection.getVersion(); } + } + + public string Name + { + get { return "MySQL Asset storage engine"; } + } + + #endregion + } +} diff --git a/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs b/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs index 804fd5f..6423f28 100644 --- a/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs +++ b/OpenSim/Framework/Data.MySQL/MySQLInventoryData.cs @@ -28,7 +28,6 @@ using System; using System.IO; using System.Data; -using System.Reflection; using System.Collections.Generic; using libsecondlife; using OpenSim.Framework.Types; @@ -65,41 +64,13 @@ namespace OpenSim.Framework.Data.MySQL } #region Test and initialization code - /// - /// Extract a named string resource from the embedded resources - /// - /// name of embedded resource - /// string contained within the embedded resource - 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) + private void UpgradeFoldersTable(string oldVersion) { // null as the version, indicates that the table didn't exist if (oldVersion == null) { - ExecuteResourceSql(conn, "CreateFoldersTable.sql"); + database.ExecuteResourceSql("CreateFoldersTable.sql"); return; } @@ -107,15 +78,15 @@ namespace OpenSim.Framework.Data.MySQL if (oldVersion == "Rev. 2") return; - ExecuteResourceSql(conn, "UpgradeFoldersTableToVersion2.sql"); + database.ExecuteResourceSql("UpgradeFoldersTableToVersion2.sql"); } - private void UpgradeItemsTable(MySqlConnection conn, string oldVersion) + private void UpgradeItemsTable(string oldVersion) { // null as the version, indicates that the table didn't exist if (oldVersion == null) { - ExecuteResourceSql(conn, "CreateItemsTable.sql"); + database.ExecuteResourceSql("CreateItemsTable.sql"); return; } @@ -123,7 +94,7 @@ namespace OpenSim.Framework.Data.MySQL if (oldVersion == "Rev. 2") return; - ExecuteResourceSql(conn, "UpgradeItemsTableToVersion2.sql"); + database.ExecuteResourceSql("UpgradeItemsTableToVersion2.sql"); } private void TestTables(MySqlConnection conn) @@ -134,25 +105,10 @@ namespace OpenSim.Framework.Data.MySQL 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(); + database.GetTableVersion(tableList); - UpgradeFoldersTable(conn, tableList["inventoryfolders"]); - UpgradeItemsTable(conn, tableList["inventoryitems"]); + UpgradeFoldersTable(tableList["inventoryfolders"]); + UpgradeItemsTable(tableList["inventoryitems"]); } #endregion @@ -179,12 +135,7 @@ namespace OpenSim.Framework.Data.MySQL /// A string containing the DB provider public string getVersion() { - 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); + return database.getVersion(); } /// @@ -201,7 +152,7 @@ namespace OpenSim.Framework.Data.MySQL List items = new List(); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); while(reader.Read()) @@ -233,8 +184,8 @@ namespace OpenSim.Framework.Data.MySQL lock (database) { MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", user.ToStringHyphenated()); - result.Parameters.Add("?zero", LLUUID.Zero.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", user.ToStringHyphenated()); + result.Parameters.AddWithValue("?zero", LLUUID.Zero.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); List items = new List(); @@ -267,13 +218,9 @@ namespace OpenSim.Framework.Data.MySQL { lock (database) { - Dictionary param = new Dictionary(); - param["?uuid"] = user.ToStringHyphenated(); - param["?zero"] = LLUUID.Zero.ToStringHyphenated(); - MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", user.ToStringHyphenated()); - result.Parameters.Add("?zero", LLUUID.Zero.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", user.ToStringHyphenated()); + result.Parameters.AddWithValue("?zero", LLUUID.Zero.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); @@ -308,7 +255,7 @@ namespace OpenSim.Framework.Data.MySQL lock (database) { MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", parentID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", parentID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); List items = new List(); @@ -378,7 +325,7 @@ namespace OpenSim.Framework.Data.MySQL Dictionary param = new Dictionary(); MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", itemID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", itemID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); InventoryItemBase item = null; @@ -438,7 +385,7 @@ namespace OpenSim.Framework.Data.MySQL lock (database) { MySqlCommand result = new MySqlCommand("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", database.Connection); - result.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + result.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); MySqlDataReader reader = result.ExecuteReader(); reader.Read(); @@ -469,19 +416,19 @@ namespace OpenSim.Framework.Data.MySQL try { 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.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(); } @@ -509,7 +456,7 @@ namespace OpenSim.Framework.Data.MySQL try { MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE inventoryID=?uuid", database.Connection); - cmd.Parameters.Add("?uuid", itemID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?uuid", itemID.ToStringHyphenated()); cmd.ExecuteNonQuery(); } catch (MySqlException e) @@ -529,12 +476,12 @@ namespace OpenSim.Framework.Data.MySQL 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); + 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 { @@ -590,7 +537,7 @@ namespace OpenSim.Framework.Data.MySQL try { MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryfolders WHERE folderID=?uuid", database.Connection); - cmd.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); cmd.ExecuteNonQuery(); } catch (MySqlException e) @@ -605,7 +552,7 @@ namespace OpenSim.Framework.Data.MySQL try { MySqlCommand cmd = new MySqlCommand("DELETE FROM inventoryitems WHERE parentFolderID=?uuid", database.Connection); - cmd.Parameters.Add("?uuid", folderID.ToStringHyphenated()); + cmd.Parameters.AddWithValue("?uuid", folderID.ToStringHyphenated()); cmd.ExecuteNonQuery(); } catch (MySqlException e) diff --git a/OpenSim/Framework/Data.MySQL/MySQLManager.cs b/OpenSim/Framework/Data.MySQL/MySQLManager.cs index ea174b2..d3f6976 100644 --- a/OpenSim/Framework/Data.MySQL/MySQLManager.cs +++ b/OpenSim/Framework/Data.MySQL/MySQLManager.cs @@ -26,10 +26,14 @@ * */ using System; -using System.Collections.Generic; +using System.IO; using System.Data; +using System.Reflection; +using System.Collections.Generic; using libsecondlife; + using MySql.Data.MySqlClient; + using OpenSim.Framework.Types; using OpenSim.Framework.Console; @@ -114,6 +118,88 @@ namespace OpenSim.Framework.Data.MySQL } /// + /// Returns the version of this DB provider + /// + /// A string containing the DB provider + public string getVersion() + { + 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); + } + + + /// + /// Extract a named string resource from the embedded resources + /// + /// name of embedded resource + /// string contained within the embedded resource + 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)); + } + + /// + /// Execute a SQL statement stored in a resource, as a string + /// + /// + public void ExecuteResourceSql(string name) + { + MySqlCommand cmd = new MySqlCommand(getResourceString(name), dbcon); + cmd.ExecuteNonQuery(); + } + + /// + /// Given a list of tables, return the version of the tables, as seen in the database + /// + /// + public void GetTableVersion(Dictionary tableList) + { + lock (dbcon) + { + MySqlCommand tablesCmd = new MySqlCommand("SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?dbname", dbcon); + tablesCmd.Parameters.AddWithValue("?dbname", dbcon.Database); + using (MySqlDataReader tables = tablesCmd.ExecuteReader()) + { + while (tables.Read()) + { + try + { + string tableName = (string)tables["TABLE_NAME"]; + string comment = (string)tables["TABLE_COMMENT"]; + if(tableList.ContainsKey(tableName)) + tableList[tableName] = comment; + } + catch (Exception e) + { + MainLog.Instance.Error(e.ToString()); + } + } + tables.Close(); + } + } + } + + + // at some time this code should be cleaned up + + /// /// Runs a query with protection against SQL Injection by using parameterised input. /// /// The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y @@ -127,7 +213,7 @@ namespace OpenSim.Framework.Data.MySQL dbcommand.CommandText = sql; foreach (KeyValuePair param in parameters) { - dbcommand.Parameters.Add(param.Key, param.Value); + dbcommand.Parameters.AddWithValue(param.Key, param.Value); } return (IDbCommand)dbcommand; @@ -161,7 +247,7 @@ namespace OpenSim.Framework.Data.MySQL dbcommand.CommandText = sql; foreach (KeyValuePair param in parameters) { - dbcommand.Parameters.Add(param.Key, param.Value); + dbcommand.Parameters.AddWithValue(param.Key, param.Value); } return (IDbCommand)dbcommand; diff --git a/OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql b/OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql new file mode 100644 index 0000000..049a3a2 --- /dev/null +++ b/OpenSim/Framework/Data.MySQL/Resources/CreateAssetsTable.sql @@ -0,0 +1,11 @@ +CREATE TABLE `assets` ( + `id` binary(16) NOT NULL, + `name` varchar(64) NOT NULL, + `description` varchar(64) NOT NULL, + `assetType` smallint(5) unsigned NOT NULL, + `invType` smallint(5) unsigned NOT NULL, + `local` tinyint(1) NOT NULL, + `temporary` tinyint(1) NOT NULL, + `data` longblob NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Rev. 1'; \ No newline at end of file -- cgit v1.1