Ejemplo n.º 1
0
        internal static bool TruncateTable(MySqlConnection ExistingConnection, string TableName = null, Type ForceType = null, MySqlTransaction SqlTransaction = null)
        {
            var tableName = TableName ?? ForceType.GetCustomAttribute <DALTable>()?.TableName ?? throw new CustomAttributeFormatException(DatabaseCoreUtilities.NoDalTableAttributeError);

            var truncateQuery = $"TRUNCATE {tableName};";

            var rowsUpdated = DatabaseWorkHelper.DoDatabaseWork <int>(ExistingConnection, truncateQuery, UseTransaction: true, SqlTransaction: SqlTransaction);

            var success = rowsUpdated > 0;

            return(success);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Will bulk write data to the database.
        /// </summary>
        /// <param name="DataTableFunction">A function that takes in an object and a column name, and resolves those two pieces of information into a piece of data.</param>
        /// <returns>The number of rows affected by this bulk write.</returns>
        public int Write(Func <string, T, object> DataTableFunction)
        {
            var sourceDataCount = SourceData?.Count() ?? 0;

            if (sourceDataCount == 0)
            {
                return(0);
            }

            PopulateColumnDetails();

            var outputIterations = sourceDataCount <= BatchSize
                ? 1
                : sourceDataCount / BatchSize;

            if (outputIterations * BatchSize < sourceDataCount)
            {
                outputIterations++;
            }

            var recordsInserted = 0;

            for (var i = 0; i < outputIterations; i++)
            {
                CreateOutputDataTable(DataTableFunction, i);

                if (ExistingConnection != null)
                {
                    recordsInserted += DatabaseWorkHelper.DoDatabaseWork <int>(ExistingConnection, InsertQuery, CommonDatabaseWork, UseTransaction: WriteWithTransaction, ThrowException: ShouldThrowException, SqlTransaction: SqlTransaction);
                }
                else
                {
                    recordsInserted += DatabaseWorkHelper.DoDatabaseWork <int>(ConfigConnectionString, InsertQuery, CommonDatabaseWork, UseTransaction: WriteWithTransaction, ThrowException: ShouldThrowException, AllowUserVariables: ShouldAllowUserVariables);
                }
            }

            return(recordsInserted);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Execute a query on the database using the provided function, returning value of of the specified type.
 /// </summary>
 /// <typeparam name="T">The data return type.</typeparam>
 /// <param name="EstablishedConnection">An open and established connection to a MySQL database</param>
 /// <param name="QueryString">SQL query to retrieve the data requested</param>
 /// <param name="ActionCallback">Customized function to execute when connected to the database</param>
 /// <param name="ThrowException">Throw exception or swallow and return default(T)</param>
 /// <param name="UseTransaction">Specify whether to use a transaction for this call</param>
 /// <returns>Data of any of the specified type.</returns>
 public static T DoDatabaseWork <T>(MySqlConnection EstablishedConnection, string QueryString, Func <MySqlCommand, object> ActionCallback, bool ThrowException = true, bool UseTransaction = false, MySqlTransaction SqlTransaction = null)
 => DatabaseWorkHelper.DoDatabaseWork <T>(EstablishedConnection, QueryString, ActionCallback, ThrowException, UseTransaction, SqlTransaction);
Ejemplo n.º 4
0
 /// <summary>
 /// Execute a query on the database using the provided function, returning value of of the specified type.
 /// </summary>
 /// <typeparam name="T">The data return type.</typeparam>
 /// <param name="ConfigConnectionString">A ConnectionStringTypes type to reference a connection string defined in web.config</param>
 /// <param name="QueryString">The full SQL query string to be used when executing the command.</param>
 /// <param name="ActionCallback">Custom function to execute when connected to the database.</param>
 /// <param name="ThrowException">Throw exception or cache in LastExecutionException and continue.</param>
 /// <param name="UseTransaction">Indicate whether to write all the data in a single transaction.</param>
 /// <param name="SqlTransaction">Supply an existing transaction for use in this operation.</param>
 /// <param name="AllowUserVariables">Will allow special user variables (variables start with "@") to be defined in the query that will be eventually executed.</param>
 /// <returns>Data of any of the specified type.</returns>
 public static T DoDatabaseWork <T>(Enum ConfigConnectionString, string QueryString, Func <MySqlCommand, object> ActionCallback, bool ThrowException = true, bool UseTransaction = false, MySqlTransaction SqlTransaction = null, bool AllowUserVariables = false)
 => DatabaseWorkHelper.DoDatabaseWork <T>(ConfigConnectionString, QueryString, ActionCallback, ThrowException, UseTransaction, SqlTransaction, AllowUserVariables);
Ejemplo n.º 5
0
 /// <summary>
 /// Execute a query on the database and return the number of rows affected.
 /// </summary>
 /// <typeparam name="T">Return type - only accepts String or Int</typeparam>
 /// <param name="EstablishedConnection">An open and established connection to a MySQL database.</param>
 /// <param name="QueryString">The full SQL query string to be used when executing the command.</param>
 /// <param name="Parameters">Dictionary of named parameters</param>
 /// <param name="ThrowException">Throw exception or cache in LastExecutionException and continue.</param>
 /// <param name="UseTransaction">Indicate whether to write all the data in a single transaction.</param>
 /// <param name="SqlTransaction">Supply an existing transaction for use in this operation.</param>
 /// <returns>Number of rows affected.</returns>
 public static T DoDatabaseWork <T>(MySqlConnection EstablishedConnection, string QueryString, Dictionary <string, object> Parameters = null, bool ThrowException = true, bool UseTransaction = false, MySqlTransaction SqlTransaction = null)
 => DatabaseWorkHelper.DoDatabaseWork <T>(EstablishedConnection, QueryString, Parameters, ThrowException, UseTransaction, SqlTransaction);
Ejemplo n.º 6
0
 /// <summary>
 /// Execute a query on the database and return the number of rows affected.
 /// </summary>
 /// <typeparam name="T">Return type - only accepts String or Int</typeparam>
 /// <param name="ConfigConnectionString">A ConnectionStringTypes type to reference a connection string defined in web.config</param>
 /// <param name="QueryString">The full SQL query string to be used when executing the command.</param>
 /// <param name="Parameters">Dictionary of named parameters</param>
 /// <param name="ThrowException">Throw exception or cache in LastExecutionException and continue.</param>
 /// <param name="UseTransaction">Indicate whether to write all the data in a single transaction.</param>
 /// <returns>Number of rows affected.</returns>
 public static T DoDatabaseWork <T>(Enum ConfigConnectionString, string QueryString, Dictionary <string, object> Parameters = null, bool ThrowException = true, bool UseTransaction = false, MySqlTransaction SqlTransaction = null, bool AllowUserVariables = false)
 => DatabaseWorkHelper.DoDatabaseWork <T>(ConfigConnectionString, QueryString, Parameters, ThrowException, UseTransaction, SqlTransaction, AllowUserVariables);