-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaReaderService.cs
More file actions
247 lines (208 loc) · 8.98 KB
/
Copy pathSchemaReaderService.cs
File metadata and controls
247 lines (208 loc) · 8.98 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using Microsoft.Data.SqlClient;
using SQL2Graph.Models;
namespace SQL2Graph.Services;
/// <summary>
/// Service for reading SQL Server database schema metadata
/// </summary>
public class SchemaReaderService
{
private readonly ILogger<SchemaReaderService> _logger;
public SchemaReaderService(ILogger<SchemaReaderService> logger)
{
_logger = logger;
}
/// <summary>
/// Reads the complete schema from a SQL Server database
/// </summary>
public async Task<SqlSchema> ReadSchemaAsync(string connectionString)
{
var schema = new SqlSchema();
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
schema.DatabaseName = connection.Database;
// Read tables and columns
schema.Tables = await ReadTablesAsync(connection);
// Read foreign keys
schema.ForeignKeys = await ReadForeignKeysAsync(connection);
// Mark FK columns
foreach (var fk in schema.ForeignKeys)
{
var table = schema.Tables.FirstOrDefault(t => t.FullName == fk.FromTable || t.TableName == fk.FromTable);
var column = table?.Columns.FirstOrDefault(c => c.ColumnName == fk.FromColumn);
if (column != null)
{
column.IsForeignKey = true;
}
}
_logger.LogInformation("Read schema: {TableCount} tables, {FkCount} foreign keys",
schema.Tables.Count, schema.ForeignKeys.Count);
return schema;
}
private async Task<List<TableInfo>> ReadTablesAsync(SqlConnection connection)
{
var tables = new List<TableInfo>();
const string query = @"
SELECT
t.TABLE_SCHEMA,
t.TABLE_NAME,
c.COLUMN_NAME,
c.DATA_TYPE,
c.IS_NULLABLE,
c.CHARACTER_MAXIMUM_LENGTH,
CASE WHEN pk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END AS IS_PRIMARY_KEY
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.COLUMNS c
ON t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME
LEFT JOIN (
SELECT ku.TABLE_SCHEMA, ku.TABLE_NAME, ku.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ku
ON tc.CONSTRAINT_NAME = ku.CONSTRAINT_NAME
WHERE tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
) pk ON c.TABLE_SCHEMA = pk.TABLE_SCHEMA
AND c.TABLE_NAME = pk.TABLE_NAME
AND c.COLUMN_NAME = pk.COLUMN_NAME
WHERE t.TABLE_TYPE = 'BASE TABLE'
ORDER BY t.TABLE_SCHEMA, t.TABLE_NAME, c.ORDINAL_POSITION";
await using var cmd = new SqlCommand(query, connection);
await using var reader = await cmd.ExecuteReaderAsync();
TableInfo? currentTable = null;
while (await reader.ReadAsync())
{
var schemaName = reader.GetString(0);
var tableName = reader.GetString(1);
var fullName = $"{schemaName}.{tableName}";
if (currentTable == null || currentTable.FullName != fullName)
{
currentTable = new TableInfo
{
SchemaName = schemaName,
TableName = tableName
};
tables.Add(currentTable);
}
var column = new ColumnInfo
{
ColumnName = reader.GetString(2),
DataType = reader.GetString(3),
IsNullable = reader.GetString(4) == "YES",
MaxLength = reader.IsDBNull(5) ? null : reader.GetInt32(5),
IsPrimaryKey = reader.GetInt32(6) == 1
};
if (column.IsPrimaryKey)
{
currentTable.PrimaryKeyColumn = column.ColumnName;
}
currentTable.Columns.Add(column);
}
return tables;
}
private async Task<List<ForeignKeyInfo>> ReadForeignKeysAsync(SqlConnection connection)
{
var foreignKeys = new List<ForeignKeyInfo>();
const string query = @"
SELECT
fk.name AS ConstraintName,
SCHEMA_NAME(tp.schema_id) + '.' + tp.name AS FromTable,
cp.name AS FromColumn,
SCHEMA_NAME(tr.schema_id) + '.' + tr.name AS ToTable,
cr.name AS ToColumn
FROM sys.foreign_keys fk
INNER JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tp ON fkc.parent_object_id = tp.object_id
INNER JOIN sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id
INNER JOIN sys.tables tr ON fkc.referenced_object_id = tr.object_id
INNER JOIN sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id
ORDER BY fk.name";
await using var cmd = new SqlCommand(query, connection);
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
foreignKeys.Add(new ForeignKeyInfo
{
ConstraintName = reader.GetString(0),
FromTable = reader.GetString(1),
FromColumn = reader.GetString(2),
ToTable = reader.GetString(3),
ToColumn = reader.GetString(4)
});
}
return foreignKeys;
}
/// <summary>
/// Tests the connection to the database
/// </summary>
public async Task<(bool Success, string Message)> TestConnectionAsync(string connectionString)
{
try
{
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
return (true, $"Connected to {connection.Database}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Connection test failed");
return (false, ex.Message);
}
}
/// <summary>
/// Reads sample data from tables using TABLESAMPLE for efficient random sampling
/// </summary>
/// <param name="connectionString">Database connection string</param>
/// <param name="tables">Tables to sample from</param>
/// <param name="sampleSize">Number of rows to sample per table (default 5)</param>
/// <returns>Dictionary of table name to list of sample rows</returns>
public async Task<Dictionary<string, List<Dictionary<string, object?>>>> ReadSampleDataAsync(
string connectionString,
List<TableInfo> tables,
int sampleSize = 5)
{
var samples = new Dictionary<string, List<Dictionary<string, object?>>>();
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
foreach (var table in tables)
{
try
{
// Use TABLESAMPLE for efficient random sampling
// TABLESAMPLE works at page level, so we request more rows than needed
// and use TOP to limit to exact count
var query = $@"
SELECT TOP {sampleSize} *
FROM [{table.SchemaName}].[{table.TableName}]
TABLESAMPLE (1000 ROWS)
ORDER BY NEWID()";
await using var cmd = new SqlCommand(query, connection);
cmd.CommandTimeout = 10; // Don't wait too long for large tables
await using var reader = await cmd.ExecuteReaderAsync();
var rows = new List<Dictionary<string, object?>>();
while (await reader.ReadAsync())
{
var row = new Dictionary<string, object?>();
for (int i = 0; i < reader.FieldCount; i++)
{
var columnName = reader.GetName(i);
var value = reader.IsDBNull(i) ? null : reader.GetValue(i);
// Truncate long strings for LLM context
if (value is string str && str.Length > 100)
{
value = str.Substring(0, 100) + "...";
}
row[columnName] = value;
}
rows.Add(row);
}
samples[table.FullName] = rows;
_logger.LogInformation("Sampled {Count} rows from {Table}", rows.Count, table.FullName);
}
catch (Exception ex)
{
// Log but continue - some tables might be empty or have issues
_logger.LogWarning(ex, "Failed to sample from {Table}", table.FullName);
samples[table.FullName] = [];
}
}
return samples;
}
}