Skip to content

Commit

Permalink
Add option for SQL Server [#17]
Browse files Browse the repository at this point in the history
  • Loading branch information
ccheetham committed May 27, 2021
1 parent 7192df8 commit 9056d71
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"longName": "no-restore",
"shortName": ""
},
"SqlServerConnector": {
"longName": "sqlserver",
"shortName": ""
},
"SteeltoeVersion": {
"longName": "steeltoe"
}
Expand Down
8 changes: 7 additions & 1 deletion src/DotNetNew.WebApi/CSharp/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@
"description": "Skip the automatic restore of the project on create.",
"defaultValue": "false"
},
"SqlServerConnector": {
"type": "parameter",
"datatype": "bool",
"description": "Add access to Microsoft SQL Server.",
"defaultValue": "false"
},
"Steeltoe2": {
"type": "generated",
"generator": "regexMatch",
Expand Down Expand Up @@ -257,7 +263,7 @@
},
"SteeltoeConnector": {
"type": "computed",
"value": "RedisConnector || RabbitMqConnector"
"value": "RedisConnector || RabbitMqConnector || SqlServerConnector"
},
"SteeltoeVersion": {
"description": "Set the Steeltoe version for the project.",
Expand Down
3 changes: 2 additions & 1 deletion src/DotNetNew.WebApi/CSharp/Company.WebApplication1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(NetCoreApp21Version)" />
</ItemGroup>
<!--#endif -->
<!--#if (CloudConfig || CloudFoundryHosting || DynamicLogger || Eureka || Hystrix || ManagementEndpoints || PlaceholderConfiguration || RabbitMqConnector || RandomValueConfiguration || RedisConnector) -->
<!--#if (CloudConfig || CloudFoundryHosting || DynamicLogger || Eureka || Hystrix || ManagementEndpoints || PlaceholderConfiguration || RabbitMqConnector || RandomValueConfiguration || RedisConnector || SqlServerConnector) -->

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Condition="'$(RedisConnector)' == 'True'" Version="$(RedisVersion)" />
Expand All @@ -44,6 +44,7 @@
<PackageReference Include="Steeltoe.Extensions.Logging.DynamicLogger" Condition="'$(DynamicLogger)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Management.CloudFoundryCore" Condition="'$(Steeltoe2ManagementEndpoints)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Management.EndpointCore" Condition="'$(Steeltoe3ManagementEndpoints)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="System.Data.SqlClient" Condition="'$(SqlServerConnector)' == 'True'" Version="4.8.*" />
</ItemGroup>
<!--#endif -->
<!--#if (AzureSpringCloudHosting && !FrameworkNetCoreApp21) -->
Expand Down
24 changes: 24 additions & 0 deletions src/DotNetNew.WebApi/CSharp/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#if (!RabbitMqConnector)
using System.Collections.Generic;
#endif
#if (SqlServerConnector)
using System.Data;
using System.Data.SqlClient;
#endif
#if (RabbitMqConnector)
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -71,6 +75,14 @@ public ValuesController(IDistributedCache cache)
_cache = cache;
}
#endif
#if (SqlServerConnector)
private readonly SqlConnection _dbConnection;

public ValuesController([FromServices] SqlConnection dbConnection)
{
_dbConnection = dbConnection;
}
#endif

[HttpGet]
#if (RabbitMqConnector)
Expand Down Expand Up @@ -144,6 +156,18 @@ public ActionResult<IEnumerable<string>> Get()
var val3 = _configuration["random:string"];

return new[] { val1, val2, val3 };
#elif (SqlServerConnector)
List<string> tables = new List<string>();
_dbConnection.Open();
DataTable dt = _dbConnection.GetSchema("Tables");
_dbConnection.Close();
foreach (DataRow row in dt.Rows)
{
string tablename = (string)row[2];
tables.Add(tablename);
}

return tables;
#else
return new[] { "value" };
#endif
Expand Down
1 change: 1 addition & 0 deletions src/DotNetNew.WebApi/CSharp/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<RabbitMqVersion>5.1.*</RabbitMqVersion>
<RedisConnector>false</RedisConnector>
<RedisVersion>3.1.*</RedisVersion>
<SqlServerConnector>false</SqlServerConnector>
<Steeltoe2>false</Steeltoe2>
</PropertyGroup>

