Skip to content

Commit 8c7777f

Browse files
committed
Tighten up nullability
1 parent 674894b commit 8c7777f

File tree

12 files changed

+67
-36
lines changed

12 files changed

+67
-36
lines changed

MssqlMcp/MssqlMcp.Tests/UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22

3-
using Mssql.McpServer;
43
using Microsoft.Extensions.Logging;
54
using Moq;
5+
using Mssql.McpServer;
66

77
namespace MssqlMcp.Tests
88
{

MssqlMcp/MssqlMcp/DbOperationResult.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22

33
namespace Mssql.McpServer;
44

5-
public class DbOperationResult
5+
/// <summary>
6+
/// Represents the result of a database operation, including success status, error message, number of rows affected, and any returned data.
7+
/// </summary>
8+
public class DbOperationResult(bool success, string? error = null, int? rowsAffected = null, object? data = null)
69
{
7-
public bool Success { get; set; }
8-
public string? Error { get; set; }
9-
public int? RowsAffected { get; set; }
10-
public object? Data { get; set; }
10+
/// <summary>
11+
/// Gets a value indicating whether the database operation was successful.
12+
/// </summary>
13+
public bool Success { get; } = success;
14+
15+
/// <summary>
16+
/// Gets the error message if the operation failed; otherwise, null.
17+
/// </summary>
18+
public string? Error { get; } = error;
19+
20+
/// <summary>
21+
/// Gets the number of rows affected by the operation, if applicable.
22+
/// </summary>
23+
public int? RowsAffected { get; } = rowsAffected;
24+
25+
/// <summary>
26+
/// Gets any data returned by the operation, such as query results.
27+
/// </summary>
28+
public object? Data { get; } = data;
1129
}

MssqlMcp/MssqlMcp/ISqlConnectionFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Mssql.McpServer;
66

7+
/// <summary>
8+
/// Defines a factory interface for creating SQL database connections.
9+
/// </summary>
710
public interface ISqlConnectionFactory
811
{
912
Task<SqlConnection> GetOpenConnectionAsync();

MssqlMcp/MssqlMcp/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ private static async Task Main(string[] args)
6262
{
6363
Console.Error.WriteLine($"Unhandled exception: {ex}");
6464
}
65-
// Optionally, set a non-zero exit code
65+
66+
// Set a non-zero exit code
6667
Environment.ExitCode = 1;
6768
}
6869
}

MssqlMcp/MssqlMcp/Tools/CreateTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22

33
using System.ComponentModel;
4-
using ModelContextProtocol.Server;
54
using Microsoft.Extensions.Logging;
5+
using ModelContextProtocol.Server;
66

77
namespace Mssql.McpServer;
88

@@ -19,13 +19,13 @@ public async Task<DbOperationResult> CreateTable(
1919
{
2020
using var cmd = new Microsoft.Data.SqlClient.SqlCommand(sql, conn);
2121
_ = await cmd.ExecuteNonQueryAsync();
22-
return new DbOperationResult { Success = true };
22+
return new DbOperationResult(success: true);
2323
}
2424
}
2525
catch (Exception ex)
2626
{
2727
_logger.LogError(ex, "CreateTable failed: {Message}", ex.Message);
28-
return new DbOperationResult { Success = false, Error = ex.Message };
28+
return new DbOperationResult(success: false, error: ex.Message);
2929
}
3030
}
3131
}

MssqlMcp/MssqlMcp/Tools/DescribeTable.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
using System.ComponentModel;
44
using Microsoft.Data.SqlClient;
5-
using ModelContextProtocol.Server;
65
using Microsoft.Extensions.Logging;
6+
using ModelContextProtocol.Server;
77

88
namespace Mssql.McpServer;
99

@@ -70,7 +70,7 @@ FROM sys.key_constraints kc
7070
}
7171
else
7272
{
73-
return new DbOperationResult { Success = false, Error = $"Table '{name}' not found." };
73+
return new DbOperationResult(success: false, error: $"Table '{name}' not found.");
7474
}
7575
}
7676
// Columns
@@ -128,13 +128,13 @@ FROM sys.key_constraints kc
128128
}
129129
result["constraints"] = constraints;
130130
}
131-
return new DbOperationResult { Success = true, Data = result };
131+
return new DbOperationResult(success: true, data: result);
132132
}
133133
}
134134
catch (Exception ex)
135135
{
136136
_logger.LogError(ex, "DescribeTable failed: {Message}", ex.Message);
137-
return new DbOperationResult { Success = false, Error = ex.Message };
137+
return new DbOperationResult(success: false, error: ex.Message);
138138
}
139139
}
140140
}

MssqlMcp/MssqlMcp/Tools/DropTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22

