aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateInventoryData.cs')
-rw-r--r--OpenSim/Data/NHibernate/NHibernateInventoryData.cs107
1 files changed, 72 insertions, 35 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
index f7de8eb..bceb5d5 100644
--- a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
+++ b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs
@@ -33,7 +33,10 @@ using System.Text.RegularExpressions;
33using OpenMetaverse; 33using OpenMetaverse;
34using log4net; 34using log4net;
35using NHibernate; 35using NHibernate;
36using NHibernate.Criterion; 36using NHibernate.Cfg;
37using NHibernate.Expression;
38using NHibernate.Mapping.Attributes;
39using NHibernate.Tool.hbm2ddl;
37using OpenSim.Framework; 40using OpenSim.Framework;
38using Environment=NHibernate.Cfg.Environment; 41using Environment=NHibernate.Cfg.Environment;
39 42
@@ -43,7 +46,9 @@ namespace OpenSim.Data.NHibernate
43 { 46 {
44 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 47 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45 48
46 private NHibernateManager manager; 49 private Configuration cfg;
50 private ISessionFactory factory;
51 private ISession session;
47 52
48 public void Initialise() 53 public void Initialise()
49 { 54 {
@@ -56,8 +61,35 @@ namespace OpenSim.Data.NHibernate
56 /// </summary> 61 /// </summary>
57 public void Initialise(string connect) 62 public void Initialise(string connect)
58 { 63 {
59 m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateInventoryData"); 64 // Split out the dialect, driver, and connect string
60 manager = new NHibernateManager(connect, "InventoryStore"); 65 char[] split = {';'};
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
61 } 93 }
62 94
63 /***************************************************************** 95 /*****************************************************************
@@ -77,8 +109,7 @@ namespace OpenSim.Data.NHibernate
77 { 109 {
78 try 110 try
79 { 111 {
80 m_log.InfoFormat("[NHIBERNATE] getInventoryItem {0}", item); 112 return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase;
81 return (InventoryItemBase)manager.Load(typeof(InventoryItemBase), item);
82 } 113 }
83 catch 114 catch
84 { 115 {
@@ -95,11 +126,15 @@ namespace OpenSim.Data.NHibernate
95 { 126 {
96 if (!ExistsItem(item.ID)) 127 if (!ExistsItem(item.ID))
97 { 128 {
98 manager.Save(item); 129 using (ITransaction transaction = session.BeginTransaction())
130 {
131 session.Save(item);
132 transaction.Commit();
133 }
99 } 134 }
100 else 135 else
101 { 136 {
102 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists, updating instead", item.ID); 137 m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists, updating instead", item.ID);
103 updateInventoryItem(item); 138 updateInventoryItem(item);
104 } 139 }
105 } 140 }
@@ -112,11 +147,15 @@ namespace OpenSim.Data.NHibernate
112 { 147 {
113 if (ExistsItem(item.ID)) 148 if (ExistsItem(item.ID))
114 { 149 {
115 manager.Update(item); 150 using (ITransaction transaction = session.BeginTransaction())
151 {
152 session.Update(item);
153 transaction.Commit();
154 }
116 } 155 }
117 else 156 else
118 { 157 {
119 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists", item.ID); 158 m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists", item.ID);
120 } 159 }
121 } 160 }
122 161
@@ -126,16 +165,11 @@ namespace OpenSim.Data.NHibernate
126 /// <param name="item"></param> 165 /// <param name="item"></param>
127 public void deleteInventoryItem(UUID itemID) 166 public void deleteInventoryItem(UUID itemID)
128 { 167 {
129 InventoryItemBase item = (InventoryItemBase)manager.Load(typeof(InventoryItemBase), itemID); 168 using (ITransaction transaction = session.BeginTransaction())
130 if (item != null)
131 { 169 {
132 manager.Delete(item); 170 session.Delete(itemID);
171 transaction.Commit();
133 } 172 }
134 else
135 {
136 m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryItemBase {0}", itemID);
137 }
138
139 } 173 }
140 174
141 /// <summary> 175 /// <summary>
@@ -147,11 +181,11 @@ namespace OpenSim.Data.NHibernate
147 { 181 {
148 try 182 try
149 { 183 {
150 return (InventoryFolderBase)manager.Load(typeof(InventoryFolderBase), folder); 184 return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase;
151 } 185 }
152 catch 186 catch
153 { 187 {
154 m_log.ErrorFormat("[NHIBERNATE] Couldn't find inventory item: {0}", folder); 188 m_log.ErrorFormat("Couldn't find inventory item: {0}", folder);
155 return null; 189 return null;
156 } 190 }
157 } 191 }
@@ -164,11 +198,15 @@ namespace OpenSim.Data.NHibernate
164 { 198 {
165 if (!ExistsFolder(folder.ID)) 199 if (!ExistsFolder(folder.ID))
166 { 200 {
167 manager.Save(folder); 201 using (ITransaction transaction = session.BeginTransaction())
202 {
203 session.Save(folder);
204 transaction.Commit();
205 }
168 } 206 }
169 else 207 else
170 { 208 {
171 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID); 209 m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID);
172 updateInventoryFolder(folder); 210 updateInventoryFolder(folder);
173 } 211 }
174 } 212 }
@@ -181,11 +219,15 @@ namespace OpenSim.Data.NHibernate
181 { 219 {
182 if (ExistsFolder(folder.ID)) 220 if (ExistsFolder(folder.ID))
183 { 221 {
184 manager.Update(folder); 222 using (ITransaction transaction = session.BeginTransaction())
223 {
224 session.Update(folder);
225 transaction.Commit();
226 }
185 } 227 }
186 else 228 else
187 { 229 {
188 m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists", folder.ID); 230 m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists", folder.ID);
189 } 231 }
190 } 232 }
191 233
@@ -195,16 +237,11 @@ namespace OpenSim.Data.NHibernate
195 /// <param name="folder"></param> 237 /// <param name="folder"></param>
196 public void deleteInventoryFolder(UUID folderID) 238 public void deleteInventoryFolder(UUID folderID)
197 { 239 {
198 InventoryFolderBase item = (InventoryFolderBase)manager.Load(typeof(InventoryFolderBase), folderID); 240 using (ITransaction transaction = session.BeginTransaction())
199 if (item != null)
200 {
201 manager.Delete(item);
202 }
203 else
204 { 241 {
205 m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryFolderBase {0}", folderID); 242 session.Delete(folderID.ToString());
243 transaction.Commit();
206 } 244 }
207 manager.Delete(folderID);
208 } 245 }
209 246
210 // useful private methods 247 // useful private methods
@@ -280,7 +317,7 @@ namespace OpenSim.Data.NHibernate
280 public List<InventoryItemBase> getInventoryInFolder(UUID folderID) 317 public List<InventoryItemBase> getInventoryInFolder(UUID folderID)
281 { 318 {
282 // try { 319 // try {
283 ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryItemBase)); 320 ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase));
284 criteria.Add(Expression.Eq("Folder", folderID)); 321 criteria.Add(Expression.Eq("Folder", folderID));
285 List<InventoryItemBase> list = new List<InventoryItemBase>(); 322 List<InventoryItemBase> list = new List<InventoryItemBase>();
286 foreach (InventoryItemBase item in criteria.List()) 323 foreach (InventoryItemBase item in criteria.List())
@@ -303,7 +340,7 @@ namespace OpenSim.Data.NHibernate
303 // see InventoryItemBase.getUserRootFolder 340 // see InventoryItemBase.getUserRootFolder
304 public InventoryFolderBase getUserRootFolder(UUID user) 341 public InventoryFolderBase getUserRootFolder(UUID user)
305 { 342 {
306 ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase)); 343 ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase));
307 criteria.Add(Expression.Eq("ParentID", UUID.Zero)); 344 criteria.Add(Expression.Eq("ParentID", UUID.Zero));
308 criteria.Add(Expression.Eq("Owner", user)); 345 criteria.Add(Expression.Eq("Owner", user));
309 foreach (InventoryFolderBase folder in criteria.List()) 346 foreach (InventoryFolderBase folder in criteria.List())
@@ -321,7 +358,7 @@ namespace OpenSim.Data.NHibernate
321 /// <param name="parentID">ID of parent</param> 358 /// <param name="parentID">ID of parent</param>
322 private void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID) 359 private void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
323 { 360 {
324 ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase)); 361 ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase));
325 criteria.Add(Expression.Eq("ParentID", parentID)); 362 criteria.Add(Expression.Eq("ParentID", parentID));
326 foreach (InventoryFolderBase item in criteria.List()) 363 foreach (InventoryFolderBase item in criteria.List())
327 { 364 {