aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/NHibernate/NHibernateManager.cs
diff options
context:
space:
mode:
authorJustin Clarke Casey2008-12-02 15:22:58 +0000
committerJustin Clarke Casey2008-12-02 15:22:58 +0000
commitdde32f1130f83f9471fea474b1ebc9e0520be561 (patch)
treedc8683d7b14ada4aa07497da3ed4b5d7b79c57b8 /OpenSim/Data/NHibernate/NHibernateManager.cs
parent* Resolve http://opensimulator.org/mantis/view.php?id=2743 and http://opensim... (diff)
downloadopensim-SC-dde32f1130f83f9471fea474b1ebc9e0520be561.zip
opensim-SC-dde32f1130f83f9471fea474b1ebc9e0520be561.tar.gz
opensim-SC-dde32f1130f83f9471fea474b1ebc9e0520be561.tar.bz2
opensim-SC-dde32f1130f83f9471fea474b1ebc9e0520be561.tar.xz
* 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)
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/NHibernate/NHibernateManager.cs142
1 files changed, 124 insertions, 18 deletions
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 @@
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28using System;
28using System.Reflection; 29using System.Reflection;
30using System.IO;
29using log4net; 31using log4net;
30using NHibernate; 32using NHibernate;
33using NHibernate.Cfg;
34using NHibernate.Mapping.Attributes;
35using NHibernate.Tool.hbm2ddl;
36using OpenMetaverse;
37using Environment = NHibernate.Cfg.Environment;
31 38
32namespace OpenSim.Data.NHibernate 39namespace OpenSim.Data.NHibernate
33{ 40{
34 internal class NHibernateManager 41 public class NHibernateManager
35 { 42 {
36 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 43 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
37 44
38 // private ISessionFactory factory; 45 private string dialect;
39 // private ISession session; 46 private Configuration cfg;
40 // private ITransaction transaction; 47 private ISessionFactory factory;
48 private ISession session;
41 49
42 public NHibernateManager() 50 public NHibernateManager(string connect, string store)
43 { 51 {
44 // This is stubbing for now, it will become dynamic later and support different db backends 52
45// NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); 53 // Split out the dialect, driver, and connect string
46// cfg.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = 54 char[] split = { ';' };
47// "NHibernate.Connection.DriverConnectionProvider"; 55 string[] parts = connect.Split(split, 3);
48// cfg.Properties[NHibernate.Cfg.Environment.Dialect] = 56 if (parts.Length != 3)
49// "NHibernate.Dialect.SQLite"; 57 {
50// cfg.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = 58 // TODO: make this a real exception type
51// "NHibernate.Driver.SqliteClientDriver"; 59 throw new Exception("Malformed Inventory connection string '" + connect + "'");
52// cfg.Properties[NHibernate.Cfg.Environment.ConnectionString] = 60 }
53// "URI=file:opensim-nh.db,version=3"; 61
54 62 dialect = parts[0];
55// factory = cfg.BuildSessionFactory(); 63
64 // NHibernate setup
65 cfg = new Configuration();
66 cfg.SetProperty(Environment.ConnectionProvider,
67 "NHibernate.Connection.DriverConnectionProvider");
68 cfg.SetProperty(Environment.Dialect,
69 "NHibernate.Dialect." + dialect);
70 cfg.SetProperty(Environment.ConnectionDriver,
71 "NHibernate.Driver." + parts[1]);
72 cfg.SetProperty(Environment.ConnectionString, parts[2]);
73 cfg.AddAssembly("OpenSim.Data.NHibernate");
74
75 //To create sql file uncomment code below and write the name of the file
76 //SchemaExport exp = new SchemaExport(cfg);
77 //exp.SetOutputFile("nameofthefile.sql");
78 //exp.Create(false, true);
79
80 HbmSerializer.Default.Validate = true;
81 using (MemoryStream stream =
82 HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
83 cfg.AddInputStream(stream);
84
85 factory = cfg.BuildSessionFactory();
86 session = factory.OpenSession();
87
88 Assembly assem = GetType().Assembly;
89 Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, store);
90 m.Update();
56 } 91 }
57 92
93 public object Load(Type type, UUID uuid)
94 {
95 object obj = null;
96 try
97 {
98 obj = session.Load(type, uuid);
99 }
100 catch (Exception)
101 {
102 m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid);
103 }
104 return obj;
105
106 }
107
108 public bool Save(object obj)
109 {
110 try
111 {
112 session.BeginTransaction();
113 session.Save(obj);
114 session.Transaction.Commit();
115 session.Flush();
116 return true;
117 }
118 catch (Exception e)
119 {
120 m_log.Error("[NHIBERNATE] issue saving object ", e);
121 }
122 return false;
123 }
124
125 public bool Update(object obj)
126 {
127 try
128 {
129 session.BeginTransaction();
130 session.Update(obj);
131 session.Transaction.Commit();
132 session.Flush();
133 return true;
134 }
135 catch (Exception e)
136 {
137 m_log.Error("[NHIBERNATE] issue updating object ", e);
138 }
139 return false;
140 }
141
142 public bool Delete(object obj)
143 {
144 try
145 {
146 session.BeginTransaction();
147 session.Delete(obj);
148 session.Transaction.Commit();
149 session.Flush();
150 return true;
151 }
152 catch (Exception e)
153 {
154
155 m_log.Error("[NHIBERNATE] issue deleting object ", e);
156 }
157 return false;
158 }
159
160 public ISession GetSession()
161 {
162 return session;
163 }
58 } 164 }
59} 165}