Skip to content

Feat/better conversion #8

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

Merged
merged 15 commits into from
Mar 8, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,25 @@
<PackageReference Include="Philips.CodeAnalysis.MaintainabilityAnalyzers" Version="1.2.32" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="SmartAnalyzers.CSharpExtensions.Annotations" Version="4.2.8" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.54.0.64047" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.3" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="ToStringWithoutOverrideAnalyzer" Version="0.6.0" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="SmartAnalyzers.CSharpExtensions.Annotations" Version="4.2.8" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.54.0.64047" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.3" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="ToStringWithoutOverrideAnalyzer" Version="0.6.0" PrivateAssets="All" ExcludeAssets="runtime"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Credfeto.Database.Interfaces\Credfeto.Database.Interfaces.csproj" />
<ProjectReference Include="..\Credfeto.Database.Pgsql\Credfeto.Database.Pgsql.csproj" />
<ProjectReference Include="..\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Credfeto.Database.Interfaces\Credfeto.Database.Interfaces.csproj"/>
<ProjectReference Include="..\Credfeto.Database.Pgsql\Credfeto.Database.Pgsql.csproj"/>
<ProjectReference Include="..\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
<Import Project="$(SolutionDir)KeepGeneratedFiles.props" Condition="Exists('$(SolutionDir)KeepGeneratedFiles.props')" />
<ItemGroup>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.GetAddressMeaningOfLifeAsync.Database.generated.cs"/>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.GetAllAsync.Database.generated.cs"/>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.GetAsync.Database.generated.cs"/>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.GetMeaningOfLifeAsync.Database.generated.cs"/>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.GetOptionalAddressMeaningOfLifeAsync.Database.generated.cs"/>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.GetOptionalMeaningOfLifeAsync.Database.generated.cs"/>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.GetStringMeaningOfLifeAsync.Database.generated.cs"/>
<Compile Include="Generated\Credfeto.Database.Source.Generation\Credfeto.Database.Source.Generation.DatabaseCodeGenerator\Credfeto.Database.Source.Generation.Example.DatabaseWrapper.InsertAsync.Database.generated.cs"/>
</ItemGroup>
<Import Project="$(SolutionDir)KeepGeneratedFiles.props" Condition="Exists('$(SolutionDir)KeepGeneratedFiles.props')"/>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
throw new InvalidOperationException(""No result returned."");
}

return (int)result;
return Convert.ToInt32(result);
}
}
")
Expand All @@ -84,7 +84,7 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
}

[Fact]
public Task SimpleScalarFunctionIntWithOneParameterAsync()
public Task SimpleScalarFunctionIntWithOneIntParameterAsync()
{
const string test = @"
using System;
Expand Down Expand Up @@ -134,6 +134,7 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
DbCommand command = connection.CreateCommand();
command.CommandText = ""select example.scalarfunction(@factor)"";
DbParameter p0 = command.CreateParameter();
p0.DbType = DbType.Int32;
p0.Value = factor;
p0.ParameterName = ""@factor"";
command.Parameters.Add(p0);
Expand All @@ -145,7 +146,86 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
throw new InvalidOperationException(""No result returned."");
}

return (int)result;
return Convert.ToInt32(result);
}
}
")
};

return this.VerifyAsync(code: test, expected: expected, cancellationToken: CancellationToken.None);
}

