From dde32f1130f83f9471fea474b1ebc9e0520be561 Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Tue, 2 Dec 2008 15:22:58 +0000 Subject: * Reapply http://opensimulator.org/mantis/view.php?id=2710 * This patch gets NHibernate working *** PLEASE NOTE: This patch now requires the libmono-oracle2.0-cil library to be installed on Ubuntu (to stop the System.Data.Oracle missing failure) *** * Not sure what the dependency is on other distros. Adding this info to http://opensimulator.org/wiki/Build_Instructions would be most welcome * Adds Castle.* libraries that were missing last time (note, dlls have been added from http://downloads.sourceforge.net/nhibernate/NHibernate-2.0.1.GA-bin.zip) --- OpenSim/Data/NHibernate/NHibernateManager.cs | 142 +++++++++++++++++++++++---- 1 file changed, 124 insertions(+), 18 deletions(-) (limited to 'OpenSim/Data/NHibernate/NHibernateManager.cs') diff --git a/OpenSim/Data/NHibernate/NHibernateManager.cs b/OpenSim/Data/NHibernate/NHibernateManager.cs index 161ec1d..36d84e2 100644 --- a/OpenSim/Data/NHibernate/NHibernateManager.cs +++ b/OpenSim/Data/NHibernate/NHibernateManager.cs @@ -25,35 +25,141 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +using System; using System.Reflection; +using System.IO; using log4net; using NHibernate; +using NHibernate.Cfg; +using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; +using OpenMetaverse; +using Environment = NHibernate.Cfg.Environment; namespace OpenSim.Data.NHibernate { - internal class NHibernateManager + public class NHibernateManager { - // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - // private ISessionFactory factory; - // private ISession session; - // private ITransaction transaction; + private string dialect; + private Configuration cfg; + private ISessionFactory factory; + private ISession session; - public NHibernateManager() + public NHibernateManager(string connect, string store) { - // This is stubbing for now, it will become dynamic later and support different db backends -// NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); -// cfg.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = -// "NHibernate.Connection.DriverConnectionProvider"; -// cfg.Properties[NHibernate.Cfg.Environment.Dialect] = -// "NHibernate.Dialect.SQLite"; -// cfg.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = -// "NHibernate.Driver.SqliteClientDriver"; -// cfg.Properties[NHibernate.Cfg.Environment.ConnectionString] = -// "URI=file:opensim-nh.db,version=3"; - -// factory = cfg.BuildSessionFactory(); + + // Split out the dialect, driver, and connect string + char[] split = { ';' }; + string[] parts = connect.Split(split, 3); + if (parts.Length != 3) + { + // TODO: make this a real exception type + throw new Exception("Malformed Inventory connection string '" + connect + "'"); + } + + dialect = parts[0]; + + // NHibernate setup + cfg = new Configuration(); + cfg.SetProperty(Environment.ConnectionProvider, + "NHibernate.Connection.DriverConnectionProvider"); + cfg.SetProperty(Environment.Dialect, + "NHibernate.Dialect." + dialect); + cfg.SetProperty(Environment.ConnectionDriver, + "NHibernate.Driver." + parts[1]); + cfg.SetProperty(Environment.ConnectionString, parts[2]); + cfg.AddAssembly("OpenSim.Data.NHibernate"); + + //To create sql file uncomment code below and write the name of the file + //SchemaExport exp = new SchemaExport(cfg); + //exp.SetOutputFile("nameofthefile.sql"); + //exp.Create(false, true); + + HbmSerializer.Default.Validate = true; + using (MemoryStream stream = + HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly())) + cfg.AddInputStream(stream); + + factory = cfg.BuildSessionFactory(); + session = factory.OpenSession(); + + Assembly assem = GetType().Assembly; + Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, store); + m.Update(); } + public object Load(Type type, UUID uuid) + { + object obj = null; + try + { + obj = session.Load(type, uuid); + } + catch (Exception) + { + m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid); + } + return obj; + + } + + public bool Save(object obj) + { + try + { + session.BeginTransaction(); + session.Save(obj); + session.Transaction.Commit(); + session.Flush(); + return true; + } + catch (Exception e) + { + m_log.Error("[NHIBERNATE] issue saving object ", e); + } + return false; + } + + public bool Update(object obj) + { + try + { + session.BeginTransaction(); + session.Update(obj); + session.Transaction.Commit(); + session.Flush(); + return true; + } + catch (Exception e) + { + m_log.Error("[NHIBERNATE] issue updating object ", e); + } + return false; + } + + public bool Delete(object obj) + { + try + { + session.BeginTransaction(); + session.Delete(obj); + session.Transaction.Commit(); + session.Flush(); + return true; + } + catch (Exception e) + { + + m_log.Error("[NHIBERNATE] issue deleting object ", e); + } + return false; + } + + public ISession GetSession() + { + return session; + } } } -- cgit v1.1