aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
diff options
context:
space:
mode:
authorAdam Frisby2008-12-01 07:50:38 +0000
committerAdam Frisby2008-12-01 07:50:38 +0000
commit4ed7ca62b153817a142ded621cc5c5c1b1c055c5 (patch)
treee8cad8abe60b6becd22da1a8d2c2dfefbf6f9784 /OpenSim/Data/NHibernate/NHibernateInventoryData.cs
parentMantis #2729 (diff)
downloadopensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.zip
opensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.tar.gz
opensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.tar.bz2
opensim-SC-4ed7ca62b153817a142ded621cc5c5c1b1c055c5.tar.xz
* From Mantis#2701 - Implements NHibernate data adapter, adds support for MsSql2005 to NHibernate.
* Patch courtesy of Adminotech and realXtend. Thanks!
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateInventoryData.cs')
-rw-r--r--OpenSim/Data/NHibernate/NHibernateInventoryData.cs107
1 files changed, 35 insertions, 72 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
index bceb5d5..f7de8eb 100644
--- a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
+++ b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
@@ -33,10 +33,7 @@ using System.Text.RegularExpressions;
33using OpenMetaverse; 33using OpenMetaverse;
34using log4net; 34using log4net;
35using NHibernate; 35using NHibernate;
36using NHibernate.Cfg; 36using NHibernate.Criterion;
37using NHibernate.Expression;
38using NHibernate.Mapping.Attributes;
39using NHibernate.Tool.hbm2ddl;
40using OpenSim.Framework; 37using OpenSim.Framework;
41using Environment=NHibernate.Cfg.Environment; 38using Environment=NHibernate.Cfg.Environment;
42 39
@@ -46,9 +43,7 @@ namespace OpenSim.Data.NHibernate
46 { 43 {
47 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 44 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 45
49 private Configuration cfg; 46 private NHibernateManager manager;
50 private ISessionFactory factory;
51 private ISession session;
52 47
53 public void Initialise() 48 public void Initialise()
54 { 49 {
@@ -61,35 +56,8 @@ namespace OpenSim.Data.NHibernate
61 /// </summary> 56 /// </summary>
62 public void Initialise(string connect) 57 public void Initialise(string connect)
63 { 58 {
64 // Split out the dialect, driver, and connect string 59 m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateInventoryData");
65 char[] split = {';'}; 60 manager = new NHibernateManager(connect, "InventoryStore");
66 string[] parts = connect.Split(split, 3);
67 if (parts.Length != 3)
68 {
69 // TODO: make this a real exception type
70 throw new Exception("Malformed Inventory connection string '" + connect + "'");
71 }
72 string dialect = parts[0];
73
74 // Establish NHibernate Connection
75 cfg = new Configuration();
76 cfg.SetProperty(Environment.ConnectionProvider,
77 "NHibernate.Connection.DriverConnectionProvider");
78 cfg.SetProperty(Environment.Dialect,
79 "NHibernate.Dialect." + parts[0]);
80 cfg.SetProperty(Environment.ConnectionDriver,
81 "NHibernate.Driver." + parts[1]);
82 cfg.SetProperty(Environment.ConnectionString, parts[2]);
83 cfg.AddAssembly("OpenSim.Data.NHibernate");
84
85 factory = cfg.BuildSessionFactory();
86 session = factory.OpenSession();
87
88 // This actually does the roll forward assembly stuff
89 Assembly assem = GetType().Assembly;
90 Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "InventoryStore");
91 m.Update();
92
93 } 61 }
94 62
95 /***************************************************************** 63 /*****************************************************************
@@ -109,7 +77,8 @@ namespace OpenSim.Data.NHibernate
109 { 77 {
110 try 78 try
111 { 79 {
112 return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase; 80 m_log.InfoFormat("[NHIBERNATE] getInventoryItem {0}", item);
81 return (InventoryItemBase)manager.Load(typeof(InventoryItemBase), item);
113 } 82 }
114 catch 83 catch
115 { 84 {
@@ -126,15 +95,11 @@ namespace OpenSim.Data.NHibernate
126 { 95 {
127 if (!ExistsItem(item.ID)) 96 if (!ExistsItem(item.ID))
128 { 97 {
129 using (ITransaction transaction = session.BeginTransaction()) 98 manager.Save(item);
130 {
131 session.Save(item);
132 transaction.Commit();
133 }
134 } 99 }
135 else 100 else
136 { 101 {
137 m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists, updating instead", item.ID); 102 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists, updating instead", item.ID);
138 updateInventoryItem(item); 103 updateInventoryItem(item);
139 } 104 }
140 } 105 }
@@ -147,15 +112,11 @@ namespace OpenSim.Data.NHibernate
147 { 112 {
148 if (ExistsItem(item.ID)) 113 if (ExistsItem(item.ID))
149 { 114 {
150 using (ITransaction transaction = session.BeginTransaction()) 115 manager.Update(item);
151 {
152 session.Update(item);
153 transaction.Commit();
154 }
155 } 116 }
156 else 117 else
157 { 118 {
158 m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists", item.ID); 119 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists", item.ID);
159 } 120 }
160 } 121 }
161 122
@@ -165,11 +126,16 @@ namespace OpenSim.Data.NHibernate
165 /// <param name="item"></param> 126 /// <param name="item"></param>
166 public void deleteInventoryItem(UUID itemID) 127 public void deleteInventoryItem(UUID itemID)
167 { 128 {
168 using (ITransaction transaction = session.BeginTransaction()) 129 InventoryItemBase item = (InventoryItemBase)manager.Load(typeof(InventoryItemBase), itemID);
130 if (item != null)
169 { 131 {
170 session.Delete(itemID); 132 manager.Delete(item);
171 transaction.Commit();
172 } 133 }
134 else
135 {
136 m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryItemBase {0}", itemID);
137 }
138
173 } 139 }
174 140
175 /// <summary> 141 /// <summary>
@@ -181,11 +147,11 @@ namespace OpenSim.Data.NHibernate
181 { 147 {
182 try 148 try
183 { 149 {
184 return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase; 150 return (InventoryFolderBase)manager.Load(typeof(InventoryFolderBase), folder);
185 } 151 }
186 catch 152 catch
187 { 153 {
188 m_log.ErrorFormat("Couldn't find inventory item: {0}", folder); 154 m_log.ErrorFormat("[NHIBERNATE] Couldn't find inventory item: {0}", folder);
189 return null; 155 return null;
190 } 156 }
191 } 157 }
@@ -198,15 +164,11 @@ namespace OpenSim.Data.NHibernate
198 { 164 {
199 if (!ExistsFolder(folder.ID)) 165 if (!ExistsFolder(folder.ID))
200 { 166 {
201 using (ITransaction transaction = session.BeginTransaction()) 167 manager.Save(folder);
202 {
203 session.Save(folder);
204 transaction.Commit();
205 }
206 } 168 }
207 else 169 else
208 { 170 {
209 m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID); 171 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID);
210 updateInventoryFolder(folder); 172 updateInventoryFolder(folder);
211 } 173 }
212 } 174 }
@@ -219,15 +181,11 @@ namespace OpenSim.Data.NHibernate
219 { 181 {
220 if (ExistsFolder(folder.ID)) 182 if (ExistsFolder(folder.ID))
221 { 183 {
222 using (ITransaction transaction = session.BeginTransaction()) 184 manager.Update(folder);
223 {
224 session.Update(folder);
225 transaction.Commit();
226 }
227 } 185 }
228 else 186 else
229 { 187 {
230 m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists", folder.ID); 188 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists", folder.ID);
231 } 189 }
232 } 190 }
233 191
@@ -237,11 +195,16 @@ namespace OpenSim.Data.NHibernate
237 /// <param name="folder"></param> 195 /// <param name="folder"></param>
238 public void deleteInventoryFolder(UUID folderID) 196 public void deleteInventoryFolder(UUID folderID)
239 { 197 {
240 using (ITransaction transaction = session.BeginTransaction()) 198 InventoryFolderBase item = (InventoryFolderBase)manager.Load(typeof(InventoryFolderBase), folderID);
199 if (item != null)
200 {
201 manager.Delete(item);
202 }
203 else
241 { 204 {
242 session.Delete(folderID.ToString()); 205 m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryFolderBase {0}", folderID);
243 transaction.Commit();
244 } 206 }
207 manager.Delete(folderID);
245 } 208 }
246 209
247 // useful private methods 210 // useful private methods
@@ -317,7 +280,7 @@ namespace OpenSim.Data.NHibernate
317 public List<InventoryItemBase> getInventoryInFolder(UUID folderID) 280 public List<InventoryItemBase> getInventoryInFolder(UUID folderID)
318 { 281 {
319 // try { 282 // try {
320 ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase)); 283 ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryItemBase));
321 criteria.Add(Expression.Eq("Folder", folderID)); 284 criteria.Add(Expression.Eq("Folder", folderID));
322 List<InventoryItemBase> list = new List<InventoryItemBase>(); 285 List<InventoryItemBase> list = new List<InventoryItemBase>();
323 foreach (InventoryItemBase item in criteria.List()) 286 foreach (InventoryItemBase item in criteria.List())
@@ -340,7 +303,7 @@ namespace OpenSim.Data.NHibernate
340 // see InventoryItemBase.getUserRootFolder 303 // see InventoryItemBase.getUserRootFolder
341 public InventoryFolderBase getUserRootFolder(UUID user) 304 public InventoryFolderBase getUserRootFolder(UUID user)
342 { 305 {
343 ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); 306 ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase));
344 criteria.Add(Expression.Eq("ParentID", UUID.Zero)); 307 criteria.Add(Expression.Eq("ParentID", UUID.Zero));
345 criteria.Add(Expression.Eq("Owner", user)); 308 criteria.Add(Expression.Eq("Owner", user));
346 foreach (InventoryFolderBase folder in criteria.List()) 309 foreach (InventoryFolderBase folder in criteria.List())
@@ -358,7 +321,7 @@ namespace OpenSim.Data.NHibernate
358 /// <param name="parentID">ID of parent</param> 321 /// <param name="parentID">ID of parent</param>
359 private void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID) 322 private void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
360 { 323 {
361 ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase)); 324 ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase));
362 criteria.Add(Expression.Eq("ParentID", parentID)); 325 criteria.Add(Expression.Eq("ParentID", parentID));
363 foreach (InventoryFolderBase item in criteria.List()) 326 foreach (InventoryFolderBase item in criteria.List())
364 { 327 {