33
using System.ComponentModel;
4-
using ModelContextProtocol.Server;
54
using Microsoft.Extensions.Logging;
5+
using ModelContextProtocol.Server;
66

77
namespace Mssql.McpServer;
88

@@ -19,13 +19,13 @@ public async Task<DbOperationResult> DropTable(
1919
{
2020
using var cmd = new Microsoft.Data.SqlClient.SqlCommand(sql, conn);
2121
_ = await cmd.ExecuteNonQueryAsync();
22-
return new DbOperationResult { Success = true };
22+
return new DbOperationResult(success: true);
2323
}
2424
}
2525
catch (Exception ex)
2626
{
2727
_logger.LogError(ex, "DropTable failed: {Message}", ex.Message);
28-
return new DbOperationResult { Success = false, Error = ex.Message };
28+
return new DbOperationResult(success: false, error: ex.Message);
2929
}
3030
}
3131
}

MssqlMcp/MssqlMcp/Tools/InsertData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22

33
using System.ComponentModel;
4-
using ModelContextProtocol.Server;
54
using Microsoft.Extensions.Logging;
5+
using ModelContextProtocol.Server;
66

77
namespace Mssql.McpServer;
88

@@ -19,13 +19,13 @@ public async Task<DbOperationResult> InsertData(
1919
{
2020
using var cmd = new Microsoft.Data.SqlClient.SqlCommand(sql, conn);
2121
var rows = await cmd.ExecuteNonQueryAsync();
22-
return new DbOperationResult { Success = true, RowsAffected = rows };
22+
return new DbOperationResult(success: true, rowsAffected: rows);
2323
}
2424
}
2525
catch (Exception ex)
2626
{
2727
_logger.LogError(ex, "InsertData failed: {Message}", ex.Message);
28-
return new DbOperationResult { Success = false, Error = ex.Message };
28+
return new DbOperationResult(success: false, error: ex.Message);
2929
}
3030
}
3131
}

MssqlMcp/MssqlMcp/Tools/ListTables.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22

33
using System.ComponentModel;
4-
using System.Data;
54
using Microsoft.Data.SqlClient;
6-
using ModelContextProtocol.Server;
75
using Microsoft.Extensions.Logging;
6+
using ModelContextProtocol.Server;
87

98
namespace Mssql.McpServer;
109

@@ -21,16 +20,19 @@ public async Task<DbOperationResult> ListTables()
2120
using (conn)
2221
{
2322
using var cmd = new SqlCommand(ListTablesQuery, conn);
23+
var tables = new List<string>();
2424
using var reader = await cmd.ExecuteReaderAsync();
25-
var table = new DataTable();
26-
table.Load(reader);
27-
return new DbOperationResult { Success = true, Data = DataTableToList(table) };
25+
while (await reader.ReadAsync())
26+
{
27+
tables.Add($"{reader.GetString(0)}.{reader.GetString(1)}");
28+
}
29+
return new DbOperationResult(success: true, data: tables);
2830
}
2931
}
3032
catch (Exception ex)
3133
{
3234
_logger.LogError(ex, "ListTables failed: {Message}", ex.Message);
33-
return new DbOperationResult { Success = false, Error = ex.Message };
35+
return new DbOperationResult(success: false, error: ex.Message);
3436
}
3537
}
3638
}

MssqlMcp/MssqlMcp/Tools/ReadData.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22

33
using System.ComponentModel;
4-
using System.Data;
54
using Microsoft.Data.SqlClient;
6-
using ModelContextProtocol.Server;
75
using Microsoft.Extensions.Logging;
6+
using ModelContextProtocol.Server;
87

98
namespace Mssql.McpServer;
109
public partial class Tools
@@ -20,15 +19,23 @@ public async Task<DbOperationResult> ReadData(
2019
{
2120
using var cmd = new SqlCommand(sql, conn);
2221
using var reader = await cmd.ExecuteReaderAsync();
23-
var table = new DataTable();
24-
table.Load(reader);
25-
return new DbOperationResult { Success = true, Data = DataTableToList(table) };
22+
var results = new List<Dictionary<string, object?>>();
23+
while (await reader.ReadAsync())
24+
{
25+
var row = new Dictionary<string, object?>();
26+
for (int i = 0; i < reader.FieldCount; i++)
27+
{
28+
row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i);
29+
}
30+
results.Add(row);
31+
}
32+
return new DbOperationResult(success: true, data: results);
2633
}
2734
}
2835
catch (Exception ex)
2936
{
3037
_logger.LogError(ex, "ReadData failed: {Message}", ex.Message);
31-
return new DbOperationResult { Success = false, Error = ex.Message };
38+
return new DbOperationResult(success: false, error: ex.Message);
3239
}
3340
}
3441
}

0 commit comments

Comments
 (0)