示例#1
0
        public IAsyncResult BeginExecuteScalar(AsyncCallback callback)
        {
            var asyncContext = new AsyncContext
            {
                InitiallyClosed = _connection.State == System.Data.ConnectionState.Closed,
                StartTime       = PerformanceTimer.TimeNow
            };

            try
            {
                if (asyncContext.InitiallyClosed)
                {
                    _connection.Open();
                }
                asyncContext.Result = _command.ExecuteScalar();
                _repository.RecordSuccess(this, PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime));
                if (asyncContext.InitiallyClosed)
                {
                    _connection.Close();
                }
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                _errorReporter.ReportError(ex, "Failed to ExecuteScalar on PostgrSql " + _repository.Name, _repository, this);
                throw;
            }
            return(new SyncronousResult(asyncContext, callback));
        }
示例#2
0
        public T ExecuteScalar <T>()
        {
            var initiallyClosed = _connection.State == ConnectionState.Closed;
            var startTime       = PerformanceTimer.TimeNow;

            try
            {
                if (initiallyClosed)
                {
                    OpenConnection();
                }
                var result = _sqlCommand.ExecuteScalar();

                var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - startTime);
                _repository.RecordSuccess(this, elapsedSeconds);
                AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                {
                    Connection     = this,
                    Command        = _command,
                    ElapsedSeconds = elapsedSeconds
                });

                if (result == null)
                {
                    return(default(T));
                }
                var resultType = typeof(T);
                if (resultType.IsNullable())
                {
                    resultType = resultType.GetGenericArguments()[0];
                }
                return((T)Convert.ChangeType(result, resultType));
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, _sqlCommand, "Failed to ExecuteScalar on SQL Server " + _repository.Name, _repository, this);
                throw;
            }
            finally
            {
                if (initiallyClosed && _connection.State == System.Data.ConnectionState.Open)
                {
                    _connection.Close();
                }
            }
        }
示例#3
0
        public long EndExecuteNonQuery(IAsyncResult asyncResult)
        {
            Trace("End MySQL execute non query");

            var asyncContext = (AsyncContext)asyncResult.AsyncState;

            try
            {
                if (asyncContext.Result != null)
                {
                    return((long)asyncContext.Result);
                }

                var rowsAffected = _mySqlCommand.EndExecuteNonQuery(asyncResult);

                var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime);
                _repository.RecordSuccess(this, elapsedSeconds);
                AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                {
                    Connection     = this,
                    Command        = _command,
                    ElapsedSeconds = elapsedSeconds
                });

                foreach (var parameter in _command.GetParameters())
                {
                    parameter.StoreOutputValue(parameter);
                }

                return(rowsAffected);
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, "Failed to ExecuteNonQuery on MySQL " + _repository.Name, _repository, this);
                throw;
            }
            finally
            {
                if (asyncContext.InitiallyClosed)
                {
                    CloseConnection();
                }
            }
        }
示例#4
0
        public IAsyncResult BeginExecuteReader(AsyncCallback callback)
        {
            var asyncContext = new AsyncContext
            {
                InitiallyClosed = _connection.State == System.Data.ConnectionState.Closed,
                StartTime       = PerformanceTimer.TimeNow
            };

            try
            {
                if (asyncContext.InitiallyClosed)
                {
                    _connection.Open();
                }
                var reader        = _command.ExecuteReader();
                var dataShapeName = _connection.DataSource + ":" + _connection.Database + ":" + _command.CommandType + ":" + _command.CommandText;
                asyncContext.Result = new DataReader(_errorReporter).Initialize(
                    reader,
                    dataShapeName,
                    () =>
                {
                    reader.Dispose();
                    _repository.RecordSuccess(this, PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime));
                    if (asyncContext.InitiallyClosed)
                    {
                        _connection.Close();
                    }
                },
                    () =>
                {
                    _repository.RecordFailure(this);
                    if (asyncContext.InitiallyClosed && _connection.State == System.Data.ConnectionState.Open)
                    {
                        _connection.Close();
                    }
                }
                    );
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                _errorReporter.ReportError(ex, "Failed to ExecuteReader on PostgrSql " + _repository.Name, _repository, this);
                if (asyncContext.InitiallyClosed && _connection.State == System.Data.ConnectionState.Open)
                {
                    _connection.Close();
                }
                throw;
            }
            return(new SyncronousResult(asyncContext, callback));
        }
示例#5
0
        public IDataReader EndExecuteReader(IAsyncResult asyncResult)
        {
            Trace("SQlite end execute reader");
            var asyncContext   = (AsyncContext)asyncResult.AsyncState;
            var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime);

            _repository.RecordSuccess(this, elapsedSeconds);
            AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
            {
                Connection     = this,
                Command        = _command,
                ElapsedSeconds = elapsedSeconds
            });
            return((IDataReader)asyncContext.Result);
        }
