Skip to content

Commit 6702ce3

Browse files
committed
init
1 parent 699c6dc commit 6702ce3

File tree

8 files changed

+233
-0
lines changed

8 files changed

+233
-0
lines changed

RestODb.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33617.297
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestODb", "RestODb\RestODb.csproj", "{6C460A87-064D-4E1C-A66E-F0274CE13E12}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{6C460A87-064D-4E1C-A66E-F0274CE13E12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6C460A87-064D-4E1C-A66E-F0274CE13E12}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6C460A87-064D-4E1C-A66E-F0274CE13E12}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6C460A87-064D-4E1C-A66E-F0274CE13E12}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {BB91A9A8-3A2D-48F7-84B1-0FA54B66C393}
24+
EndGlobalSection
25+
EndGlobal

RestODb/Core/DbProviders.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+

2+
public enum DbProviders
3+
{
4+
NpgSql, SqlServer
5+
}

RestODb/Core/SqlKataQueryFactory.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+

2+
using Microsoft.Data.SqlClient;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Options;
5+
using Npgsql;
6+
using SqlKata;
7+
using SqlKata.Compilers;
8+
using SqlKata.Execution;
9+
using System.Data;
10+
11+
12+
public abstract class SqlKataQueryFactory
13+
{
14+
private readonly IConfiguration _config;
15+
private readonly ILogger<SqlKataQueryFactory> _logger;
16+
private static readonly Action<SqlResult> _emptyLogger = (compiled) => { };
17+
18+
public SqlKataQueryFactory(IConfiguration config, ILogger<SqlKataQueryFactory> logger)
19+
{
20+
_config = config;
21+
_logger = logger;
22+
}
23+
24+
protected QueryFactory GetFactory(string connectionString, bool log = false)
25+
{
26+
IDbConnection connection = GetConnection(connectionString);
27+
Compiler compiler = GetCompiler();
28+
29+
return new QueryFactory(connection, compiler)
30+
{
31+
Logger = log ? compiled => _logger.LogDebug(compiled.ToString()) : _emptyLogger
32+
};
33+
}
34+
35+
public Query Create(string tableName, bool log = false)
36+
{
37+
QueryFactory factory = GetFactory(_config.GetConnectionString("TestDB"), log);
38+
39+
return factory.Query(tableName);
40+
}
41+
42+
protected abstract IDbConnection GetConnection(string connectionString);
43+
44+
protected abstract Compiler GetCompiler();
45+
}
46+
47+
public class NpgSqlQueryFactory : SqlKataQueryFactory
48+
{
49+
public NpgSqlQueryFactory(IConfiguration config, ILogger<SqlKataQueryFactory> logger) : base(config, logger)
50+
{
51+
}
52+
53+
protected override Compiler GetCompiler() => new PostgresCompiler();
54+
55+
protected override IDbConnection GetConnection(string connectionString)
56+
=> new NpgsqlConnection(connectionString);
57+
}
58+
59+
public class SqlServerQueryFactory : SqlKataQueryFactory
60+
{
61+
public SqlServerQueryFactory(IConfiguration config, ILogger<SqlKataQueryFactory> logger) : base(config, logger)
62+
{
63+
}
64+
65+
protected override Compiler GetCompiler() => new SqlServerCompiler();
66+
67+
protected override IDbConnection GetConnection(string connectionString)
68+
=> new SqlConnection(connectionString);
69+
}

RestODb/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using SqlKata;
3+
using SqlKata.Execution;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
builder.Configuration.AddEnvironmentVariables();
7+
builder.Configuration.AddJsonFile("appSettings.json");
8+
if (builder.Environment.IsDevelopment()) builder.Configuration.AddJsonFile("appSettings.Development.json");
9+
10+
bool swaggerEnabled = builder.Configuration.GetValue<bool>("enableSwagger");
11+
DbProviders provider = builder.Configuration.GetValue<DbProviders>("provider");
12+
13+
if (provider == DbProviders.NpgSql) builder.Services.AddSingleton<SqlKataQueryFactory, NpgSqlQueryFactory>();
14+
else if (provider == DbProviders.SqlServer) builder.Services.AddSingleton<SqlKataQueryFactory, SqlServerQueryFactory>();
15+
16+
if (swaggerEnabled) builder.Services.AddEndpointsApiExplorer().AddOpenApiDocument(c => { c.Title = "toto"; c.Version = "v1"; });
17+
18+
builder.Services.AddLogging();
19+
var app = builder.Build();
20+
21+
//récupération des routes possibles
22+
var tables = await GetTablesListAsync(provider, app.Services.GetService<SqlKataQueryFactory>());
23+
24+
var limitTo = builder.Configuration.GetSection("limitTo").Get<string[]>();
25+
if (limitTo?.Any() == true) tables = tables.Where(t => limitTo.Contains(t));
26+
27+
foreach (var table in tables)
28+
{
29+
app.MapGet($"/{table}", ([FromServices] SqlKataQueryFactory factory) =>
30+
{
31+
Query query = factory.Create(table);
32+
33+
return query.GetAsync();
34+
});
35+
}
36+
37+
if (swaggerEnabled)
38+
{
39+
app.UseOpenApi();
40+
app.UseSwaggerUi3();
41+
}
42+
43+
await app.RunAsync();
44+
45+
46+
static async Task<IEnumerable<string>> GetTablesListAsync(DbProviders provider, SqlKataQueryFactory factory)
47+
{
48+
if (provider == DbProviders.NpgSql)
49+
return await factory.Create("pg_catalog.pg_tables")
50+
.WhereNotIn("schemaname", new string[] { "pg_catalog", "information_schema" })
51+
.Select("tablename")
52+
.GetAsync<string>();
53+
else return await factory.Create("INFORMATION_SCHEMA.TABLES")
54+
.WhereNot("TABLE_SCHEMA", "sys")
55+
.Select("TABLE_NAME")
56+
.GetAsync<string>();
57+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:55611",
7+
"sslPort": 44339
8+
}
9+
},
10+
"profiles": {
11+
"http": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
14+
"launchBrowser": true,
15+
"applicationUrl": "http://localhost:5002/swagger",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"https": {
21+
"commandName": "Project",
22+
"dotnetRunMessages": true,
23+
"launchBrowser": true,
24+
"launchUrl": "https://localhost:7205/swagger",
25+
"applicationUrl": "https://localhost:7205;http://localhost:5002",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}

RestODb/RestODb.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
11+
<PackageReference Include="Npgsql" Version="7.0.4" />
12+
<PackageReference Include="NSwag.AspNetCore" Version="13.18.3" />
13+
<PackageReference Include="SqlKata.Execution" Version="2.4.0" />
14+
</ItemGroup>
15+
16+
</Project>

RestODb/appsettings.Development.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"ConnectionStrings": {
9+
"TestDB": "Host=localhost;Database=hvs-simmotion;Username=postgres;Password=postgres"
10+
},
11+
"limitTo": [ "Tags", "Records" ],
12+
"provider": "NpgSql",
13+
"enableSwagger": true
14+
}

RestODb/appsettings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)