SQLiteAssetData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 Mono.Data.SqliteClient;
  32. using OpenSim.Framework.Console;
  33. namespace OpenSim.Framework.Data.SQLite
  34. {
  35. /// <summary>
  36. /// A User storage interface for the DB4o database system
  37. /// </summary>
  38. public class SQLiteAssetData : AssetDataBase
  39. {
  40. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// The database manager
  43. /// </summary>
  44. /// <summary>
  45. /// Artificial constructor called upon plugin load
  46. /// </summary>
  47. private const string SelectAssetSQL = "select * from assets where UUID=:UUID";
  48. private const string DeleteAssetSQL = "delete from assets where UUID=:UUID";
  49. private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, InvType, Local, Temporary, Data) values(:UUID, :Name, :Description, :Type, :InvType, :Local, :Temporary, :Data)";
  50. private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, InvType=:InvType, Local=:Local, Temporary=:Temporary, Data=:Data where UUID=:UUID";
  51. private const string assetSelect = "select * from assets";
  52. private SqliteConnection m_conn;
  53. public void Initialise(string dbfile, string dbname)
  54. {
  55. m_conn = new SqliteConnection("URI=file:" + dbfile + ",version=3");
  56. m_conn.Open();
  57. TestTables(m_conn);
  58. return;
  59. }
  60. override public AssetBase FetchAsset(LLUUID uuid)
  61. {
  62. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  63. {
  64. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  65. using (IDataReader reader = cmd.ExecuteReader())
  66. {
  67. if (reader.Read())
  68. {
  69. AssetBase asset = buildAsset(reader);
  70. reader.Close();
  71. return asset;
  72. }
  73. else
  74. {
  75. reader.Close();
  76. return null;
  77. }
  78. }
  79. }
  80. }
  81. override public void CreateAsset(AssetBase asset)
  82. {
  83. m_log.Info("[SQLITE]: Creating Asset " + Util.ToRawUuidString(asset.FullID));
  84. if (ExistsAsset(asset.FullID))
  85. {
  86. m_log.Info("[SQLITE]: Asset exists already, ignoring.");
  87. }
  88. else
  89. {
  90. using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
  91. {
  92. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID)));
  93. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  94. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  95. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  96. cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType));
  97. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  98. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  99. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  100. cmd.ExecuteNonQuery();
  101. }
  102. }
  103. }
  104. override public void UpdateAsset(AssetBase asset)
  105. {
  106. LogAssetLoad(asset);
  107. using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
  108. {
  109. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(asset.FullID)));
  110. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  111. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  112. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  113. cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType));
  114. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  115. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  116. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  117. cmd.ExecuteNonQuery();
  118. }
  119. }
  120. private void LogAssetLoad(AssetBase asset)
  121. {
  122. string temporary = asset.Temporary ? "Temporary" : "Stored";
  123. string local = asset.Local ? "Local" : "Remote";
  124. int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
  125. m_log.Info("[SQLITE]: " +
  126. string.Format("Loaded {6} {5} Asset: [{0}][{3}/{4}] \"{1}\":{2} ({7} bytes)",
  127. asset.FullID, asset.Name, asset.Description, asset.Type,
  128. asset.InvType, temporary, local, assetLength));
  129. }
  130. override public bool ExistsAsset(LLUUID uuid)
  131. {
  132. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  133. {
  134. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  135. using (IDataReader reader = cmd.ExecuteReader())
  136. {
  137. if(reader.Read())
  138. {
  139. reader.Close();
  140. return true;
  141. }
  142. else
  143. {
  144. reader.Close();
  145. return false;
  146. }
  147. }
  148. }
  149. }
  150. public void DeleteAsset(LLUUID uuid)
  151. {
  152. using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
  153. {
  154. cmd.Parameters.Add(new SqliteParameter(":UUID", Util.ToRawUuidString(uuid)));
  155. cmd.ExecuteNonQuery();
  156. }
  157. }
  158. override public void CommitAssets() // force a sync to the database
  159. {
  160. m_log.Info("[SQLITE]: Attempting commit");
  161. // lock (ds)
  162. // {
  163. // da.Update(ds, "assets");
  164. // ds.AcceptChanges();
  165. // }
  166. }
  167. /***********************************************************************
  168. *
  169. * Database Definition Functions
  170. *
  171. * This should be db agnostic as we define them in ADO.NET terms
  172. *
  173. **********************************************************************/
  174. private DataTable createAssetsTable()
  175. {
  176. DataTable assets = new DataTable("assets");
  177. SQLiteUtil.createCol(assets, "UUID", typeof (String));
  178. SQLiteUtil.createCol(assets, "Name", typeof (String));
  179. SQLiteUtil.createCol(assets, "Description", typeof (String));
  180. SQLiteUtil.createCol(assets, "Type", typeof (Int32));
  181. SQLiteUtil.createCol(assets, "InvType", typeof (Int32));
  182. SQLiteUtil.createCol(assets, "Local", typeof (Boolean));
  183. SQLiteUtil.createCol(assets, "Temporary", typeof (Boolean));
  184. SQLiteUtil.createCol(assets, "Data", typeof (Byte[]));
  185. // Add in contraints
  186. assets.PrimaryKey = new DataColumn[] {assets.Columns["UUID"]};
  187. return assets;
  188. }
  189. /***********************************************************************
  190. *
  191. * Convert between ADO.NET <=> OpenSim Objects
  192. *
  193. * These should be database independant
  194. *
  195. **********************************************************************/
  196. private AssetBase buildAsset(IDataReader row)
  197. {
  198. // TODO: this doesn't work yet because something more
  199. // interesting has to be done to actually get these values
  200. // back out. Not enough time to figure it out yet.
  201. AssetBase asset = new AssetBase();
  202. asset.FullID = new LLUUID((String) row["UUID"]);
  203. asset.Name = (String) row["Name"];
  204. asset.Description = (String) row["Description"];
  205. asset.Type = Convert.ToSByte(row["Type"]);
  206. asset.InvType = Convert.ToSByte(row["InvType"]);
  207. asset.Local = Convert.ToBoolean(row["Local"]);
  208. asset.Temporary = Convert.ToBoolean(row["Temporary"]);
  209. asset.Data = (byte[]) row["Data"];
  210. return asset;
  211. }
  212. /***********************************************************************
  213. *
  214. * Database Binding functions
  215. *
  216. * These will be db specific due to typing, and minor differences
  217. * in databases.
  218. *
  219. **********************************************************************/
  220. private void InitDB(SqliteConnection conn)
  221. {
  222. string createAssets = SQLiteUtil.defineTable(createAssetsTable());
  223. SqliteCommand pcmd = new SqliteCommand(createAssets, conn);
  224. pcmd.ExecuteNonQuery();
  225. }
  226. private bool TestTables(SqliteConnection conn)
  227. {
  228. SqliteCommand cmd = new SqliteCommand(assetSelect, conn);
  229. SqliteDataAdapter pDa = new SqliteDataAdapter(cmd);
  230. DataSet tmpDS = new DataSet();
  231. try
  232. {
  233. pDa.Fill(tmpDS, "assets");
  234. }
  235. catch (SqliteSyntaxException)
  236. {
  237. m_log.Info("[SQLITE]: SQLite Database doesn't exist... creating");
  238. InitDB(conn);
  239. }
  240. return true;
  241. }
  242. #region IPlugin interface
  243. override public string Version
  244. {
  245. get
  246. {
  247. Module module = GetType().Module;
  248. string dllName = module.Assembly.ManifestModule.Name;
  249. Version dllVersion = module.Assembly.GetName().Version;
  250. return
  251. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  252. dllVersion.Revision);
  253. }
  254. }
  255. override public void Initialise()
  256. {
  257. Initialise("AssetStorage.db", "");
  258. }
  259. override public string Name
  260. {
  261. get { return "SQLite Asset storage engine"; }
  262. }
  263. #endregion
  264. }
  265. }