aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/NHibernate/NHibernateManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateManager.cs')
-rw-r--r--OpenSim/Data/NHibernate/NHibernateManager.cs345
1 files changed, 0 insertions, 345 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateManager.cs b/OpenSim/Data/NHibernate/NHibernateManager.cs
deleted file mode 100644
index 2e7081e..0000000
--- a/OpenSim/Data/NHibernate/NHibernateManager.cs
+++ /dev/null
@@ -1,345 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Data.Common;
30using System.Reflection;
31using log4net;
32using NHibernate;
33using NHibernate.Cfg;
34using NHibernate.Tool.hbm2ddl;
35using OpenMetaverse;
36using Environment=NHibernate.Cfg.Environment;
37
38namespace OpenSim.Data.NHibernate
39{
40 public class NHibernateManager
41 {
42 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43
44 private string dialect;
45 private Configuration configuration;
46 private ISessionFactory sessionFactory;
47
48 #region Initialization
49
50 /// <summary>
51 /// Initiate NHibernate Manager
52 /// </summary>
53 /// <param name="connect">NHibernate dialect, driver and connection string separated by ';'</param>
54 /// <param name="store">Name of the store</param>
55 public NHibernateManager(string connect, string store)
56 {
57 ParseConnectionString(connect);
58
59 //To create sql file uncomment code below and write the name of the file
60 //SchemaExport exp = new SchemaExport(cfg);
61 //exp.SetOutputFile("nameofthefile.sql");
62 //exp.Create(false, true);
63
64 Assembly assembly = GetType().Assembly;
65
66 sessionFactory = configuration.BuildSessionFactory();
67 RunMigration(dialect, assembly, store);
68 }
69
70 /// <summary>
71 /// Initiate NHibernate Manager with spesific assembly
72 /// </summary>
73 /// <param name="connect">NHibernate dialect, driver and connection string separated by ';'</param>
74 /// <param name="store">Name of the store</param>
75 /// <param name="assembly">Outside assembly to be included </param>
76 public NHibernateManager(string connect, string store, Assembly assembly)
77 {
78 ParseConnectionString(connect);
79
80 configuration.AddAssembly(assembly);
81 sessionFactory = configuration.BuildSessionFactory();
82 RunMigration(dialect, assembly, store);
83 }
84
85 /// <summary>
86 /// Parses the connection string and creates the NHibernate configuration
87 /// </summary>
88 /// <param name="connect">NHibernate dialect, driver and connection string separated by ';'</param>
89 private void ParseConnectionString(string connect)
90 {
91 // Split out the dialect, driver, and connect string
92 char[] split = { ';' };
93 string[] parts = connect.Split(split, 3);
94 if (parts.Length != 3)
95 {
96 // TODO: make this a real exception type
97 throw new Exception("Malformed Inventory connection string '" + connect + "'");
98 }
99
100 dialect = parts[0];
101
102 // NHibernate setup
103 configuration = new Configuration();
104 configuration.SetProperty(Environment.ConnectionProvider,
105 "NHibernate.Connection.DriverConnectionProvider");
106 configuration.SetProperty(Environment.Dialect,
107 "NHibernate.Dialect." + dialect);
108 configuration.SetProperty(Environment.ConnectionDriver,
109 "NHibernate.Driver." + parts[1]);
110 configuration.SetProperty(Environment.ConnectionString, parts[2]);
111 configuration.AddAssembly("OpenSim.Data.NHibernate");
112 }
113
114 /// <summary>
115 /// Runs migration for the the store in assembly
116 /// </summary>
117 /// <param name="dialect">Dialect in use</param>
118 /// <param name="assembly">Assembly where migration files exist</param>
119 /// <param name="store">Name of the store in use</param>
120 private void RunMigration(string dialect, Assembly assembly, string store)
121 {
122 // Migration subtype is the folder name under which migrations are stored. For mysql this folder is
123 // MySQLDialect instead of MySQL5Dialect which is the dialect currently in use. To avoid renaming
124 // this folder each time the mysql version changes creating simple mapping:
125 String migrationSubType = dialect;
126 if (dialect.StartsWith("MySQL"))
127 {
128 migrationSubType = "MySQLDialect";
129 }
130
131 Migration migration = new Migration((DbConnection)sessionFactory.ConnectionProvider.GetConnection(), assembly, migrationSubType, store);
132 migration.Update();
133 }
134
135 #endregion
136
137 /// <summary>
138 /// Gets object of given type from database with given id.
139 /// Uses stateless session for efficiency.
140 /// </summary>
141 /// <param name="type">Type of the object.</param>
142 /// <param name="id">Id of the object.</param>
143 /// <returns>The object or null if object was not found.</returns>
144 public object Get(Type type, Object id)
145 {
146 using (IStatelessSession session = sessionFactory.OpenStatelessSession())
147 {
148 object obj = null;
149 try
150 {
151 obj = session.Get(type.FullName, id);
152 }
153 catch (Exception e)
154 {
155 m_log.ErrorFormat("[NHIBERNATE] {0} of id {1} loading threw exception: " + e.ToString(), type.Name, id);
156 }
157 return obj;
158 }
159 }
160
161 /// <summary>
162 /// Gets object of given type from database with given id.
163 /// Use this method for objects containing collections. For flat objects stateless mode is more efficient.
164 /// </summary>
165 /// <param name="type">Type of the object.</param>
166 /// <param name="id">Id of the object.</param>
167 /// <returns>The object or null if object was not found.</returns>
168 public object GetWithStatefullSession(Type type, Object id)
169 {
170 using (ISession session = sessionFactory.OpenSession())
171 {
172 object obj = null;
173 try
174 {
175 obj = session.Get(type.FullName, id);
176 }
177 catch (Exception e)
178 {
179 m_log.ErrorFormat("[NHIBERNATE] {0} of id {1} loading threw exception: " + e.ToString(), type.Name, id);
180 }
181 return obj;
182 }
183
184 }
185
186 /// <summary>
187 /// Inserts given object to database.
188 /// Uses stateless session for efficiency.
189 /// </summary>
190 /// <param name="obj">Object to be insterted.</param>
191 /// <returns>Identifier of the object. Useful for situations when NHibernate generates the identifier.</returns>
192 public object Insert(object obj)
193 {
194 try
195 {
196 using (IStatelessSession session = sessionFactory.OpenStatelessSession())
197 {
198 using (ITransaction transaction=session.BeginTransaction())
199 {
200 Object identifier=session.Insert(obj);
201 transaction.Commit();
202 return identifier;
203 }
204 }
205 }
206 catch (Exception e)
207 {
208 m_log.Error("[NHIBERNATE] issue inserting object ", e);
209 return null;
210 }
211 }
212
213 /// <summary>
214 /// Inserts given object to database.
215 /// Use this method for objects containing collections. For flat objects stateless mode is more efficient.
216 /// </summary>
217 /// <param name="obj">Object to be insterted.</param>
218 /// <returns>Identifier of the object. Useful for situations when NHibernate generates the identifier.</returns>
219 public object InsertWithStatefullSession(object obj)
220 {
221 try
222 {
223 using (ISession session = sessionFactory.OpenSession())
224 {
225 using (ITransaction transaction = session.BeginTransaction())
226 {
227 Object identifier = session.Save(obj);
228 transaction.Commit();
229 return identifier;
230 }
231 }
232 }
233 catch (Exception e)
234 {
235 m_log.Error("[NHIBERNATE] issue inserting object ", e);
236 return null;
237 }
238 }
239
240 /// <summary>
241 /// Updates given object to database.
242 /// Uses stateless session for efficiency.
243 /// </summary>
244 /// <param name="obj">Object to be updated.</param>
245 /// <returns>True if operation was succesful.</returns>
246 public bool Update(object obj)
247 {
248 try
249 {
250 using (IStatelessSession session = sessionFactory.OpenStatelessSession())
251 {
252 using (ITransaction transaction = session.BeginTransaction())
253 {
254 session.Update(obj);
255 transaction.Commit();
256 return true;
257 }
258 }
259 }
260 catch (Exception e)
261 {
262 m_log.Error("[NHIBERNATE] issue updating object ", e);
263 return false;
264 }
265 }
266
267 /// <summary>
268 /// Updates given object to database.
269 /// Use this method for objects containing collections. For flat objects stateless mode is more efficient.
270 /// </summary>
271 /// <param name="obj">Object to be updated.</param>
272 /// <returns>True if operation was succesful.</returns>
273 public bool UpdateWithStatefullSession(object obj)
274 {
275 try
276 {
277 using (ISession session = sessionFactory.OpenSession())
278 {
279 using (ITransaction transaction = session.BeginTransaction())
280 {
281 session.Update(obj);
282 transaction.Commit();
283 return true;
284 }
285 }
286 }
287 catch (Exception e)
288 {
289 m_log.Error("[NHIBERNATE] issue updating object ", e);
290 return false;
291 }
292 }
293
294 /// <summary>
295 /// Deletes given object from database.
296 /// </summary>
297 /// <param name="obj">Object to be deleted.</param>
298 /// <returns>True if operation was succesful.</returns>
299 public bool Delete(object obj)
300 {
301 try
302 {
303 using (IStatelessSession session = sessionFactory.OpenStatelessSession())
304 {
305 using (ITransaction transaction = session.BeginTransaction())
306 {
307 session.Delete(obj);
308 transaction.Commit();
309 return true;
310 }
311 }
312 }
313 catch (Exception e)
314 {
315 m_log.Error("[NHIBERNATE] issue deleting object ", e);
316 return false;
317 }
318 }
319
320 /// <summary>
321 /// Returns statefull session which can be used to execute custom nhibernate or sql queries.
322 /// </summary>
323 /// <returns>Statefull session</returns>
324 public ISession GetSession()
325 {
326 return sessionFactory.OpenSession();
327 }
328
329 /// <summary>
330 /// Drops the database schema. This exist for unit tests. It should not be invoked from other than test teardown.
331 /// </summary>
332 public void DropSchema()
333 {
334 SchemaExport export = new SchemaExport(this.configuration);
335 export.Drop(true, true);
336
337 using (ISession session = sessionFactory.OpenSession())
338 {
339 ISQLQuery sqlQuery = session.CreateSQLQuery("drop table migrations");
340 sqlQuery.ExecuteUpdate();
341 }
342 }
343
344 }
345}