diff options
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateManager.cs')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateManager.cs | 142 |
1 files changed, 18 insertions, 124 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateManager.cs b/OpenSim/Data/NHibernate/NHibernateManager.cs index 36d84e2..161ec1d 100644 --- a/OpenSim/Data/NHibernate/NHibernateManager.cs +++ b/OpenSim/Data/NHibernate/NHibernateManager.cs | |||
@@ -25,141 +25,35 @@ | |||
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 | ||
28 | using System; | ||
29 | using System.Reflection; | 28 | using System.Reflection; |
30 | using System.IO; | ||
31 | using log4net; | 29 | using log4net; |
32 | using NHibernate; | 30 | using NHibernate; |
33 | using NHibernate.Cfg; | ||
34 | using NHibernate.Mapping.Attributes; | ||
35 | using NHibernate.Tool.hbm2ddl; | ||
36 | using OpenMetaverse; | ||
37 | using Environment = NHibernate.Cfg.Environment; | ||
38 | 31 | ||
39 | namespace OpenSim.Data.NHibernate | 32 | namespace OpenSim.Data.NHibernate |
40 | { | 33 | { |
41 | public class NHibernateManager | 34 | internal class NHibernateManager |
42 | { | 35 | { |
43 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 36 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
44 | 37 | ||
45 | private string dialect; | 38 | // private ISessionFactory factory; |
46 | private Configuration cfg; | 39 | // private ISession session; |
47 | private ISessionFactory factory; | 40 | // private ITransaction transaction; |
48 | private ISession session; | ||
49 | 41 | ||
50 | public NHibernateManager(string connect, string store) | 42 | public NHibernateManager() |
51 | { | 43 | { |
52 | 44 | // This is stubbing for now, it will become dynamic later and support different db backends | |
53 | // Split out the dialect, driver, and connect string | 45 | // NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); |
54 | char[] split = { ';' }; | 46 | // cfg.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = |
55 | string[] parts = connect.Split(split, 3); | 47 | // "NHibernate.Connection.DriverConnectionProvider"; |
56 | if (parts.Length != 3) | 48 | // cfg.Properties[NHibernate.Cfg.Environment.Dialect] = |
57 | { | 49 | // "NHibernate.Dialect.SQLite"; |
58 | // TODO: make this a real exception type | 50 | // cfg.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = |
59 | throw new Exception("Malformed Inventory connection string '" + connect + "'"); | 51 | // "NHibernate.Driver.SqliteClientDriver"; |
60 | } | 52 | // cfg.Properties[NHibernate.Cfg.Environment.ConnectionString] = |
61 | 53 | // "URI=file:opensim-nh.db,version=3"; | |
62 | dialect = parts[0]; | 54 | |
63 | 55 | // factory = cfg.BuildSessionFactory(); | |
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(); | ||
91 | } | 56 | } |
92 | 57 | ||
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 | } | ||
164 | } | 58 | } |
165 | } | 59 | } |