<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using log4net;
using NHibernate.AdoNet;
using NHibernate.Cache;
using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Exceptions;
using NHibernate.Hql;
using NHibernate.Transaction;
using NHibernate.Util;

namespace NHibernate.Cfg
{
	/// <summary>
	/// Reads configuration properties and configures a <see cref="Settings"/> instance. 
	/// </summary>
	public sealed class SettingsFactory
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(SettingsFactory));
		private static readonly string DefaultCacheProvider = typeof(NoCacheProvider).AssemblyQualifiedName;

		public Settings BuildSettings(IDictionary<string, string> properties)
		{
			Settings settings = new Settings();

			Dialect.Dialect dialect;
			try
			{
				dialect = Dialect.Dialect.GetDialect(properties);
				Dictionary<string, string> temp = new Dictionary<string, string>();

				foreach (KeyValuePair<string, string> de in dialect.DefaultProperties)
				{
					temp[de.Key] = de.Value;
				}
				foreach (KeyValuePair<string, string> de in properties)
				{
					temp[de.Key] = de.Value;
				}
				properties = temp;
			}
			catch (HibernateException he)
			{
				log.Warn("No dialect set - using GenericDialect: " + he.Message);
				dialect = new GenericDialect();
			}
			settings.Dialect = dialect;

			#region SQL Exception converter

			ISQLExceptionConverter sqlExceptionConverter;
			try
			{
				sqlExceptionConverter = SQLExceptionConverterFactory.BuildSQLExceptionConverter(dialect, properties);
			}
			catch (HibernateException)
			{
				log.Warn("Error building SQLExceptionConverter; using minimal converter");
				sqlExceptionConverter = SQLExceptionConverterFactory.BuildMinimalSQLExceptionConverter();
			}
			settings.SqlExceptionConverter = sqlExceptionConverter;

			#endregion

			bool comments = PropertiesHelper.GetBoolean(Environment.UseSqlComments, properties);
			log.Info("Generate SQL with comments: " + EnabledDisabled(comments));
			settings.IsCommentsEnabled = comments;

			int maxFetchDepth = PropertiesHelper.GetInt32(Environment.MaxFetchDepth, properties, -1);
			if (maxFetchDepth != -1)
			{
				log.Info("Maximum outer join fetch depth: " + maxFetchDepth);
			}

			IConnectionProvider connectionProvider = ConnectionProviderFactory.NewConnectionProvider(properties);
			ITransactionFactory transactionFactory = CreateTransactionFactory(properties);
			// TransactionManagerLookup transactionManagerLookup = TransactionManagerLookupFactory.GetTransactionManagerLookup( properties );

			// Not ported: useGetGeneratedKeys, useScrollableResultSets

			bool useMinimalPuts = PropertiesHelper.GetBoolean(Environment.UseMinimalPuts, properties, false);
			log.Info("Optimize cache for minimal puts: " + useMinimalPuts);

			string releaseModeName = PropertiesHelper.GetString(Environment.ReleaseConnections, properties, "auto");
			log.Info("Connection release mode: " + releaseModeName);
			ConnectionReleaseMode releaseMode;
			if ("auto".Equals(releaseModeName))
			{
				releaseMode = ConnectionReleaseMode.AfterTransaction; //transactionFactory.DefaultReleaseMode;
			}
			else
			{
				releaseMode = ParseConnectionReleaseMode(releaseModeName);
			}
			settings.ConnectionReleaseMode = releaseMode;

			string defaultSchema = PropertiesHelper.GetString(Environment.DefaultSchema, properties, null);
			string defaultCatalog = PropertiesHelper.GetString(Environment.DefaultCatalog, properties, null);
			if (defaultSchema != null)
				log.Info("Default schema: " + defaultSchema);
			if (defaultCatalog != null)
				log.Info("Default catalog: " + defaultCatalog);
			settings.DefaultSchemaName = defaultSchema;
			settings.DefaultCatalogName = defaultCatalog;

			int batchFetchSize = PropertiesHelper.GetInt32(Environment.DefaultBatchFetchSize, properties, 1);
			log.Info("Default batch fetch size: " + batchFetchSize);
			settings.DefaultBatchFetchSize = batchFetchSize;

			//Statistics and logging:

			bool showSql = PropertiesHelper.GetBoolean(Environment.ShowSql, properties, false);
			if (showSql)
			{
				log.Info("echoing all SQL to stdout");
			}

			bool useStatistics = PropertiesHelper.GetBoolean(Environment.GenerateStatistics, properties);
			log.Info("Statistics: " + EnabledDisabled(useStatistics));
			settings.IsStatisticsEnabled = useStatistics;

			bool useIdentifierRollback = PropertiesHelper.GetBoolean(Environment.UseIdentifierRollBack, properties);
			log.Info("Deleted entity synthetic identifier rollback: " + EnabledDisabled(useIdentifierRollback));
			settings.IsIdentifierRollbackEnabled = useIdentifierRollback;

			// queries:

			settings.QueryTranslatorFactory = CreateQueryTranslatorFactory(properties);

			IDictionary<string, string> querySubstitutions = PropertiesHelper.ToDictionary(Environment.QuerySubstitutions,
			                                                                               " ,=;:\n\t\r\f", properties);
			if (log.IsInfoEnabled)
			{
				log.Info("Query language substitutions: " + CollectionPrinter.ToString((IDictionary) querySubstitutions));
			}

			string autoSchemaExport = PropertiesHelper.GetString(Environment.Hbm2ddlAuto, properties, null);
			if ("update" == autoSchemaExport)
			{
				settings.IsAutoUpdateSchema = true;
			}
			if ("create" == autoSchemaExport)
			{
				settings.IsAutoCreateSchema = true;
			}
			if ("create-drop" == autoSchemaExport)
			{
				settings.IsAutoCreateSchema = true;
				settings.IsAutoDropSchema = true;
			}

			bool useSecondLevelCache = PropertiesHelper.GetBoolean(Environment.UseSecondLevelCache, properties, true);
			bool useQueryCache = PropertiesHelper.GetBoolean(Environment.UseQueryCache, properties);

			if (useSecondLevelCache || useQueryCache)
			{
				// The cache provider is needed when we either have second-level cache enabled
				// or query cache enabled.  Note that useSecondLevelCache is enabled by default
				settings.CacheProvider = CreateCacheProvider(properties);
			}
			else
			{
				settings.CacheProvider = new NoCacheProvider();
			}
<commit_msg>Minors.<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using log4net;
using NHibernate.AdoNet;
using NHibernate.Cache;
using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Exceptions;
using NHibernate.Hql;
using NHibernate.Transaction;
using NHibernate.Util;

namespace NHibernate.Cfg
{
	/// <summary>
	/// Reads configuration properties and configures a <see cref="Settings"/> instance. 
	/// </summary>
	public sealed class SettingsFactory
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(SettingsFactory));
		private static readonly string DefaultCacheProvider = typeof(NoCacheProvider).AssemblyQualifiedName;

		public Settings BuildSettings(IDictionary<string, string> properties)
		{
			Settings settings = new Settings();

			Dialect.Dialect dialect;
			try
			{
				dialect = Dialect.Dialect.GetDialect(properties);
				Dictionary<string, string> temp = new Dictionary<string, string>();

				foreach (KeyValuePair<string, string> de in dialect.DefaultProperties)
				{
					temp[de.Key] = de.Value;
				}
				foreach (KeyValuePair<string, string> de in properties)
				{
					temp[de.Key] = de.Value;
				}
				properties = temp;
			}
			catch (HibernateException he)
			{
				log.Warn("No dialect set - using GenericDialect: " + he.Message);
				dialect = new GenericDialect();
			}
			settings.Dialect = dialect;

			#region SQL Exception converter

			ISQLExceptionConverter sqlExceptionConverter;
			try
			{
				sqlExceptionConverter = SQLExceptionConverterFactory.BuildSQLExceptionConverter(dialect, properties);
			}
			catch (HibernateException)
			{
				log.Warn("Error building SQLExceptionConverter; using minimal converter");
				sqlExceptionConverter = SQLExceptionConverterFactory.BuildMinimalSQLExceptionConverter();
			}
			settings.SqlExceptionConverter = sqlExceptionConverter;

			#endregion

			bool comments = PropertiesHelper.GetBoolean(Environment.UseSqlComments, properties);
			log.Info("Generate SQL with comments: " + EnabledDisabled(comments));
			settings.IsCommentsEnabled = comments;

			int maxFetchDepth = PropertiesHelper.GetInt32(Environment.MaxFetchDepth, properties, -1);
			if (maxFetchDepth != -1)
			{
				log.Info("Maximum outer join fetch depth: " + maxFetchDepth);
			}

			IConnectionProvider connectionProvider = ConnectionProviderFactory.NewConnectionProvider(properties);
			ITransactionFactory transactionFactory = CreateTransactionFactory(properties);
			// TransactionManagerLookup transactionManagerLookup = TransactionManagerLookupFactory.GetTransactionManagerLookup( properties );

			// Not ported: useGetGeneratedKeys, useScrollableResultSets

			bool useMinimalPuts = PropertiesHelper.GetBoolean(Environment.UseMinimalPuts, properties, false);
			log.Info("Optimize cache for minimal puts: " + useMinimalPuts);

			string releaseModeName = PropertiesHelper.GetString(Environment.ReleaseConnections, properties, "auto");
			log.Info("Connection release mode: " + releaseModeName);
			ConnectionReleaseMode releaseMode;
			if ("auto".Equals(releaseModeName))
			{
				releaseMode = ConnectionReleaseMode.AfterTransaction; //transactionFactory.DefaultReleaseMode;
			}
			else
			{
				releaseMode = ParseConnectionReleaseMode(releaseModeName);
			}
			settings.ConnectionReleaseMode = releaseMode;

			string defaultSchema = PropertiesHelper.GetString(Environment.DefaultSchema, properties, null);
			string defaultCatalog = PropertiesHelper.GetString(Environment.DefaultCatalog, properties, null);
			if (defaultSchema != null)
				log.Info("Default schema: " + defaultSchema);
			if (defaultCatalog != null)
				log.Info("Default catalog: " + defaultCatalog);
			settings.DefaultSchemaName = defaultSchema;
			settings.DefaultCatalogName = defaultCatalog;

			int batchFetchSize = PropertiesHelper.GetInt32(Environment.DefaultBatchFetchSize, properties, 1);
			log.Info("Default batch fetch size: " + batchFetchSize);
			settings.DefaultBatchFetchSize = batchFetchSize;

			//Statistics and logging:

			bool showSql = PropertiesHelper.GetBoolean(Environment.ShowSql, properties, false);
			if (showSql)
			{
				log.Info("echoing all SQL to stdout");
			}

			bool useStatistics = PropertiesHelper.GetBoolean(Environment.GenerateStatistics, properties);
			log.Info("Statistics: " + EnabledDisabled(useStatistics));
			settings.IsStatisticsEnabled = useStatistics;

			bool useIdentifierRollback = PropertiesHelper.GetBoolean(Environment.UseIdentifierRollBack, properties);
			log.Info("Deleted entity synthetic identifier rollback: " + EnabledDisabled(useIdentifierRollback));
			settings.IsIdentifierRollbackEnabled = useIdentifierRollback;

			// queries:

			settings.QueryTranslatorFactory = CreateQueryTranslatorFactory(properties);

			IDictionary<string, string> querySubstitutions = PropertiesHelper.ToDictionary(Environment.QuerySubstitutions,
			                                                                               " ,=;:\n\t\r\f", properties);
			if (log.IsInfoEnabled)
			{
				log.Info("Query language substitutions: " + CollectionPrinter.ToString((IDictionary) querySubstitutions));
			}

			string autoSchemaExport = PropertiesHelper.GetString(Environment.Hbm2ddlAuto, properties, null);
			if ("update" == autoSchemaExport)
			{
				settings.IsAutoUpdateSchema = true;
			}
			if ("create" == autoSchemaExport)
			{
				settings.IsAutoCreateSchema = true;
			}
			if ("create-drop" == autoSchemaExport)
			{
				settings.IsAutoCreateSchema = true;
				settings.IsAutoDropSchema = true;
			}
			if ("validate" == autoSchemaExport)
			{
				settings.IsAutoValidateSchema = true;
			}
			bool useSecondLevelCache = PropertiesHelper.GetBoolean(Environment.UseSecondLevelCache, properties, true);
			bool useQueryCache = PropertiesHelper.GetBoolean(Environment.UseQueryCache, properties);

			if (useSecondLevelCache || useQueryCache)
			{
				// The cache provider is needed when we either have second-level cache enabled
				// or query cache enabled.  Note that useSecondLevelCache is enabled by default
				settings.CacheProvider = CreateCacheProvider(properties);
			}
			else
			{
				settings.CacheProvider = new NoCacheProvider();
			}

{'fraction_non_alphanumeric': 0.09067399385808954, 'fraction_numerical': 0.0014546630030709553, 'mean_word_length': 4.534436493738819, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7134349', 'n_tokens_mistral': 3838, 'n_tokens_neox': 3184, 'n_words': 711}