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.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}