Skip to content

Commit

Permalink
Merge pull request #286 from HicServices/feature/warningfixes
Browse files Browse the repository at this point in the history
Fix up various nullability warnings
  • Loading branch information
JFriel authored Jun 18, 2024
2 parents 11cfcd0 + 2a6c5cb commit af8e146
Show file tree
Hide file tree
Showing 19 changed files with 354 additions and 358 deletions.
2 changes: 1 addition & 1 deletion FAnsiSql/DatabaseOperationArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public int ExecuteNonQuery(DbCommand cmd)
/// </summary>
/// <param name="cmd"></param>
/// <exception cref="OperationCanceledException"></exception>
public object ExecuteScalar(DbCommand cmd)
public object? ExecuteScalar(DbCommand cmd)
{
return Execute(cmd, ()=>cmd.ExecuteScalarAsync(CancellationToken));
}
Expand Down
2 changes: 1 addition & 1 deletion FAnsiSql/Discovery/BulkCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected void ConvertStringTypesToHardTypes(DataTable dt)
{
//if the destination column is a problematic type
var dataType = discoveredColumn.DataType?.GetCSharpDataType();
if (!deciders.TryGetValue(dataType, out var decider)) continue;
if (dataType == null || !deciders.TryGetValue(dataType, out var decider)) continue;
//if it's already not a string then that's fine (hopefully it's a legit Type e.g. DateTime!)
if (dataColumn.DataType != typeof(string))
continue;
Expand Down
2 changes: 1 addition & 1 deletion FAnsiSql/Discovery/DatabaseColumnRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public sealed class DatabaseColumnRequest(string columnName, DatabaseTypeRequest
/// Applies only if the <see cref="TypeRequested"/> is string based. Setting this will override the default collation and specify
/// a specific collation. The value specified must be an installed collation supported by the DBMS
/// </summary>
public string Collation { get; set; }
public string? Collation { get; set; }

public DatabaseColumnRequest(string columnName, string explicitDbType, bool allowNulls = true) : this(columnName, (DatabaseTypeRequest?)null, allowNulls)
{
Expand Down
2 changes: 1 addition & 1 deletion FAnsiSql/Discovery/DiscoveredParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public sealed class DiscoveredParameter(string parameterName)
/// <summary>
/// The <see cref="DiscoveredDataType"/> the parameter is declared as e.g. varchar(10)
/// </summary>
public DiscoveredDataType DataType { get; set; }
public DiscoveredDataType? DataType { get; set; }
}
2 changes: 1 addition & 1 deletion FAnsiSql/Discovery/DiscoveredServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public DiscoveredServer(string? connectionString, DatabaseType databaseType)
/// <param name="usernameIfAny">Optional username to set in the connection string</param>
/// <param name="passwordIfAny">Optional password to set in the connection string</param>
/// <exception cref="ImplementationNotFoundException"></exception>
public DiscoveredServer(string server, string database, DatabaseType databaseType, string usernameIfAny, string passwordIfAny)
public DiscoveredServer(string server, string? database, DatabaseType databaseType, string usernameIfAny, string passwordIfAny)
{
Helper = ImplementationManager.GetImplementation(databaseType).GetServerHelper();

Expand Down
548 changes: 274 additions & 274 deletions Tests/FAnsiTests/CrossPlatformTests.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Tests/FAnsiTests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void CheckFiles()

var constr = element.Element("ConnectionString")?.Value;

TestConnectionStrings.Add(databaseType,constr);
TestConnectionStrings.Add(databaseType,constr ?? throw new InvalidOperationException());
}
}
catch (Exception exception)
Expand Down
8 changes: 4 additions & 4 deletions Tests/FAnsiTests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void Simple_Example()
Assert.Multiple(() =>
{
//Database types are compatible with all the data
Assert.That(table.DiscoverColumn("Date of Birth").DataType.SQLType, Is.EqualTo("datetime2"));
Assert.That(table.DiscoverColumn("Name").DataType.SQLType, Is.EqualTo("varchar(25)"));
Assert.That(table.DiscoverColumn("Date of Birth").DataType?.SQLType, Is.EqualTo("datetime2"));
Assert.That(table.DiscoverColumn("Name").DataType?.SQLType, Is.EqualTo("varchar(25)"));
//And the (string) data is now properly typed and sat in our DBMS
Assert.That(table.GetRowCount(), Is.EqualTo(2));
Expand Down Expand Up @@ -73,8 +73,8 @@ public void Example_TableCreation()

//Table has 2 rows in it
TestContext.WriteLine("Table {0} has {1} rows" ,table.GetFullyQualifiedName(), table.GetRowCount());
TestContext.WriteLine("Column Name is of type {0}", table.DiscoverColumn("Name").DataType.SQLType);
TestContext.WriteLine("Column DateOfBirth is of type {0}", table.DiscoverColumn("DateOfBirth").DataType.SQLType);
TestContext.WriteLine("Column Name is of type {0}", table.DiscoverColumn("Name").DataType?.SQLType);
TestContext.WriteLine("Column DateOfBirth is of type {0}", table.DiscoverColumn("DateOfBirth").DataType?.SQLType);

using var con = server.GetConnection();
con.Open();
Expand Down
14 changes: 7 additions & 7 deletions Tests/FAnsiTests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ public void Test_SetDoNotReType_RepeatCalls()
{
using var dt = new DataTable();

Assert.DoesNotThrow(()=>dt.SetDoNotReType(true));
Assert.DoesNotThrow(() => dt.SetDoNotReType(true));

dt.Columns.Add("FFF");
var dc = dt.Columns.Add("FFF");

Assert.That(dt.Columns["FFF"].GetDoNotReType(), Is.False);
Assert.That(dc.GetDoNotReType(), Is.False);

Assert.DoesNotThrow(()=>dt.SetDoNotReType(true));
Assert.DoesNotThrow(() => dt.SetDoNotReType(true));

Assert.That(dt.Columns["FFF"].GetDoNotReType());
Assert.That(dc.GetDoNotReType(), Is.True);

Assert.DoesNotThrow(()=>dt.SetDoNotReType(false));
Assert.DoesNotThrow(() => dt.SetDoNotReType(false));

Assert.That(dt.Columns["FFF"].GetDoNotReType(), Is.False);
Assert.That(dc.GetDoNotReType(), Is.False);
}
}
12 changes: 6 additions & 6 deletions Tests/FAnsiTests/ManagedConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public void Test_GetManagedConnection_OngoingTransaction_WithCommitRollback(Data
Assert.That(con.Connection.State, Is.EqualTo(ConnectionState.Open));

if(commit)
ongoingCon.ManagedTransaction.CommitAndCloseConnection();
ongoingCon.ManagedTransaction?.CommitAndCloseConnection();
else
ongoingCon.ManagedTransaction.AbandonAndCloseConnection();
ongoingCon.ManagedTransaction?.AbandonAndCloseConnection();

//that should really have closed it!
Assert.That(ongoingCon.Connection.State, Is.EqualTo(ConnectionState.Closed));
Expand All @@ -171,10 +171,10 @@ public void Test_ManagedTransaction_MultipleCancel(DatabaseType dbType)
//pretend that there is an ongoing transaction already
using (ongoingCon = db.Server.BeginNewTransactedConnection())
{
ongoingCon.ManagedTransaction.AbandonAndCloseConnection();
ongoingCon.ManagedTransaction.AbandonAndCloseConnection();
ongoingCon.ManagedTransaction.AbandonAndCloseConnection();
ongoingCon.ManagedTransaction.CommitAndCloseConnection();
ongoingCon.ManagedTransaction?.AbandonAndCloseConnection();
ongoingCon.ManagedTransaction?.AbandonAndCloseConnection();
ongoingCon.ManagedTransaction?.AbandonAndCloseConnection();
ongoingCon.ManagedTransaction?.CommitAndCloseConnection();
}
}

Expand Down
32 changes: 14 additions & 18 deletions Tests/FAnsiTests/Server/ServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,14 @@ public void ServerHelper_GetConnectionStringBuilder(DatabaseType type)

var server = new DiscoveredServer(builder);

Assert.That(server.Name, Is.EqualTo("loco"));

//Oracle does not persist database in connection string
if(type == DatabaseType.Oracle)
Assert.That(server.GetCurrentDatabase(), Is.Null);
else
Assert.That(server.GetCurrentDatabase().GetRuntimeName(), Is.EqualTo("bob"));

Assert.Multiple(() =>
{
Assert.That(server.ExplicitUsernameIfAny, Is.EqualTo("franko"));
Assert.That(server.ExplicitPasswordIfAny, Is.EqualTo("wacky"));
Assert.That(server.Name,Is.EqualTo("loco"));
//Oracle does not persist database in connection string
Assert.That(server.GetCurrentDatabase()?.GetRuntimeName(),type == DatabaseType.Oracle ? Is.Null : Is.EqualTo("bob"));
Assert.That(server.ExplicitUsernameIfAny,Is.EqualTo("franko"));
Assert.That(server.ExplicitPasswordIfAny,Is.EqualTo("wacky"));
});
}

Expand Down Expand Up @@ -123,7 +119,7 @@ public void ServerHelper_ChangeDatabase(DatabaseType type,bool expectCaps)
Assert.That(server.Name, Is.EqualTo("loco"));
//this failure is already exposed by Server_Helper_GetConnectionStringBuilder
Assert.That(server.GetCurrentDatabase().GetRuntimeName(), Is.EqualTo(expectCaps ? "BOB" : "bob"));
Assert.That(server.GetCurrentDatabase()?.GetRuntimeName(), Is.EqualTo(expectCaps ? "BOB" : "bob"));
Assert.That(server.ExplicitUsernameIfAny, Is.EqualTo("franko"));
Assert.That(server.ExplicitPasswordIfAny, Is.EqualTo("wacky"));
Expand All @@ -135,7 +131,7 @@ public void ServerHelper_ChangeDatabase(DatabaseType type,bool expectCaps)
{
Assert.That(server.Name, Is.EqualTo("loco"));
Assert.That(server.GetCurrentDatabase().GetRuntimeName(), Is.EqualTo(expectCaps ? "OMGGGG" : "omgggg"));
Assert.That(server.GetCurrentDatabase()?.GetRuntimeName(), Is.EqualTo(expectCaps ? "OMGGGG" : "omgggg"));
Assert.That(server.ExplicitUsernameIfAny, Is.EqualTo("franko"));
Assert.That(server.ExplicitPasswordIfAny, Is.EqualTo("wacky"));
});
Expand All @@ -160,7 +156,7 @@ public void ServerHelper_ChangeDatabase_AdHoc(DatabaseType type, bool useApiFirs
Assert.Multiple(() =>
{
Assert.That(server.Name, Is.EqualTo("loco"));
Assert.That(server.GetCurrentDatabase().GetRuntimeName(), Is.EqualTo("bob"));
Assert.That(server.GetCurrentDatabase()?.GetRuntimeName(), Is.EqualTo("bob"));
});

//Use API to change databases
Expand All @@ -170,7 +166,7 @@ public void ServerHelper_ChangeDatabase_AdHoc(DatabaseType type, bool useApiFirs
Assert.Multiple(() =>
{
Assert.That(server.Name, Is.EqualTo("loco"));
Assert.That(server.GetCurrentDatabase().GetRuntimeName(), Is.EqualTo("omgggg"));
Assert.That(server.GetCurrentDatabase()?.GetRuntimeName(), Is.EqualTo("omgggg"));
});
}

Expand All @@ -179,14 +175,14 @@ public void ServerHelper_ChangeDatabase_AdHoc(DatabaseType type, bool useApiFirs
Assert.Multiple(() =>
{
Assert.That(server.Name, Is.EqualTo("loco"));
Assert.That(server.GetCurrentDatabase().GetRuntimeName(), Is.EqualTo("Fisss"));
Assert.That(server.GetCurrentDatabase()?.GetRuntimeName(), Is.EqualTo("Fisss"));
});

server.Builder["Server"] = "Amagad";
Assert.Multiple(() =>
{
Assert.That(server.Name, Is.EqualTo("Amagad"));
Assert.That(server.GetCurrentDatabase().GetRuntimeName(), Is.EqualTo("Fisss"));
Assert.That(server.GetCurrentDatabase()?.GetRuntimeName(), Is.EqualTo("Fisss"));
});
}

Expand All @@ -198,7 +194,7 @@ public void ServerHelper_ChangeDatabase_AdHoc(DatabaseType type, bool useApiFirs
public void MoveData_BetweenServerTypes(DatabaseType from, DatabaseType to)
{
//Create some test data
var dtToMove = new DataTable();
using var dtToMove = new DataTable();
dtToMove.Columns.Add("MyCol");
dtToMove.Columns.Add("DateOfBirth");
dtToMove.Columns.Add("Sanity");
Expand All @@ -207,7 +203,7 @@ public void MoveData_BetweenServerTypes(DatabaseType from, DatabaseType to)
dtToMove.Rows.Add("Tony", null,"9.99");
dtToMove.Rows.Add("Jez", new DateTime(2001, 05, 01),"100.0");

dtToMove.PrimaryKey = [dtToMove.Columns["MyCol"]];
dtToMove.PrimaryKey = [dtToMove.Columns["MyCol"] ?? throw new InvalidOperationException()];

//Upload it to the first database
var fromDb = GetTestDatabase(from);
Expand Down
2 changes: 1 addition & 1 deletion Tests/FAnsiTests/Table/BadNamesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void BadNames_AlterType(DatabaseType dbType)

var (_, badColumnName, _) = GetBadNames(dbType);
var col = tbl.DiscoverColumn(badColumnName);
Assert.That(col.DataType.GetLengthIfString(), Is.EqualTo(100));
Assert.That(col.DataType?.GetLengthIfString(), Is.EqualTo(100));

var varcharType = tbl.Database.Server.GetQuerySyntaxHelper().TypeTranslater.GetSQLDBTypeForCSharpType(new DatabaseTypeRequest(typeof(string),10));

Expand Down
12 changes: 6 additions & 6 deletions Tests/FAnsiTests/Table/BulkInsertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void TestBulkInsert_Transaction(DatabaseType type)
Assert.That(tbl.GetRowCount(transaction.ManagedTransaction), Is.EqualTo(3));
}

transaction.ManagedTransaction.CommitAndCloseConnection();
transaction.ManagedTransaction?.CommitAndCloseConnection();
}

//Transaction was committed final row count should be 3
Expand Down Expand Up @@ -220,7 +220,7 @@ public void TestBulkInsert_AbandonTransaction(DatabaseType type)
Assert.That(tbl.GetRowCount(transaction.ManagedTransaction), Is.EqualTo(3));
}

