30 using System.Reflection;
31 using System.Collections.Generic;
34 using Community.CsharpSqlite.Sqlite;
36 using Mono.Data.Sqlite;
40 using OpenSim.Framework;
42 namespace OpenSim.Data.SQLite
49 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51 private const string SelectAssetSQL =
"select * from assets where UUID=:UUID";
52 private const string SelectAssetMetadataSQL =
"select Name, Description, Type, Temporary, asset_flags, UUID, CreatorID from assets limit :start, :count";
53 private const string DeleteAssetSQL =
"delete from assets where UUID=:UUID";
54 private const string InsertAssetSQL =
"insert into assets(UUID, Name, Description, Type, Local, Temporary, asset_flags, CreatorID, Data) values(:UUID, :Name, :Description, :Type, :Local, :Temporary, :Flags, :CreatorID, :Data)";
55 private const string UpdateAssetSQL =
"update assets set Name=:Name, Description=:Description, Type=:Type, Local=:Local, Temporary=:Temporary, asset_flags=:Flags, CreatorID=:CreatorID, Data=:Data where UUID=:UUID";
56 private const string assetSelect =
"select * from assets";
58 private SqliteConnection m_conn;
60 protected virtual Assembly Assembly
62 get {
return GetType().Assembly; }
85 Util.LoadArchSpecificWindowsDll(
"sqlite3.dll");
87 if (dbconnect ==
string.Empty)
89 dbconnect =
"URI=file:Asset.db,version=3";
91 m_conn =
new SqliteConnection(dbconnect);
109 using (SqliteCommand cmd =
new SqliteCommand(SelectAssetSQL, m_conn))
111 cmd.Parameters.Add(
new SqliteParameter(
":UUID", uuid.ToString()));
112 using (IDataReader reader = cmd.ExecuteReader())
136 string assetName = asset.Name;
139 assetName = asset.Name.Substring(0, AssetBase.MAX_ASSET_NAME);
141 "[ASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
142 asset.Name, asset.ID, asset.Name.Length, assetName.Length);
145 string assetDescription = asset.Description;
148 assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC);
150 "[ASSET DB]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
151 asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
155 if (AssetsExist(
new[] { asset.FullID })[0])
161 using (SqliteCommand cmd =
new SqliteCommand(UpdateAssetSQL, m_conn))
163 cmd.Parameters.Add(
new SqliteParameter(
":UUID", asset.
FullID.ToString()));
164 cmd.Parameters.Add(
new SqliteParameter(
":Name", assetName));
165 cmd.Parameters.Add(
new SqliteParameter(
":Description", assetDescription));
166 cmd.Parameters.Add(
new SqliteParameter(
":Type", asset.
Type));
167 cmd.Parameters.Add(
new SqliteParameter(
":Local", asset.
Local));
168 cmd.Parameters.Add(
new SqliteParameter(
":Temporary", asset.
Temporary));
169 cmd.Parameters.Add(
new SqliteParameter(
":Flags", asset.
Flags));
171 cmd.Parameters.Add(
new SqliteParameter(
":Data", asset.
Data));
173 cmd.ExecuteNonQuery();
182 using (SqliteCommand cmd =
new SqliteCommand(InsertAssetSQL, m_conn))
184 cmd.Parameters.Add(
new SqliteParameter(
":UUID", asset.
FullID.ToString()));
185 cmd.Parameters.Add(
new SqliteParameter(
":Name", assetName));
186 cmd.Parameters.Add(
new SqliteParameter(
":Description", assetDescription));
187 cmd.Parameters.Add(
new SqliteParameter(
":Type", asset.
Type));
188 cmd.Parameters.Add(
new SqliteParameter(
":Local", asset.
Local));
189 cmd.Parameters.Add(
new SqliteParameter(
":Temporary", asset.
Temporary));
190 cmd.Parameters.Add(
new SqliteParameter(
":Flags", asset.
Flags));
192 cmd.Parameters.Add(
new SqliteParameter(
":Data", asset.
Data));
194 cmd.ExecuteNonQuery();
225 if (uuids.Length == 0)
228 HashSet<UUID> exist =
new HashSet<UUID>();
230 string ids =
"'" + string.Join(
"','", uuids) +
"'";
231 string sql = string.Format(
"select UUID from assets where UUID in ({0})", ids);
235 using (SqliteCommand cmd =
new SqliteCommand(sql, m_conn))
237 using (IDataReader reader = cmd.ExecuteReader())
239 while (reader.Read())
241 UUID
id =
new UUID((
string)reader[
"UUID"]);
248 bool[] results =
new bool[uuids.Length];
249 for (
int i = 0; i < uuids.Length; i++)
250 results[i] = exist.Contains(uuids[i]);
259 private static AssetBase buildAsset(IDataReader row)
265 new UUID((String)row[
"UUID"]),
267 Convert.ToSByte(row[
"Type"]),
268 (String)row[
"CreatorID"]
271 asset.Description = (String) row[
"Description"];
272 asset.Local = Convert.ToBoolean(row[
"Local"]);
273 asset.Temporary = Convert.ToBoolean(row[
"Temporary"]);
274 asset.Flags = (
AssetFlags)Convert.ToInt32(row[
"asset_flags"]);
275 asset.Data = (byte[])row[
"Data"];
279 private static AssetMetadata buildAssetMetadata(IDataReader row)
283 metadata.FullID =
new UUID((
string) row[
"UUID"]);
284 metadata.Name = (string) row[
"Name"];
285 metadata.Description = (string) row[
"Description"];
286 metadata.Type = Convert.ToSByte(row[
"Type"]);
287 metadata.Temporary = Convert.ToBoolean(row[
"Temporary"]);
288 metadata.Flags = (
AssetFlags)Convert.ToInt32(row[
"asset_flags"]);
289 metadata.CreatorID = row[
"CreatorID"].ToString();
292 metadata.SHA1 =
new byte[] {};
307 List<AssetMetadata> retList =
new List<AssetMetadata>(count);
311 using (SqliteCommand cmd =
new SqliteCommand(SelectAssetMetadataSQL, m_conn))
313 cmd.Parameters.Add(
new SqliteParameter(
":start", start));
314 cmd.Parameters.Add(
new SqliteParameter(
":count", count));
316 using (IDataReader reader = cmd.ExecuteReader())
318 while (reader.Read())
321 retList.Add(metadata);
339 #region IPlugin interface
344 override public string Version
348 Module module = GetType().Module;
350 Version dllVersion = module.Assembly.GetName().Version;
353 string.Format(
"{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
354 dllVersion.Revision);
363 Initialise(
"URI=file:Asset.db,version=3");
369 override public string Name
371 get {
return "SQLite Asset storage engine"; }
384 using (SqliteCommand cmd =
new SqliteCommand(DeleteAssetSQL, m_conn))
386 cmd.Parameters.Add(
new SqliteParameter(
":UUID", uuid.ToString()));
387 cmd.ExecuteNonQuery();
398 if (!UUID.TryParse(
id, out assetID))
401 return DeleteAsset(assetID);
override AssetBase GetAsset(UUID uuid)
Fetch Asset
override void Initialise()
Initialise the AssetData interface using default URI
An asset storage interface for the SQLite database system
override List< AssetMetadata > FetchAssetMetadataSet(int start, int count)
Returns a list of AssetMetadata objects. The list is a subset of the entire data set offset by start ...
bool DeleteAsset(UUID uuid)
Delete an asset from database
sbyte Type
(sbyte) AssetType enum
override bool StoreAsset(AssetBase asset)
Create an asset
bool Local
Is this a region only asset, or does this exist on the asset server also
Asset class. All Assets are reference by this class or a class derived from this class ...
static readonly int MAX_ASSET_NAME
static readonly int MAX_ASSET_DESC
override void Initialise(string dbconnect)
Initialises AssetData interface Loads and initialises a new SQLite connection and maintains it...
override bool[] AssetsExist(UUID[] uuids)
Check if the assets exist in the database.
override bool Delete(string id)
bool Temporary
Is this asset going to be saved to the asset database?