| | | 1 | | namespace ZhEaIsNsAaBn.Infrastructure.SqlServer; |
| | | 2 | | |
| | | 3 | | /// <summary>SQL Server <see cref="IStoredProcedureExecutor"/>: <c>EXEC [schema].[proc] @A = @A, …</c> with every value |
| | | 4 | | internal sealed class SqlServerStoredProcedureExecutor : IStoredProcedureExecutor |
| | | 5 | | { |
| | | 6 | | public IReadOnlyList<TEntity> GetFromSql< TEntity >( DbContext dbContext, string storedName, int? cmdType, Dictionar |
| | | 7 | | where TEntity : class |
| | | 8 | | { |
| | 0 | 9 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 10 | | |
| | 0 | 11 | | var (sql, sqlParameters) = SqlServerParameters.StoredProcedureCommand( storedName, cmdType, parameters ); |
| | | 12 | | |
| | | 13 | | |
| | 0 | 14 | | return dbContext.Set<TEntity>().FromSqlRaw( sql, sqlParameters ).AsNoTracking().ToList(); |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task<IReadOnlyList<TEntity>> GetFromSqlAsync< TEntity >( |
| | | 18 | | DbContext dbContext, string storedName, int? cmdType, Dictionary<object, object>[] parameters, CancellationT |
| | | 19 | | ) |
| | | 20 | | where TEntity : class |
| | | 21 | | { |
| | 28 | 22 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 23 | | |
| | 28 | 24 | | var (sql, sqlParameters) = SqlServerParameters.StoredProcedureCommand( storedName, cmdType, parameters ); |
| | | 25 | | |
| | | 26 | | |
| | 8 | 27 | | return await dbContext.Set<TEntity>().FromSqlRaw( sql, sqlParameters ).AsNoTracking().ToListAsync( cancellationT |
| | 8 | 28 | | } |
| | | 29 | | |
| | | 30 | | public int OperateFromSql( DbContext dbContext, string storedName, int? cmdType, Dictionary<object, object>[] parame |
| | | 31 | | { |
| | 0 | 32 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 33 | | |
| | 0 | 34 | | var output = RetValParameter(); |
| | 0 | 35 | | var (sql, arguments) = SqlServerParameters.StoredProcedureCommand( storedName, cmdType, parameters, output ); |
| | | 36 | | |
| | | 37 | | |
| | 0 | 38 | | dbContext.Database.ExecuteSqlRaw( sql, arguments ); |
| | | 39 | | |
| | | 40 | | |
| | 0 | 41 | | return output.Value is int value ? value : 0; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public async Task<int> OperateFromSqlAsync( DbContext dbContext, string storedName, int? cmdType, Dictionary<object, |
| | | 45 | | { |
| | 8 | 46 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 47 | | |
| | 8 | 48 | | var output = RetValParameter(); |
| | 8 | 49 | | var (sql, arguments) = SqlServerParameters.StoredProcedureCommand( storedName, cmdType, parameters, output ); |
| | | 50 | | |
| | | 51 | | |
| | 8 | 52 | | await dbContext.Database.ExecuteSqlRawAsync( sql, arguments, cancellationToken ).ConfigureAwait( false ); |
| | | 53 | | |
| | | 54 | | |
| | 8 | 55 | | return output.Value is int value ? value : 0; |
| | 8 | 56 | | } |
| | | 57 | | |
| | | 58 | | |
| | 8 | 59 | | private static SqlParameter RetValParameter() => new() { ParameterName = "@retVal", DbType = DbType.Int32, Direction |
| | | 60 | | } |