Skip to content

Commit

Permalink
enable reading AE date as DateOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEJ committed Dec 18, 2023
1 parent 5794ab3 commit e5ed8d8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2863,11 +2863,11 @@ private T GetFieldValueFromSqlBufferInternal<T>(SqlBuffer data, _SqlMetaData met
return (T)(object)data.DateTime;
}
#if NET6_0_OR_GREATER
else if (typeof(T) == typeof(DateOnly) && dataType == typeof(DateTime) && _typeSystem > SqlConnectionString.TypeSystem.SQLServer2005 && metaData.Is2008DateTimeType)
else if (typeof(T) == typeof(DateOnly) && dataType == typeof(DateTime) && _typeSystem > SqlConnectionString.TypeSystem.SQLServer2005)
{
return (T)(object)data.DateOnly;
}
else if (typeof(T) == typeof(TimeOnly) && dataType == typeof(TimeOnly) && _typeSystem > SqlConnectionString.TypeSystem.SQLServer2005 && metaData.Is2008DateTimeType)
else if (typeof(T) == typeof(TimeOnly) && dataType == typeof(TimeOnly) && _typeSystem > SqlConnectionString.TypeSystem.SQLServer2005)
{
return (T)(object)data.TimeOnly;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3160,13 +3160,20 @@ public Customer(int id, string firstName, string lastName)
Id = id;
FirstName = firstName;
LastName = lastName;
#if NET6_0_OR_GREATER
DateOfBirth = new DateOnly(1990, 1, 31);
#endif
}

public int Id { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

#if NET6_0_OR_GREATER
public DateOnly DateOfBirth { get; set; }
#endif
}

internal class TestAsyncCallBackStateObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public IEnumerator<object[]> GetEnumerator()
yield return new object[] { connStrAE, @"select CustomerId, FirstName, LastName from [{0}] ", 3, new string[] { @"int", @"string", @"string" } };
yield return new object[] { connStrAE, @"select CustomerId, FirstName from [{0}] ", 2, new string[] { @"int", @"string" } };
yield return new object[] { connStrAE, @"select LastName from [{0}] ", 1, new string[] { @"string" }};
#if NET6_0_OR_GREATER
yield return new object[] { connStrAE, @"select DateOfBirth from [{0}] ", 1, new string[] { @"DateOnly" } };
#endif
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ class DatabaseHelper
internal static void InsertCustomerData(SqlConnection sqlConnection, SqlTransaction transaction, string tableName, Customer customer)
{
using SqlCommand sqlCommand = new(
#if NET6_0_OR_GREATER
$"INSERT INTO [{tableName}] (CustomerId, FirstName, LastName, DateOfBirth) VALUES (@CustomerId, @FirstName, @LastName, @DateOfBirth);",
#else
$"INSERT INTO [{tableName}] (CustomerId, FirstName, LastName) VALUES (@CustomerId, @FirstName, @LastName);",
#endif
connection: sqlConnection,
transaction: transaction,
columnEncryptionSetting: SqlCommandColumnEncryptionSetting.Enabled);

sqlCommand.Parameters.AddWithValue(@"CustomerId", customer.Id);
sqlCommand.Parameters.AddWithValue(@"FirstName", customer.FirstName);
sqlCommand.Parameters.AddWithValue(@"LastName", customer.LastName);
#if NET6_0_OR_GREATER
sqlCommand.Parameters.AddWithValue(@"DateOfBirth", customer.DateOfBirth);
#endif
sqlCommand.ExecuteNonQuery();
}

Expand Down Expand Up @@ -156,6 +163,11 @@ public static void CompareResults(SqlDataReader sqlDataReader, string[] paramete
Assert.True(sqlDataReader.GetInt32(columnsRead) == 45, "FAILED: read int value does not match.");
break;

#if NET6_0_OR_GREATER
case "DateOnly":
Assert.True(sqlDataReader.GetFieldValue<DateOnly>(columnsRead) == new DateOnly(1990, 1, 31), "FAILED: read DateOnly value does not match.");
break;
#endif
default:
Assert.Fail("FAILED: unexpected data type.");
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public override void Create(SqlConnection sqlConnection)
(
[CustomerId] [int] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [{columnEncryptionKey1.Name}], ENCRYPTION_TYPE = {encryptionType}, ALGORITHM = '{ColumnEncryptionAlgorithmName}'),
[FirstName] [nvarchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [{columnEncryptionKey2.Name}], ENCRYPTION_TYPE = DETERMINISTIC, ALGORITHM = '{ColumnEncryptionAlgorithmName}'),
[LastName] [nvarchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [{columnEncryptionKey2.Name}], ENCRYPTION_TYPE = DETERMINISTIC, ALGORITHM = '{ColumnEncryptionAlgorithmName}')
)";
[LastName] [nvarchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [{columnEncryptionKey2.Name}], ENCRYPTION_TYPE = DETERMINISTIC, ALGORITHM = '{ColumnEncryptionAlgorithmName}')";

#if NET6_0_OR_GREATER
sql = sql + $",[DateOfBirth] [date] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [{columnEncryptionKey1.Name}], ENCRYPTION_TYPE = RANDOMIZED, ALGORITHM = '{ColumnEncryptionAlgorithmName}')";
#endif
using (SqlCommand command = sqlConnection.CreateCommand())
{
command.CommandText = sql;
command.CommandText = sql + ")";
command.ExecuteNonQuery();
}
}
Expand Down

0 comments on commit e5ed8d8

Please sign in to comment.