diff options
Diffstat (limited to 'OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs')
-rw-r--r-- | OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs | 349 |
1 files changed, 0 insertions, 349 deletions
diff --git a/OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs b/OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs deleted file mode 100644 index 3da298b..0000000 --- a/OpenSim/Data/SQLiteLegacy/SQLiteAssetData.cs +++ /dev/null | |||
@@ -1,349 +0,0 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Data; | ||
30 | using System.Reflection; | ||
31 | using System.Collections.Generic; | ||
32 | using log4net; | ||
33 | using Mono.Data.SqliteClient; | ||
34 | using OpenMetaverse; | ||
35 | using OpenSim.Framework; | ||
36 | |||
37 | namespace 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 bool 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 | return true; | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | else | ||
147 | { | ||
148 | lock (this) | ||
149 | { | ||
150 | using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn)) | ||
151 | { | ||
152 | cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString())); | ||
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 | |||
160 | cmd.ExecuteNonQuery(); | ||
161 | return true; | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | |||
167 | // /// <summary> | ||
168 | // /// Some... logging functionnality | ||
169 | // /// </summary> | ||
170 | // /// <param name="asset"></param> | ||
171 | // private static void LogAssetLoad(AssetBase asset) | ||
172 | // { | ||
173 | // string temporary = asset.Temporary ? "Temporary" : "Stored"; | ||
174 | // string local = asset.Local ? "Local" : "Remote"; | ||
175 | // | ||
176 | // int assetLength = (asset.Data != null) ? asset.Data.Length : 0; | ||
177 | // | ||
178 | // m_log.Debug("[ASSET DB]: " + | ||
179 | // string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)", | ||
180 | // asset.FullID, asset.Name, asset.Description, asset.Type, | ||
181 | // temporary, local, assetLength)); | ||
182 | // } | ||
183 | |||
184 | /// <summary> | ||
185 | /// Check if an asset exist in database | ||
186 | /// </summary> | ||
187 | /// <param name="uuid">The asset UUID</param> | ||
188 | /// <returns>True if exist, or false.</returns> | ||
189 | override public bool ExistsAsset(UUID uuid) | ||
190 | { | ||
191 | lock (this) { | ||
192 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn)) | ||
193 | { | ||
194 | cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString())); | ||
195 | using (IDataReader reader = cmd.ExecuteReader()) | ||
196 | { | ||
197 | if (reader.Read()) | ||
198 | { | ||
199 | reader.Close(); | ||
200 | return true; | ||
201 | } | ||
202 | else | ||
203 | { | ||
204 | reader.Close(); | ||
205 | return false; | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | } | ||
211 | |||
212 | /// <summary> | ||
213 | /// Delete an asset from database | ||
214 | /// </summary> | ||
215 | /// <param name="uuid"></param> | ||
216 | public void DeleteAsset(UUID uuid) | ||
217 | { | ||
218 | using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn)) | ||
219 | { | ||
220 | cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString())); | ||
221 | |||
222 | cmd.ExecuteNonQuery(); | ||
223 | } | ||
224 | } | ||
225 | |||
226 | /// <summary> | ||
227 | /// | ||
228 | /// </summary> | ||
229 | /// <param name="row"></param> | ||
230 | /// <returns></returns> | ||
231 | private static AssetBase buildAsset(IDataReader row) | ||
232 | { | ||
233 | // TODO: this doesn't work yet because something more | ||
234 | // interesting has to be done to actually get these values | ||
235 | // back out. Not enough time to figure it out yet. | ||
236 | AssetBase asset = new AssetBase( | ||
237 | new UUID((String)row["UUID"]), | ||
238 | (String)row["Name"], | ||
239 | Convert.ToSByte(row["Type"]), | ||
240 | UUID.Zero.ToString() | ||
241 | ); | ||
242 | |||
243 | asset.Description = (String) row["Description"]; | ||
244 | asset.Local = Convert.ToBoolean(row["Local"]); | ||
245 | asset.Temporary = Convert.ToBoolean(row["Temporary"]); | ||
246 | asset.Data = (byte[]) row["Data"]; | ||
247 | return asset; | ||
248 | } | ||
249 | |||
250 | private static AssetMetadata buildAssetMetadata(IDataReader row) | ||
251 | { | ||
252 | AssetMetadata metadata = new AssetMetadata(); | ||
253 | |||
254 | metadata.FullID = new UUID((string) row["UUID"]); | ||
255 | metadata.Name = (string) row["Name"]; | ||
256 | metadata.Description = (string) row["Description"]; | ||
257 | metadata.Type = Convert.ToSByte(row["Type"]); | ||
258 | metadata.Temporary = Convert.ToBoolean(row["Temporary"]); // Not sure if this is correct. | ||
259 | |||
260 | // Current SHA1s are not stored/computed. | ||
261 | metadata.SHA1 = new byte[] {}; | ||
262 | |||
263 | return metadata; | ||
264 | } | ||
265 | |||
266 | /// <summary> | ||
267 | /// Returns a list of AssetMetadata objects. The list is a subset of | ||
268 | /// the entire data set offset by <paramref name="start" /> containing | ||
269 | /// <paramref name="count" /> elements. | ||
270 | /// </summary> | ||
271 | /// <param name="start">The number of results to discard from the total data set.</param> | ||
272 | /// <param name="count">The number of rows the returned list should contain.</param> | ||
273 | /// <returns>A list of AssetMetadata objects.</returns> | ||
274 | public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count) | ||
275 | { | ||
276 | List<AssetMetadata> retList = new List<AssetMetadata>(count); | ||
277 | |||
278 | lock (this) | ||
279 | { | ||
280 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn)) | ||
281 | { | ||
282 | cmd.Parameters.Add(new SqliteParameter(":start", start)); | ||
283 | cmd.Parameters.Add(new SqliteParameter(":count", count)); | ||
284 | |||
285 | using (IDataReader reader = cmd.ExecuteReader()) | ||
286 | { | ||
287 | while (reader.Read()) | ||
288 | { | ||
289 | AssetMetadata metadata = buildAssetMetadata(reader); | ||
290 | retList.Add(metadata); | ||
291 | } | ||
292 | } | ||
293 | } | ||
294 | } | ||
295 | |||
296 | return retList; | ||
297 | } | ||
298 | |||
299 | /*********************************************************************** | ||
300 | * | ||
301 | * Database Binding functions | ||
302 | * | ||
303 | * These will be db specific due to typing, and minor differences | ||
304 | * in databases. | ||
305 | * | ||
306 | **********************************************************************/ | ||
307 | |||
308 | #region IPlugin interface | ||
309 | |||
310 | /// <summary> | ||
311 | /// | ||
312 | /// </summary> | ||
313 | override public string Version | ||
314 | { | ||
315 | get | ||
316 | { | ||
317 | Module module = GetType().Module; | ||
318 | // string dllName = module.Assembly.ManifestModule.Name; | ||
319 | Version dllVersion = module.Assembly.GetName().Version; | ||
320 | |||
321 | return | ||
322 | string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, | ||
323 | dllVersion.Revision); | ||
324 | } | ||
325 | } | ||
326 | |||
327 | /// <summary> | ||
328 | /// Initialise the AssetData interface using default URI | ||
329 | /// </summary> | ||
330 | override public void Initialise() | ||
331 | { | ||
332 | Initialise("URI=file:Asset.db,version=3"); | ||
333 | } | ||
334 | |||
335 | /// <summary> | ||
336 | /// Name of this DB provider | ||
337 | /// </summary> | ||
338 | override public string Name | ||
339 | { | ||
340 | get { return "SQLite Asset storage engine"; } | ||
341 | } | ||
342 | |||
343 | public override bool Delete(string id) | ||
344 | { | ||
345 | return false; | ||
346 | } | ||
347 | #endregion | ||
348 | } | ||
349 | } | ||