private bool IsSqlEditionSupported(string connectionString)
        {
            var operation = new DbOperation(connectionString, "SELECT SERVERPROPERTY ( 'EngineEdition' )", _trace);
            var edition = (int)operation.ExecuteScalar();

            return edition >= SqlEngineEdition.Standard && edition <= SqlEngineEdition.Express;
        }
Beispiel #2
0
        private void Receive(object state)
        {
            var tcs = (TaskCompletionSource<object>)state;

            if (!_lastPayloadId.HasValue)
            {
                var lastPayloadIdOperation = new DbOperation(_connectionString, _maxIdSql, _trace)
                {
                    TracePrefix = _tracePrefix
                };

                try
                {
                    _lastPayloadId = (long?)lastPayloadIdOperation.ExecuteScalar();
                    OnQuery();

                    // Complete the StartReceiving task as we've successfully initialized the payload ID
                    tcs.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                    return;
                }
            }

            // NOTE: This is called from a BG thread so any uncaught exceptions will crash the process
            lock (this)
            {
                if (_disposed)
                {
                    return;
                }

                var parameter = _dbProviderFactory.CreateParameter();
                parameter.ParameterName = "PayloadId";
                parameter.Value = _lastPayloadId.Value;

                _dbOperation = new ObservableDbOperation(_connectionString, _selectSql, _trace, parameter)
                {
                    OnError = ex => _onError(ex),
                    OnQuery = () => OnQuery(),
                    TracePrefix = _tracePrefix
                };
            }

            _dbOperation.ExecuteReaderWithUpdates((rdr, o) => ProcessRecord(rdr, o));

            _trace.TraceWarning("{0}SqlReceiver.Receive returned", _tracePrefix);
        }
        private void Receive(object state)
        {
            var tcs = (TaskCompletionSource<object>)state;

            if (!_lastPayloadId.HasValue)
            {
                var lastPayloadIdOperation = new DbOperation(_connectionString, _maxIdSql, _logger)
                {
                    LoggerPrefix = _loggerPrefix
                };

                try
                {
                    _lastPayloadId = (long?)lastPayloadIdOperation.ExecuteScalar();
                    Queried();

                    _logger.LogVerbose(String.Format("{0}SqlReceiver started, initial payload id={1}", _loggerPrefix, _lastPayloadId));

                    // Complete the StartReceiving task as we've successfully initialized the payload ID
                    tcs.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    _logger.LogError(String.Format("{0}SqlReceiver error starting: {1}", _loggerPrefix, ex));

                    tcs.TrySetException(ex);
                    return;
                }
            }

            // NOTE: This is called from a BG thread so any uncaught exceptions will crash the process
            lock (this)
            {
                if (_disposed)
                {
                    return;
                }

                var parameter = _dbProviderFactory.CreateParameter();
                parameter.ParameterName = "PayloadId";
                parameter.Value = _lastPayloadId.Value;

                _dbOperation = new ObservableDbOperation(_connectionString, _selectSql, _logger, parameter)
                {
                    LoggerPrefix = _loggerPrefix
                };
            }

            _dbOperation.Queried += () => Queried();
            _dbOperation.Faulted += ex => Faulted(ex);
#if DNX451
            _dbOperation.Changed += () =>
            {
                _logger.LogInformation("{0}Starting receive loop again to process updates", _loggerPrefix);

                _dbOperation.ExecuteReaderWithUpdates(ProcessRecord);
            };
#endif
            _logger.LogVerbose(String.Format("{0}Executing receive reader, initial payload ID parameter={1}", _loggerPrefix, _dbOperation.Parameters[0].Value));

            _dbOperation.ExecuteReaderWithUpdates(ProcessRecord);

            _logger.LogInformation("{0}SqlReceiver.Receive returned", _loggerPrefix);
        }
Beispiel #4
0
        public void DbExceptionThrown()
        {
            string msg = Guid.NewGuid().ToString("N");

            var fakeDbCommand = A.Fake <IDbCommand>();

            A.CallTo(() => fakeDbCommand.ExecuteNonQuery()).Throws(new Exception(msg));
            A.CallTo(() => fakeDbCommand.ExecuteScalar()).Throws(new Exception(msg));
            A.CallTo(() => fakeDbCommand.ExecuteReader()).Throws(new Exception(msg));

            var fakeConnection = A.Fake <IDbConnection>();

            A.CallTo(() => fakeConnection.CreateCommand())
            .Returns(fakeDbCommand);

            IDbProviderFactory fakeDbProviderFactory = A.Fake <IDbProviderFactory>();
            var createConnectionCall = A.CallTo(() => fakeDbProviderFactory.CreateConnection());

            createConnectionCall.Returns(fakeConnection);

            DbOperation dbOperation = new DbOperation(string.Empty, string.Empty, new TraceSource("ss"), fakeDbProviderFactory);

            try
            {
                dbOperation.ExecuteNonQuery();
                Assert.Fail("Expected exception was not thrown.");
            }
            catch (Exception e)
            {
                Assert.AreEqual(msg, e.Message);
            }

            try
            {
                dbOperation.ExecuteScalar();
                Assert.Fail("Expected exception was not thrown.");
            }
            catch (Exception e)
            {
                Assert.AreEqual(msg, e.Message);
            }

            try
            {
                dbOperation.ExecuteReader(A.Fake <Action <IDataRecord, IDbOperation> >());
                Assert.Fail("Expected exception was not thrown.");
            }
            catch (Exception e)
            {
                Assert.AreEqual(msg, e.Message);
            }

            try
            {
                dbOperation.ExecuteNonQueryAsync().Wait();
                Assert.Fail("Expected exception was not thrown.");
            }
            catch (Exception e)
            {
                Assert.AreEqual(msg, e.Message);
            }
        }