internal ConfigurationInfo(IPersistenceConfigurer configurer, FluentNHibernatePersistenceBuilderOptions options, ProviderTypeEnum providerType) { PersistenceConfigurer = configurer; ProviderType = providerType; Options = options; }
public static SessionFactoryInfo GetFromAssemblyOf <T>(IPersistenceConfigurer configurer, FluentNHibernatePersistenceBuilderOptions options = null) { return(GetFromAssemblies(new List <Assembly> { typeof(T).Assembly }, configurer, options)); }
public SessionFactoryInfo(string key, ISessionFactory sessionFactory, ProviderTypeEnum providerType, FluentNHibernatePersistenceBuilderOptions options) { Key = key; SessionFactory = sessionFactory; Options = options; ProviderType = providerType; }
/// <summary> /// Factory method. Return simple configuration info based on the given provider type, connection string, and default /// schema /// </summary> /// <param name="nameOrConnectionString">Connection string or its name</param> /// <param name="providerType">Provider type from enumeration</param> /// <param name="options"></param> public static ConfigurationInfo Build(ProviderTypeEnum providerType, string nameOrConnectionString, FluentNHibernatePersistenceBuilderOptions options = null) { options = options ?? new FluentNHibernatePersistenceBuilderOptions(); var configurer = GetPersistenceConfigurer(providerType, nameOrConnectionString, options); return(new ConfigurationInfo(configurer, options, providerType)); }
public static SessionFactoryInfo GetFromAssemblyOf <T>(ProviderTypeEnum providerType, string nameOrConnectionString, FluentNHibernatePersistenceBuilderOptions options = null) { return(GetFromAssemblies(new List <Assembly> { typeof(T).Assembly }, providerType, nameOrConnectionString, options)); }
public static SessionFactoryInfo GetFromAssemblyOf <T>(ProviderTypeEnum providerType, string nameOrConnectionString, FluentNHibernatePersistenceBuilderOptions options = null) { options = options ?? new FluentNHibernatePersistenceBuilderOptions(); Func <ConfigurationInfo> configFunc = () => FluentNHibernatePersistenceBuilder.Build(providerType, nameOrConnectionString, options); var keyInfo = new KeyInfo { ProviderType = providerType, NameOrConnectionString = nameOrConnectionString, options = options, TypeFullName = typeof(T).FullName }; return(GetSessionFactoryInfo <T>(options, keyInfo, providerType, configFunc)); }
public static SessionFactoryInfo GetFromAssemblies(List <Assembly> sourceAssemblies, ProviderTypeEnum providerType, string nameOrConnectionString, FluentNHibernatePersistenceBuilderOptions options = null) { options = options ?? new FluentNHibernatePersistenceBuilderOptions(); Func <ConfigurationInfo> configFunc = () => FluentNHibernatePersistenceBuilder.Build(providerType, nameOrConnectionString, options); var keyInfo = new KeyInfo { ProviderType = providerType, NameOrConnectionString = nameOrConnectionString, Options = options, AssemblyNames = string.Join(",", sourceAssemblies.Distinct().OrderBy(i => i.FullName)) }; return(GetSessionFactoryInfo(sourceAssemblies, options, keyInfo, providerType, configFunc)); }
private static SessionFactoryInfo GetSessionFactoryInfo <T>(FluentNHibernatePersistenceBuilderOptions options, KeyInfo keyInfo, ProviderTypeEnum providerType, Func <ConfigurationInfo> configFunc) { var key = CalculateSHA512Hash(new JavaScriptSerializer().Serialize(keyInfo)); lock (Mutex) { if (SessionFactoryInfos.ContainsKey(key)) { return(SessionFactoryInfos[key]); } var configurationInfo = configFunc(); var configurationInfoPersistenceConfigurer = configurationInfo.PersistenceConfigurer; var fluentConfiguration = Fluently.Configure() .Mappings(x => x.FluentMappings.AddFromAssemblyOf <T>()) .Database(configurationInfoPersistenceConfigurer); fluentConfiguration.ExposeConfiguration(cfg => { if (options.UpdateSchema) { var schemaUpdate = new SchemaUpdate(cfg); using (var stringWriter = new StringWriter()) { try { schemaUpdate.Execute(i => stringWriter.WriteLine(i), true); } catch (Exception ex) { throw; } var d = stringWriter.ToString(); } } }); fluentConfiguration.BuildConfiguration(); SessionFactoryInfos[key] = new SessionFactoryInfo(key, fluentConfiguration.BuildSessionFactory(), providerType, options); return(SessionFactoryInfos[key]); } }
public void RenameObjects(FluentNHibernatePersistenceBuilderOptions options, Configuration cfg) { if (options.ObjectRenamer == null) { return; } foreach (var table in cfg.ClassMappings.Select(i => i.Table)) { RenameObject(table.IndexIterator, ObjectTypeEnum.Index, options, i => i.Name, (index, name) => index.Name = name); RenameObject(table.UniqueKeyIterator, ObjectTypeEnum.UniqueKey, options, i => i.Name, (uniqueKey, name) => uniqueKey.Name = name); RenameObject(table.ForeignKeyIterator, ObjectTypeEnum.ForeignKey, options, i => i.Name, (foreignKey, name) => foreignKey.Name = name); RenameObject(new List <Table> { table }, ObjectTypeEnum.Table, options, i => i.Name, (table1, name) => table1.Name = name); } }
private void RenameObject <T>(IEnumerable <T> items, ObjectTypeEnum objectType, FluentNHibernatePersistenceBuilderOptions options, Func <T, string> nameGetter, Action <T, string> nameSetter) { foreach (var item in items) { var originalName = nameGetter(item); var newName = options.ObjectRenamer.Rename(objectType, originalName); if (!string.IsNullOrWhiteSpace(newName) && newName != originalName) { _recoveryActions.Enqueue(() => { Logger.Debug("Renaming {0} '{1}' back to '{2}'", typeof(T).Name.ToLower(), newName, originalName); nameSetter(item, originalName); }); Logger.Debug("Temporarily renamed {2} {0} to {1}", originalName, newName, typeof(T).Name.ToLower()); nameSetter(item, newName); } } }
public void RenameObjects(FluentNHibernatePersistenceBuilderOptions options, Configuration cfg) { if (options.ObjectRenamer == null) { return; } foreach (var table in cfg.ClassMappings.Select(i => i.Table)) { foreach (var foreignKey in table.ForeignKeyIterator) { var newForeignKeyName = options.ObjectRenamer.Rename(ObjectTypeEnum.ForeignKey, foreignKey.Name); if (!string.IsNullOrWhiteSpace(newForeignKeyName) && newForeignKeyName != foreignKey.Name) { _foreignKeys.Add(new ForeignKeyNameRestorer(foreignKey, foreignKey.Name)); foreignKey.Name = newForeignKeyName; } } foreach (var uniqueKey in table.UniqueKeyIterator) { var newUniqueKeyName = options.ObjectRenamer.Rename(ObjectTypeEnum.UniqueKey, uniqueKey.Name); if (!string.IsNullOrWhiteSpace(newUniqueKeyName) && newUniqueKeyName != uniqueKey.Name) { _uniqueKeys.Add(new UniqueKeyNameRestorer(uniqueKey, uniqueKey.Name)); uniqueKey.Name = newUniqueKeyName; } } var newTableName = options.ObjectRenamer.Rename(ObjectTypeEnum.Table, table.Name); if (!string.IsNullOrWhiteSpace(newTableName) && newTableName != table.Name) { _tables.Add(new TableNameRestorer(table, table.Name)); table.Name = newTableName; } } }
public static SessionFactoryInfo GetFromAssemblyOf <T>(IPersistenceConfigurer configurer, FluentNHibernatePersistenceBuilderOptions options = null) { options = options ?? new FluentNHibernatePersistenceBuilderOptions(); var providerType = ProviderTypeHelper.InferProviderType(configurer); var derivedInfo = PersistenceConfigurationHelper.GetDerivedConnectionInfo(configurer); if (derivedInfo == null) { throw new ArgumentException("Could not derive connection string info from configurer"); } options.DefaultSchema = derivedInfo.DefaultSchema; Func <ConfigurationInfo> configFunc = () => new ConfigurationInfo(configurer, options, providerType); var keyInfo = new KeyInfo { ProviderType = providerType, NameOrConnectionString = derivedInfo.ConnectionString, options = options, TypeFullName = typeof(T).FullName }; return(GetSessionFactoryInfo <T>(options, keyInfo, providerType, configFunc)); }
public static SessionFactoryInfo GetFromAssemblies(List <Assembly> sourceAssemblies, IPersistenceConfigurer configurer, FluentNHibernatePersistenceBuilderOptions options = null) { options = options ?? new FluentNHibernatePersistenceBuilderOptions(); var providerType = ProviderTypeHelper.InferProviderType(configurer); var derivedInfo = PersistenceConfigurationHelper.GetDerivedConnectionInfo(configurer); if (derivedInfo == null) { throw new ArgumentException("Could not derive connection string info from configurer"); } options.DefaultSchema = derivedInfo.DefaultSchema; Func <ConfigurationInfo> configFunc = () => new ConfigurationInfo(configurer, options, providerType); var keyInfo = new KeyInfo { ProviderType = providerType, NameOrConnectionString = derivedInfo.ConnectionString, Options = options, AssemblyNames = string.Join(",", sourceAssemblies.Distinct().OrderBy(i => i.FullName)) }; return(GetSessionFactoryInfo(sourceAssemblies, options, keyInfo, providerType, configFunc)); }
/// <summary> /// Return an NHibernate persistence configurerTells the bootstrapper to use a FluentNHibernate provider as a job /// storage, /// that can be accessed using the given connection string or /// its name. /// </summary> /// <param name="nameOrConnectionString">Connection string or its name</param> /// <param name="providerType">Provider type from enumeration</param> /// <param name="options"></param> public static IPersistenceConfigurer GetPersistenceConfigurer(ProviderTypeEnum providerType, string nameOrConnectionString, FluentNHibernatePersistenceBuilderOptions options = null) { options = options ?? new FluentNHibernatePersistenceBuilderOptions(); var connectionString = GetConnectionString(nameOrConnectionString); IPersistenceConfigurer configurer; switch (providerType) { case ProviderTypeEnum.SQLAnywhere9: configurer = ConfigureProvider(() => SQLAnywhereConfiguration.SQLAnywhere9, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.SQLAnywhere10: configurer = ConfigureProvider(() => SQLAnywhereConfiguration.SQLAnywhere10, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.SQLAnywhere11: configurer = ConfigureProvider(() => SQLAnywhereConfiguration.SQLAnywhere11, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.SQLAnywhere12: configurer = ConfigureProvider(() => SQLAnywhereConfiguration.SQLAnywhere12, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.SQLAnywhere17: configurer = ConfigureProvider(() => SQLAnywhereConfiguration.SQLAnywhere17, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.SQLite: configurer = ConfigureProvider(() => SQLiteConfiguration.Standard, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.JetDriver: configurer = ConfigureProvider(() => JetDriverConfiguration.Standard, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.MsSqlCe40: configurer = ConfigureProvider(() => MsSqlCeConfiguration.MsSqlCe40, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.OracleClient10Managed: configurer = ConfigureProvider(() => OracleClientConfiguration.Oracle10, connectionString, options.DefaultSchema) .Driver <OracleManagedDataClientDriver>(); break; case ProviderTypeEnum.OracleClient9Managed: configurer = ConfigureProvider(() => OracleClientConfiguration.Oracle9, connectionString, options.DefaultSchema) .Driver <OracleManagedDataClientDriver>(); break; case ProviderTypeEnum.OracleClient10: configurer = ConfigureProvider(() => OracleClientConfiguration.Oracle10, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.OracleClient9: configurer = ConfigureProvider(() => OracleClientConfiguration.Oracle9, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.PostgreSQLStandard: configurer = ConfigureProvider(() => PostgreSQLConfiguration.Standard, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.PostgreSQL81: configurer = ConfigureProvider(() => PostgreSQLConfiguration.PostgreSQL81, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.PostgreSQL82: configurer = ConfigureProvider(() => PostgreSQLConfiguration.PostgreSQL82, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.Firebird: configurer = ConfigureProvider(() => new FirebirdConfiguration(), connectionString, options.DefaultSchema); break; case ProviderTypeEnum.DB2Informix1150: configurer = ConfigureProvider(() => DB2Configuration.Informix1150, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.DB2Standard: configurer = ConfigureProvider(() => DB2Configuration.Standard, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.MySQL: configurer = ConfigureProvider(() => MySQLConfiguration.Standard, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.MsSql2008: configurer = ConfigureProvider(() => MsSqlConfiguration.MsSql2008, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.MsSql2012: configurer = ConfigureProvider(() => MsSqlConfiguration.MsSql2012, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.MsSql2005: configurer = ConfigureProvider(() => MsSqlConfiguration.MsSql2005, connectionString, options.DefaultSchema); break; case ProviderTypeEnum.MsSql2000: configurer = ConfigureProvider(() => MsSqlConfiguration.MsSql2000, connectionString, options.DefaultSchema); break; default: throw new ArgumentException("type"); } return(configurer); }
private static SessionFactoryInfo GetSessionFactoryInfo(List <Assembly> sourceAssemblies, FluentNHibernatePersistenceBuilderOptions options, KeyInfo keyInfo, ProviderTypeEnum providerType, Func <ConfigurationInfo> configFunc) { var key = CalculateSHA512Hash(JsonConvert.SerializeObject(keyInfo)); lock (Mutex) { if (SessionFactoryInfos.ContainsKey(key)) { return(SessionFactoryInfos[key]); } var configurationInfo = configFunc(); var configurationInfoPersistenceConfigurer = configurationInfo.PersistenceConfigurer; var configuration = Fluently.Configure().Database(configurationInfoPersistenceConfigurer); foreach (var assembly in sourceAssemblies) { configuration = configuration.Mappings(x => x.FluentMappings.AddFromAssembly(assembly)); } using (var objectNameStore = new ObjectRenameManager()) { //make sure we only execute ExposeConfiguration once. It will try to execute again when we build the session factory var exposed = false; configuration.ExposeConfiguration(cfg => { if (exposed) { return; } exposed = true; objectNameStore.RenameObjects(options, cfg); if (options.UpdateSchema) { var schemaUpdate = new SchemaUpdate(cfg); using (LogProvider.OpenNestedContext("Schema update")) { Logger.Debug("Starting schema update"); schemaUpdate.Execute(i => Logger.Debug(i), true); Logger.Debug("Done with schema update"); } if (schemaUpdate.Exceptions != null && schemaUpdate.Exceptions.Any()) { throw new SchemaException( string.Format("Schema update failed with {1} exceptions. See {0} property", nameof(SchemaException.Exceptions), schemaUpdate.Exceptions.Count), schemaUpdate.Exceptions); } } }); SessionFactoryInfos[key] = new SessionFactoryInfo(key, configuration.BuildSessionFactory(), providerType, options); } return(SessionFactoryInfos[key]); } }