-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommandTypeProvider.cs
More file actions
51 lines (48 loc) · 2.08 KB
/
CommandTypeProvider.cs
File metadata and controls
51 lines (48 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
using MGR.CommandLineParser.Extensibility.Command;
using Microsoft.EntityFrameworkCore;
namespace MGR.CommandLineParser.Command.OracleProcedure
{
internal class CommandTypeProvider : ICommandTypeProvider
{
private readonly OracleSystemContext _dbContext;
public CommandTypeProvider(OracleSystemContext dbContext)
{
_dbContext = dbContext;
}
public async Task<IEnumerable<ICommandType>> GetAllCommandTypes()
{
var procedures = await _dbContext.Procedures.Include(procedure => procedure.Parameters).ToListAsync();
return procedures.Select(procedure => MapProcedureToCommandType(procedure, _dbContext.Database.GetDbConnection()));
}
public async Task<ICommandType> GetCommandType(string commandName)
{
var procedure = await _dbContext.Procedures.Include(procedure => procedure.Parameters).Where(procedure => procedure.Name == commandName).SingleOrDefaultAsync();
if (procedure != null)
{
return MapProcedureToCommandType(procedure, _dbContext.Database.GetDbConnection());
}
return null;
}
private static CommandType MapProcedureToCommandType(Procedure procedure, DbConnection dbConnection)
{
return new CommandType(
new CommandMetadata(procedure.Name),
procedure.Parameters
.Where(parameter => parameter.Direction.HasFlag(Direction.In))
.Select(parameter => new CommandOptionMetadata(
new OptionDisplayInfo(parameter.Name),
!parameter.HasDefaultValue,
parameter.DefaultValue
)
),
procedure.Parameters
.Where(parameter => parameter.Direction.HasFlag(Direction.Out)),
dbConnection
);
}
}
}