30 using System.Reflection;
31 using System.Collections.Generic;
33 using MySql.Data.MySqlClient;
35 using OpenSim.Framework;
38 namespace OpenSim.Data.MySQL
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 private string m_connectionString;
49 protected virtual Assembly Assembly
51 get {
return GetType().Assembly; }
54 #region IPlugin Members
56 public override string Version {
get {
return "1.0.0.0"; } }
71 m_connectionString = connect;
73 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
83 throw new NotImplementedException();
91 override public string Name
93 get {
return "MySQL Asset storage engine"; }
98 #region IAssetDataPlugin Members
110 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
114 using (MySqlCommand cmd =
new MySqlCommand(
115 "SELECT name, description, assetType, local, temporary, asset_flags, CreatorID, data FROM assets WHERE id=?id",
118 cmd.Parameters.AddWithValue(
"?id", assetID.ToString());
122 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
126 asset =
new AssetBase(assetID, (
string)dbReader[
"name"], (sbyte)dbReader[
"assetType"], dbReader[
"CreatorID"].ToString());
127 asset.Data = (byte[])dbReader[
"data"];
128 asset.Description = (string)dbReader[
"description"];
130 string local = dbReader[
"local"].ToString();
131 if (local.Equals(
"1") || local.Equals(
"true", StringComparison.InvariantCultureIgnoreCase))
136 asset.Temporary = Convert.ToBoolean(dbReader[
"temporary"]);
137 asset.Flags = (
AssetFlags)Convert.ToInt32(dbReader[
"asset_flags"]);
144 string.Format(
"[ASSETS DB]: MySql failure fetching asset {0}. Exception ", assetID), e);
159 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
163 string assetName = asset.Name;
166 assetName = asset.Name.Substring(0, AssetBase.MAX_ASSET_NAME);
168 "[ASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
169 asset.Name, asset.ID, asset.Name.Length, assetName.Length);
172 string assetDescription = asset.Description;
175 assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC);
177 "[ASSET DB]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
178 asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
181 using (MySqlCommand cmd =
183 "replace INTO assets(id, name, description, assetType, local, temporary, create_time, access_time, asset_flags, CreatorID, data)" +
184 "VALUES(?id, ?name, ?description, ?assetType, ?local, ?temporary, ?create_time, ?access_time, ?asset_flags, ?CreatorID, ?data)",
190 int now = (int)
Utils.DateTimeToUnixTime(DateTime.UtcNow);
191 cmd.Parameters.AddWithValue(
"?id", asset.ID);
192 cmd.Parameters.AddWithValue(
"?name", assetName);
193 cmd.Parameters.AddWithValue(
"?description", assetDescription);
194 cmd.Parameters.AddWithValue(
"?assetType", asset.Type);
195 cmd.Parameters.AddWithValue(
"?local", asset.Local);
196 cmd.Parameters.AddWithValue(
"?temporary", asset.Temporary);
197 cmd.Parameters.AddWithValue(
"?create_time", now);
198 cmd.Parameters.AddWithValue(
"?access_time", now);
199 cmd.Parameters.AddWithValue(
"?CreatorID", asset.Metadata.CreatorID);
200 cmd.Parameters.AddWithValue(
"?asset_flags", (int)asset.
Flags);
201 cmd.Parameters.AddWithValue(
"?data", asset.Data);
202 cmd.ExecuteNonQuery();
207 m_log.ErrorFormat(
"[ASSET DB]: MySQL failure creating asset {0} with name \"{1}\". Error: {2}",
208 asset.FullID, asset.Name, e.Message);
215 private void UpdateAccessTime(
AssetBase asset)
217 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
221 using (MySqlCommand cmd
222 =
new MySqlCommand(
"update assets set access_time=?access_time where id=?id", dbcon))
227 int now = (int)
Utils.DateTimeToUnixTime(DateTime.UtcNow);
228 cmd.Parameters.AddWithValue(
"?id", asset.ID);
229 cmd.Parameters.AddWithValue(
"?access_time", now);
230 cmd.ExecuteNonQuery();
236 "[ASSETS DB]: Failure updating access_time for asset {0} with name {1}. Exception ",
237 asset.FullID, asset.Name),
251 if (uuids.Length == 0)
254 HashSet<UUID> exist =
new HashSet<UUID>();
256 string ids =
"'" + string.Join(
"','", uuids) +
"'";
257 string sql = string.Format(
"SELECT id FROM assets WHERE id IN ({0})", ids);
259 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
262 using (MySqlCommand cmd =
new MySqlCommand(sql, dbcon))
264 using (MySqlDataReader dbReader = cmd.ExecuteReader())
266 while (dbReader.Read())
268 UUID
id = DBGuid.FromDB(dbReader[
"id"]);
275 bool[] results =
new bool[uuids.Length];
276 for (
int i = 0; i < uuids.Length; i++)
277 results[i] = exist.Contains(uuids[i]);
292 List<AssetMetadata> retList =
new List<AssetMetadata>(count);
294 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
298 using (MySqlCommand cmd
300 "SELECT name,description,assetType,temporary,id,asset_flags,CreatorID FROM assets LIMIT ?start, ?count",
303 cmd.Parameters.AddWithValue(
"?start", start);
304 cmd.Parameters.AddWithValue(
"?count", count);
308 using (MySqlDataReader dbReader = cmd.ExecuteReader())
310 while (dbReader.Read())
313 metadata.Name = (string)dbReader[
"name"];
314 metadata.Description = (string)dbReader[
"description"];
315 metadata.Type = (sbyte)dbReader[
"assetType"];
316 metadata.Temporary = Convert.ToBoolean(dbReader[
"temporary"]);
317 metadata.Flags = (
AssetFlags)Convert.ToInt32(dbReader[
"asset_flags"]);
318 metadata.FullID = DBGuid.FromDB(dbReader[
"id"]);
319 metadata.CreatorID = dbReader[
"CreatorID"].ToString();
322 metadata.SHA1 =
new byte[] { };
324 retList.Add(metadata);
332 "[ASSETS DB]: MySql failure fetching asset set from {0}, count {1}. Exception ",
344 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
348 using (MySqlCommand cmd =
new MySqlCommand(
"delete from assets where id=?id", dbcon))
350 cmd.Parameters.AddWithValue(
"?id", id);
351 cmd.ExecuteNonQuery();
OpenSim.Server.Handlers.Simulation.Utils Utils
override AssetBase GetAsset(UUID assetID)
Fetch Asset assetID from database
override void Initialise()
Default-initialises the plugin
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 ...
A MySQL Interface for the Asset Server
override bool StoreAsset(AssetBase asset)
Create an asset in database, or update it if existing.
Asset class. All Assets are reference by this class or a class derived from this class ...
static readonly int MAX_ASSET_NAME
override bool[] AssetsExist(UUID[] uuids)
Check if the assets exist in the database.
static readonly int MAX_ASSET_DESC
override void Initialise(string connect)
override bool Delete(string id)