Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target .Net 8, enable AOT warnings #227

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions FAnsiSql/Discovery/BulkCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,24 @@ protected void ConvertStringTypesToHardTypes(DataTable dt)
var deciders = factory.Dictionary;

//for each column in the destination
foreach(var kvp in dict)
foreach(var (dataColumn, discoveredColumn) in dict)
{
//if the destination column is a problematic type
var dataType = kvp.Value.DataType.GetCSharpDataType();
var dataType = discoveredColumn.DataType.GetCSharpDataType();
if (!deciders.TryGetValue(dataType, out var decider)) continue;
//if it's already not a string then that's fine (hopefully its a legit Type e.g. DateTime!)
if(kvp.Key.DataType != typeof(string))
//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;

//create a new column hard typed to DateTime
var newColumn = dt.Columns.Add($"{kvp.Key.ColumnName}_{Guid.NewGuid()}",dataType);
var newColumn = dt.Columns.Add($"{dataColumn.ColumnName}_{Guid.NewGuid()}",dataType);

//if it's a DateTime decider then guess DateTime culture based on values in the table
if(decider is DateTimeTypeDecider)
{
//also use this one in case the user has set up explicit stuff on it e.g. Culture/Settings
decider = DateTimeDecider;
DateTimeDecider.GuessDateFormat(dt.Rows.Cast<DataRow>().Take(500).Select(r=>r[kvp.Key] as string));
DateTimeDecider.GuessDateFormat(dt.Rows.Cast<DataRow>().Take(500).Select(r=>r[dataColumn] as string));
}


Expand All @@ -134,27 +134,27 @@ protected void ConvertStringTypesToHardTypes(DataTable dt)
try
{
//parse the value
dr[newColumn] = decider.Parse(dr[kvp.Key] as string)??DBNull.Value;
dr[newColumn] = decider.Parse(dr[dataColumn] as string)??DBNull.Value;

}
catch(Exception ex)
{
throw new Exception($"Failed to parse value '{dr[kvp.Key]}' in column '{kvp.Key}'",ex);
throw new Exception($"Failed to parse value '{dr[dataColumn]}' in column '{dataColumn}'",ex);
}
}

//if the DataColumn is part of the Primary Key of the DataTable (in memory)
//then we need to update the primary key to include the new column not the old one
if(dt.PrimaryKey != null && dt.PrimaryKey.Contains(kvp.Key))
dt.PrimaryKey = dt.PrimaryKey.Except(new []{kvp.Key }).Union(new []{newColumn }).ToArray();
if(dt.PrimaryKey != null && dt.PrimaryKey.Contains(dataColumn))
dt.PrimaryKey = dt.PrimaryKey.Except(new [] { dataColumn }).Union(new []{newColumn }).ToArray();

var oldOrdinal = kvp.Key.Ordinal;
var oldOrdinal = dataColumn.Ordinal;

//drop the original column
dt.Columns.Remove(kvp.Key);
dt.Columns.Remove(dataColumn);

//rename the hard typed column to match the old column name
newColumn.ColumnName = kvp.Key.ColumnName;
newColumn.ColumnName = dataColumn.ColumnName;
if(oldOrdinal != -1)
newColumn.SetOrdinal(oldOrdinal);
}
Expand Down
2 changes: 2 additions & 0 deletions FAnsiSql/Discovery/DiscoveredDataType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using FAnsi.Connections;
using FAnsi.Discovery.TypeTranslation;
using FAnsi.Exceptions;
Expand Down Expand Up @@ -65,6 +66,7 @@ public DecimalSize GetDecimalSize()
/// Returns the System.Type that should be used to store values read out of columns of this data type (See <see cref="ITypeTranslater.GetCSharpTypeForSQLDBType"/>
/// </summary>
/// <returns></returns>
[return:DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties|DynamicallyAccessedMemberTypes.PublicFields)]
public Type GetCSharpDataType()
{
return Column.Table.Database.Server.GetQuerySyntaxHelper().TypeTranslater.GetCSharpTypeForSQLDBType(SQLType);
Expand Down
7 changes: 7 additions & 0 deletions FAnsiSql/Discovery/TypeTranslation/ITypeTranslater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using TypeGuesser;

namespace FAnsi.Discovery.TypeTranslation;
Expand Down Expand Up @@ -36,6 +37,9 @@ public interface ITypeTranslater
/// </summary>
/// <param name="sqlType"></param>
/// <returns>The C# Type which can be used to store values of this database type</returns>
[return:
DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties |
DynamicallyAccessedMemberTypes.PublicFields)]
Type GetCSharpTypeForSQLDBType(string sqlType);

