MySQLAssetData.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using libsecondlife;
  31. using MySql.Data.MySqlClient;
  32. using OpenSim.Framework.Console;
  33. namespace OpenSim.Framework.Data.MySQL
  34. {
  35. internal class MySQLAssetData : AssetDataBase, IPlugin
  36. {
  37. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  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. m_log.Info("[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. override 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 e)
  91. {
  92. m_log.ErrorFormat(
  93. "[ASSETS]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  94. + Environment.NewLine + "Attempting reconnection", assetID);
  95. _dbConnection.Reconnect();
  96. }
  97. }
  98. return asset;
  99. }
  100. override public void CreateAsset(AssetBase asset)
  101. {
  102. lock (_dbConnection)
  103. {
  104. MySqlCommand cmd =
  105. new MySqlCommand(
  106. "REPLACE INTO assets(id, name, description, assetType, invType, local, temporary, data)" +
  107. "VALUES(?id, ?name, ?description, ?assetType, ?invType, ?local, ?temporary, ?data)",
  108. _dbConnection.Connection);
  109. // need to ensure we dispose
  110. try
  111. {
  112. using (cmd)
  113. {
  114. MySqlParameter p = cmd.Parameters.Add("?id", MySqlDbType.Binary, 16);
  115. p.Value = asset.FullID.GetBytes();
  116. cmd.Parameters.AddWithValue("?name", asset.Name);
  117. cmd.Parameters.AddWithValue("?description", asset.Description);
  118. cmd.Parameters.AddWithValue("?assetType", asset.Type);
  119. cmd.Parameters.AddWithValue("?invType", asset.InvType);
  120. cmd.Parameters.AddWithValue("?local", asset.Local);
  121. cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
  122. cmd.Parameters.AddWithValue("?data", asset.Data);
  123. cmd.ExecuteNonQuery();
  124. cmd.Dispose();
  125. }
  126. }
  127. catch (Exception e)
  128. {
  129. m_log.ErrorFormat(
  130. "[ASSETS]: " +
  131. "MySql failure creating asset {0} with name {1}" + Environment.NewLine + e.ToString()
  132. + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
  133. _dbConnection.Reconnect();
  134. }
  135. }
  136. }
  137. override public void UpdateAsset(AssetBase asset)
  138. {
  139. CreateAsset(asset);
  140. }
  141. override public bool ExistsAsset(LLUUID uuid)
  142. {
  143. throw new Exception("The method or operation is not implemented.");
  144. }
  145. /// <summary>
  146. /// All writes are immediately commited to the database, so this is a no-op
  147. /// </summary>
  148. override public void CommitAssets()
  149. {
  150. }
  151. #endregion
  152. #region IPlugin Members
  153. override public void Initialise()
  154. {
  155. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  156. string hostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  157. string database = GridDataMySqlFile.ParseFileReadValue("database");
  158. string username = GridDataMySqlFile.ParseFileReadValue("username");
  159. string password = GridDataMySqlFile.ParseFileReadValue("password");
  160. string pooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  161. string port = GridDataMySqlFile.ParseFileReadValue("port");
  162. _dbConnection = new MySQLManager(hostname, database, username, password, pooling, port);
  163. TestTables();
  164. }
  165. override public string Version
  166. {
  167. get { return _dbConnection.getVersion(); }
  168. }
  169. override public string Name
  170. {
  171. get { return "MySQL Asset storage engine"; }
  172. }
  173. #endregion
  174. }
  175. }