auto table creation for nhibernate backends

0.6.0-stable
Sean Dague 2008-04-24 15:00:42 +00:00
parent 2a2ef42e64
commit 56ef67ec6d
2 changed files with 57 additions and 1 deletions

View File

@ -28,11 +28,13 @@
using System; using System;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions;
using libsecondlife; using libsecondlife;
using log4net; using log4net;
using NHibernate; using NHibernate;
using NHibernate.Cfg; using NHibernate.Cfg;
using NHibernate.Mapping.Attributes; using NHibernate.Mapping.Attributes;
using NHibernate.Tool.hbm2ddl;
using OpenSim.Framework; using OpenSim.Framework;
using Environment=NHibernate.Cfg.Environment; using Environment=NHibernate.Cfg.Environment;
@ -58,6 +60,10 @@ namespace OpenSim.Data.NHibernate
// Split out the dialect, driver, and connect string // Split out the dialect, driver, and connect string
char[] split = {';'}; char[] split = {';'};
string[] parts = connect.Split(split); string[] parts = connect.Split(split);
if (parts.Length != 3) {
// TODO: make this a real exception type
throw new Exception("Malformed Inventory connection string '" + connect + "'");
}
// NHibernate setup // NHibernate setup
cfg = new Configuration(); cfg = new Configuration();
@ -75,13 +81,34 @@ namespace OpenSim.Data.NHibernate
HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly())) HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
cfg.AddInputStream(stream); cfg.AddInputStream(stream);
factory = cfg.BuildSessionFactory();
// If uncommented this will auto create tables, but it // If uncommented this will auto create tables, but it
// does drops of the old tables, so we need a smarter way // does drops of the old tables, so we need a smarter way
// to acturally manage this. // to acturally manage this.
// new SchemaExport(cfg).Create(true, true); // new SchemaExport(cfg).Create(true, true);
factory = cfg.BuildSessionFactory(); InitDB();
}
private void InitDB()
{
string regex = @"no such table: Assets";
Regex RE = new Regex(regex, RegexOptions.Multiline);
try {
using(ISession session = factory.OpenSession()) {
session.Load(typeof(AssetBase), LLUUID.Zero);
}
} catch (ObjectNotFoundException e) {
// yes, we know it's not there, but that's ok
} catch (ADOException e) {
Match m = RE.Match(e.ToString());
if(m.Success) {
// We don't have this table, so create it.
new SchemaExport(cfg).Create(true, true);
}
}
} }
override public AssetBase FetchAsset(LLUUID uuid) override public AssetBase FetchAsset(LLUUID uuid)

View File

@ -29,12 +29,14 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions;
using libsecondlife; using libsecondlife;
using log4net; using log4net;
using NHibernate; using NHibernate;
using NHibernate.Cfg; using NHibernate.Cfg;
using NHibernate.Expression; using NHibernate.Expression;
using NHibernate.Mapping.Attributes; using NHibernate.Mapping.Attributes;
using NHibernate.Tool.hbm2ddl;
using OpenSim.Framework; using OpenSim.Framework;
using Environment=NHibernate.Cfg.Environment; using Environment=NHibernate.Cfg.Environment;
@ -55,6 +57,10 @@ namespace OpenSim.Data.NHibernate
// Split out the dialect, driver, and connect string // Split out the dialect, driver, and connect string
char[] split = {';'}; char[] split = {';'};
string[] parts = connect.Split(split); string[] parts = connect.Split(split);
if (parts.Length != 3) {
// TODO: make this a real exception type
throw new Exception("Malformed Inventory connection string '" + connect + "'");
}
// Establish NHibernate Connection // Establish NHibernate Connection
cfg = new Configuration(); cfg = new Configuration();
@ -79,8 +85,31 @@ namespace OpenSim.Data.NHibernate
// new SchemaExport(cfg).Create(true, true); // new SchemaExport(cfg).Create(true, true);
factory = cfg.BuildSessionFactory(); factory = cfg.BuildSessionFactory();
InitDB();
} }
private void InitDB()
{
string regex = @"no such table: Inventory";
Regex RE = new Regex(regex, RegexOptions.Multiline);
try {
using(ISession session = factory.OpenSession()) {
session.Load(typeof(InventoryItemBase), LLUUID.Zero);
}
} catch (ObjectNotFoundException e) {
// yes, we know it's not there, but that's ok
} catch (ADOException e) {
Match m = RE.Match(e.ToString());
if(m.Success) {
// We don't have this table, so create it.
new SchemaExport(cfg).Create(true, true);
}
}
}
/***************************************************************** /*****************************************************************
* *
* Basic CRUD operations on Data * Basic CRUD operations on Data