SQLiteAssetData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.Data;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using log4net;
  32. using Mono.Data.SqliteClient;
  33. using OpenSim.Framework;
  34. namespace OpenSim.Data.SQLite
  35. {
  36. /// <summary>
  37. /// A User storage interface for the DB4o database system
  38. /// </summary>
  39. public class SQLiteAssetData : AssetDataBase
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. /// <summary>
  43. /// The database manager
  44. /// </summary>
  45. /// <summary>
  46. /// Artificial constructor called upon plugin load
  47. /// </summary>
  48. private const string SelectAssetSQL = "select * from assets where UUID=:UUID";
  49. private const string DeleteAssetSQL = "delete from assets where UUID=:UUID";
  50. private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, Local, Temporary, Data) values(:UUID, :Name, :Description, :Type, :Local, :Temporary, :Data)";
  51. private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, Local=:Local, Temporary=:Temporary, Data=:Data where UUID=:UUID";
  52. private const string assetSelect = "select * from assets";
  53. private SqliteConnection m_conn;
  54. override public void Dispose()
  55. {
  56. if (m_conn != null)
  57. {
  58. m_conn.Close();
  59. m_conn = null;
  60. }
  61. }
  62. /// <summary>
  63. /// <list type="bullet">
  64. /// <item>Initialises AssetData interface</item>
  65. /// <item>Loads and initialises a new SQLite connection and maintains it.</item>
  66. /// <item>use default URI if connect string is empty.</item>
  67. /// </list>
  68. /// </summary>
  69. /// <param name="dbconnect">connect string</param>
  70. override public void Initialise(string dbconnect)
  71. {
  72. if (dbconnect == string.Empty)
  73. {
  74. dbconnect = "URI=file:AssetStorage.db,version=3";
  75. }
  76. m_conn = new SqliteConnection(dbconnect);
  77. m_conn.Open();
  78. Assembly assem = GetType().Assembly;
  79. Migration m = new Migration(m_conn, assem, "AssetStore");
  80. m.Update();
  81. return;
  82. }
  83. /// <summary>
  84. /// Fetch Asset
  85. /// </summary>
  86. /// <param name="uuid">UUID of ... ?</param>
  87. /// <returns>Asset base</returns>
  88. override public AssetBase FetchAsset(UUID uuid)
  89. {
  90. lock (this)
  91. {
  92. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  93. {
  94. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  95. using (IDataReader reader = cmd.ExecuteReader())
  96. {
  97. if (reader.Read())
  98. {
  99. AssetBase asset = buildAsset(reader);
  100. reader.Close();
  101. return asset;
  102. }
  103. else
  104. {
  105. reader.Close();
  106. return null;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// Create an asset
  114. /// </summary>
  115. /// <param name="asset">Asset Base</param>
  116. override public void CreateAsset(AssetBase asset)
  117. {
  118. //m_log.Info("[ASSET DB]: Creating Asset " + Util.ToRawUuidString(asset.FullID));
  119. if (ExistsAsset(asset.FullID))
  120. {
  121. //m_log.Info("[ASSET DB]: Asset exists already, ignoring.");
  122. }
  123. else
  124. {
  125. lock (this)
  126. {
  127. using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
  128. {
  129. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID)));
  130. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  131. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  132. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  133. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  134. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  135. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  136. cmd.ExecuteNonQuery();
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Update an asset
  143. /// </summary>
  144. /// <param name="asset"></param>
  145. override public void UpdateAsset(AssetBase asset)
  146. {
  147. LogAssetLoad(asset);
  148. lock (this)
  149. {
  150. using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
  151. {
  152. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID)));
  153. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  154. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  155. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  156. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  157. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  158. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  159. cmd.ExecuteNonQuery();
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// Some... logging functionnality
  165. /// </summary>
  166. /// <param name="asset"></param>
  167. private static void LogAssetLoad(AssetBase asset)
  168. {
  169. string temporary = asset.Temporary ? "Temporary" : "Stored";
  170. string local = asset.Local ? "Local" : "Remote";
  171. int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
  172. m_log.Info("[ASSET DB]: " +
  173. string.Format("Loaded {6} {5} Asset: [{0}][{3}] \"{1}\":{2} ({7} bytes)",
  174. asset.FullID, asset.Name, asset.Description, asset.Type,
  175. temporary, local, assetLength));
  176. }
  177. /// <summary>
  178. /// Check if an asset exist in database
  179. /// </summary>
  180. /// <param name="uuid">The asset UUID</param>
  181. /// <returns>True if exist, or false.</returns>
  182. override public bool ExistsAsset(UUID uuid)
  183. {
  184. lock (this) {
  185. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  186. {
  187. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  188. using (IDataReader reader = cmd.ExecuteReader())
  189. {
  190. if (reader.Read())
  191. {
  192. reader.Close();
  193. return true;
  194. }
  195. else
  196. {
  197. reader.Close();
  198. return false;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// Delete an asset from database
  206. /// </summary>
  207. /// <param name="uuid"></param>
  208. public void DeleteAsset(UUID uuid)
  209. {
  210. using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
  211. {
  212. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  213. cmd.ExecuteNonQuery();
  214. }
  215. }
  216. /// <summary>
  217. ///
  218. /// </summary>
  219. /// <param name="row"></param>
  220. /// <returns></returns>
  221. private static AssetBase buildAsset(IDataReader row)
  222. {
  223. // TODO: this doesn't work yet because something more
  224. // interesting has to be done to actually get these values
  225. // back out. Not enough time to figure it out yet.
  226. AssetBase asset = new AssetBase();
  227. asset.FullID = new UUID((String) row["UUID"]);
  228. asset.Name = (String) row["Name"];
  229. asset.Description = (String) row["Description"];
  230. asset.Type = Convert.ToSByte(row["Type"]);
  231. asset.Local = Convert.ToBoolean(row["Local"]);
  232. asset.Temporary = Convert.ToBoolean(row["Temporary"]);
  233. asset.Data = (byte[]) row["Data"];
  234. return asset;
  235. }
  236. /***********************************************************************
  237. *
  238. * Database Binding functions
  239. *
  240. * These will be db specific due to typing, and minor differences
  241. * in databases.
  242. *
  243. **********************************************************************/
  244. #region IPlugin interface
  245. /// <summary>
  246. ///
  247. /// </summary>
  248. override public string Version
  249. {
  250. get
  251. {
  252. Module module = GetType().Module;
  253. // string dllName = module.Assembly.ManifestModule.Name;
  254. Version dllVersion = module.Assembly.GetName().Version;
  255. return
  256. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  257. dllVersion.Revision);
  258. }
  259. }
  260. /// <summary>
  261. /// Initialise the AssetData interface using default URI
  262. /// </summary>
  263. override public void Initialise()
  264. {
  265. Initialise("URI=file:AssetStorage.db,version=3");
  266. }
  267. /// <summary>
  268. /// Name of this DB provider
  269. /// </summary>
  270. override public string Name
  271. {
  272. get { return "SQLite Asset storage engine"; }
  273. }
  274. #endregion
  275. }
  276. }