-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathUtilities.cs
More file actions
123 lines (113 loc) · 4.46 KB
/
Copy pathUtilities.cs
File metadata and controls
123 lines (113 loc) · 4.46 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text.Json;
using Azure.DataApiBuilder.Config.ObjectModel;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using Npgsql;
namespace Azure.DataApiBuilder.Service.HealthCheck
{
public static class Utilities
{
public const string JSON_CONTENT_TYPE = "application/json";
public static string GetDatSourceQuery(DatabaseType dbType)
{
switch (dbType)
{
case DatabaseType.MySQL:
return "SELECT 1";
case DatabaseType.PostgreSQL:
return "SELECT 1";
case DatabaseType.MSSQL:
return "SELECT 1";
case DatabaseType.CosmosDB_NoSQL:
return "SELECT VALUE 1";
case DatabaseType.CosmosDB_PostgreSQL:
return "SELECT VALUE 1";
case DatabaseType.DWSQL:
return "SELECT 1";
default:
return string.Empty;
}
}
public static DbProviderFactory GetDbProviderFactory(DatabaseType dbType)
{
switch (dbType)
{
case DatabaseType.PostgreSQL:
return NpgsqlFactory.Instance;
case DatabaseType.MSSQL:
case DatabaseType.DWSQL:
return SqlClientFactory.Instance;
default:
throw new NotSupportedException($"Database type '{dbType}' is not supported.");
}
}
public static string CreateHttpGraphQLQuery(string entityName, List<string> columnNames, int first)
{
var payload = new
{
//{"query":"{publishers(first:4) {items {id name} }}"}
query = $"{{{entityName} (first: {first}) {{items {{ {string.Join(" ", columnNames)} }}}}}}"
};
// Serialize the payload to a JSON string
string jsonPayload = JsonSerializer.Serialize(payload);
return jsonPayload;
}
public static string CreateHttpRestQuery(string entityName, int first)
{
// Create the payload for the REST HTTP request.
// "EntityName?$first=4"
return $"/{entityName}?$first={first}";
}
public static string CreateHttpMcpQuery()
{
// Create a minimal MCP request (initialize) as a valid JSON-RPC request.
// 'initialize' is used because other methods (e.g. 'tools/list') require an active
// session in the MCP Streamable HTTP transport.
var payload = new
{
jsonrpc = "2.0",
id = 1,
method = "initialize",
@params = new
{
protocolVersion = "2025-03-26",
capabilities = new { },
clientInfo = new { name = "dab-health-check", version = "1.0.0" }
}
};
return JsonSerializer.Serialize(payload);
}
public static string NormalizeConnectionString(string connectionString, DatabaseType dbType, ILogger? logger = null)
{
try
{
switch (dbType)
{
case DatabaseType.PostgreSQL:
return new NpgsqlConnectionStringBuilder(connectionString).ToString();
case DatabaseType.MySQL:
return new MySqlConnectionStringBuilder(connectionString).ToString();
case DatabaseType.MSSQL:
case DatabaseType.DWSQL:
return new SqlConnectionStringBuilder(connectionString).ToString();
default:
return connectionString;
}
}
catch (Exception ex)
{
// Log the exception if a logger is provided
logger?.LogWarning(ex, "Failed to parse connection string for database type {DatabaseType}. Returning original connection string.", dbType);
// If the connection string cannot be parsed by the builder,
// return the original string to avoid failing the health check.
return connectionString;
}
}
}
}