diff options
author | Adam Frisby | 2008-12-01 07:50:38 +0000 |
---|---|---|
committer | Adam Frisby | 2008-12-01 07:50:38 +0000 |
commit | 4ed7ca62b153817a142ded621cc5c5c1b1c055c5 (patch) | |
tree | e8cad8abe60b6becd22da1a8d2c2dfefbf6f9784 /OpenSim/Data/NHibernate/NHibernateUserData.cs | |
parent | Mantis #2729 (diff) | |
download | opensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.zip opensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.tar.gz opensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.tar.bz2 opensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.tar.xz |
* From Mantis#2701 - Implements NHibernate data adapter, adds support for MsSql2005 to NHibernate.
* Patch courtesy of Adminotech and realXtend. Thanks!
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateUserData.cs')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateUserData.cs | 155 |
1 files changed, 56 insertions, 99 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateUserData.cs b/OpenSim/Data/NHibernate/NHibernateUserData.cs index 3a22dc3..ee0d2b8 100644 --- a/OpenSim/Data/NHibernate/NHibernateUserData.cs +++ b/OpenSim/Data/NHibernate/NHibernateUserData.cs | |||
@@ -33,10 +33,7 @@ using System.Text.RegularExpressions; | |||
33 | using OpenMetaverse; | 33 | using OpenMetaverse; |
34 | using log4net; | 34 | using log4net; |
35 | using NHibernate; | 35 | using NHibernate; |
36 | using NHibernate.Cfg; | 36 | using NHibernate.Criterion; |
37 | using NHibernate.Expression; | ||
38 | using NHibernate.Mapping.Attributes; | ||
39 | using NHibernate.Tool.hbm2ddl; | ||
40 | using OpenSim.Framework; | 37 | using OpenSim.Framework; |
41 | using Environment=NHibernate.Cfg.Environment; | 38 | using Environment=NHibernate.Cfg.Environment; |
42 | 39 | ||
@@ -49,9 +46,7 @@ namespace OpenSim.Data.NHibernate | |||
49 | { | 46 | { |
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
51 | 48 | ||
52 | private Configuration cfg; | 49 | private NHibernateManager manager; |
53 | private ISessionFactory factory; | ||
54 | private ISession session; | ||
55 | 50 | ||
56 | public override void Initialise() | 51 | public override void Initialise() |
57 | { | 52 | { |
@@ -61,62 +56,40 @@ namespace OpenSim.Data.NHibernate | |||
61 | 56 | ||
62 | public override void Initialise(string connect) | 57 | public override void Initialise(string connect) |
63 | { | 58 | { |
64 | char[] split = {';'}; | 59 | m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateUserData"); |
65 | string[] parts = connect.Split(split, 3); | 60 | manager = new NHibernateManager(connect, "UserStore"); |
66 | if (parts.Length != 3) | ||
67 | { | ||
68 | // TODO: make this a real exception type | ||
69 | throw new Exception("Malformed Inventory connection string '" + connect + "'"); | ||
70 | } | ||
71 | string dialect = parts[0]; | ||
72 | |||
73 | // This is stubbing for now, it will become dynamic later and support different db backends | ||
74 | cfg = new Configuration(); | ||
75 | cfg.SetProperty(Environment.ConnectionProvider, | ||
76 | "NHibernate.Connection.DriverConnectionProvider"); | ||
77 | cfg.SetProperty(Environment.Dialect, | ||
78 | "NHibernate.Dialect." + parts[0]); | ||
79 | cfg.SetProperty(Environment.ConnectionDriver, | ||
80 | "NHibernate.Driver." + parts[1]); | ||
81 | cfg.SetProperty(Environment.ConnectionString, parts[2]); | ||
82 | cfg.AddAssembly("OpenSim.Data.NHibernate"); | ||
83 | |||
84 | factory = cfg.BuildSessionFactory(); | ||
85 | session = factory.OpenSession(); | ||
86 | |||
87 | // This actually does the roll forward assembly stuff | ||
88 | Assembly assem = GetType().Assembly; | ||
89 | Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "UserStore"); | ||
90 | m.Update(); | ||
91 | } | 61 | } |
92 | 62 | ||
93 | private bool ExistsUser(UUID uuid) | 63 | private bool ExistsUser(UUID uuid) |
94 | { | 64 | { |
95 | UserProfileData user = null; | 65 | UserProfileData user = null; |
96 | try | 66 | |
97 | { | 67 | m_log.InfoFormat("[NHIBERNATE] ExistsUser; {0}", uuid); |
98 | user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; | 68 | user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); |
99 | } | 69 | |
100 | catch (ObjectNotFoundException) | 70 | if (user == null) |
101 | { | 71 | { |
102 | user = null; | 72 | m_log.InfoFormat("[NHIBERNATE] User with given UUID does not exist {0} ", uuid); |
73 | return false; | ||
103 | } | 74 | } |
104 | 75 | ||
105 | return (user != null); | 76 | return true; |
77 | |||
106 | } | 78 | } |
107 | 79 | ||
108 | override public UserProfileData GetUserByUUID(UUID uuid) | 80 | override public UserProfileData GetUserByUUID(UUID uuid) |
109 | { | 81 | { |
110 | UserProfileData user; | 82 | UserProfileData user; |
111 | // TODO: I'm sure I'll have to do something silly here | 83 | m_log.InfoFormat("[NHIBERNATE] GetUserByUUID: {0} ", uuid); |
112 | try | 84 | |
85 | user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); | ||
86 | if (user != null) | ||
113 | { | 87 | { |
114 | user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; | 88 | UserAgentData agent = GetAgentByUUID(uuid); |
115 | user.CurrentAgent = GetAgentByUUID(uuid); | 89 | if (agent != null) |
116 | } | 90 | { |
117 | catch (ObjectNotFoundException) | 91 | user.CurrentAgent = agent; |
118 | { | 92 | } |
119 | user = null; | ||
120 | } | 93 | } |
121 | 94 | ||
122 | return user; | 95 | return user; |
@@ -126,33 +99,30 @@ namespace OpenSim.Data.NHibernate | |||
126 | { | 99 | { |
127 | if (!ExistsUser(profile.ID)) | 100 | if (!ExistsUser(profile.ID)) |
128 | { | 101 | { |
129 | session.Save(profile); | 102 | m_log.InfoFormat("[NHIBERNATE] AddNewUserProfile {0}", profile.ID); |
103 | manager.Save(profile); | ||
130 | SetAgentData(profile.ID, profile.CurrentAgent); | 104 | SetAgentData(profile.ID, profile.CurrentAgent); |
105 | |||
131 | } | 106 | } |
132 | else | 107 | else |
133 | { | 108 | { |
134 | m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); | 109 | m_log.ErrorFormat("[NHIBERNATE] Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); |
135 | UpdateUserProfile(profile); | 110 | UpdateUserProfile(profile); |
136 | } | 111 | } |
137 | } | 112 | } |
138 | 113 | ||
139 | private void SetAgentData(UUID uuid, UserAgentData agent) | 114 | private void SetAgentData(UUID uuid, UserAgentData agent) |
140 | { | 115 | { |
141 | if (agent == null) | 116 | UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), uuid); |
117 | if (old != null) | ||
142 | { | 118 | { |
143 | // TODO: got to figure out how to do a delete right | 119 | m_log.InfoFormat("[NHIBERNATE] SetAgentData deleting old: {0} ",uuid); |
120 | manager.Delete(old); | ||
144 | } | 121 | } |
145 | else | 122 | if (agent != null) |
146 | { | 123 | { |
147 | try | 124 | m_log.InfoFormat("[NHIBERNATE] SetAgentData: {0} ", agent.ProfileID); |
148 | { | 125 | manager.Save(agent); |
149 | UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData; | ||
150 | session.Delete(old); | ||
151 | } | ||
152 | catch (ObjectNotFoundException) | ||
153 | { | ||
154 | } | ||
155 | session.Save(agent); | ||
156 | } | 126 | } |
157 | 127 | ||
158 | } | 128 | } |
@@ -160,13 +130,13 @@ namespace OpenSim.Data.NHibernate | |||
160 | { | 130 | { |
161 | if (ExistsUser(profile.ID)) | 131 | if (ExistsUser(profile.ID)) |
162 | { | 132 | { |
163 | session.Update(profile); | 133 | manager.Update(profile); |
164 | SetAgentData(profile.ID, profile.CurrentAgent); | 134 | SetAgentData(profile.ID, profile.CurrentAgent); |
165 | return true; | 135 | return true; |
166 | } | 136 | } |
167 | else | 137 | else |
168 | { | 138 | { |
169 | m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); | 139 | m_log.ErrorFormat("[NHIBERNATE] Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); |
170 | AddNewUserProfile(profile); | 140 | AddNewUserProfile(profile); |
171 | return true; | 141 | return true; |
172 | } | 142 | } |
@@ -174,37 +144,32 @@ namespace OpenSim.Data.NHibernate | |||
174 | 144 | ||
175 | override public void AddNewUserAgent(UserAgentData agent) | 145 | override public void AddNewUserAgent(UserAgentData agent) |
176 | { | 146 | { |
177 | try | 147 | UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), agent.ProfileID); |
148 | if (old != null) | ||
178 | { | 149 | { |
179 | UserAgentData old = session.Load(typeof(UserAgentData), agent.ProfileID) as UserAgentData; | 150 | manager.Delete(old); |
180 | session.Delete(old); | ||
181 | } | 151 | } |
182 | catch (ObjectNotFoundException) | 152 | |
183 | { | 153 | manager.Save(agent); |
184 | } | 154 | |
185 | session.Save(agent); | ||
186 | } | 155 | } |
187 | 156 | ||
188 | public void UpdateUserAgent(UserAgentData agent) | 157 | public void UpdateUserAgent(UserAgentData agent) |
189 | { | 158 | { |
190 | session.Update(agent); | 159 | m_log.InfoFormat("[NHIBERNATE] UpdateUserAgent: {0} ", agent.ProfileID); |
160 | manager.Update(agent); | ||
191 | } | 161 | } |
192 | 162 | ||
193 | override public UserAgentData GetAgentByUUID(UUID uuid) | 163 | override public UserAgentData GetAgentByUUID(UUID uuid) |
194 | { | 164 | { |
195 | try | 165 | m_log.InfoFormat("[NHIBERNATE] GetAgentByUUID: {0} ", uuid); |
196 | { | 166 | return (UserAgentData)manager.Load(typeof(UserAgentData), uuid); |
197 | return session.Load(typeof(UserAgentData), uuid) as UserAgentData; | ||
198 | } | ||
199 | catch | ||
200 | { | ||
201 | return null; | ||
202 | } | ||
203 | } | 167 | } |
204 | 168 | ||
205 | override public UserProfileData GetUserByName(string fname, string lname) | 169 | override public UserProfileData GetUserByName(string fname, string lname) |
206 | { | 170 | { |
207 | ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); | 171 | m_log.InfoFormat("[NHIBERNATE] GetUserByName: {0} {1} ", fname, lname); |
172 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); | ||
208 | criteria.Add(Expression.Eq("FirstName", fname)); | 173 | criteria.Add(Expression.Eq("FirstName", fname)); |
209 | criteria.Add(Expression.Eq("SurName", lname)); | 174 | criteria.Add(Expression.Eq("SurName", lname)); |
210 | foreach (UserProfileData profile in criteria.List()) | 175 | foreach (UserProfileData profile in criteria.List()) |
@@ -233,7 +198,7 @@ namespace OpenSim.Data.NHibernate | |||
233 | 198 | ||
234 | if (querysplit.Length == 2) | 199 | if (querysplit.Length == 2) |
235 | { | 200 | { |
236 | ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); | 201 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); |
237 | criteria.Add(Expression.Like("FirstName", querysplit[0])); | 202 | criteria.Add(Expression.Like("FirstName", querysplit[0])); |
238 | criteria.Add(Expression.Like("SurName", querysplit[1])); | 203 | criteria.Add(Expression.Like("SurName", querysplit[1])); |
239 | foreach (UserProfileData profile in criteria.List()) | 204 | foreach (UserProfileData profile in criteria.List()) |
@@ -262,26 +227,18 @@ namespace OpenSim.Data.NHibernate | |||
262 | /// TODO: stubs for now to get us to a compiling state gently | 227 | /// TODO: stubs for now to get us to a compiling state gently |
263 | public override AvatarAppearance GetUserAppearance(UUID user) | 228 | public override AvatarAppearance GetUserAppearance(UUID user) |
264 | { | 229 | { |
265 | AvatarAppearance appearance; | 230 | return (AvatarAppearance)manager.Load(typeof(AvatarAppearance), user); |
266 | // TODO: I'm sure I'll have to do something silly here | ||
267 | try { | ||
268 | appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance; | ||
269 | } catch (ObjectNotFoundException) { | ||
270 | appearance = null; | ||
271 | } | ||
272 | return appearance; | ||
273 | } | 231 | } |
274 | 232 | ||
275 | private bool ExistsAppearance(UUID uuid) | 233 | private bool ExistsAppearance(UUID uuid) |
276 | { | 234 | { |
277 | AvatarAppearance appearance; | 235 | AvatarAppearance appearance = (AvatarAppearance)manager.Load(typeof(AvatarAppearance), uuid); |
278 | try { | 236 | if (appearance == null) |
279 | appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance; | 237 | { |
280 | } catch (ObjectNotFoundException) { | 238 | return false; |
281 | appearance = null; | ||
282 | } | 239 | } |
283 | 240 | ||
284 | return (appearance == null) ? false : true; | 241 | return true; |
285 | } | 242 | } |
286 | 243 | ||
287 | 244 | ||
@@ -295,11 +252,11 @@ namespace OpenSim.Data.NHibernate | |||
295 | bool exists = ExistsAppearance(user); | 252 | bool exists = ExistsAppearance(user); |
296 | if (exists) | 253 | if (exists) |
297 | { | 254 | { |
298 | session.Update(appearance); | 255 | manager.Update(appearance); |
299 | } | 256 | } |
300 | else | 257 | else |
301 | { | 258 | { |
302 | session.Save(appearance); | 259 | manager.Save(appearance); |
303 | } | 260 | } |
304 | } | 261 | } |
305 | 262 | ||