Beispiel #1
0
        /// <summary>
        /// Gets the cached field definitions of the entity.
        /// </summary>
        /// <typeparam name="TDbConnection">The type of <see cref="IDbConnection"/> object.</typeparam>
        /// <param name="connection">The instance of the <see cref="IDbConnection"/> object.</param>
        /// <param name="tableName">The name of the target table.</param>
        /// <param name="transaction">The transaction object that is currently in used.</param>
        /// <returns>The cached field definitions of the entity.</returns>
        internal static IEnumerable <DbField> GetInternal <TDbConnection>(TDbConnection connection,
                                                                          string tableName,
                                                                          IDbTransaction transaction)
            where TDbConnection : IDbConnection
        {
            var type   = connection.GetType();
            var key    = (long)type.FullName.GetHashCode();
            var result = (IEnumerable <DbField>)null;

            // Note: For SqlConnection, the ConnectionString is changing if the (Integrated Security=False). Actually for this isolation, the database name is enough.
            if (!string.IsNullOrEmpty(connection?.Database))
            {
                key += connection.Database.GetHashCode();
            }

            // Add the hashcode of the table name
            if (string.IsNullOrEmpty(tableName) == false)
            {
                key += tableName.GetHashCode();
            }

            // Try get the value
            if (m_cache.TryGetValue(key, out result) == false)
            {
                var dbHelper = DbHelperMapper.Get(type);
                result = dbHelper?.GetFields(connection, tableName, transaction);
                m_cache.TryAdd(key, result);
            }

            // Return the value
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes all necessary settings for SqLite.
        /// </summary>
        public static void Initialize()
        {
            // Skip if already initialized
            if (IsInitialized == true)
            {
                return;
            }

            #region SDS

            // Map the DbSetting
            var sdsDbSetting = new SqLiteDbSetting(true);
            DbSettingMapper.Add <SQLiteConnection>(sdsDbSetting, true);

            // Map the DbHelper
            DbHelperMapper.Add <SQLiteConnection>(new SqLiteDbHelper(sdsDbSetting, new SdsSqLiteDbTypeNameToClientTypeResolver()), true);

            // Map the Statement Builder
            StatementBuilderMapper.Add <SQLiteConnection>(new SqLiteStatementBuilder(sdsDbSetting,
                                                                                     new SqLiteConvertFieldResolver(),
                                                                                     new ClientTypeToAverageableClientTypeResolver()), true);

            #endregion

            // Set the flag
            IsInitialized = true;
        }
Beispiel #3
0
        /// <summary>
        /// Gets the cached field definitions of the entity.
        /// </summary>
        /// <param name="type">The type of <see cref="IDbConnection"/> object.</param>
        /// <param name="connectionString">The connection string to be used.</param>
        /// <param name="tableName">The name of the target table.</param>
        /// <returns>The cached field definitions of the entity.</returns>
        internal static IEnumerable <DbField> Get(Type type, string connectionString, string tableName)
        {
            var key    = (long)type?.FullName.GetHashCode();
            var result = (IEnumerable <DbField>)null;

            // Set the keys
            if (string.IsNullOrEmpty(connectionString) == false)
            {
                key ^= connectionString.GetHashCode();
            }
            if (string.IsNullOrEmpty(tableName) == false)
            {
                key ^= tableName.GetHashCode();
            }

            // Try get the value
            if (m_cache.TryGetValue(key, out result) == false)
            {
                var dbHelper = DbHelperMapper.Get(type);
                result = dbHelper?.GetFields(connectionString, tableName);
                m_cache.TryAdd(key, result);
            }

            // Return the value
            return(result);
        }
        /// <summary>
        /// Initializes all the necessary settings for SQL Server.
        /// </summary>
        public static void Initialize()
        {
            // Skip if already initialized
            if (IsInitialized == true)
            {
                return;
            }

            // Map the DbSetting
            var dbSetting = new SqlServerDbSetting();

            DbSettingMapper.Add <SqlConnection>(dbSetting, true);

            // Map the DbHelper
            var dbHelper = new SqlServerDbHelper();

            DbHelperMapper.Add <SqlConnection>(dbHelper, true);

            // Map the Statement Builder
            var statementBuilder = new SqlServerStatementBuilder(dbSetting);

            StatementBuilderMapper.Add <SqlConnection>(statementBuilder, true);

            // Set the flag
            IsInitialized = true;
        }
        /// <summary>
        /// Initializes all necessary settings for SqlServer.
        /// </summary>
        public static void Initialize()
        {
            // Skip if already initialized
            if (IsInitialized == true)
            {
                return;
            }

            // Map the DbSetting
            var dbSetting = new SqlServerDbSetting();

            DbSettingMapper.Add(typeof(Microsoft.Data.SqlClient.SqlConnection), dbSetting, true);
            DbSettingMapper.Add(typeof(System.Data.SqlClient.SqlConnection), dbSetting, true);

            // Map the DbHelper
            var dbHelper = new SqlServerDbHelper();

            DbHelperMapper.Add(typeof(Microsoft.Data.SqlClient.SqlConnection), dbHelper, true);
            DbHelperMapper.Add(typeof(System.Data.SqlClient.SqlConnection), dbHelper, true);

            // Map the Statement Builder
            var statementBuilder = new SqlServerStatementBuilder(dbSetting);

            StatementBuilderMapper.Add(typeof(Microsoft.Data.SqlClient.SqlConnection), statementBuilder, true);
            StatementBuilderMapper.Add(typeof(System.Data.SqlClient.SqlConnection), statementBuilder, true);

            // Set the flag
            IsInitialized = true;
        }
Beispiel #6
0
        /// <summary>
        /// Gets the cached field definitions of the entity in an asychronous way.
        /// </summary>
        /// <typeparam name="TDbConnection">The type of <see cref="IDbConnection"/> object.</typeparam>
        /// <param name="connection">The instance of the <see cref="IDbConnection"/> object.</param>
        /// <param name="tableName">The name of the target table.</param>
        /// <param name="transaction">The transaction object that is currently in used.</param>
        /// <param name="enableValidation">Enables the validation after retrieving the database fields.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
        /// <returns>The cached field definitions of the entity.</returns>
        internal static async Task <IEnumerable <DbField> > GetAsyncInternal <TDbConnection>(TDbConnection connection,
                                                                                             string tableName,
                                                                                             IDbTransaction transaction,
                                                                                             bool enableValidation,
                                                                                             CancellationToken cancellationToken = default)
            where TDbConnection : IDbConnection
        {
            var type   = connection.GetType();
            var key    = (long)type.GetHashCode();
            var result = (IEnumerable <DbField>)null;

            // Note: For SqlConnection, the ConnectionString is changing if the (Integrated Security=False). Actually for this isolation, the database name is enough.
            if (!string.IsNullOrWhiteSpace(connection?.Database))
            {
                key += connection.Database.GetHashCode();
            }

            // Add the hashcode of the table name
            if (string.IsNullOrWhiteSpace(tableName) == false)
            {
                key += tableName.GetHashCode();
            }

            // Try get the value
            if (cache.TryGetValue(key, out result) == false)
            {
                // Get from DB
                var dbHelper = DbHelperMapper.Get(type);
                result = await dbHelper?.GetFieldsAsync(connection, tableName, transaction, cancellationToken);

                // Validate
                if (enableValidation)
                {
                    ValidateDbFields(tableName, result);
                }

                // Add to cache
                cache.TryAdd(key, result);
            }

            // Return the value
            return(result);
        }
Beispiel #7
0
        /// <summary>
        /// Initializes all necessary settings for MySql.
        /// </summary>
        public static void Initialize()
        {
            // Skip if already initialized
            if (IsInitialized == true)
            {
                return;
            }

            // Map the DbSetting
            DbSettingMapper.Add <MySqlConnection>(new MySqlDbSetting(), true);

            // Map the DbHelper
            DbHelperMapper.Add <MySqlConnection>(new MySqlDbHelper(), true);

            // Map the Statement Builder
            StatementBuilderMapper.Add <MySqlConnection>(new MySqlStatementBuilder(), true);

            // Set the flag
            IsInitialized = true;
        }
Beispiel #8
0
        /// <summary>
        /// Initializes all necessary settings for SqLite.
        /// </summary>
        public static void Initialize()
        {
            // Skip if already initialized
            if (IsInitialized == true)
            {
                return;
            }

            // Map the DbSetting
            DbSettingMapper.Add(typeof(SqliteConnection), new SqLiteDbSetting(), true);

            // Map the DbHelper
            DbHelperMapper.Add(typeof(SqliteConnection), new SqLiteDbHelper(), true);

            // Map the Statement Builder
            StatementBuilderMapper.Add(typeof(SqliteConnection), new SqLiteStatementBuilder(), true);

            // Set the flag
            IsInitialized = true;
        }
Beispiel #9
0
        /// <summary>
        /// Initializes all necessary settings for SqLite.
        /// </summary>
        public static void Initialize()
        {
            // Skip if already initialized
            if (IsInitialized == true)
            {
                return;
            }

            #region SDS

            // Map the DbSetting
            DbSettingMapper.Add(typeof(SQLiteConnection), new SqLiteDbSetting(true), true);

            // Map the DbHelper
            DbHelperMapper.Add(typeof(SQLiteConnection), new SqLiteDbHelper(new SdsSqLiteDbTypeNameToClientTypeResolver()), true);

            // Map the Statement Builder
            StatementBuilderMapper.Add(typeof(SQLiteConnection), new SqLiteStatementBuilder(DbSettingMapper.Get(typeof(SQLiteConnection)),
                                                                                            new SqLiteConvertFieldResolver(),
                                                                                            new ClientTypeToAverageableClientTypeResolver()), true);

            #endregion

            #region MDS

            // Map the DbSetting
            DbSettingMapper.Add(typeof(SqliteConnection), new SqLiteDbSetting(false), true);

            // Map the DbHelper
            DbHelperMapper.Add(typeof(SqliteConnection), new SqLiteDbHelper(new MdsSqLiteDbTypeNameToClientTypeResolver()), true);

            // Map the Statement Builder
            StatementBuilderMapper.Add(typeof(SqliteConnection), new SqLiteStatementBuilder(DbSettingMapper.Get(typeof(SqliteConnection)),
                                                                                            new SqLiteConvertFieldResolver(),
                                                                                            new ClientTypeToAverageableClientTypeResolver()), true);

            #endregion

            // Set the flag
            IsInitialized = true;
        }