public void UpdateReservedWordsInDialect() { var reservedDb = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var configuration = TestConfigurationHelper.GetDefaultConfiguration(); var dialect = Dialect.Dialect.GetDialect(configuration.Properties); var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties); connectionHelper.Prepare(); try { var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection); foreach (var rw in metaData.GetReservedWords()) { reservedDb.Add(rw.ToLowerInvariant()); } } finally { connectionHelper.Release(); } var sf = (ISessionFactoryImplementor)configuration.BuildSessionFactory(); SchemaMetadataUpdater.Update(sf); var match = reservedDb.Intersect(sf.Dialect.Keywords, StringComparer.OrdinalIgnoreCase); // tests that nothing in the first metaData.GetReservedWords() is left out of the second metaData.GetReservedWords() call. // i.e. always passes. Assert.That(match, Is.EquivalentTo(reservedDb)); }
public void ExplicitAutoQuote() { var configuration = TestConfigurationHelper.GetDefaultConfiguration(); configuration.AddResource("NHibernate.Test.Tools.hbm2ddl.SchemaMetadataUpdaterTest.HeavyEntity.hbm.xml", GetType().Assembly); var dialect = Dialect.Dialect.GetDialect(configuration.GetDerivedProperties()); dialect.Keywords.Add("Abracadabra"); SchemaMetadataUpdater.Update(configuration, dialect); SchemaMetadataUpdater.QuoteTableAndColumns(configuration, dialect); var cm = configuration.GetClassMapping(typeof(Order)); Assert.That(cm.Table.IsQuoted); var culs = new List <Column>(cm.Table.ColumnIterator); Assert.That(GetColumnByName(culs, "From").IsQuoted); Assert.That(GetColumnByName(culs, "And").IsQuoted); Assert.That(GetColumnByName(culs, "Select").IsQuoted); Assert.That(GetColumnByName(culs, "Abracadabra").IsQuoted); Assert.That(!GetColumnByName(culs, "Name").IsQuoted); }
public void UpdateReservedWordsInDialect() { var reservedDb = new HashedSet <string>(); var configuration = TestConfigurationHelper.GetDefaultConfiguration(); var dialect = Dialect.Dialect.GetDialect(configuration.Properties); var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties); connectionHelper.Prepare(); try { var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection); foreach (var rw in metaData.GetReservedWords()) { reservedDb.Add(rw.ToLowerInvariant()); } } finally { connectionHelper.Release(); } var sf = (ISessionFactoryImplementor)configuration.BuildSessionFactory(); SchemaMetadataUpdater.Update(sf); var match = reservedDb.Intersect(sf.Dialect.Keywords); Assert.That(match, Is.EquivalentTo(reservedDb)); }
public void WhenConfiguredOnlyExplicitAutoQuote() { var configuration = TestConfigurationHelper.GetDefaultConfiguration(); var configuredDialect = Dialect.Dialect.GetDialect(); if (!configuredDialect.DefaultProperties.ContainsKey(Environment.ConnectionDriver)) { Assert.Ignore(GetType() + " does not apply to " + configuredDialect); } configuration.Properties.Remove(Environment.ConnectionDriver); configuration.AddResource("NHibernate.Test.Tools.hbm2ddl.SchemaMetadataUpdaterTest.HeavyEntity.hbm.xml", GetType().Assembly); var dialect = Dialect.Dialect.GetDialect(configuration.GetDerivedProperties()); dialect.Keywords.Add("Abracadabra"); SchemaMetadataUpdater.Update(configuration, dialect); SchemaMetadataUpdater.QuoteTableAndColumns(configuration, dialect); var cm = configuration.GetClassMapping(typeof(Order)); Assert.That(cm.Table.IsQuoted); var culs = new List <Column>(cm.Table.ColumnIterator); Assert.That(GetColumnByName(culs, "From").IsQuoted); Assert.That(GetColumnByName(culs, "And").IsQuoted); Assert.That(GetColumnByName(culs, "Select").IsQuoted); Assert.That(GetColumnByName(culs, "Abracadabra").IsQuoted); Assert.That(!GetColumnByName(culs, "Name").IsQuoted); }
public void WhenConfiguredOnlyExplicitAutoQuote() { var configuration = TestConfigurationHelper.GetDefaultConfiguration(); var driverClass = ReflectHelper.ClassForName(configuration.GetProperty(Environment.ConnectionDriver)); // Test uses the default dialect driver, which will not accept Odbc or OleDb connection strings. if (typeof(OdbcDriver).IsAssignableFrom(driverClass) || typeof(OleDbDriver).IsAssignableFrom(driverClass)) { Assert.Ignore("Test is not compatible with OleDb or ODBC driver connection strings"); } var configuredDialect = Dialect.Dialect.GetDialect(); if (!configuredDialect.DefaultProperties.ContainsKey(Environment.ConnectionDriver)) { Assert.Ignore(GetType() + " does not apply to " + configuredDialect); } configuration.Properties.Remove(Environment.ConnectionDriver); configuration.AddResource("NHibernate.Test.Tools.hbm2ddl.SchemaMetadataUpdaterTest.HeavyEntity.hbm.xml", GetType().Assembly); var dialect = Dialect.Dialect.GetDialect(configuration.GetDerivedProperties()); dialect.Keywords.Add("Abracadabra"); SchemaMetadataUpdater.Update(configuration, dialect); SchemaMetadataUpdater.QuoteTableAndColumns(configuration, dialect); var cm = configuration.GetClassMapping(typeof(Order)); Assert.That(cm.Table.IsQuoted); var culs = new List <Column>(cm.Table.ColumnIterator); Assert.That(GetColumnByName(culs, "From").IsQuoted); Assert.That(GetColumnByName(culs, "And").IsQuoted); Assert.That(GetColumnByName(culs, "Select").IsQuoted); Assert.That(GetColumnByName(culs, "Abracadabra").IsQuoted); Assert.That(!GetColumnByName(culs, "Name").IsQuoted); }
public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) { Init(); log.Info("building session factory"); properties = new Dictionary <string, string>(cfg.Properties); interceptor = cfg.Interceptor; this.settings = settings; sqlFunctionRegistry = new SQLFunctionRegistry(settings.Dialect, cfg.SqlFunctions); eventListeners = listeners; filters = new Dictionary <string, FilterDefinition>(cfg.FilterDefinitions); if (log.IsDebugEnabled) { log.Debug("Session factory constructed with filter configurations : " + CollectionPrinter.ToString(filters)); } if (log.IsDebugEnabled) { log.Debug("instantiating session factory with properties: " + CollectionPrinter.ToString(properties)); } try { if (settings.IsKeywordsImportEnabled) { SchemaMetadataUpdater.Update(this); } if (settings.IsAutoQuoteEnabled) { SchemaMetadataUpdater.QuoteTableAndColumns(cfg); } } catch (NotSupportedException) { // Ignore if the Dialect does not provide DataBaseSchema } #region Caches settings.CacheProvider.Start(properties); #endregion #region Generators identifierGenerators = new Dictionary <string, IIdentifierGenerator>(); foreach (PersistentClass model in cfg.ClassMappings) { if (!model.IsInherited) { IIdentifierGenerator generator = model.Identifier.CreateIdentifierGenerator(settings.Dialect, settings.DefaultCatalogName, settings.DefaultSchemaName, (RootClass)model); identifierGenerators[model.EntityName] = generator; } } #endregion #region Persisters Dictionary <string, ICacheConcurrencyStrategy> caches = new Dictionary <string, ICacheConcurrencyStrategy>(); entityPersisters = new Dictionary <string, IEntityPersister>(); implementorToEntityName = new Dictionary <System.Type, string>(); Dictionary <string, IClassMetadata> classMeta = new Dictionary <string, IClassMetadata>(); foreach (PersistentClass model in cfg.ClassMappings) { model.PrepareTemporaryTables(mapping, settings.Dialect); string cacheRegion = model.RootClazz.CacheRegionName; ICacheConcurrencyStrategy cache; if (!caches.TryGetValue(cacheRegion, out cache)) { cache = CacheFactory.CreateCache(model.CacheConcurrencyStrategy, cacheRegion, model.IsMutable, settings, properties); if (cache != null) { caches.Add(cacheRegion, cache); allCacheRegions.Add(cache.RegionName, cache.Cache); } } IEntityPersister cp = PersisterFactory.CreateClassPersister(model, cache, this, mapping); entityPersisters[model.EntityName] = cp; classMeta[model.EntityName] = cp.ClassMetadata; if (model.HasPocoRepresentation) { implementorToEntityName[model.MappedClass] = model.EntityName; } } classMetadata = new UnmodifiableDictionary <string, IClassMetadata>(classMeta); Dictionary <string, ISet <string> > tmpEntityToCollectionRoleMap = new Dictionary <string, ISet <string> >(); collectionPersisters = new Dictionary <string, ICollectionPersister>(); foreach (Mapping.Collection model in cfg.CollectionMappings) { ICacheConcurrencyStrategy cache = CacheFactory.CreateCache(model.CacheConcurrencyStrategy, model.CacheRegionName, model.Owner.IsMutable, settings, properties); if (cache != null) { allCacheRegions[cache.RegionName] = cache.Cache; } ICollectionPersister persister = PersisterFactory.CreateCollectionPersister(cfg, model, cache, this); collectionPersisters[model.Role] = persister; IType indexType = persister.IndexType; if (indexType != null && indexType.IsAssociationType && !indexType.IsAnyType) { string entityName = ((IAssociationType)indexType).GetAssociatedEntityName(this); ISet <string> roles; if (!tmpEntityToCollectionRoleMap.TryGetValue(entityName, out roles)) { roles = new HashSet <string>(); tmpEntityToCollectionRoleMap[entityName] = roles; } roles.Add(persister.Role); } IType elementType = persister.ElementType; if (elementType.IsAssociationType && !elementType.IsAnyType) { string entityName = ((IAssociationType)elementType).GetAssociatedEntityName(this); ISet <string> roles; if (!tmpEntityToCollectionRoleMap.TryGetValue(entityName, out roles)) { roles = new HashSet <string>(); tmpEntityToCollectionRoleMap[entityName] = roles; } roles.Add(persister.Role); } } Dictionary <string, ICollectionMetadata> tmpcollectionMetadata = new Dictionary <string, ICollectionMetadata>(collectionPersisters.Count); foreach (KeyValuePair <string, ICollectionPersister> collectionPersister in collectionPersisters) { tmpcollectionMetadata.Add(collectionPersister.Key, collectionPersister.Value.CollectionMetadata); } collectionMetadata = new UnmodifiableDictionary <string, ICollectionMetadata>(tmpcollectionMetadata); collectionRolesByEntityParticipant = new UnmodifiableDictionary <string, ISet <string> >(tmpEntityToCollectionRoleMap); #endregion #region Named Queries namedQueries = new Dictionary <string, NamedQueryDefinition>(cfg.NamedQueries); namedSqlQueries = new Dictionary <string, NamedSQLQueryDefinition>(cfg.NamedSQLQueries); sqlResultSetMappings = new Dictionary <string, ResultSetMappingDefinition>(cfg.SqlResultSetMappings); #endregion imports = new Dictionary <string, string>(cfg.Imports); #region after *all* persisters and named queries are registered foreach (IEntityPersister persister in entityPersisters.Values) { persister.PostInstantiate(); } foreach (ICollectionPersister persister in collectionPersisters.Values) { persister.PostInstantiate(); } #endregion #region Serialization info name = settings.SessionFactoryName; try { uuid = (string)UuidGenerator.Generate(null, null); } catch (Exception) { throw new AssertionFailure("Could not generate UUID"); } SessionFactoryObjectFactory.AddInstance(uuid, name, this, properties); #endregion log.Debug("Instantiated session factory"); #region Schema management if (settings.IsAutoCreateSchema) { new SchemaExport(cfg).Create(false, true); } if (settings.IsAutoUpdateSchema) { new SchemaUpdate(cfg).Execute(false, true); } if (settings.IsAutoValidateSchema) { new SchemaValidator(cfg, settings).Validate(); } if (settings.IsAutoDropSchema) { schemaExport = new SchemaExport(cfg); } #endregion #region Obtaining TransactionManager // not ported yet #endregion currentSessionContext = BuildCurrentSessionContext(); if (settings.IsQueryCacheEnabled) { updateTimestampsCache = new UpdateTimestampsCache(settings, properties); queryCache = settings.QueryCacheFactory.GetQueryCache(null, updateTimestampsCache, settings, properties); queryCaches = new ThreadSafeDictionary <string, IQueryCache>(new Dictionary <string, IQueryCache>()); } else { updateTimestampsCache = null; queryCache = null; queryCaches = null; } #region Checking for named queries if (settings.IsNamedQueryStartupCheckingEnabled) { IDictionary <string, HibernateException> errors = CheckNamedQueries(); if (errors.Count > 0) { StringBuilder failingQueries = new StringBuilder("Errors in named queries: "); foreach (KeyValuePair <string, HibernateException> pair in errors) { failingQueries.Append('{').Append(pair.Key).Append('}'); log.Error("Error in named query: " + pair.Key, pair.Value); } throw new HibernateException(failingQueries.ToString()); } } #endregion Statistics.IsStatisticsEnabled = settings.IsStatisticsEnabled; // EntityNotFoundDelegate IEntityNotFoundDelegate enfd = cfg.EntityNotFoundDelegate; if (enfd == null) { enfd = new DefaultEntityNotFoundDelegate(); } entityNotFoundDelegate = enfd; }