From 80520206fcd4c8c85eb28b4800c42136365b164d Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Mon, 1 Dec 2008 18:42:14 +0000 Subject: * Unforunately it turns out not to be that simple. Revert the rest of r7560 for now. --- OpenSim/Data/NHibernate/NHibernateUserData.cs | 155 ++++++++++++++++---------- 1 file changed, 99 insertions(+), 56 deletions(-) (limited to 'OpenSim/Data/NHibernate/NHibernateUserData.cs') diff --git a/OpenSim/Data/NHibernate/NHibernateUserData.cs b/OpenSim/Data/NHibernate/NHibernateUserData.cs index ee0d2b8..3a22dc3 100644 --- a/OpenSim/Data/NHibernate/NHibernateUserData.cs +++ b/OpenSim/Data/NHibernate/NHibernateUserData.cs @@ -33,7 +33,10 @@ using System.Text.RegularExpressions; using OpenMetaverse; using log4net; using NHibernate; -using NHibernate.Criterion; +using NHibernate.Cfg; +using NHibernate.Expression; +using NHibernate.Mapping.Attributes; +using NHibernate.Tool.hbm2ddl; using OpenSim.Framework; using Environment=NHibernate.Cfg.Environment; @@ -46,7 +49,9 @@ namespace OpenSim.Data.NHibernate { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private NHibernateManager manager; + private Configuration cfg; + private ISessionFactory factory; + private ISession session; public override void Initialise() { @@ -56,40 +61,62 @@ namespace OpenSim.Data.NHibernate public override void Initialise(string connect) { - m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateUserData"); - manager = new NHibernateManager(connect, "UserStore"); + 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 + "'"); + } + string dialect = parts[0]; + + // This is stubbing for now, it will become dynamic later and support different db backends + cfg = new Configuration(); + cfg.SetProperty(Environment.ConnectionProvider, + "NHibernate.Connection.DriverConnectionProvider"); + cfg.SetProperty(Environment.Dialect, + "NHibernate.Dialect." + parts[0]); + cfg.SetProperty(Environment.ConnectionDriver, + "NHibernate.Driver." + parts[1]); + cfg.SetProperty(Environment.ConnectionString, parts[2]); + cfg.AddAssembly("OpenSim.Data.NHibernate"); + + factory = cfg.BuildSessionFactory(); + session = factory.OpenSession(); + + // This actually does the roll forward assembly stuff + Assembly assem = GetType().Assembly; + Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "UserStore"); + m.Update(); } private bool ExistsUser(UUID uuid) { UserProfileData user = null; - - m_log.InfoFormat("[NHIBERNATE] ExistsUser; {0}", uuid); - user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); - - if (user == null) + try { - m_log.InfoFormat("[NHIBERNATE] User with given UUID does not exist {0} ", uuid); - return false; + user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; + } + catch (ObjectNotFoundException) + { + user = null; } - return true; - + return (user != null); } override public UserProfileData GetUserByUUID(UUID uuid) { UserProfileData user; - m_log.InfoFormat("[NHIBERNATE] GetUserByUUID: {0} ", uuid); - - user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); - if (user != null) + // TODO: I'm sure I'll have to do something silly here + try { - UserAgentData agent = GetAgentByUUID(uuid); - if (agent != null) - { - user.CurrentAgent = agent; - } + user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; + user.CurrentAgent = GetAgentByUUID(uuid); + } + catch (ObjectNotFoundException) + { + user = null; } return user; @@ -99,30 +126,33 @@ namespace OpenSim.Data.NHibernate { if (!ExistsUser(profile.ID)) { - m_log.InfoFormat("[NHIBERNATE] AddNewUserProfile {0}", profile.ID); - manager.Save(profile); + session.Save(profile); SetAgentData(profile.ID, profile.CurrentAgent); - } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); + m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); UpdateUserProfile(profile); } } private void SetAgentData(UUID uuid, UserAgentData agent) { - UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), uuid); - if (old != null) + if (agent == null) { - m_log.InfoFormat("[NHIBERNATE] SetAgentData deleting old: {0} ",uuid); - manager.Delete(old); + // TODO: got to figure out how to do a delete right } - if (agent != null) + else { - m_log.InfoFormat("[NHIBERNATE] SetAgentData: {0} ", agent.ProfileID); - manager.Save(agent); + try + { + UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData; + session.Delete(old); + } + catch (ObjectNotFoundException) + { + } + session.Save(agent); } } @@ -130,13 +160,13 @@ namespace OpenSim.Data.NHibernate { if (ExistsUser(profile.ID)) { - manager.Update(profile); + session.Update(profile); SetAgentData(profile.ID, profile.CurrentAgent); return true; } else { - m_log.ErrorFormat("[NHIBERNATE] Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); + m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); AddNewUserProfile(profile); return true; } @@ -144,32 +174,37 @@ namespace OpenSim.Data.NHibernate override public void AddNewUserAgent(UserAgentData agent) { - UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), agent.ProfileID); - if (old != null) + try { - manager.Delete(old); + UserAgentData old = session.Load(typeof(UserAgentData), agent.ProfileID) as UserAgentData; + session.Delete(old); } - - manager.Save(agent); - + catch (ObjectNotFoundException) + { + } + session.Save(agent); } public void UpdateUserAgent(UserAgentData agent) { - m_log.InfoFormat("[NHIBERNATE] UpdateUserAgent: {0} ", agent.ProfileID); - manager.Update(agent); + session.Update(agent); } override public UserAgentData GetAgentByUUID(UUID uuid) { - m_log.InfoFormat("[NHIBERNATE] GetAgentByUUID: {0} ", uuid); - return (UserAgentData)manager.Load(typeof(UserAgentData), uuid); + try + { + return session.Load(typeof(UserAgentData), uuid) as UserAgentData; + } + catch + { + return null; + } } override public UserProfileData GetUserByName(string fname, string lname) { - m_log.InfoFormat("[NHIBERNATE] GetUserByName: {0} {1} ", fname, lname); - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); + ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); criteria.Add(Expression.Eq("FirstName", fname)); criteria.Add(Expression.Eq("SurName", lname)); foreach (UserProfileData profile in criteria.List()) @@ -198,7 +233,7 @@ namespace OpenSim.Data.NHibernate if (querysplit.Length == 2) { - ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); + ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); criteria.Add(Expression.Like("FirstName", querysplit[0])); criteria.Add(Expression.Like("SurName", querysplit[1])); foreach (UserProfileData profile in criteria.List()) @@ -227,18 +262,26 @@ namespace OpenSim.Data.NHibernate /// TODO: stubs for now to get us to a compiling state gently public override AvatarAppearance GetUserAppearance(UUID user) { - return (AvatarAppearance)manager.Load(typeof(AvatarAppearance), user); + AvatarAppearance appearance; + // TODO: I'm sure I'll have to do something silly here + try { + appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance; + } catch (ObjectNotFoundException) { + appearance = null; + } + return appearance; } private bool ExistsAppearance(UUID uuid) { - AvatarAppearance appearance = (AvatarAppearance)manager.Load(typeof(AvatarAppearance), uuid); - if (appearance == null) - { - return false; + AvatarAppearance appearance; + try { + appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance; + } catch (ObjectNotFoundException) { + appearance = null; } - return true; + return (appearance == null) ? false : true; } @@ -252,11 +295,11 @@ namespace OpenSim.Data.NHibernate bool exists = ExistsAppearance(user); if (exists) { - manager.Update(appearance); + session.Update(appearance); } else { - manager.Save(appearance); + session.Save(appearance); } } -- cgit v1.1