Skip to content

Commit 9cd74cb

Browse files
committed
Pass db connection to the command builder
1 parent f18a044 commit 9cd74cb

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed

samples/SimpleApp/SimpleApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework />
55
<OutputType>Exe</OutputType>
6-
<TargetFrameworks>netcoreapp2.0;net471</TargetFrameworks>
6+
<TargetFrameworks>netcoreapp3.1;net471</TargetFrameworks>
77
<LangVersion>latest</LangVersion>
88
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
99
<DocumentationFile />
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Text;
3+
using System.Data.Common;
44
using MGR.CommandLineParser.Extensibility.Command;
55

66
namespace MGR.CommandLineParser.Command.OracleProcedure
77
{
8-
class CommandObjectBuilder : ICommandObjectBuilder
8+
internal class CommandObjectBuilder : ICommandObjectBuilder
99
{
10-
public CommandObjectBuilder(IEnumerable<CommandOption> options, )
11-
{
10+
private IEnumerable<ICommandOptionMetadata> _options;
11+
private IEnumerable<Parameter> _outParameters;
12+
private DbConnection _dbConnection;
1213

14+
public CommandObjectBuilder(IEnumerable<ICommandOptionMetadata> options, IEnumerable<Parameter> outParameters, DbConnection dbConnection)
15+
{
16+
_options = options;
17+
_outParameters = outParameters;
18+
_dbConnection = dbConnection;
1319
}
20+
1421
public void AddArguments(string argument) => throw new NotImplementedException("Oracle procedures does not define unamed arguments");
15-
public ICommandOption FindOption(string optionName) => throw new NotImplementedException();
16-
public ICommandOption FindOptionByShortName(string optionShortName) => throw new NotImplementedException("Oracle procedures does not define 'short name' for the parameters");
17-
public ICommandObject GenerateCommandObject() => throw new NotImplementedException();
18-
public CommandValidationResult Validate(IServiceProvider serviceProvider) => throw new NotImplementedException();
22+
public ICommandOption FindOption(string optionName)
23+
{
24+
throw new NotImplementedException();
25+
}
26+
27+
public ICommandOption FindOptionByShortName(string optionShortName)
28+
{
29+
throw new NotImplementedException($"Oracle procedures does not define 'short name' for the parameters ('{ optionShortName }')");
30+
}
31+
32+
public ICommandObject GenerateCommandObject()
33+
{
34+
throw new NotImplementedException();
35+
}
36+
37+
public CommandValidationResult Validate(IServiceProvider serviceProvider)
38+
{
39+
throw new NotImplementedException();
40+
}
1941
}
2042
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.Common;
34
using MGR.CommandLineParser.Extensibility.Command;
45

56
namespace MGR.CommandLineParser.Command.OracleProcedure
67
{
78
internal class CommandType : ICommandType
89
{
910
private readonly IEnumerable<Parameter> _outParameters;
11+
private readonly DbConnection _dbConnection;
1012

11-
public CommandType(CommandMetadata commandMetadata, IEnumerable<CommandOptionMetadata> options, IEnumerable<Parameter> outParameters)
13+
public CommandType(CommandMetadata commandMetadata, IEnumerable<CommandOptionMetadata> options, IEnumerable<Parameter> outParameters, DbConnection dbConnection)
1214
{
1315
Metadata = commandMetadata;
1416
Options = options;
1517
_outParameters = outParameters;
18+
_dbConnection = dbConnection;
1619
}
1720
public ICommandMetadata Metadata { get; }
1821

1922
public IEnumerable<ICommandOptionMetadata> Options { get; }
2023

21-
public ICommandObjectBuilder CreateCommandObjectBuilder(IServiceProvider serviceProvider) => throw new NotImplementedException();
24+
public ICommandObjectBuilder CreateCommandObjectBuilder(IServiceProvider serviceProvider)
25+
{
26+
return new CommandObjectBuilder(Options, _outParameters, _dbConnection);
27+
}
2228
}
2329
}
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Data.Common;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using MGR.CommandLineParser.Extensibility.Command;
@@ -16,24 +17,35 @@ public CommandTypeProvider(OracleSystemContext dbContext)
1617
}
1718
public async Task<IEnumerable<ICommandType>> GetAllCommandTypes()
1819
{
19-
var procedures = _dbContext.Procedures.Include(procedure => procedure.Parameters).ToListAsync();
20-
return procedures.Select(MapProcedureToCommandType);
20+
var procedures = await _dbContext.Procedures.Include(procedure => procedure.Parameters).ToListAsync();
21+
return procedures.Select(procedure => MapProcedureToCommandType(procedure, _dbContext.Database.GetDbConnection()));
2122
}
22-
public ICommandType GetCommandType(string commandName)
23+
public async Task<ICommandType> GetCommandType(string commandName)
2324
{
24-
var procedure = _dbContext.Procedures.Include(procedure => procedure.Parameters).Where(procedure => procedure.Name == commandName).SingleOrDefault();
25+
var procedure = await _dbContext.Procedures.Include(procedure => procedure.Parameters).Where(procedure => procedure.Name == commandName).SingleOrDefaultAsync();
2526
if (procedure != null)
2627
{
27-
return MapProcedureToCommandType(procedure);
28+
return MapProcedureToCommandType(procedure, _dbContext.Database.GetDbConnection());
2829
}
2930
return null;
3031
}
3132

32-
private static CommandType MapProcedureToCommandType(Procedure procedure) => new CommandType(new CommandMetadata(procedure.Name),
33-
procedure.Parameters.Where(parameter => parameter.Direction.HasFlag(Direction.In)).Select(parameter => new CommandOptionMetadata(
34-
new OptionDisplayInfo(parameter.Name),
35-
!parameter.HasDefaultValue,
36-
parameter.DefaultValue)),
37-
procedure.Parameters.Where(parameter => parameter.Direction.HasFlag(Direction.Out));
33+
private static CommandType MapProcedureToCommandType(Procedure procedure, DbConnection dbConnection)
34+
{
35+
return new CommandType(
36+
new CommandMetadata(procedure.Name),
37+
procedure.Parameters
38+
.Where(parameter => parameter.Direction.HasFlag(Direction.In))
39+
.Select(parameter => new CommandOptionMetadata(
40+
new OptionDisplayInfo(parameter.Name),
41+
!parameter.HasDefaultValue,
42+
parameter.DefaultValue
43+
)
44+
),
45+
procedure.Parameters
46+
.Where(parameter => parameter.Direction.HasFlag(Direction.Out)),
47+
dbConnection
48+
);
49+
}
3850
}
3951
}

src/MGR.CommandLineParser.Command.OracleProcedure/OracleSystemContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ internal sealed class OracleSystemContext : DbContext
77
{
88
public DbSet<Procedure> Procedures { get; set; }
99

10+
public OracleSystemContext(DbContextOptions<OracleSystemContext> options)
11+
: base(options)
12+
{ }
13+
1014
protected override void OnModelCreating(ModelBuilder modelBuilder)
1115
{
12-
modelBuilder.ApplyConfiguration(new ProcedureMap())
16+
modelBuilder
17+
.ApplyConfiguration(new ProcedureMap())
1318
.ApplyConfiguration(new ParameterMap());
1419
base.OnModelCreating(modelBuilder);
1520
}

0 commit comments

Comments
 (0)