MySQLAssetData.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 System.Reflection;
  31. using libsecondlife;
  32. using log4net;
  33. using MySql.Data.MySqlClient;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.MySQL
  36. {
  37. internal class MySQLAssetData : AssetDataBase, IPlugin
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private MySQLManager _dbConnection;
  41. #region IAssetProvider Members
  42. private void UpgradeAssetsTable(string oldVersion)
  43. {
  44. // null as the version, indicates that the table didn't exist
  45. if (oldVersion == null)
  46. {
  47. m_log.Info("[ASSETS DB]: Creating new database tables");
  48. _dbConnection.ExecuteResourceSql("CreateAssetsTable.sql");
  49. return;
  50. }
  51. }
  52. /// <summary>
  53. /// Ensure that the assets related tables exists and are at the latest version
  54. /// </summary>
  55. private void TestTables()
  56. {
  57. Dictionary<string, string> tableList = new Dictionary<string, string>();
  58. tableList["assets"] = null;
  59. _dbConnection.GetTableVersion(tableList);
  60. UpgradeAssetsTable(tableList["assets"]);
  61. }
  62. override public AssetBase FetchAsset(LLUUID assetID)
  63. {
  64. AssetBase asset = null;
  65. lock (_dbConnection)
  66. {
  67. MySqlCommand cmd =
  68. new MySqlCommand(
  69. "SELECT name, description, assetType, invType, local, temporary, data FROM assets WHERE id=?id",
  70. _dbConnection.Connection);
  71. MySqlParameter p = cmd.Parameters.Add("?id", MySqlDbType.Binary, 16);
  72. p.Value = assetID.GetBytes();
  73. try
  74. {
  75. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  76. {
  77. if (dbReader.Read())
  78. {
  79. asset = new AssetBase();
  80. asset.Data = (byte[]) dbReader["data"];
  81. asset.Description = (string) dbReader["description"];
  82. asset.FullID = assetID;
  83. asset.InvType = (sbyte) dbReader["invType"];
  84. asset.Local = ((sbyte) dbReader["local"]) != 0 ? true : false;
  85. asset.Name = (string) dbReader["name"];
  86. asset.Type = (sbyte) dbReader["assetType"];
  87. }
  88. dbReader.Close();
  89. cmd.Dispose();
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. m_log.ErrorFormat(
  95. "[ASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  96. + Environment.NewLine + "Attempting reconnection", assetID);
  97. _dbConnection.Reconnect();
  98. }
  99. }
  100. return asset;
  101. }
  102. override public void CreateAsset(AssetBase asset)
  103. {
  104. lock (_dbConnection)
  105. {
  106. MySqlCommand cmd =
  107. new MySqlCommand(
  108. "REPLACE INTO assets(id, name, description, assetType, invType, local, temporary, data)" +
  109. "VALUES(?id, ?name, ?description, ?assetType, ?invType, ?local, ?temporary, ?data)",
  110. _dbConnection.Connection);
  111. // need to ensure we dispose
  112. try
  113. {
  114. using (cmd)
  115. {
  116. MySqlParameter p = cmd.Parameters.Add("?id", MySqlDbType.Binary, 16);
  117. p.Value = asset.FullID.GetBytes();
  118. cmd.Parameters.AddWithValue("?name", asset.Name);
  119. cmd.Parameters.AddWithValue("?description", asset.Description);
  120. cmd.Parameters.AddWithValue("?assetType", asset.Type);
  121. cmd.Parameters.AddWithValue("?invType", asset.InvType);
  122. cmd.Parameters.AddWithValue("?local", asset.Local);
  123. cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
  124. cmd.Parameters.AddWithValue("?data", asset.Data);
  125. cmd.ExecuteNonQuery();
  126. cmd.Dispose();
  127. }
  128. }
  129. catch (Exception e)
  130. {
  131. m_log.ErrorFormat(
  132. "[ASSETS DB]: " +
  133. "MySql failure creating asset {0} with name {1}" + Environment.NewLine + e.ToString()
  134. + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
  135. _dbConnection.Reconnect();
  136. }
  137. }
  138. }
  139. override public void UpdateAsset(AssetBase asset)
  140. {
  141. CreateAsset(asset);
  142. }
  143. override public bool ExistsAsset(LLUUID uuid)
  144. {
  145. throw new Exception("The method or operation is not implemented.");
  146. }
  147. /// <summary>
  148. /// All writes are immediately commited to the database, so this is a no-op
  149. /// </summary>
  150. override public void CommitAssets()
  151. {
  152. }
  153. #endregion
  154. #region IPlugin Members
  155. override public void Initialise(string connect)
  156. {
  157. // TODO: This will let you pass in the connect string in
  158. // the config, though someone will need to write that.
  159. if (connect == String.Empty) {
  160. // This is old seperate config file
  161. Initialise();
  162. } else {
  163. _dbConnection = new MySQLManager(connect);
  164. TestTables();
  165. }
  166. }
  167. override public void Initialise()
  168. {
  169. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  170. string hostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  171. string database = GridDataMySqlFile.ParseFileReadValue("database");
  172. string username = GridDataMySqlFile.ParseFileReadValue("username");
  173. string password = GridDataMySqlFile.ParseFileReadValue("password");
  174. string pooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  175. string port = GridDataMySqlFile.ParseFileReadValue("port");
  176. _dbConnection = new MySQLManager(hostname, database, username, password, pooling, port);
  177. TestTables();
  178. }
  179. override public string Version
  180. {
  181. get { return _dbConnection.getVersion(); }
  182. }
  183. override public string Name
  184. {
  185. get { return "MySQL Asset storage engine"; }
  186. }
  187. #endregion
  188. }
  189. }