SQLiteAssetData.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 libsecondlife;
  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. /// <summary>
  56. /// <list type="bullet">
  57. /// <item>Initialises AssetData interface</item>
  58. /// <item>Loads and initialises a new SQLite connection and maintains it.</item>
  59. /// <item>use default URI if connect string is empty.</item>
  60. /// </list>
  61. /// </summary>
  62. /// <param name="dbconnect">connect string</param>
  63. override public void Initialise(string dbconnect)
  64. {
  65. if (dbconnect == string.Empty)
  66. {
  67. dbconnect = "URI=file:AssetStorage.db,version=3";
  68. }
  69. m_conn = new SqliteConnection(dbconnect);
  70. m_conn.Open();
  71. Assembly assem = GetType().Assembly;
  72. Migration m = new Migration(m_conn, assem, "AssetStore");
  73. // TODO: remove this next line after changeset 6000,
  74. // people should have all gotten into the migration swing
  75. // again.
  76. TestTables(m_conn, m);
  77. m.Update();
  78. return;
  79. }
  80. /// <summary>
  81. /// Fetch Asset
  82. /// </summary>
  83. /// <param name="uuid">UUID of ... ?</param>
  84. /// <returns>Asset base</returns>
  85. override public AssetBase FetchAsset(LLUUID uuid)
  86. {
  87. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  88. {
  89. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  90. using (IDataReader reader = cmd.ExecuteReader())
  91. {
  92. if (reader.Read())
  93. {
  94. AssetBase asset = buildAsset(reader);
  95. reader.Close();
  96. return asset;
  97. }
  98. else
  99. {
  100. reader.Close();
  101. return null;
  102. }
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// Create an asset
  108. /// </summary>
  109. /// <param name="asset">Asset Base</param>
  110. override public void CreateAsset(AssetBase asset)
  111. {
  112. m_log.Info("[ASSET DB]: Creating Asset " + Util.ToRawUuidString(asset.FullID));
  113. if (ExistsAsset(asset.FullID))
  114. {
  115. m_log.Info("[ASSET DB]: Asset exists already, ignoring.");
  116. }
  117. else
  118. {
  119. using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
  120. {
  121. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID)));
  122. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  123. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  124. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  125. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  126. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  127. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  128. cmd.ExecuteNonQuery();
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Update an asset
  134. /// </summary>
  135. /// <param name="asset"></param>
  136. override public void UpdateAsset(AssetBase asset)
  137. {
  138. LogAssetLoad(asset);
  139. using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
  140. {
  141. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID)));
  142. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  143. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  144. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  145. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  146. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  147. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  148. cmd.ExecuteNonQuery();
  149. }
  150. }
  151. /// <summary>
  152. /// Some... logging functionnality
  153. /// </summary>
  154. /// <param name="asset"></param>
  155. private static void LogAssetLoad(AssetBase asset)
  156. {
  157. string temporary = asset.Temporary ? "Temporary" : "Stored";
  158. string local = asset.Local ? "Local" : "Remote";
  159. int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
  160. m_log.Info("[ASSET DB]: " +
  161. string.Format("Loaded {6} {5} Asset: [{0}][{3}] \"{1}\":{2} ({7} bytes)",
  162. asset.FullID, asset.Name, asset.Description, asset.Type,
  163. temporary, local, assetLength));
  164. }
  165. /// <summary>
  166. /// Check if an asset exist in database
  167. /// </summary>
  168. /// <param name="uuid">The asset UUID</param>
  169. /// <returns>True if exist, or false.</returns>
  170. override public bool ExistsAsset(LLUUID uuid)
  171. {
  172. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  173. {
  174. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  175. using (IDataReader reader = cmd.ExecuteReader())
  176. {
  177. if (reader.Read())
  178. {
  179. reader.Close();
  180. return true;
  181. }
  182. else
  183. {
  184. reader.Close();
  185. return false;
  186. }
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// Delete an asset from database
  192. /// </summary>
  193. /// <param name="uuid"></param>
  194. public void DeleteAsset(LLUUID uuid)
  195. {
  196. using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
  197. {
  198. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  199. cmd.ExecuteNonQuery();
  200. }
  201. }
  202. /***********************************************************************
  203. *
  204. * Database Definition Functions
  205. *
  206. * This should be db agnostic as we define them in ADO.NET terms
  207. *
  208. **********************************************************************/
  209. /// <summary>
  210. /// Create the "assets" table
  211. /// </summary>
  212. /// <returns></returns>
  213. // private static DataTable createAssetsTable()
  214. // {
  215. // DataTable assets = new DataTable("assets");
  216. // SQLiteUtil.createCol(assets, "UUID", typeof (String));
  217. // SQLiteUtil.createCol(assets, "Name", typeof (String));
  218. // SQLiteUtil.createCol(assets, "Description", typeof (String));
  219. // SQLiteUtil.createCol(assets, "Type", typeof (Int32));
  220. // SQLiteUtil.createCol(assets, "Local", typeof (Boolean));
  221. // SQLiteUtil.createCol(assets, "Temporary", typeof (Boolean));
  222. // SQLiteUtil.createCol(assets, "Data", typeof (Byte[]));
  223. // // Add in contraints
  224. // assets.PrimaryKey = new DataColumn[] {assets.Columns["UUID"]};
  225. // return assets;
  226. // }
  227. /***********************************************************************
  228. *
  229. * Convert between ADO.NET <=> OpenSim Objects
  230. *
  231. * These should be database independant
  232. *
  233. **********************************************************************/
  234. /// <summary>
  235. ///
  236. /// </summary>
  237. /// <param name="row"></param>
  238. /// <returns></returns>
  239. private static AssetBase buildAsset(IDataReader row)
  240. {
  241. // TODO: this doesn't work yet because something more
  242. // interesting has to be done to actually get these values
  243. // back out. Not enough time to figure it out yet.
  244. AssetBase asset = new AssetBase();
  245. asset.FullID = new LLUUID((String) row["UUID"]);
  246. asset.Name = (String) row["Name"];
  247. asset.Description = (String) row["Description"];
  248. asset.Type = Convert.ToSByte(row["Type"]);
  249. asset.Local = Convert.ToBoolean(row["Local"]);
  250. asset.Temporary = Convert.ToBoolean(row["Temporary"]);
  251. asset.Data = (byte[]) row["Data"];
  252. return asset;
  253. }
  254. /***********************************************************************
  255. *
  256. * Database Binding functions
  257. *
  258. * These will be db specific due to typing, and minor differences
  259. * in databases.
  260. *
  261. **********************************************************************/
  262. /// <summary>
  263. ///
  264. /// </summary>
  265. /// <param name="conn"></param>
  266. // private static void InitDB(SqliteConnection conn)
  267. // {
  268. // string createAssets = SQLiteUtil.defineTable(createAssetsTable());
  269. // SqliteCommand pcmd = new SqliteCommand(createAssets, conn);
  270. // pcmd.ExecuteNonQuery();
  271. // }
  272. /// <summary>
  273. ///
  274. /// </summary>
  275. /// <param name="conn"></param>
  276. /// <param name="m"></param>
  277. /// <returns></returns>
  278. private static bool TestTables(SqliteConnection conn, Migration m)
  279. {
  280. SqliteCommand cmd = new SqliteCommand(assetSelect, conn);
  281. SqliteDataAdapter pDa = new SqliteDataAdapter(cmd);
  282. DataSet tmpDS = new DataSet();
  283. try
  284. {
  285. pDa.Fill(tmpDS, "assets");
  286. }
  287. catch (SqliteSyntaxException)
  288. {
  289. m_log.Info("[ASSET DB]: SQLite Database doesn't exist... creating");
  290. return false;
  291. }
  292. // if the tables are here, and we don't have a migration,
  293. // set it to 1, as we're migrating off of legacy bits
  294. if (m.Version == 0)
  295. m.Version = 1;
  296. return true;
  297. }
  298. #region IPlugin interface
  299. /// <summary>
  300. ///
  301. /// </summary>
  302. override public string Version
  303. {
  304. get
  305. {
  306. Module module = GetType().Module;
  307. // string dllName = module.Assembly.ManifestModule.Name;
  308. Version dllVersion = module.Assembly.GetName().Version;
  309. return
  310. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  311. dllVersion.Revision);
  312. }
  313. }
  314. /// <summary>
  315. /// Initialise the AssetData interface using default URI
  316. /// </summary>
  317. override public void Initialise()
  318. {
  319. Initialise("URI=file:AssetStorage.db,version=3");
  320. }
  321. /// <summary>
  322. /// Name of this DB provider
  323. /// </summary>
  324. override public string Name
  325. {
  326. get { return "SQLite Asset storage engine"; }
  327. }
  328. #endregion
  329. }
  330. }