internal void Flush()
        {
            _currentAddFieldStatment = null;
            _currentTableInfo = null;
            _currentTableStatment = null;

            string commandText = string.Join(";", _tableStatments.Select(x => ParseStatement(x)).Where(x => !string.IsNullOrWhiteSpace(x)));

            if (string.IsNullOrWhiteSpace(commandText))
                return;

            using (DbCommand command = _getCommandFunc())
            {
                DbConnection connection = _databaseProvider.GetConnection(_connectionString);
                try
                {
                    if (connection.State != System.Data.ConnectionState.Open)
                        connection.Open();

                    command.Connection = connection;
                    command.CommandText = commandText;
                    command.ExecuteNonQuery();
                }
                finally
                {
                    _databaseProvider.ReleaseConnection(connection);
                }
            }
        }
Esempio n. 2
0
        private void ThreadFunction()
        {
            DbConnection connection = _dataBaseProvider.GetConnection(_connectionStringName);

            try
            {
                if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                while (true)
                {
                    List <QueueItem> queue;
                    lock (this)
                    {
                        if (_queue.Count == 0)
                        {
                            return;
                        }

                        queue  = _queue;
                        _queue = new List <QueueItem>();
                    }

                    try
                    {
                        List <ICommitContext> commitContexts = new List <ICommitContext>(queue.Count);

                        using (DbCommand command = _dataBaseProvider.CombineCommands(queue.Select(x => x.Command)))
                        {
                            command.Connection = connection;
                            using (IValueSource valueSource = _dataBaseProvider.GetValueSource(command))
                            {
                                for (int i = 0; i < queue.Count; i++)
                                {
                                    commitContexts.Add(queue[i].FillAction(valueSource, queue[i].Context));
                                }
                            }
                        }

                        foreach (ICommitContext context in commitContexts.Where(x => x != null))
                        {
                            context.Commit();
                        }
                    }
                    finally
                    {
                        foreach (TaskCompletionSource <bool> result in queue.Select(x => x.TaskCompletionSource))
                        {
                            result.SetResult(true);
                        }
                    }

                    lock (this)
                    {
                        if (_queue.Count == 0)
                        {
                            return;
                        }
                    }
                }
            }
            finally
            {
                _workerThread = null;
                _dataBaseProvider.ReleaseConnection(connection);
            }
        }