[Fact]
public Task SimpleScalarFunctionIntWithOneNullableIntParameterAsync()
{
const string test = @"
using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Credfeto.Database.Interfaces;

#nullable enable

" + Constants.DatabaseTypes + @"

namespace ConsoleApplication1
{
public static partial class DatabaseWrapper
{
[SqlObjectMap(name: ""example.scalarfunction"", sqlObjectType: SqlObjectType.SCALAR_FUNCTION)]
public static partial Task<int> GetValueAsync(DbConnection connection, int? factor, CancellationToken cancellationToken);
}
}";

(string filename, string generated)[] expected =
{
(filename: "ConsoleApplication1.DatabaseWrapper.GetValueAsync.Database.generated.cs", generated: @"using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

#nullable enable

namespace ConsoleApplication1;

public static partial class DatabaseWrapper
{
[GeneratedCode(tool: ""Credfeto.Database.Source.Generation.DatabaseCodeGenerator"", version: """ + VersionInformation.Version() + @""")]
public static async partial System.Threading.Tasks.Task<int> GetValueAsync(System.Data.Common.DbConnection connection, int? factor, System.Threading.CancellationToken cancellationToken)
{
DbCommand command = connection.CreateCommand();
command.CommandText = ""select example.scalarfunction(@factor)"";
DbParameter p0 = command.CreateParameter();
p0.DbType = DbType.Int32;
if(factor == null)
{
p0.Value = DBNull.Value;
}
else
{
p0.Value = factor;
}
p0.ParameterName = ""@factor"";
command.Parameters.Add(p0);

object? result = await command.ExecuteScalarAsync(cancellationToken: cancellationToken);

if (result is null)
{
throw new InvalidOperationException(""No result returned."");
}

return Convert.ToInt32(result);
}
}
")
Expand Down Expand Up @@ -205,11 +285,14 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
DbCommand command = connection.CreateCommand();
command.CommandText = ""select example.scalarfunction(@factor, @name)"";
DbParameter p0 = command.CreateParameter();
p0.DbType = DbType.Int32;
p0.Value = factor;
p0.ParameterName = ""@factor"";
command.Parameters.Add(p0);
DbParameter p1 = command.CreateParameter();
p1.DbType = DbType.String;
p1.Value = name;
p1.Size = name.Length;
p1.ParameterName = ""@name"";
command.Parameters.Add(p1);

Expand All @@ -220,7 +303,7 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
throw new InvalidOperationException(""No result returned."");
}

return (int)result;
return Convert.ToInt32(result);
}
}
")
Expand Down Expand Up @@ -288,11 +371,14 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
DbCommand command = connection.CreateCommand();
command.CommandText = ""select example.scalarfunction(@factor, @name, @address)"";
DbParameter p0 = command.CreateParameter();
p0.DbType = DbType.Int32;
p0.Value = factor;
p0.ParameterName = ""@factor"";
command.Parameters.Add(p0);
DbParameter p1 = command.CreateParameter();
p1.DbType = DbType.String;
p1.Value = name;
p1.Size = name.Length;
p1.ParameterName = ""@name"";
command.Parameters.Add(p1);
DbParameter p2 = command.CreateParameter();
Expand All @@ -307,7 +393,7 @@ public static async partial System.Threading.Tasks.Task<int> GetValueAsync(Syste
throw new InvalidOperationException(""No result returned."");
}

return (int)result;
return Convert.ToInt32(result);
}
}
")
Expand Down Expand Up @@ -511,7 +597,7 @@ public static partial class DatabaseWrapper
return null;
}

return (int)result;
return Convert.ToInt32(result);
}
}
")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,23 @@
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
<Import Project="$(SolutionDir)SourceGenerator.props" />
<Import Project="$(SolutionDir)SourceGenerator.props"/>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" PrivateAssets="all" ExcludeAssets="runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Credfeto.Enumeration.Source.Generation" Version="1.0.7.19" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="codecracker.CSharp" Version="1.1.0" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="FunFair.CodeAnalysis" Version="5.9.0.1493" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.19" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Nullable.Extended.Analyzer" Version="1.10.4539" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Philips.CodeAnalysis.DuplicateCodeAnalyzer" Version="1.1.7" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Philips.CodeAnalysis.MaintainabilityAnalyzers" Version="1.2.32" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="Credfeto.Enumeration.Source.Generation.Attributes" Version="1.0.7.19"/>
</ItemGroup>
<ItemGroup>

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" PrivateAssets="all" ExcludeAssets="runtime"/>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="Credfeto.Enumeration.Source.Generation" Version="1.0.7.19" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="codecracker.CSharp" Version="1.1.0" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="FunFair.CodeAnalysis" Version="5.9.0.1493" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.19" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="Nullable.Extended.Analyzer" Version="1.10.4539" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="Philips.CodeAnalysis.DuplicateCodeAnalyzer" Version="1.1.7" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="Philips.CodeAnalysis.MaintainabilityAnalyzers" Version="1.2.32" PrivateAssets="All" ExcludeAssets="runtime"/>
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" ExcludeAssets="runtime" />
<PackageReference Include="SmartAnalyzers.CSharpExtensions.Annotations" Version="4.2.8" PrivateAssets="All" ExcludeAssets="runtime" />
Expand Down
Loading