Пример #1
0
        protected RelationalDataStore(
            [NotNull] StateManager stateManager,
            [NotNull] DbContextService <IModel> model,
            [NotNull] EntityKeyFactorySource entityKeyFactorySource,
            [NotNull] EntityMaterializerSource entityMaterializerSource,
            [NotNull] ClrCollectionAccessorSource collectionAccessorSource,
            [NotNull] ClrPropertySetterSource propertySetterSource,
            [NotNull] RelationalConnection connection,
            [NotNull] CommandBatchPreparer batchPreparer,
            [NotNull] BatchExecutor batchExecutor,
            [NotNull] DbContextService <IDbContextOptions> options,
            [NotNull] ILoggerFactory loggerFactory)
            : base(stateManager, model, entityKeyFactorySource, entityMaterializerSource,
                   collectionAccessorSource, propertySetterSource, loggerFactory)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(batchPreparer, "batchPreparer");
            Check.NotNull(batchExecutor, "batchExecutor");
            Check.NotNull(options, "options");

            _batchPreparer = batchPreparer;
            _batchExecutor = batchExecutor;
            _connection    = connection;
            _options       = options.Service;
        }
Пример #2
0
        public virtual object Execute(
            [NotNull] RelationalConnection connection,
            [NotNull] Func <object> action)
        {
            Check.NotNull(connection, "connection");

            // TODO Deal with suppressing transactions etc.

            var connectionWasOpen = connection.DbConnection.State == ConnectionState.Open;

            if (!connectionWasOpen)
            {
                Logger.OpeningConnection(connection.ConnectionString);

                connection.Open();
            }

            try
            {
                return(action());
            }
            finally
            {
                if (!connectionWasOpen)
                {
                    Logger.ClosingConnection(connection.ConnectionString);

                    connection.Close();
                }
            }
        }
Пример #3
0
        public virtual async Task <object> ExecuteAsync(
            [NotNull] RelationalConnection connection,
            [NotNull] Func <Task <object> > action,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotNull(connection, "connection");

            // TODO Deal with suppressing transactions etc.

            var connectionWasOpen = connection.DbConnection.State == ConnectionState.Open;

            if (!connectionWasOpen)
            {
                Logger.OpeningConnection(connection.ConnectionString);

                await connection.OpenAsync(cancellationToken).WithCurrentCulture();
            }

            try
            {
                return(await action().WithCurrentCulture());
            }
            finally
            {
                if (!connectionWasOpen)
                {
                    Logger.ClosingConnection(connection.ConnectionString);

                    connection.Close();
                }
            }
        }
 public FakeRelationalDataStore(
     DbContextConfiguration configuration,
     RelationalConnection connection,
     CommandBatchPreparer batchPreparer,
     BatchExecutor batchExecutor)
     : base(configuration, connection, batchPreparer, batchExecutor)
 {
 }
        public RelationalTransaction([NotNull] RelationalConnection connection, [NotNull] DbTransaction dbTransaction, bool transactionOwned)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(dbTransaction, "dbTransaction");
            if (connection.DbConnection != dbTransaction.Connection)
            {
                throw new InvalidOperationException(Strings.FormatTransactionAssociatedWithDifferentConnection());
            }

            _connection       = connection;
            _dbTransaction    = dbTransaction;
            _transactionOwned = transactionOwned;
        }
Пример #6
0
        public RelationalQueryContext(
            [NotNull] IModel model,
            [NotNull] ILogger logger,
            [NotNull] StateManager stateManager,
            [NotNull] RelationalConnection connection,
            [NotNull] RelationalValueReaderFactory valueReaderFactory)
            : base(model, logger, stateManager)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(valueReaderFactory, "valueReaderFactory");

            _connection         = connection;
            _valueReaderFactory = valueReaderFactory;
        }
Пример #7
0
        protected RelationalDataStore(
            [NotNull] DbContextConfiguration configuration,
            [NotNull] RelationalConnection connection,
            [NotNull] CommandBatchPreparer batchPreparer,
            [NotNull] BatchExecutor batchExecutor)
            : base(configuration)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(batchPreparer, "batchPreparer");
            Check.NotNull(batchExecutor, "batchExecutor");

            _batchPreparer = batchPreparer;
            _batchExecutor = batchExecutor;
            _connection    = connection;
        }
