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