aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs')
-rw-r--r--OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs343
1 files changed, 343 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs b/OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs
new file mode 100644
index 0000000..0d63dea
--- /dev/null
+++ b/OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs
@@ -0,0 +1,343 @@
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 OpenSimulator 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
28using System;
29using System.Data;
30using System.Reflection;
31using System.Collections.Generic;
32using log4net;
33using Mono.Data.SqliteClient;
34using OpenMetaverse;
35using OpenSim.Framework;
36
37namespace OpenSim.Data.SQLiteLegacy
38{
39 /// <summary>
40 /// An asset storage interface for the SQLite database system
41 /// </summary>
42 public class SQLiteAssetData : AssetDataBase
43 {
44// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45
46 private const string SelectAssetSQL = "select * from assets where UUID=:UUID";
47 private const string SelectAssetMetadataSQL = "select Name, Description, Type, Temporary, UUID from assets limit :start, :count";
48 private const string DeleteAssetSQL = "delete from assets where UUID=:UUID";
49 private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, Local, Temporary, Data) values(:UUID, :Name, :Description, :Type, :Local, :Temporary, :Data)";
50 private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, Local=:Local, Temporary=:Temporary, Data=:Data where UUID=:UUID";
51 private const string assetSelect = "select * from assets";
52
53 private SqliteConnection m_conn;
54
55 override public void Dispose()
56 {
57 if (m_conn != null)
58 {
59 m_conn.Close();
60 m_conn = null;
61 }
62 }
63
64 /// <summary>
65 /// <list type="bullet">
66 /// <item>Initialises AssetData interface</item>
67 /// <item>Loads and initialises a new SQLite connection and maintains it.</item>
68 /// <item>use default URI if connect string is empty.</item>
69 /// </list>
70 /// </summary>
71 /// <param name="dbconnect">connect string</param>
72 override public void Initialise(string dbconnect)
73 {
74 if (dbconnect == string.Empty)
75 {
76 dbconnect = "URI=file:Asset.db,version=3";
77 }
78 m_conn = new SqliteConnection(dbconnect);
79 m_conn.Open();
80
81 Assembly assem = GetType().Assembly;
82 Migration m = new Migration(m_conn, assem, "AssetStore");
83 m.Update();
84
85 return;
86 }
87
88 /// <summary>
89 /// Fetch Asset
90 /// </summary>
91 /// <param name="uuid">UUID of ... ?</param>
92 /// <returns>Asset base</returns>
93 override public AssetBase GetAsset(UUID uuid)
94 {
95 lock (this)
96 {
97 using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
98 {
99 cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
100 using (IDataReader reader = cmd.ExecuteReader())
101 {
102 if (reader.Read())
103 {
104 AssetBase asset = buildAsset(reader);
105 reader.Close();
106 return asset;
107 }
108 else
109 {
110 reader.Close();
111 return null;
112 }
113 }
114 }
115 }
116 }
117
118 /// <summary>
119 /// Create an asset
120 /// </summary>
121 /// <param name="asset">Asset Base</param>
122 override public void StoreAsset(AssetBase asset)
123 {
124 //m_log.Info("[ASSET DB]: Creating Asset " + asset.FullID.ToString());
125 if (ExistsAsset(asset.FullID))
126 {
127 //LogAssetLoad(asset);
128
129 lock (this)
130 {
131 using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
132 {
133 cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
134 cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
135 cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
136 cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
137 cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
138 cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
139 cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
140
141 cmd.ExecuteNonQuery();
142 }
143 }
144 }
145 else
146 {
147 lock (this)
148 {
149 using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
150 {
151 cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
152 cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
153 cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
154 cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
155 cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
156 cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
157 cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
158
159 cmd.ExecuteNonQuery();
160 }
161 }
162 }
163 }
164
165// /// <summary>
166// /// Some... logging functionnality
167// /// </summary>
168// /// <param name="asset"></param>
169// private static void LogAssetLoad(AssetBase asset)
170// {
171// string temporary = asset.Temporary ? "Temporary" : "Stored";
172// string local = asset.Local ? "Local" : "Remote";
173//
174// int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
175//
176// m_log.Debug("[ASSET DB]: " +
177// string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)",
178// asset.FullID, asset.Name, asset.Description, asset.Type,
179// temporary, local, assetLength));
180// }
181
182 /// <summary>
183 /// Check if an asset exist in database
184 /// </summary>
185 /// <param name="uuid">The asset UUID</param>
186 /// <returns>True if exist, or false.</returns>
187 override public bool ExistsAsset(UUID uuid)
188 {
189 lock (this) {
190 using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
191 {
192 cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
193 using (IDataReader reader = cmd.ExecuteReader())
194 {
195 if (reader.Read())
196 {
197 reader.Close();
198 return true;
199 }
200 else
201 {
202 reader.Close();
203 return false;
204 }
205 }
206 }
207 }
208 }
209
210 /// <summary>
211 /// Delete an asset from database
212 /// </summary>
213 /// <param name="uuid"></param>
214 public void DeleteAsset(UUID uuid)
215 {
216 using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
217 {
218 cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
219
220 cmd.ExecuteNonQuery();
221 }
222 }
223
224 /// <summary>
225 ///
226 /// </summary>
227 /// <param name="row"></param>
228 /// <returns></returns>
229 private static AssetBase buildAsset(IDataReader row)
230 {
231 // TODO: this doesn't work yet because something more
232 // interesting has to be done to actually get these values
233 // back out. Not enough time to figure it out yet.
234 AssetBase asset = new AssetBase(
235 new UUID((String)row["UUID"]),
236 (String)row["Name"],
237 Convert.ToSByte(row["Type"]),
238 UUID.Zero.ToString()
239 );
240
241 asset.Description = (String) row["Description"];
242 asset.Local = Convert.ToBoolean(row["Local"]);
243 asset.Temporary = Convert.ToBoolean(row["Temporary"]);
244 asset.Data = (byte[]) row["Data"];
245 return asset;
246 }
247
248 private static AssetMetadata buildAssetMetadata(IDataReader row)
249 {
250 AssetMetadata metadata = new AssetMetadata();
251
252 metadata.FullID = new UUID((string) row["UUID"]);
253 metadata.Name = (string) row["Name"];
254 metadata.Description = (string) row["Description"];
255 metadata.Type = Convert.ToSByte(row["Type"]);
256 metadata.Temporary = Convert.ToBoolean(row["Temporary"]); // Not sure if this is correct.
257
258 // Current SHA1s are not stored/computed.
259 metadata.SHA1 = new byte[] {};
260
261 return metadata;
262 }
263
264 /// <summary>
265 /// Returns a list of AssetMetadata objects. The list is a subset of
266 /// the entire data set offset by <paramref name="start" /> containing
267 /// <paramref name="count" /> elements.
268 /// </summary>
269 /// <param name="start">The number of results to discard from the total data set.</param>
270 /// <param name="count">The number of rows the returned list should contain.</param>
271 /// <returns>A list of AssetMetadata objects.</returns>
272 public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
273 {
274 List<AssetMetadata> retList = new List<AssetMetadata>(count);
275
276 lock (this)
277 {
278 using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn))
279 {
280 cmd.Parameters.Add(new SqliteParameter(":start", start));
281 cmd.Parameters.Add(new SqliteParameter(":count", count));
282
283 using (IDataReader reader = cmd.ExecuteReader())
284 {
285 while (reader.Read())
286 {
287 AssetMetadata metadata = buildAssetMetadata(reader);
288 retList.Add(metadata);
289 }
290 }
291 }
292 }
293
294 return retList;
295 }
296
297 /***********************************************************************
298 *
299 * Database Binding functions
300 *
301 * These will be db specific due to typing, and minor differences
302 * in databases.
303 *
304 **********************************************************************/
305
306 #region IPlugin interface
307
308 /// <summary>
309 ///
310 /// </summary>
311 override public string Version
312 {
313 get
314 {
315 Module module = GetType().Module;
316 // string dllName = module.Assembly.ManifestModule.Name;
317 Version dllVersion = module.Assembly.GetName().Version;
318
319 return
320 string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
321 dllVersion.Revision);
322 }
323 }
324
325 /// <summary>
326 /// Initialise the AssetData interface using default URI
327 /// </summary>
328 override public void Initialise()
329 {
330 Initialise("URI=file:Asset.db,version=3");
331 }
332
333 /// <summary>
334 /// Name of this DB provider
335 /// </summary>
336 override public string Name
337 {
338 get { return "SQLite Asset storage engine"; }
339 }
340
341 #endregion
342 }
343} \ No newline at end of file