From cd4348738a57fcb85305642bf5e191b292c8d017 Mon Sep 17 00:00:00 2001
From: Sean Dague
Date: Thu, 3 Apr 2008 20:59:20 +0000
Subject: Check in the remaining bits to do Asset store via NHibernate.  Still
 need to work out auto table creation in a reasonable way.  Tommorrow I'll
 work on getting the NHibernate bits in place to be able to put this into the
 main tree.

---
 OpenSim/Data/NHibernate/NHibernateAssetData.cs     | 56 +++++++++++-----------
 .../Data/NHibernate/Resources/AssetBase.hbm.xml    | 15 +++---
 2 files changed, 36 insertions(+), 35 deletions(-)

(limited to 'OpenSim')

diff --git a/OpenSim/Data/NHibernate/NHibernateAssetData.cs b/OpenSim/Data/NHibernate/NHibernateAssetData.cs
index cdb93df..41acd4c 100644
--- a/OpenSim/Data/NHibernate/NHibernateAssetData.cs
+++ b/OpenSim/Data/NHibernate/NHibernateAssetData.cs
@@ -47,6 +47,7 @@ namespace OpenSim.Data.NHibernate
     {
         private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
 
+        private Configuration cfg;
         private ISessionFactory factory;
 
         public override void Initialise()
@@ -54,7 +55,7 @@ namespace OpenSim.Data.NHibernate
             // TODO: hard coding for sqlite based stuff to begin with, just making it easier to test
 
             // This is stubbing for now, it will become dynamic later and support different db backends
-            Configuration cfg = new Configuration();
+            cfg = new Configuration();
             cfg.SetProperty(Environment.ConnectionProvider, 
                             "NHibernate.Connection.DriverConnectionProvider");
             cfg.SetProperty(Environment.Dialect, 
@@ -65,48 +66,49 @@ namespace OpenSim.Data.NHibernate
                             "URI=file:Asset.db,version=3");
             cfg.AddAssembly("OpenSim.Data.NHibernate");
 
-            // HbmSerializer.Default.Validate = true;
+            HbmSerializer.Default.Validate = true;
 //             using ( System.IO.MemoryStream stream = 
 //                     HbmSerializer.Default.Serialize(System.Reflection.Assembly.GetExecutingAssembly()))
 //                 cfg.AddInputStream(stream);
-
+            
 //             new SchemaExport(cfg).Create(true, true);
 
-            factory = cfg.BuildSessionFactory();
-                        
+            factory  = cfg.BuildSessionFactory();
         }
 
         override public AssetBase FetchAsset(LLUUID uuid)
         {
-            return null;
+            using(ISession session = factory.OpenSession()) {
+                try {
+                    return session.Load(typeof(AssetBase), uuid.ToString()) as AssetBase;
+                } catch {
+                    return null;
+                }
+            }
         }
 
-        override public void CreateAsset(AssetBase asset)
+      override public void CreateAsset(AssetBase asset)
         {
-            ISession session = null;
-            ITransaction transaction = null;
-            try {
-                session = factory.OpenSession();
-                transaction = session.BeginTransaction();
-
-                session.SaveOrUpdate(asset);
-                // CRUD operations here (with the session)
-                transaction.Commit();
-            }
-            catch {
-                if(transaction != null)
-                    transaction.Rollback();
-                throw; // Don't trap the exception
-            }
-            finally {
-                if(session != null)
-                    session.Close();
+            if (!ExistsAsset(asset.FullID)) {
+                using(ISession session = factory.OpenSession()) {
+                    using(ITransaction transaction = session.BeginTransaction()) {
+                        session.Save(asset);
+                        transaction.Commit();
+                    }
+                }
             }
         }
 
         override public void UpdateAsset(AssetBase asset)
         {
-
+            if (ExistsAsset(asset.FullID)) {
+                using(ISession session = factory.OpenSession()) {
+                    using(ITransaction transaction = session.BeginTransaction()) {
+                        session.Update(asset);
+                        transaction.Commit();
+                    }
+                }
+            }
         }
 
         private void LogAssetLoad(AssetBase asset)
@@ -124,7 +126,7 @@ namespace OpenSim.Data.NHibernate
 
         override public bool ExistsAsset(LLUUID uuid)
         {
-            return false;
+            return (FetchAsset(uuid) != null) ? true : false;
         }
 
         public void DeleteAsset(LLUUID uuid)
diff --git a/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml b/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml
index 82c30fe..ec4d168 100644
--- a/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml
+++ b/OpenSim/Data/NHibernate/Resources/AssetBase.hbm.xml
@@ -1,16 +1,15 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
-    <class name="OpenSim.Framework.AssetBase, OpenSim.Framework" table="Assets">
+    <class name="OpenSim.Framework.AssetBase, OpenSim.Framework" table="Assets" lazy="false">
         <id name="ID" column="UUID" type="String" length="36" > 
             <generator class="assigned" /> 
         </id> 
-<property name="Type" type="integer" />
-<property name="InvType" type="integer" />
-<property name="Name" type="String" length="50" />
-<property name="Description" type="String" length="50" />
-<property name="Local" type="boolean" />
-<property name="Temporary" type="boolean" />
+        <property name="Type" type="SByte" />
+        <property name="InvType" type="SByte" />
+        <property name="Name" type="String" length="50" />
+        <property name="Description" type="String" length="50" />
+        <property name="Local" type="boolean" />
+        <property name="Temporary" type="boolean" />
         <property name="Data" type="binary" />
-
     </class>
 </hibernate-mapping>
\ No newline at end of file
-- 
cgit v1.1