MySQLAssetData.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.Collections.Generic;
  30. using System.Data;
  31. using libsecondlife;
  32. using MySql.Data.MySqlClient;
  33. using OpenSim.Framework.Console;
  34. namespace OpenSim.Framework.Data.MySQL
  35. {
  36. internal class MySQLAssetData : IAssetProvider
  37. {
  38. private MySQLManager _dbConnection;
  39. #region IAssetProvider Members
  40. private void UpgradeAssetsTable(string oldVersion)
  41. {
  42. // null as the version, indicates that the table didn't exist
  43. if (oldVersion == null)
  44. {
  45. MainLog.Instance.Notice("ASSETS", "Creating new database tables");
  46. _dbConnection.ExecuteResourceSql("CreateAssetsTable.sql");
  47. return;
  48. }
  49. }
  50. /// <summary>
  51. /// Ensure that the assets related tables exists and are at the latest version
  52. /// </summary>
  53. private void TestTables()
  54. {
  55. Dictionary<string, string> tableList = new Dictionary<string, string>();
  56. tableList["assets"] = null;
  57. _dbConnection.GetTableVersion(tableList);
  58. UpgradeAssetsTable(tableList["assets"]);
  59. }
  60. public AssetBase FetchAsset(LLUUID assetID)
  61. {
  62. AssetBase asset = null;
  63. lock (_dbConnection)
  64. {
  65. MySqlCommand cmd =
  66. new MySqlCommand(
  67. "SELECT name, description, assetType, invType, local, temporary, data FROM assets WHERE id=?id",
  68. _dbConnection.Connection);
  69. MySqlParameter p = cmd.Parameters.Add("?id", MySqlDbType.Binary, 16);
  70. p.Value = assetID.GetBytes();
  71. try
  72. {
  73. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  74. {
  75. if (dbReader.Read())
  76. {
  77. asset = new AssetBase();
  78. asset.Data = (byte[]) dbReader["data"];
  79. asset.Description = (string) dbReader["description"];
  80. asset.FullID = assetID;
  81. asset.InvType = (sbyte) dbReader["invType"];
  82. asset.Local = ((sbyte) dbReader["local"]) != 0 ? true : false;
  83. asset.Name = (string) dbReader["name"];
  84. asset.Type = (sbyte) dbReader["assetType"];
  85. }
  86. dbReader.Close();
  87. cmd.Dispose();
  88. }
  89. }
  90. catch (Exception)
  91. {
  92. MainLog.Instance.Warn("ASSETS", "MySql failure fetching asset");
  93. }
  94. }
  95. return asset;
  96. }
  97. public void CreateAsset(AssetBase asset)
  98. {
  99. MySqlCommand cmd =
  100. new MySqlCommand(
  101. "REPLACE INTO assets(id, name, description, assetType, invType, local, temporary, data)" +
  102. "VALUES(?id, ?name, ?description, ?assetType, ?invType, ?local, ?temporary, ?data)",
  103. _dbConnection.Connection);
  104. // need to ensure we dispose
  105. using (cmd)
  106. {
  107. MySqlParameter p = cmd.Parameters.Add("?id", MySqlDbType.Binary, 16);
  108. p.Value = asset.FullID.GetBytes();
  109. cmd.Parameters.AddWithValue("?name", asset.Name);
  110. cmd.Parameters.AddWithValue("?description", asset.Description);
  111. cmd.Parameters.AddWithValue("?assetType", asset.Type);
  112. cmd.Parameters.AddWithValue("?invType", asset.InvType);
  113. cmd.Parameters.AddWithValue("?local", asset.Local);
  114. cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
  115. cmd.Parameters.AddWithValue("?data", asset.Data);
  116. cmd.ExecuteNonQuery();
  117. cmd.Dispose();
  118. }
  119. }
  120. public void UpdateAsset(AssetBase asset)
  121. {
  122. CreateAsset(asset);
  123. }
  124. public bool ExistsAsset(LLUUID uuid)
  125. {
  126. throw new Exception("The method or operation is not implemented.");
  127. }
  128. // rex, new function, fixme not implemented
  129. public List<AssetBase> GetAssetList(int vAssetType)
  130. {
  131. List<AssetBase> retvals = new List<AssetBase>();
  132. return retvals;
  133. }
  134. /// <summary>
  135. /// All writes are immediately commited to the database, so this is a no-op
  136. /// </summary>
  137. public void CommitAssets()
  138. {
  139. }
  140. // rex new function for "replace assets" functionality
  141. // TODO: actual implementation by someone, should return LLUUID of an asset
  142. // with matching type & name, or zero if not in DB
  143. public LLUUID ExistsAsset(sbyte type, string name)
  144. {
  145. return LLUUID.Zero;
  146. }
  147. #endregion
  148. #region IPlugin Members
  149. public void Initialise()
  150. {
  151. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  152. string hostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  153. string database = GridDataMySqlFile.ParseFileReadValue("database");
  154. string username = GridDataMySqlFile.ParseFileReadValue("username");
  155. string password = GridDataMySqlFile.ParseFileReadValue("password");
  156. string pooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  157. string port = GridDataMySqlFile.ParseFileReadValue("port");
  158. _dbConnection = new MySQLManager(hostname, database, username, password, pooling, port);
  159. TestTables();
  160. }
  161. public string Version
  162. {
  163. get { return _dbConnection.getVersion(); }
  164. }
  165. public string Name
  166. {
  167. get { return "MySQL Asset storage engine"; }
  168. }
  169. #endregion
  170. }
  171. }