예제 #1
0
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 protected virtual int SaveChanges(
     [NotNull] IReadOnlyList <InternalEntityEntry> entriesToSave)
 {
     using (_concurrencyDetector.EnterCriticalSection())
     {
         return(_database.SaveChanges(entriesToSave));
     }
 }
예제 #2
0
            public bool MoveNext()
            {
                try
                {
                    _concurrencyDetector?.EnterCriticalSection();

                    try
                    {
                        if (!_hasExecuted)
                        {
                            if (!_readItemEnumerable.TryGetResourceId(out var resourceId))
                            {
                                throw new InvalidOperationException(CosmosStrings.ResourceIdMissing);
                            }

                            if (!_readItemEnumerable.TryGetPartitionId(out var partitionKey))
                            {
                                throw new InvalidOperationException(CosmosStrings.PartitionKeyMissing);
                            }

                            EntityFrameworkEventSource.Log.QueryExecuting();

                            _item = _cosmosQueryContext.CosmosClient.ExecuteReadItem(
                                _readItemExpression.Container,
                                partitionKey,
                                resourceId);

                            return(ShapeResult());
                        }

                        return(false);
                    }
                    finally
                    {
                        _concurrencyDetector?.ExitCriticalSection();
                    }
                }
                catch (Exception exception)
                {
                    if (_exceptionDetector.IsCancellation(exception))
                    {
                        _queryLogger.QueryCanceled(_contextType);
                    }
                    else
                    {
                        _queryLogger.QueryIterationFailed(_contextType, exception);
                    }

                    throw;
                }
            }
예제 #3
0
        public static async Task <RelationalDataReader> ExecuteSqlQueryAsync(this DatabaseFacade databaseFacade,
                                                                             string sql,
                                                                             TheGodfatherDbContext db,
                                                                             params object[] parameters)
        {
            IConcurrencyDetector concurrencyDetector = databaseFacade.GetService <IConcurrencyDetector>();

            using (concurrencyDetector.EnterCriticalSection()) {
                RawSqlCommand rawSqlCommand = databaseFacade
                                              .GetService <IRawSqlCommandBuilder>()
                                              .Build(sql, parameters);

                return(await rawSqlCommand
                       .RelationalCommand
                       .ExecuteReaderAsync(
                           new RelationalCommandParameterObject(
                               databaseFacade.GetService <IRelationalConnection>(),
                               parameterValues : rawSqlCommand.ParameterValues,
                               readerColumns : null,
                               context : db,
                               logger : null
                               )
                           ));
            }
        }
예제 #4
0
        public static async Task <int> ExecuteNonQueryAsync(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
        {
            IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
            IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
            IDiagnosticsLogger <DbLoggerCategory.Database.Command> commandLogger = ((IDatabaseFacadeDependencies)facadeDependencies).CommandLogger;

            using (concurrencyDetector.EnterCriticalSection())
            {
                var rawSqlCommand = databaseFacade.Build(sql, parameters);
                return(await rawSqlCommand.RelationalCommand.ExecuteNonQueryAsync(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, commandLogger)));
            }
        }
            public async ValueTask <bool> MoveNextAsync()
            {
                try
                {
                    _concurrencyDetector?.EnterCriticalSection();

                    try
                    {
                        if (_enumerator == null)
                        {
                            var sqlQuery = _queryingEnumerable.GenerateQuery();

                            EntityFrameworkEventSource.Log.QueryExecuting();

                            _enumerator = _cosmosQueryContext.CosmosClient
                                          .ExecuteSqlQueryAsync(
                                _selectExpression.Container,
                                _partitionKey,
                                sqlQuery)
                                          .GetAsyncEnumerator(_cancellationToken);
                            _cosmosQueryContext.InitializeStateManager(_standAloneStateManager);
                        }

                        var hasNext = await _enumerator.MoveNextAsync().ConfigureAwait(false);

                        Current
                            = hasNext
                                ? _shaper(_cosmosQueryContext, _enumerator.Current)
                                : default;

                        return(hasNext);
                    }
                    finally
                    {
                        _concurrencyDetector?.ExitCriticalSection();
                    }
                }
                catch (Exception exception)
                {
                    if (_exceptionDetector.IsCancellation(exception, _cancellationToken))
                    {
                        _queryLogger.QueryCanceled(_contextType);
                    }
                    else
                    {
                        _queryLogger.QueryIterationFailed(_contextType, exception);
                    }

                    throw;
                }
            }
