All pastes #845808 Raw Edit

Miscellany

public text v1 · immutable
#845808 ·published 2008-01-07 21:59 UTC
rendered paste body
namespace OpenSim.Framework.Data.DbEngine
{
    public class AssetData : IAssetProvider
    {
        DbConnection _Connection;
        IDbEngine DbEngine;

        public void Initialise()
        {
            IConfigSource DbEngineIni = new IniConfigSource("DbEngine.ini");
            IConfig DbConfig = DbEngineIni.Configs["Asset"];

            string DbEngineAssemblyFile = DbConfig.GetString("DbEngine",
                "OpenSim.Framework.Data.DbEngine.SQLite.dll");

            string ConnectionString = DbConfig.GetString("ConnectionString",
                "URI=file:AssetStorage.db,version=3");

            Assembly DbEngineAssembly = Assembly.LoadFrom(DbEngineAssemblyFile);

            foreach (Type DbEngineType in DbEngineAssembly.GetTypes())
                if (DbEngineType.GetInterface("IDbEngine") != null)
                    DbEngine = (IDbEngine)Activator.CreateInstance(DbEngineType);

            _Connection = DbEngine.CreateConnection(ConnectionString);
            _Connection.Open();

            EnsureTables();
        }

        void EnsureTables()
        {
            EnsureTable("assets", "CreateAssetsTable.sql");
        }

        void EnsureTable(string name, string resource)
        {
            try
            {
                DbCommand Cmd =
                    DbEngine.GetCommand(_Connection,
                        "SELECT COUNT(*) FROM [" + name + "]");

                Cmd.ExecuteScalar();
            }
            catch
            {
                DbEngine.ExecuteResourceSql(_Connection, resource);
            }
        }

        public string Version
        {
            get { return "0.1"; }
        }

        public string Name
        {
            get { return "Asset storage engine - using" + DbEngine.Name; }
        }

        public AssetBase FetchAsset(LLUUID assetID)
        {
            AssetBase Asset = null;

            try
            {
                DbCommand Cd =
                    DbEngine.GetCommand(_Connection,
                        "SELECT * FROM [assets] WHERE [id] = @id");

                DbEngine.SetParam(Cd, "@id", assetID.ToString());

                using (DbDataReader Dr = Cd.ExecuteReader())
                    if (Dr.Read())
                        Asset = getAssetRow(Dr);
            }
            catch (Exception e)
            {
                MainLog.Instance.Error(e.ToString());
            }

            return Asset;
        }

        public void CreateAsset(AssetBase asset)
        {
            if (ExistsAsset(asset.FullID))
                return;

            DbCommand Cmd =
                DbEngine.GetCommand(_Connection,
                    "INSERT INTO [assets] ([id], [name], [description], [assetType], [invType], [local], [temporary], [data]) " +
                    "VALUES (@id, @name, @description, @assetType, @invType, @local, @temporary, @data)");

            DbEngine.SetParam(Cmd, "@id", asset.FullID.ToString());
            DbEngine.SetParam(Cmd, "@name", asset.Name);
            DbEngine.SetParam(Cmd, "@description", asset.Description);
            DbEngine.SetParam(Cmd, "@assetType", asset.Type);
            DbEngine.SetParam(Cmd, "@invType", asset.InvType);
            DbEngine.SetParam(Cmd, "@local", asset.Local);
            DbEngine.SetParam(Cmd, "@temporary", asset.Temporary);
            DbEngine.SetParam(Cmd, "@data", asset.Data);

            try
            {
                Cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MainLog.Instance.Error(e.ToString());
            }
        }

        public void UpdateAsset(AssetBase asset)
        {
            DbCommand Cmd =
                DbEngine.GetCommand(_Connection,
                    "UPDATE [assets] " +
                       "SET [id] = @id, " +
                           "[name] = @name, " +
                           "[description] = @description," +
                           "[assetType] = @assetType," +
                           "[invType] = @invType," +
                           "[local] = @local," +
                           "[temporary] = @temporary," +
                           "[data] = @data " +
                     "WHERE [id] = @keyId");

            DbEngine.SetParam(Cmd, "@id", asset.FullID.ToString());
            DbEngine.SetParam(Cmd, "@name", asset.Name);
            DbEngine.SetParam(Cmd, "@description", asset.Description);
            DbEngine.SetParam(Cmd, "@assetType", asset.Type);
            DbEngine.SetParam(Cmd, "@invType", asset.InvType);
            DbEngine.SetParam(Cmd, "@local", asset.Local);
            DbEngine.SetParam(Cmd, "@temporary", asset.Temporary);
            DbEngine.SetParam(Cmd, "@data", asset.FullID.Data);
            DbEngine.SetParam(Cmd, "@keyId", asset.FullID.ToString());

            try
            {
                Cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MainLog.Instance.Error(e.ToString());
            }
        }

        public bool ExistsAsset(LLUUID uuid)
        {
            DbCommand Cmd =
                DbEngine.GetCommand(_Connection,
                    "SELECT [id] FROM [assets] WHERE [id] = @id");

            DbEngine.SetParam(Cmd, "@id", uuid.ToString());

            return (Cmd.ExecuteScalar() != null);
        }

        public void CommitAssets()
        { }

        AssetBase getAssetRow(IDataReader reader)
        {
            AssetBase asset = new AssetBase();

            asset.Data = (byte[])reader["data"];
            asset.Description = (string)reader["description"];
            asset.FullID = new LLUUID((string)reader["id"]);
            asset.InvType = Convert.ToSByte(reader["invType"]);
            asset.Local = Convert.ToBoolean(reader["local"]);
            asset.Name = (string)reader["name"];
            asset.Type = Convert.ToSByte(reader["assetType"]);

            return asset;
        }
    }
}