From a23bebdc0f106c19de09e54d88eebc6b086fdaab Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Fri, 23 Apr 2010 17:17:15 +0100
Subject: Duplicate OpenSim.Data.SQLite into OpenSim.Data.SQLiteNG. SQLiteNG
will shortly be changed to work under mono 2.6 and above
---
OpenSim/Data/SQLiteNG/SQLiteAssetData.cs | 343 +++++++++++++++++++++++++++++++
1 file changed, 343 insertions(+)
create mode 100644 OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
(limited to 'OpenSim/Data/SQLiteNG/SQLiteAssetData.cs')
diff --git a/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs b/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
new file mode 100644
index 0000000..a032670
--- /dev/null
+++ b/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
@@ -0,0 +1,343 @@
+/*
+ * Copyright (c) Contributors, http://opensimulator.org/
+ * See CONTRIBUTORS.TXT for a full list of copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the OpenSimulator Project nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+using System;
+using System.Data;
+using System.Reflection;
+using System.Collections.Generic;
+using log4net;
+using Mono.Data.SqliteClient;
+using OpenMetaverse;
+using OpenSim.Framework;
+
+namespace OpenSim.Data.SQLite
+{
+ ///
+ /// An asset storage interface for the SQLite database system
+ ///
+ 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, UUID 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, Data) values(:UUID, :Name, :Description, :Type, :Local, :Temporary, :Data)";
+ private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, Local=:Local, Temporary=:Temporary, Data=:Data where UUID=:UUID";
+ private const string assetSelect = "select * from assets";
+
+ private SqliteConnection m_conn;
+
+ override public void Dispose()
+ {
+ if (m_conn != null)
+ {
+ m_conn.Close();
+ m_conn = null;
+ }
+ }
+
+ ///
+ ///
+ /// - Initialises AssetData interface
+ /// - Loads and initialises a new SQLite connection and maintains it.
+ /// - use default URI if connect string is empty.
+ ///
+ ///
+ /// connect string
+ override public void Initialise(string dbconnect)
+ {
+ if (dbconnect == string.Empty)
+ {
+ dbconnect = "URI=file:Asset.db,version=3";
+ }
+ m_conn = new SqliteConnection(dbconnect);
+ m_conn.Open();
+
+ Assembly assem = GetType().Assembly;
+ Migration m = new Migration(m_conn, assem, "AssetStore");
+ m.Update();
+
+ return;
+ }
+
+ ///
+ /// Fetch Asset
+ ///
+ /// UUID of ... ?
+ /// Asset base
+ 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;
+ }
+ }
+ }
+ }
+ }
+
+ ///
+ /// Create an asset
+ ///
+ /// Asset Base
+ override public void StoreAsset(AssetBase asset)
+ {
+ //m_log.Info("[ASSET DB]: Creating Asset " + asset.FullID.ToString());
+ if (ExistsAsset(asset.FullID))
+ {
+ //LogAssetLoad(asset);
+
+ 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", asset.Name));
+ cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
+ 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(":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", asset.Name));
+ cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
+ 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(":Data", asset.Data));
+
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ }
+
+// ///
+// /// Some... logging functionnality
+// ///
+// ///
+// private static void LogAssetLoad(AssetBase asset)
+// {
+// string temporary = asset.Temporary ? "Temporary" : "Stored";
+// string local = asset.Local ? "Local" : "Remote";
+//
+// int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
+//
+// m_log.Debug("[ASSET DB]: " +
+// string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)",
+// asset.FullID, asset.Name, asset.Description, asset.Type,
+// temporary, local, assetLength));
+// }
+
+ ///
+ /// Check if an asset exist in database
+ ///
+ /// The asset UUID
+ /// True if exist, or false.
+ 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;
+ }
+ }
+ }
+ }
+ }
+
+ ///
+ /// Delete an asset from database
+ ///
+ ///
+ public void DeleteAsset(UUID uuid)
+ {
+ using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
+ {
+ cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
+
+ cmd.ExecuteNonQuery();
+ }
+ }
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ private static AssetBase buildAsset(IDataReader row)
+ {
+ // TODO: this doesn't work yet because something more
+ // interesting has to be done to actually get these values
+ // back out. Not enough time to figure it out yet.
+ AssetBase asset = new AssetBase(
+ new UUID((String)row["UUID"]),
+ (String)row["Name"],
+ Convert.ToSByte(row["Type"]),
+ UUID.Zero.ToString()
+ );
+
+ asset.Description = (String) row["Description"];
+ asset.Local = Convert.ToBoolean(row["Local"]);
+ asset.Temporary = Convert.ToBoolean(row["Temporary"]);
+ 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"]); // Not sure if this is correct.
+
+ // Current SHA1s are not stored/computed.
+ metadata.SHA1 = new byte[] {};
+
+ return metadata;
+ }
+
+ ///
+ /// Returns a list of AssetMetadata objects. The list is a subset of
+ /// the entire data set offset by containing
+ /// elements.
+ ///
+ /// The number of results to discard from the total data set.
+ /// The number of rows the returned list should contain.
+ /// A list of AssetMetadata objects.
+ public override List FetchAssetMetadataSet(int start, int count)
+ {
+ List retList = new List(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;
+ }
+
+ /***********************************************************************
+ *
+ * Database Binding functions
+ *
+ * These will be db specific due to typing, and minor differences
+ * in databases.
+ *
+ **********************************************************************/
+
+ #region IPlugin interface
+
+ ///
+ ///
+ ///
+ override public string Version
+ {
+ get
+ {
+ Module module = GetType().Module;
+ // string dllName = module.Assembly.ManifestModule.Name;
+ Version dllVersion = module.Assembly.GetName().Version;
+
+ return
+ string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
+ dllVersion.Revision);
+ }
+ }
+
+ ///
+ /// Initialise the AssetData interface using default URI
+ ///
+ override public void Initialise()
+ {
+ Initialise("URI=file:Asset.db,version=3");
+ }
+
+ ///
+ /// Name of this DB provider
+ ///
+ override public string Name
+ {
+ get { return "SQLite Asset storage engine"; }
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
--
cgit v1.1
From e84cc2f9db9e03a45f9fa315cf598dd5ef83b7ef Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Fri, 23 Apr 2010 18:56:50 +0100
Subject: Change SQLiteNG to work with mono 2.6 and above using the
Mono.Data.Sqlite.dll Include the library so that Windows builds correctly It
appears that Windows is okay with either SQLite or SQLiteNG Incorporate the
latest fixes made by Diva to OpenSim.Data.SQLite
---
OpenSim/Data/SQLiteNG/SQLiteAssetData.cs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
(limited to 'OpenSim/Data/SQLiteNG/SQLiteAssetData.cs')
diff --git a/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs b/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
index a032670..636bf86 100644
--- a/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
+++ b/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
@@ -30,7 +30,7 @@ using System.Data;
using System.Reflection;
using System.Collections.Generic;
using log4net;
-using Mono.Data.SqliteClient;
+using Mono.Data.Sqlite;
using OpenMetaverse;
using OpenSim.Framework;
@@ -137,7 +137,7 @@ namespace OpenSim.Data.SQLite
cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
-
+
cmd.ExecuteNonQuery();
}
}
@@ -340,4 +340,4 @@ namespace OpenSim.Data.SQLite
#endregion
}
-}
\ No newline at end of file
+}
--
cgit v1.1
From 1b488c25816fd01e893f64cec1a89f5dbac21104 Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Fri, 23 Apr 2010 20:08:24 +0100
Subject: dispose of the DbCommand used to execute migrations after we've
finished with it rather than within the loop disposing of it within the loop
causes Mono.Data.Sqlite.dll to get upset, and it's the wrong behaviour anyway
---
OpenSim/Data/SQLiteNG/SQLiteAssetData.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'OpenSim/Data/SQLiteNG/SQLiteAssetData.cs')
diff --git a/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs b/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
index 636bf86..9b34a21 100644
--- a/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
+++ b/OpenSim/Data/SQLiteNG/SQLiteAssetData.cs
@@ -34,7 +34,7 @@ using Mono.Data.Sqlite;
using OpenMetaverse;
using OpenSim.Framework;
-namespace OpenSim.Data.SQLite
+namespace OpenSim.Data.SQLiteNG
{
///
/// An asset storage interface for the SQLite database system
--
cgit v1.1