/// <summary>Alter an existing table ensuring all columns exist.</summary>
        /// <param name="table">The table definition to write to the database.</param>
        private void AlterTable(DataTable table)
        {
            bool haveBegunTransaction = false;

            foreach (DataColumn column in table.Columns)
            {
                if (!columnNamesInDb.Contains(column.ColumnName))
                {
                    if (!haveBegunTransaction)
                    {
                        haveBegunTransaction = true;
                        connection.BeginTransaction();
                    }

                    // Column is missing from database file - write it.
                    connection.AddColumn(Name, column.ColumnName, connection.GetDBDataTypeName(column.DataType));
                    columnNamesInDb.Add(column.ColumnName);
                }
            }

            // End the transaction that we started above.
            if (haveBegunTransaction)
            {
                connection.EndTransaction();
            }
        }
        /// <summary>Called to run the command. Can throw on error.</summary>
        /// <param name="cancelToken">Is cancellation pending?</param>
        public void Run(CancellationTokenSource cancelToken)
        {
            if (dataToWrite.Rows.Count > 0)
            {
                if (!tables.TryGetValue(dataToWrite.TableName, out var table))
                {
                    table = new DatabaseTableDetails(connection, dataToWrite.TableName);
                    tables.Add(dataToWrite.TableName, table);
                }

                var query = new InsertQuery(dataToWrite);

                // Make sure the table has the correct columns.
                table.EnsureTableExistsAndHasRequiredColumns(dataToWrite);

                // Get a list of column names.
                var columnNames = dataToWrite.Columns.Cast <DataColumn>().Select(col => col.ColumnName);

                try
                {
                    connection.BeginTransaction();

                    // Write all rows.
                    foreach (DataRow row in dataToWrite.Rows)
                    {
                        query.ExecuteQuery(connection, columnNames, row.ItemArray);
                    }
                }
                finally
                {
                    connection.EndTransaction();
                    query.Close(connection);
                }
            }
        }
Beispiel #3
0
        /// <summary>Called to run the command. Can throw on error.</summary>
        /// <param name="cancelToken">Is cancellation pending?</param>
        public void Run(CancellationTokenSource cancelToken)
        {
            if (dataToWrite.Rows.Count > 0)
            {
                if (!tables.TryGetValue(dataToWrite.TableName, out var table))
                {
                    table = new DatabaseTableDetails(connection, dataToWrite.TableName);
                    tables.Add(dataToWrite.TableName, table);
                }

                var query = new InsertQuery(dataToWrite);

                // Make sure the table has the correct columns.
                table.EnsureTableExistsAndHasRequiredColumns(dataToWrite);

                // Get a list of column names.
                var columnNames = dataToWrite.Columns.Cast <DataColumn>().Select(col => col.ColumnName);

                try
                {
                    connection.BeginTransaction();

                    if (deleteExistingRows)
                    {
                        // fixme - this assumes that "Current" checkpoint ID is always 1.
                        // This should always be correct afaik, but it would be better to
                        // verify this at runtime.
                        bool tableHasCheckpointID = connection.GetColumns(dataToWrite.TableName).Any(c => c.Item1 == "CheckpointID");
                        connection.ExecuteNonQuery($"DELETE FROM [{dataToWrite.TableName}] {(tableHasCheckpointID ? "WHERE CheckpointID = 1" : "")}");
                    }
                    // Write all rows.
                    foreach (DataRow row in dataToWrite.Rows)
                    {
                        query.ExecuteQuery(connection, columnNames, row.ItemArray);
                    }
                }
                finally
                {
                    connection.EndTransaction();
                    query.Close(connection);
                }
            }
        }