Expand Down
9 changes: 9 additions & 0 deletions src/DotNetNew.WebApi/CSharp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
#if (RedisConnector && !Steeltoe2)
using Steeltoe.Connector.Redis;
#endif
#if (SqlServerConnector && Steeltoe2)
using Steeltoe.CloudFoundry.Connector.SqlServer;
#endif
#if (SqlServerConnector && !Steeltoe2)
using Steeltoe.Connector.SqlServer;
#endif
#if (Eureka)
using Steeltoe.Discovery.Client;
#endif
Expand Down Expand Up @@ -67,6 +73,9 @@ public void ConfigureServices(IServiceCollection services)
#if (RedisConnector)
services.AddDistributedRedisCache(Configuration);
#endif
#if (SqlServerConnector)
services.AddSqlServerConnection(Configuration);
#endif
#if (Hystrix)
services.AddHystrixCommand<HelloHystrixCommand>("MyCircuitBreakers", Configuration);
services.AddHystrixMetricsStream(Configuration);
Expand Down
91 changes: 91 additions & 0 deletions test/DotNetNew.SteeltoeWebApi.Test/SqlServerOptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Collections.Generic;
using FluentAssertions;
using Steeltoe.DotNetNew.Test.Utilities.Assertions;
using Xunit.Abstractions;

namespace Steeltoe.DotNetNew.SteeltoeWebApi.Test
{
public class SqlServerOptionTest : OptionTest
{
public SqlServerOptionTest(ITestOutputHelper logger) : base("sqlserver", logger)
{
}

protected override void AssertHelp(string help)
{
base.AssertHelp(help);
help.Should().ContainSnippet(@"
--sqlserver Add access to Microsoft SQL Server.
bool - Optional
Default: false
");
}

protected override void AssertCsproj(Steeltoe steeltoe, Framework framework,
Dictionary<string, string> properties, string[] packageRefs)
{
base.AssertCsproj(steeltoe, framework, properties, packageRefs);
var expectedPackageRefs = new List<string>
{
"System.Data.SqlClient",
};
switch (steeltoe)
{
case Steeltoe.Steeltoe2:
expectedPackageRefs.Add("Steeltoe.CloudFoundry.ConnectorCore");
break;
default:
expectedPackageRefs.Add("Steeltoe.Connector.ConnectorCore");
break;
}
packageRefs.Should().Contain(expectedPackageRefs);
}

protected override void AssertStartupCs(Steeltoe steeltoe, Framework framework, string source)
{
base.AssertStartupCs(steeltoe, framework, source);
switch (steeltoe)
{
case Steeltoe.Steeltoe2:
source.Should().ContainSnippet("using Steeltoe.CloudFoundry.Connector.SqlServer;");
break;
default:
source.Should().ContainSnippet("using Steeltoe.Connector.SqlServer;");
break;
}

source.Should().ContainSnippet("services.AddSqlServerConnection(Configuration);");
}

protected override void AssertValuesControllerCs(Steeltoe steeltoe, Framework framework, string source)
{
base.AssertValuesControllerCs(steeltoe, framework, source);
source.Should().ContainSnippet("using System.Data;");
source.Should().ContainSnippet("using System.Data.SqlClient;");
source.Should().ContainSnippet("private readonly SqlConnection _dbConnection;");
source.Should().ContainSnippet(@"
private readonly SqlConnection _dbConnection;
public ValuesController([FromServices] SqlConnection dbConnection)
{
_dbConnection = dbConnection;
}
");
source.Should().ContainSnippet(@"
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
List<string> tables = new List<string>();
_dbConnection.Open();
DataTable dt = _dbConnection.GetSchema(""Tables"");
_dbConnection.Close();
foreach (DataRow row in dt.Rows)
{
string tablename = (string)row[2];
tables.Add(tablename);
}
return tables;
}
");
}
}
}

0 comments on commit 9056d71

Please sign in to comment.