/// <summary>
Expand All @@ -45,6 +49,9 @@ public interface ITypeTranslater
/// </summary>
/// <param name="sqlType"></param>
/// <returns>The C# Type which can be used to store values of this database type</returns>
[return:
DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties |
DynamicallyAccessedMemberTypes.PublicFields)]
Type TryGetCSharpTypeForSQLDBType(string sqlType);

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions FAnsiSql/Discovery/TypeTranslation/TypeTranslater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using TypeGuesser;

Expand Down Expand Up @@ -181,6 +182,9 @@ protected string GetGuidDataType()
}

/// <inheritdoc/>
[return:
DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties |
DynamicallyAccessedMemberTypes.PublicFields)]
public Type GetCSharpTypeForSQLDBType(string sqlType)
{
return TryGetCSharpTypeForSQLDBType(sqlType) ??
Expand All @@ -191,6 +195,9 @@ public Type GetCSharpTypeForSQLDBType(string sqlType)
}

/// <inheritdoc/>
[return:
DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties |
DynamicallyAccessedMemberTypes.PublicFields)]
public Type TryGetCSharpTypeForSQLDBType(string sqlType)
{
if (IsBit(sqlType))
Expand Down
6 changes: 3 additions & 3 deletions FAnsiSql/FAnsi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<description>FAnsiSql is a database management/ETL library that allows you to perform common SQL operations without having to know which Database Management System (DBMS) you are targetting (e.g. Sql Server, My Sql, Oracle).</description>
<copyright>Copyright 2019</copyright>
<PackageTags>Ansi,SQL</PackageTags>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyTitle>HIC.FAnsiSql</AssemblyTitle>
<Company>Health Informatics Centre, University of Dundee</Company>
<Product>HIC.FAnsiSql</Product>
<Description>FAnsiSql is a database management/ETL library that allows you to perform common SQL operations without having to know which Database Management System (DBMS) you are targetting (e.g. Sql Server, My Sql, Oracle).</Description>
<Copyright>Copyright © 2019</Copyright>
<Copyright>Copyright © 2019-2023</Copyright>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsAotCompatible>true</IsAotCompatible>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
68 changes: 34 additions & 34 deletions Tests/FAnsiTests/FAnsiTests.csproj
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AssemblyTitle>FAnsiSqlTests</AssemblyTitle>
<Product>FAnsiSqlTests</Product>
<Copyright>Copyright © 2019</Copyright>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\SharedAssemblyInfo.cs" Link="SharedAssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NunitXml.TestLogger" Version="3.1.15" />
</ItemGroup>
<ItemGroup>
<Content Include="TestDatabases.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<SubType>Designer</SubType>
</Content>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FAnsiSql\FAnsi.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AssemblyTitle>FAnsiSqlTests</AssemblyTitle>
<Product>FAnsiSqlTests</Product>
<Copyright>Copyright © 2019</Copyright>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\SharedAssemblyInfo.cs" Link="SharedAssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NunitXml.TestLogger" Version="3.1.15" />
</ItemGroup>
<ItemGroup>
<Content Include="TestDatabases.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<SubType>Designer</SubType>
</Content>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FAnsiSql\FAnsi.csproj" />
</ItemGroup>
</Project>
Loading