예제 #6
0
        private RelationalDataReader ExecuteSqlQuery(DatabaseFacade database, string sql, params object[] parameters)
        {
            IConcurrencyDetector concurrencyDetector = database.GetService <IConcurrencyDetector>();

            using (concurrencyDetector.EnterCriticalSection())
            {
                var rawSqlCommand = database
                                    .GetService <IRawSqlCommandBuilder>()
                                    .Build(sql, parameters);

                return(rawSqlCommand
                       .RelationalCommand
                       .ExecuteReader(
                           database.GetService <IRelationalConnection>(),
                           parameterValues: rawSqlCommand.ParameterValues));
            }
        }
예제 #7
0
        internal static T ExecuteReader <T>(this DatabaseFacade databaseFacade, string sql, object[] parameters, Func <DbDataReader, T> func)
        {
            T result = default(T);
            IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
            IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
            IDiagnosticsLogger <DbLoggerCategory.Database.Command> commandLogger = ((IDatabaseFacadeDependencies)facadeDependencies).CommandLogger;

            using (concurrencyDetector.EnterCriticalSection())
            {
                var rawSqlCommand = databaseFacade.Build(sql, parameters);
                using (var reader = rawSqlCommand.RelationalCommand.ExecuteReader(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, commandLogger)))
                {
                    result = func.Invoke(reader.DbDataReader);
                }
            }
            return(result);
        }
예제 #8
0
            public bool MoveNext()
            {
                try
                {
                    _concurrencyDetector?.EnterCriticalSection();

                    try
                    {
                        if (_enumerator == null)
                        {
                            var sqlQuery = _queryingEnumerable.GenerateQuery();

                            EntityFrameworkEventSource.Log.QueryExecuting();

                            _enumerator = _cosmosQueryContext.CosmosClient
                                          .ExecuteSqlQuery(
                                _selectExpression.Container,
                                _partitionKey,
                                sqlQuery)
                                          .GetEnumerator();
                            _cosmosQueryContext.InitializeStateManager(_standAloneStateManager);
                        }

                        var hasNext = _enumerator.MoveNext();

                        Current
                            = hasNext
                                ? _shaper(_cosmosQueryContext, _enumerator.Current)
                                : default;

                        return(hasNext);
                    }
                    finally
                    {
                        _concurrencyDetector?.ExitCriticalSection();
                    }
                }
                catch (Exception exception)
                {
                    _queryLogger.QueryIterationFailed(_contextType, exception);

                    throw;
                }
            }
예제 #9
0
        public static async Task <RelationalDataReader> ExecuteSqlQueryAsync(this DatabaseFacade databaseFacade,
                                                                             string sql,
                                                                             CancellationToken cancellationToken = default,
                                                                             params object[] parameters)
        {
            IConcurrencyDetector concurrencyDetector = databaseFacade.GetService <IConcurrencyDetector>();

            using (concurrencyDetector.EnterCriticalSection()) {
                RawSqlCommand rawSqlCommand = databaseFacade
                                              .GetService <IRawSqlCommandBuilder>()
                                              .Build(sql, parameters);

                return(await rawSqlCommand
                       .RelationalCommand
                       .ExecuteReaderAsync(
                           databaseFacade.GetService <IRelationalConnection>(),
                           parameterValues : rawSqlCommand.ParameterValues,
                           cancellationToken : cancellationToken));
            }
        }
        private static async Task <RelationalDataReader> ExecuteSqlQueryAsync(
            this DbContext dbContext,
            string sql,
            IEnumerable <object> parameters,
            CancellationToken cancellationToken = default)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            if (string.IsNullOrWhiteSpace(sql))
            {
                throw new ArgumentNullException(nameof(sql));
            }

            IConcurrencyDetector concurrencyDetector = dbContext.GetService <IConcurrencyDetector>();

            using (concurrencyDetector.EnterCriticalSection())
            {
                RawSqlCommand rawSqlCommand = dbContext
                                              .GetService <IRawSqlCommandBuilder>()
                                              .Build(sql, parameters);

                RelationalCommandParameterObject paramObject = new RelationalCommandParameterObject(
                    dbContext.GetService <IRelationalConnection>(),
                    rawSqlCommand.ParameterValues,
                    null,
                    null,
                    null);

                RelationalDataReader relationalDataReader = await rawSqlCommand
                                                            .RelationalCommand
                                                            .ExecuteReaderAsync(paramObject, cancellationToken);

                return(relationalDataReader);
            }
        }