Пример #8
0
        protected virtual DbCommand CreateCommand(
            RelationalConnection connection,
            DbTransaction transaction,
            string sql)
        {
            var command = connection.DbConnection.CreateCommand();

            command.CommandText = sql;
            command.Transaction = transaction;

            if (connection.CommandTimeout != null)
            {
                command.CommandTimeout = (int)connection.CommandTimeout;
            }

            return(command);
        }
Пример #9
0
        public virtual object ExecuteScalar(
            [NotNull] RelationalConnection connection,
            [CanBeNull] DbTransaction transaction,
            [NotNull] string sql)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(sql, "sql");

            return(Execute(
                       connection,
                       () =>
            {
                Logger.WriteSql(sql);

                return CreateCommand(connection, transaction, sql).ExecuteScalar();
            }));
        }
Пример #10
0
        public virtual Task <object> ExecuteScalarAsync(
            [NotNull] RelationalConnection connection,
            [CanBeNull] DbTransaction transaction,
            [NotNull] string sql,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(sql, "sql");

            return(ExecuteAsync(
                       connection,
                       () =>
            {
                Logger.WriteSql(sql);

                return CreateCommand(connection, transaction, sql).ExecuteScalarAsync(cancellationToken);
            },
                       cancellationToken));
        }
Пример #11
0
                private async Task <bool> InitializeAndReadAsync(
                    CancellationToken cancellationToken = default(CancellationToken))
                {
                    Contract.Assert(_connection == null);

                    var connection = _queryContext.Connection;
                    await connection.OpenAsync(cancellationToken);

                    _connection = connection;

                    _command             = _connection.DbConnection.CreateCommand();
                    _command.CommandText = _sql;

                    _queryContext.Logger.WriteSql(_sql);

                    _reader = await _command.ExecuteReaderAsync(cancellationToken);

                    return(await _reader.ReadAsync(cancellationToken));
                }
Пример #12
0
                public bool MoveNext()
                {
                    if (_reader == null)
                    {
                        Contract.Assert(_connection == null);

                        var connection = _queryContext.Connection;
                        connection.Open();
                        _connection = connection;

                        _command             = _connection.DbConnection.CreateCommand();
                        _command.CommandText = _sql;

                        _queryContext.Logger.WriteSql(_sql);

                        _reader = _command.ExecuteReader();
                    }

                    return(_reader.Read());
                }
Пример #13
0
        public virtual void ExecuteNonQuery(
            [NotNull] RelationalConnection connection,
            [CanBeNull] DbTransaction transaction,
            [NotNull] IEnumerable <SqlBatch> sqlBatches)
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(sqlBatches, "sqlBatches");

            Execute(
                connection,
                () =>
            {
                foreach (var sqlBatch in sqlBatches)
                {
                    Logger.WriteSql(sqlBatch.Sql);

                    CreateCommand(connection, transaction, sqlBatch.Sql).ExecuteNonQuery();
                }
                return(null);
            });
        }
Пример #14
0
        public virtual Task ExecuteNonQueryAsync(
            [NotNull] RelationalConnection connection,
            [CanBeNull] DbTransaction transaction,
            [NotNull] IEnumerable <SqlBatch> sqlBatches,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotNull(connection, "connection");
            Check.NotNull(sqlBatches, "sqlBatches");

            return(ExecuteAsync(
                       connection,
                       async() =>
            {
                foreach (var sqlBatch in sqlBatches)
                {
                    Logger.WriteSql(sqlBatch.Sql);

                    await CreateCommand(connection, transaction, sqlBatch.Sql).ExecuteNonQueryAsync(cancellationToken)
                    .WithCurrentCulture();
                }
                return Task.FromResult <object>(null);
            },
                       cancellationToken));
        }
Пример #15
0
        private static void CreateDatabaseSequence(BronieContext context, RelationalConnection storeConnection)
        {
            var executor = new SqlStatementExecutor();

            var operations = new SqlServerSequenceValueGeneratorFactory(executor)
                .GetUpMigrationOperations(context.Model.GetEntityType(typeof(Pegasus))
                    .GetProperty("Identifier"));

            var sql = new SqlServerMigrationOperationSqlGenerator(new SqlServerTypeMapper())
                .Generate(operations);

            // TODO: Should be able to get relational connection without cast
            var connection = storeConnection.DbConnection;

            executor.ExecuteNonQuery(connection, storeConnection.DbTransaction, sql);
        }
 public FakeRelationalDataStore(
     DbContextConfiguration configuration,
     RelationalConnection connection,
     CommandBatchPreparer batchPreparer,
     BatchExecutor batchExecutor)
     : base(configuration, connection, batchPreparer, batchExecutor)
 {
 }