diff options
author | Justin Clarke Casey | 2009-02-16 19:33:11 +0000 |
---|---|---|
committer | Justin Clarke Casey | 2009-02-16 19:33:11 +0000 |
commit | 80759f708b1766b6fd36bbdb6ce69f292e64e2f0 (patch) | |
tree | 2ca913692571127b1b9bc5327ddc937b8d0211ab /OpenSim/Data/NHibernate/NHibernateManager.cs | |
parent | line ending fixes and set native eol property (diff) | |
download | opensim-SC_OLD-80759f708b1766b6fd36bbdb6ce69f292e64e2f0.zip opensim-SC_OLD-80759f708b1766b6fd36bbdb6ce69f292e64e2f0.tar.gz opensim-SC_OLD-80759f708b1766b6fd36bbdb6ce69f292e64e2f0.tar.bz2 opensim-SC_OLD-80759f708b1766b6fd36bbdb6ce69f292e64e2f0.tar.xz |
* Apply http://opensimulator.org/mantis/view.php?id=3166
* Adds estate access list supports to NHibernate data module
* Thanks Tommil
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateManager.cs')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateManager.cs | 187 |
1 files changed, 150 insertions, 37 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateManager.cs b/OpenSim/Data/NHibernate/NHibernateManager.cs index 26bc219..cef98f2 100644 --- a/OpenSim/Data/NHibernate/NHibernateManager.cs +++ b/OpenSim/Data/NHibernate/NHibernateManager.cs | |||
@@ -45,6 +45,43 @@ namespace OpenSim.Data.NHibernate | |||
45 | private Configuration configuration; | 45 | private Configuration configuration; |
46 | private ISessionFactory sessionFactory; | 46 | private ISessionFactory sessionFactory; |
47 | 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 | |||
48 | /// <summary> | 85 | /// <summary> |
49 | /// Parses the connection string and creates the NHibernate configuration | 86 | /// Parses the connection string and creates the NHibernate configuration |
50 | /// </summary> | 87 | /// </summary> |
@@ -72,7 +109,6 @@ namespace OpenSim.Data.NHibernate | |||
72 | "NHibernate.Driver." + parts[1]); | 109 | "NHibernate.Driver." + parts[1]); |
73 | configuration.SetProperty(Environment.ConnectionString, parts[2]); | 110 | configuration.SetProperty(Environment.ConnectionString, parts[2]); |
74 | configuration.AddAssembly("OpenSim.Data.NHibernate"); | 111 | configuration.AddAssembly("OpenSim.Data.NHibernate"); |
75 | |||
76 | } | 112 | } |
77 | 113 | ||
78 | /// <summary> | 114 | /// <summary> |
@@ -96,44 +132,42 @@ namespace OpenSim.Data.NHibernate | |||
96 | migration.Update(); | 132 | migration.Update(); |
97 | } | 133 | } |
98 | 134 | ||
135 | #endregion | ||
136 | |||
99 | /// <summary> | 137 | /// <summary> |
100 | /// Initiate NHibernate Manager | 138 | /// Gets object of given type from database with given id. |
139 | /// Uses stateless session for efficiency. | ||
101 | /// </summary> | 140 | /// </summary> |
102 | /// <param name="connect">NHibernate dialect, driver and connection string separated by ';'</param> | 141 | /// <param name="type">Type of the object.</param> |
103 | /// <param name="store">Name of the store</param> | 142 | /// <param name="id">Id of the object.</param> |
104 | public NHibernateManager(string connect, string store) | 143 | /// <returns>The object or null if object was not found.</returns> |
144 | public object Get(Type type, Object id) | ||
105 | { | 145 | { |
106 | ParseConnectionString(connect); | 146 | using (IStatelessSession session = sessionFactory.OpenStatelessSession()) |
107 | 147 | { | |
108 | //To create sql file uncomment code below and write the name of the file | 148 | object obj = null; |
109 | //SchemaExport exp = new SchemaExport(cfg); | 149 | try |
110 | //exp.SetOutputFile("nameofthefile.sql"); | 150 | { |
111 | //exp.Create(false, true); | 151 | obj = session.Get(type.FullName, id); |
112 | 152 | } | |
113 | Assembly assembly = GetType().Assembly; | 153 | catch (Exception e) |
114 | 154 | { | |
115 | sessionFactory = configuration.BuildSessionFactory(); | 155 | m_log.ErrorFormat("[NHIBERNATE] {0} of id {1} loading threw exception: " + e.ToString(), type.Name, id); |
116 | RunMigration(dialect, assembly, store); | 156 | } |
157 | return obj; | ||
158 | } | ||
117 | } | 159 | } |
118 | 160 | ||
119 | /// <summary> | 161 | /// <summary> |
120 | /// Initiate NHibernate Manager with spesific assembly | 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. | ||
121 | /// </summary> | 164 | /// </summary> |
122 | /// <param name="connect">NHibernate dialect, driver and connection string separated by ';'</param> | 165 | /// <param name="type">Type of the object.</param> |
123 | /// <param name="store">Name of the store</param> | 166 | /// <param name="id">Id of the object.</param> |
124 | /// <param name="assembly">Outside assembly to be included </param> | 167 | /// <returns>The object or null if object was not found.</returns> |
125 | public NHibernateManager(string connect, string store, Assembly assembly) | 168 | public object GetWithStatefullSession(Type type, Object id) |
126 | { | 169 | { |
127 | ParseConnectionString(connect); | 170 | using (ISession session = sessionFactory.OpenSession()) |
128 | |||
129 | configuration.AddAssembly(assembly); | ||
130 | sessionFactory = configuration.BuildSessionFactory(); | ||
131 | RunMigration(dialect, assembly, store); | ||
132 | } | ||
133 | |||
134 | public object Load(Type type, Object id) | ||
135 | { | ||
136 | using (IStatelessSession session = sessionFactory.OpenStatelessSession()) | ||
137 | { | 171 | { |
138 | object obj = null; | 172 | object obj = null; |
139 | try | 173 | try |
@@ -146,9 +180,15 @@ namespace OpenSim.Data.NHibernate | |||
146 | } | 180 | } |
147 | return obj; | 181 | return obj; |
148 | } | 182 | } |
149 | 183 | ||
150 | } | 184 | } |
151 | 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> | ||
152 | public object Insert(object obj) | 192 | public object Insert(object obj) |
153 | { | 193 | { |
154 | try | 194 | try |
@@ -170,6 +210,39 @@ namespace OpenSim.Data.NHibernate | |||
170 | } | 210 | } |
171 | } | 211 | } |
172 | 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> | ||
173 | public bool Update(object obj) | 246 | public bool Update(object obj) |
174 | { | 247 | { |
175 | try | 248 | try |
@@ -191,6 +264,38 @@ namespace OpenSim.Data.NHibernate | |||
191 | } | 264 | } |
192 | } | 265 | } |
193 | 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> | ||
194 | public bool Delete(object obj) | 299 | public bool Delete(object obj) |
195 | { | 300 | { |
196 | try | 301 | try |
@@ -212,6 +317,18 @@ namespace OpenSim.Data.NHibernate | |||
212 | } | 317 | } |
213 | } | 318 | } |
214 | 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> | ||
215 | public void DropSchema() | 332 | public void DropSchema() |
216 | { | 333 | { |
217 | SchemaExport export = new SchemaExport(this.configuration); | 334 | SchemaExport export = new SchemaExport(this.configuration); |
@@ -219,14 +336,10 @@ namespace OpenSim.Data.NHibernate | |||
219 | 336 | ||
220 | using (ISession session = sessionFactory.OpenSession()) | 337 | using (ISession session = sessionFactory.OpenSession()) |
221 | { | 338 | { |
222 | ISQLQuery sqlQuery=session.CreateSQLQuery("drop table migrations"); | 339 | ISQLQuery sqlQuery = session.CreateSQLQuery("drop table migrations"); |
223 | sqlQuery.ExecuteUpdate(); | 340 | sqlQuery.ExecuteUpdate(); |
224 | } | 341 | } |
225 | } | 342 | } |
226 | 343 | ||
227 | public ISession GetSession() | ||
228 | { | ||
229 | return sessionFactory.OpenSession(); | ||
230 | } | ||
231 | } | 344 | } |
232 | } | 345 | } |