30 using System.Reflection;
31 using System.Collections.Generic;
34 using OpenSim.Framework;
38 namespace OpenSim.Data.PGSQL
45 private const string _migrationStore =
"AssetStore";
47 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 private long m_ticksToEpoch;
53 private string m_connectionString;
55 protected virtual Assembly Assembly
57 get {
return GetType().Assembly; }
60 #region IPlugin Members
70 m_log.Info(
"[PGSQLAssetData]: " + Name +
" cannot be default-initialized!");
83 m_ticksToEpoch =
new System.DateTime(1970, 1, 1).Ticks;
86 m_connectionString = connectionString;
89 m_database.CheckMigration(_migrationStore);
95 override public string Version
97 get {
return m_database.getVersion(); }
103 override public string Name
105 get {
return "PGSQL Asset storage engine"; }
110 #region IAssetDataPlugin Members
119 string sql =
"SELECT * FROM assets WHERE id = :id";
120 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
121 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
123 cmd.Parameters.Add(m_database.CreateParameter(
"id", assetID));
125 using (NpgsqlDataReader reader = cmd.ExecuteReader())
130 DBGuid.FromDB(reader[
"id"]),
131 (string)reader[
"name"],
132 Convert.ToSByte(reader[
"assetType"]),
133 reader[
"creatorid"].ToString()
136 asset.Description = (string)reader[
"description"];
137 asset.Local = Convert.ToBoolean(reader[
"local"]);
138 asset.Temporary = Convert.ToBoolean(reader[
"temporary"]);
139 asset.Flags = (
AssetFlags)(Convert.ToInt32(reader[
"asset_flags"]));
140 asset.Data = (byte[])reader[
"data"];
156 @"UPDATE assets set name = :name, description = :description, " +
"\"assetType\" " +
@" = :assetType,
157 local = :local, temporary = :temporary, creatorid = :creatorid, data = :data
161 (id, name, description, " +
"\"assetType\" " +
@", local,
162 temporary, create_time, access_time, creatorid, asset_flags, data)
163 Select :id, :name, :description, :assetType, :local,
164 :temporary, :create_time, :access_time, :creatorid, :asset_flags, :data
165 Where not EXISTS(SELECT * FROM assets WHERE id=:id)
168 string assetName = asset.Name;
171 assetName = asset.Name.Substring(0, AssetBase.MAX_ASSET_NAME);
173 "[ASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
174 asset.Name, asset.ID, asset.Name.Length, assetName.Length);
177 string assetDescription = asset.Description;
180 assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC);
182 "[ASSET DB]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
183 asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
186 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
187 using (NpgsqlCommand command =
new NpgsqlCommand(sql, conn))
189 int now = (int)((System.DateTime.Now.Ticks - m_ticksToEpoch) / 10000000);
190 command.Parameters.Add(m_database.CreateParameter(
"id", asset.FullID));
191 command.Parameters.Add(m_database.CreateParameter(
"name", assetName));
192 command.Parameters.Add(m_database.CreateParameter(
"description", assetDescription));
193 command.Parameters.Add(m_database.CreateParameter(
"assetType", asset.Type));
194 command.Parameters.Add(m_database.CreateParameter(
"local", asset.Local));
195 command.Parameters.Add(m_database.CreateParameter(
"temporary", asset.Temporary));
196 command.Parameters.Add(m_database.CreateParameter(
"access_time", now));
197 command.Parameters.Add(m_database.CreateParameter(
"create_time", now));
198 command.Parameters.Add(m_database.CreateParameter(
"asset_flags", (int)asset.
Flags));
199 command.Parameters.Add(m_database.CreateParameter(
"creatorid", asset.Metadata.CreatorID));
200 command.Parameters.Add(m_database.CreateParameter(
"data", asset.Data));
204 command.ExecuteNonQuery();
208 m_log.Error(
"[ASSET DB]: Error storing item :" + e.Message +
" sql "+sql);
241 if (uuids.Length == 0)
244 HashSet<UUID> exist =
new HashSet<UUID>();
246 string ids =
"'" + string.Join(
"','", uuids) +
"'";
247 string sql = string.Format(
"SELECT id FROM assets WHERE id IN ({0})", ids);
249 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
250 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
253 using (NpgsqlDataReader reader = cmd.ExecuteReader())
255 while (reader.Read())
257 UUID
id = DBGuid.FromDB(reader[
"id"]);
263 bool[] results =
new bool[uuids.Length];
264 for (
int i = 0; i < uuids.Length; i++)
265 results[i] = exist.Contains(uuids[i]);
279 List<AssetMetadata> retList =
new List<AssetMetadata>(count);
280 string sql =
@" SELECT id, name, description, " +
"\"assetType\"" +
@", temporary, creatorid
286 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
287 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
289 cmd.Parameters.Add(m_database.CreateParameter(
"start", start));
290 cmd.Parameters.Add(m_database.CreateParameter(
"stop", start + count - 1));
292 using (NpgsqlDataReader reader = cmd.ExecuteReader())
294 while (reader.Read())
297 metadata.FullID = DBGuid.FromDB(reader[
"id"]);
298 metadata.Name = (string)reader[
"name"];
299 metadata.Description = (string)reader[
"description"];
300 metadata.Type = Convert.ToSByte(reader[
"assetType"]);
301 metadata.Temporary = Convert.ToBoolean(reader[
"temporary"]);
302 metadata.CreatorID = (string)reader[
"creatorid"];
303 retList.Add(metadata);
override void Initialise(string connectionString)
Initialises asset interface
override void Initialise()
Asset class. All Assets are reference by this class or a class derived from this class ...
A PGSQL Interface for the Asset server
A management class for the MS SQL Storage Engine
static readonly int MAX_ASSET_NAME
override bool StoreAsset(AssetBase asset)
Create asset in m_database
static readonly int MAX_ASSET_DESC
Exception thrown if Initialise has been called, but failed.
override bool Delete(string id)
override AssetBase GetAsset(UUID assetID)
Fetch Asset from m_database
override bool[] AssetsExist(UUID[] uuids)
Check if the assets exist in the database.
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 ...