transaction.ManagedTransaction.AbandonAndCloseConnection();
transaction.ManagedTransaction?.AbandonAndCloseConnection();
}

//We abandoned transaction so final rowcount should be 0
Expand Down Expand Up @@ -266,15 +266,15 @@ public void TestBulkInsert_AlterColumn_MidTransaction(DatabaseType type)
var col = tbl.DiscoverColumn("Name", transaction.ManagedTransaction);

//Make it bigger
col.DataType.Resize(100, transaction.ManagedTransaction);
col.DataType?.Resize(100, transaction.ManagedTransaction);

bulk.Upload(dt);

//inside transaction the count is 3
Assert.That(tbl.GetRowCount(transaction.ManagedTransaction), Is.EqualTo(3));
}

transaction.ManagedTransaction.CommitAndCloseConnection();
transaction.ManagedTransaction?.CommitAndCloseConnection();
}

//We abandoned transaction so final rowcount should be 0
Expand Down Expand Up @@ -501,9 +501,9 @@ public void UnmatchedColumnsBulkInsertTest_UsesDefaultValues_TwoLargeBatches_Pas
//give it 300 ms delay (simulates user cancelling not DbCommand.Timeout expiring)
using var cts = new CancellationTokenSource(300);
//GetDataTable should have been cancelled at the database level
Assert.Throws<OperationCanceledException>(()=>tbl.GetDataTable(new DatabaseOperationArgs(con.ManagedTransaction,50000,
Assert.Throws<OperationCanceledException>(()=>tbl.GetDataTable(new DatabaseOperationArgs(con.ManagedTransaction ?? throw new InvalidOperationException(),50000,
cts.Token)));
tbl.GetDataTable(new DatabaseOperationArgs(con.ManagedTransaction,50000, default));
tbl.GetDataTable(new DatabaseOperationArgs(con.ManagedTransaction ?? throw new InvalidOperationException(),50000, default));
}


Expand Down
Loading

0 comments on commit af8e146

Please sign in to comment.