示例#6
0
        public IAsyncResult BeginExecuteScalar(AsyncCallback callback)
        {
            Trace("Begin MySQL execute scalar");

            var asyncContext = new AsyncContext
            {
                InitiallyClosed = _connection.State == ConnectionState.Closed,
                StartTime       = PerformanceTimer.TimeNow
            };

            try
            {
                if (asyncContext.InitiallyClosed)
                {
                    OpenConnection();
                }
                asyncContext.Result = _mySqlCommand.ExecuteScalar();

                var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime);
                _repository.RecordSuccess(this, PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime));
                AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                {
                    Connection     = this,
                    Command        = _command,
                    ElapsedSeconds = elapsedSeconds
                });
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, "Failed to ExecuteScalar on MySQL " + _repository.Name, _repository, this);
                throw;
            }
            finally
            {
                if (asyncContext.InitiallyClosed)
                {
                    CloseConnection();
                }
            }
            return(new SyncronousResult(asyncContext, callback));
        }
示例#7
0
        public long ExecuteNonQuery()
        {
            var initiallyClosed = _connection.State == ConnectionState.Closed;
            var startTime       = PerformanceTimer.TimeNow;

            try
            {
                if (initiallyClosed)
                {
                    OpenConnection();
                }
                var rowsAffected   = _sqlCommand.ExecuteNonQuery();
                var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - startTime);
                _repository.RecordSuccess(this, elapsedSeconds);
                AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                {
                    Connection     = this,
                    Command        = _command,
                    ElapsedSeconds = elapsedSeconds
                });

                foreach (var parameter in _command.GetParameters())
                {
                    parameter.StoreOutputValue(parameter);
                }

                return(rowsAffected);
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, _sqlCommand, "Failed to ExecuteNonQuery on SQL Server " + _repository.Name, _repository, this);
                throw;
            }
            finally
            {
                if (initiallyClosed)
                {
                    CloseConnection();
                }
            }
        }
示例#8
0
        public T ExecuteScalar <T>()
        {
            Trace("SQlite execute scalar");
            var initiallyClosed = _connection.State == ConnectionState.Closed;

            try
            {
                var startTime = PerformanceTimer.TimeNow;

                if (initiallyClosed)
                {
                    OpenConnection();
                }

                var result = _commandProcessor.ExecuteScalar <T>();

                var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - startTime);
                _repository.RecordSuccess(this, elapsedSeconds);
                AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                {
                    Connection     = this,
                    Command        = _command,
                    ElapsedSeconds = elapsedSeconds
                });

                return(result);
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, "Failed to ExecuteScalar on SQLite repository '" + _repository.Name + "'", _repository, this);
                throw;
            }
            finally
            {
                if (initiallyClosed)
                {
                    CloseConnection();
                }
            }
        }
示例#9
0
        public IAsyncResult BeginExecuteNonQuery(AsyncCallback callback)
        {
            Trace("SQlite begin execute non-query");
            var asyncContext = new AsyncContext
            {
                InitiallyClosed = _connection.State == ConnectionState.Closed,
                StartTime       = PerformanceTimer.TimeNow
            };

            try
            {
                if (asyncContext.InitiallyClosed)
                {
                    OpenConnection();
                }

                asyncContext.Result = _commandProcessor.ExecuteNonQuery();

                var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime);
                _repository.RecordSuccess(this, elapsedSeconds);
                AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                {
                    Connection     = this,
                    Command        = _command,
                    ElapsedSeconds = elapsedSeconds
                });
            }
            catch (Exception ex)
            {
                Trace("Exception executing SQlite non-query");
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, "Failed to ExecuteNonQuery on SQLite " + _repository.Name, _repository, this);
                asyncContext.Result = (long)0;
                throw;
            }
            return(new SyncronousResult(asyncContext, callback));
        }
示例#10
0
        public IAsyncResult BeginExecuteNonQuery(AsyncCallback callback)
        {
            var asyncContext = new AsyncContext
            {
                InitiallyClosed = _connection.State == System.Data.ConnectionState.Closed,
                StartTime       = PerformanceTimer.TimeNow
            };

            try
            {
                if (asyncContext.InitiallyClosed)
                {
                    _connection.Open();
                }

                if (_isBulkCopy && _dataTable != null)
                {
                    BulkCopy(_dataTable, _connection, _command);
                    asyncContext.Result = (long)_dataTable.Rows.Count;
                }
                else
                {
                    asyncContext.Result = (long)_command.ExecuteNonQuery();
                }

                _repository.RecordSuccess(this, PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime));
                if (asyncContext.InitiallyClosed)
                {
                    _connection.Close();
                }
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                _errorReporter.ReportError(ex, string.Format("Failed to ExecuteNonQuery on PostgreSql {0}\nConnection State: {1}", _repository.Name, _connection.FullState), _repository, this);
                asyncContext.Result = 0L;
                throw;
            }
            return(new SyncronousResult(asyncContext, callback));
        }
