123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- using System;
- using System.Data;
- using System.Reflection;
- using System.Collections.Generic;
- using log4net;
- #if CSharpSqlite
- using Community.CsharpSqlite.Sqlite;
- #else
- using Mono.Data.Sqlite;
- #endif
- using OpenMetaverse;
- using OpenSim.Framework;
- namespace OpenSim.Data.SQLite
- {
-
-
-
- public class SQLiteAssetData : AssetDataBase
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private const string SelectAssetSQL = "select * from assets where UUID=:UUID";
- private const string SelectAssetMetadataSQL = "select Name, Description, Type, Temporary, asset_flags, UUID, CreatorID from assets limit :start, :count";
- private const string DeleteAssetSQL = "delete from assets where UUID=:UUID";
- 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)";
- 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";
- private const string assetSelect = "select * from assets";
- private SqliteConnection m_conn;
- protected virtual Assembly Assembly
- {
- get { return GetType().Assembly; }
- }
- override public void Dispose()
- {
- if (m_conn != null)
- {
- m_conn.Close();
- m_conn = null;
- }
- }
-
-
-
-
-
-
-
-
- override public void Initialise(string dbconnect)
- {
- if (Util.IsWindows())
- Util.LoadArchSpecificWindowsDll("sqlite3.dll");
- if (dbconnect == string.Empty)
- {
- dbconnect = "URI=file:Asset.db,version=3";
- }
- m_conn = new SqliteConnection(dbconnect);
- m_conn.Open();
- Migration m = new Migration(m_conn, Assembly, "AssetStore");
- m.Update();
- return;
- }
-
-
-
-
-
- override public AssetBase GetAsset(UUID uuid)
- {
- lock (this)
- {
- using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
- {
- cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
- using (IDataReader reader = cmd.ExecuteReader())
- {
- if (reader.Read())
- {
- AssetBase asset = buildAsset(reader);
- reader.Close();
- return asset;
- }
- else
- {
- reader.Close();
- return null;
- }
- }
- }
- }
- }
-
-
-
-
- override public void StoreAsset(AssetBase asset)
- {
- string assetName = asset.Name;
- if (asset.Name.Length > 64)
- {
- assetName = asset.Name.Substring(0, 64);
- m_log.WarnFormat(
- "[ASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
- asset.Name, asset.ID, asset.Name.Length, assetName.Length);
- }
- string assetDescription = asset.Description;
- if (asset.Description.Length > 64)
- {
- assetDescription = asset.Description.Substring(0, 64);
- m_log.WarnFormat(
- "[ASSET DB]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
- asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
- }
-
- if (ExistsAsset(asset.FullID))
- {
-
- lock (this)
- {
- using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
- {
- cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
- cmd.Parameters.Add(new SqliteParameter(":Name", assetName));
- cmd.Parameters.Add(new SqliteParameter(":Description", assetDescription));
- cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
- cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
- cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
- cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags));
- cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID));
- cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
- cmd.ExecuteNonQuery();
- }
- }
- }
- else
- {
- lock (this)
- {
- using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
- {
- cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
- cmd.Parameters.Add(new SqliteParameter(":Name", assetName));
- cmd.Parameters.Add(new SqliteParameter(":Description", assetDescription));
- cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
- cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
- cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
- cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags));
- cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID));
- cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
- cmd.ExecuteNonQuery();
- }
- }
- }
- }
-
-
-
-
-
- override public bool ExistsAsset(UUID uuid)
- {
- lock (this)
- {
- using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
- {
- cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
- using (IDataReader reader = cmd.ExecuteReader())
- {
- if (reader.Read())
- {
- reader.Close();
- return true;
- }
- else
- {
- reader.Close();
- return false;
- }
- }
- }
- }
- }
-
-
-
-
-
- private static AssetBase buildAsset(IDataReader row)
- {
-
-
-
- AssetBase asset = new AssetBase(
- new UUID((String)row["UUID"]),
- (String)row["Name"],
- Convert.ToSByte(row["Type"]),
- (String)row["CreatorID"]
- );
- asset.Description = (String) row["Description"];
- asset.Local = Convert.ToBoolean(row["Local"]);
- asset.Temporary = Convert.ToBoolean(row["Temporary"]);
- asset.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
- asset.Data = (byte[])row["Data"];
- return asset;
- }
- private static AssetMetadata buildAssetMetadata(IDataReader row)
- {
- AssetMetadata metadata = new AssetMetadata();
- metadata.FullID = new UUID((string) row["UUID"]);
- metadata.Name = (string) row["Name"];
- metadata.Description = (string) row["Description"];
- metadata.Type = Convert.ToSByte(row["Type"]);
- metadata.Temporary = Convert.ToBoolean(row["Temporary"]);
- metadata.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
- metadata.CreatorID = row["CreatorID"].ToString();
-
- metadata.SHA1 = new byte[] {};
- return metadata;
- }
-
-
-
-
-
-
-
-
- public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
- {
- List<AssetMetadata> retList = new List<AssetMetadata>(count);
- lock (this)
- {
- using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn))
- {
- cmd.Parameters.Add(new SqliteParameter(":start", start));
- cmd.Parameters.Add(new SqliteParameter(":count", count));
- using (IDataReader reader = cmd.ExecuteReader())
- {
- while (reader.Read())
- {
- AssetMetadata metadata = buildAssetMetadata(reader);
- retList.Add(metadata);
- }
- }
- }
- }
- return retList;
- }
-
- #region IPlugin interface
-
-
-
- override public string Version
- {
- get
- {
- Module module = GetType().Module;
-
- Version dllVersion = module.Assembly.GetName().Version;
- return
- string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
- dllVersion.Revision);
- }
- }
-
-
-
- override public void Initialise()
- {
- Initialise("URI=file:Asset.db,version=3");
- }
-
-
-
- override public string Name
- {
- get { return "SQLite Asset storage engine"; }
- }
-
-
-
-
-
- public bool DeleteAsset(UUID uuid)
- {
- lock (this)
- {
- using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
- {
- cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
- cmd.ExecuteNonQuery();
- }
- }
- return true;
- }
- public override bool Delete(string id)
- {
- UUID assetID;
- if (!UUID.TryParse(id, out assetID))
- return false;
- return DeleteAsset(assetID);
- }
- #endregion
- }
- }
|