示例#11
0
        public IAsyncResult BeginExecuteReader(AsyncCallback callback)
        {
            Trace("Begin MySQL execute reader");

            var asyncContext = new AsyncContext
            {
                InitiallyClosed = _connection.State == ConnectionState.Closed,
                StartTime       = PerformanceTimer.TimeNow
            };

            try
            {
                if (asyncContext.InitiallyClosed)
                {
                    OpenConnection();
                }

                var reader = _mySqlCommand.ExecuteReader();
                if (reader == null)
                {
                    throw new PriusException("MySQL command did not return a reader", null, null, this, _repository);
                }

                foreach (var parameter in _command.GetParameters())
                {
                    parameter.StoreOutputValue(parameter);
                }

                var dataShapeName = _connection.DataSource + ":" + _connection.Database + ":" + _mySqlCommand.CommandType + ":" + _mySqlCommand.CommandText;
                asyncContext.Result = new DataReader(_errorReporter).Initialize(
                    reader,
                    dataShapeName,
                    () =>
                {
                    reader.Dispose();
                    var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime);
                    _repository.RecordSuccess(this, elapsedSeconds);
                    AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                    {
                        Connection     = this,
                        Command        = _command,
                        ElapsedSeconds = elapsedSeconds
                    });
                    if (asyncContext.InitiallyClosed)
                    {
                        CloseConnection();
                    }
                },
                    () =>
                {
                    _repository.RecordFailure(this);
                    AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                    {
                        Connection = this,
                        Command    = _command,
                    });
                    if (asyncContext.InitiallyClosed)
                    {
                        CloseConnection();
                    }
                }
                    );
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, "Failed to ExecuteReader on MySQL " + _repository.Name, _repository, this);
                if (asyncContext.InitiallyClosed)
                {
                    CloseConnection();
                }
                throw;
            }
            return(new SyncronousResult(asyncContext, callback));
        }
示例#12
0
        public IDataReader ExecuteReader()
        {
            var initiallyClosed = _connection.State == ConnectionState.Closed;
            var startTime       = PerformanceTimer.TimeNow;

            try
            {
                if (initiallyClosed)
                {
                    OpenConnection();
                }
                var reader = _sqlCommand.ExecuteReader();

                foreach (var parameter in _command.GetParameters())
                {
                    parameter.StoreOutputValue(parameter);
                }

                var dataShapeName = _connection.DataSource + ":" + _connection.Database + ":" + _sqlCommand.CommandType + ":" + _sqlCommand.CommandText;

                return(new DataReader(_errorReporter).Initialize(
                           reader,
                           dataShapeName,
                           () =>
                {
                    reader.Dispose();
                    var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - startTime);
                    _repository.RecordSuccess(this, elapsedSeconds);
                    AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                    {
                        Connection = this,
                        Command = _command,
                        ElapsedSeconds = elapsedSeconds
                    });
                    if (initiallyClosed)
                    {
                        CloseConnection();
                    }
                },
                           () =>
                {
                    _repository.RecordFailure(this);
                    AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                    {
                        Connection = this,
                        Command = _command,
                    });
                    if (initiallyClosed)
                    {
                        CloseConnection();
                    }
                }
                           ));
            }
            catch (Exception ex)
            {
                _repository.RecordFailure(this);
                AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                {
                    Connection = this,
                    Command    = _command,
                });
                _errorReporter.ReportError(ex, _sqlCommand, "Failed to ExecuteReader on SQL Server " + _repository.Name, _repository, this);
                if (initiallyClosed)
                {
                    CloseConnection();
                }
                throw;
            }
        }
示例#13
0
        public IAsyncResult BeginExecuteReader(AsyncCallback callback)
        {
            Trace("SQlite begin execute reader");
            var asyncContext = new AsyncContext
            {
                InitiallyClosed = _connection.State == ConnectionState.Closed,
                StartTime       = PerformanceTimer.TimeNow
            };

            try
            {
                if (asyncContext.InitiallyClosed)
                {
                    OpenConnection();
                }

                var reader = _commandProcessor.ExecuteReader(
                    _dataShapeName,
                    r =>
                {
                    var elapsedSeconds = PerformanceTimer.TicksToSeconds(PerformanceTimer.TimeNow - asyncContext.StartTime);
                    _repository.RecordSuccess(this, elapsedSeconds);
                    AnalyticRecorder?.CommandCompleted(new CommandAnalyticInfo
                    {
                        Connection     = this,
                        Command        = _command,
                        ElapsedSeconds = elapsedSeconds
                    });

                    if (asyncContext.InitiallyClosed)
                    {
                        CloseConnection();
                    }
                },
                    r =>
                {
                    _repository.RecordFailure(this);
                    AnalyticRecorder?.CommandFailed(new CommandAnalyticInfo
                    {
                        Connection = this,
                        Command    = _command,
                    });

                    if (asyncContext.InitiallyClosed)
                    {
                        CloseConnection();
                    }
                }
                    );

                if (reader == null)
                {
                    throw new PriusException("SQLite command did not return a reader");
                }

                foreach (var parameter in _command.GetParameters())
                {
                    parameter.StoreOutputValue(parameter);
                }

                asyncContext.Result = reader;
            }
            catch (Exception ex)
            {
                Trace("SQlite exception executing reader");
                _repository.RecordFailure(this);
                _errorReporter.ReportError(ex, "Failed to ExecuteReader on SQLite " + _repository.Name, _repository, this);

                if (asyncContext.InitiallyClosed)
                {
                    CloseConnection();
                }

                throw;
            }
            return(new SyncronousResult(asyncContext, callback));
        }