Skip to content

Commit

Permalink
Merge pull request #78 from abe545/honor-optional-for-hierarchical-re…
Browse files Browse the repository at this point in the history
…sults

2.3.0-pre01
  • Loading branch information
abe545 authored Aug 1, 2016
2 parents 31b1de3 + 7c91835 commit 3b22f86
Show file tree
Hide file tree
Showing 12 changed files with 632 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CodeContractsInstallDir>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\packages\DotNet.Contracts.1.10.20606.1\'))</CodeContractsInstallDir>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -197,6 +200,9 @@
<Compile Include="..\CodeOnlyStoredProcedure\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\CodeOnlyStoredProcedure\PropertyInfoExtensions.cs">
<Link>PropertyInfoExtensions.cs</Link>
</Compile>
<Compile Include="..\CodeOnlyStoredProcedure\RowFactory\AccessorFactoryBase.cs">
<Link>RowFactory\AccessorFactoryBase.cs</Link>
</Compile>
Expand Down Expand Up @@ -345,6 +351,7 @@
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Condition="'$(CodeContractsImported)' != 'true' AND '$(DontImportCodeContracts)' != 'true'" Project="$(CodeContractsInstallDir)\MsBuild\v$(VisualStudioVersion)\Microsoft.CodeContracts.targets"/>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
1 change: 1 addition & 0 deletions CodeOnlyStoredProcedure-NET40/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNet.Contracts" version="1.10.20606.1" targetFramework="net40-client" />
<package id="EntityFramework" version="6.1.3" targetFramework="net40-Client" />
</packages>
4 changes: 2 additions & 2 deletions CodeOnlyStoredProcedure.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 2015
VisualStudioVersion = 14.0.22823.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOnlyStoredProcedure", "CodeOnlyStoredProcedure\CodeOnlyStoredProcedure.csproj", "{37762E7B-48CC-4001-B479-ECB1923AE2E0}"
EndProject
Expand Down
6 changes: 6 additions & 0 deletions CodeOnlyStoredProcedure/CodeOnlyStoredProcedure.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CodeContractsInstallDir>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\packages\DotNet.Contracts.1.10.20606.1\'))</CodeContractsInstallDir>
</PropertyGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -136,6 +139,7 @@
<Compile Include="IDataTransformer.cs" />
<Compile Include="IDbConnectionExtensions.cs" />
<Compile Include="OptionalResultAttribute.cs" />
<Compile Include="PropertyInfoExtensions.cs" />
<Compile Include="RowFactory\AccessorFactoryBase.cs" />
<Compile Include="RowFactory\ComplexTypeRowFactory.cs" />
<Compile Include="RowFactory\EnumAccessorFactory.cs" />
Expand Down Expand Up @@ -198,6 +202,7 @@
<Compile Include="TypeExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="StoredProcedure.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>StoredProcedure.generated.cs</LastGenOutput>
Expand All @@ -223,6 +228,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Condition="'$(CodeContractsImported)' != 'true' AND '$(DontImportCodeContracts)' != 'true'" Project="$(CodeContractsInstallDir)\MsBuild\v$(VisualStudioVersion)\Microsoft.CodeContracts.targets"/>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
33 changes: 33 additions & 0 deletions CodeOnlyStoredProcedure/PropertyInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;

namespace CodeOnlyStoredProcedure
{
internal static class PropertyInfoExtensions
{
public static bool IsOptional(this PropertyInfo pi)
{
return pi.GetCustomAttributes(false).OfType<OptionalResultAttribute>().Any();
}

public static string GetSqlColumnName(this PropertyInfo pi)
{
var col = pi.GetCustomAttributes(typeof(ColumnAttribute), false)
.OfType<ColumnAttribute>()
.FirstOrDefault();

if (col != null && !string.IsNullOrWhiteSpace(col.Name))
return col.Name;

var attr = pi.GetCustomAttributes(typeof(StoredProcedureParameterAttribute), false)
.OfType<StoredProcedureParameterAttribute>()
.FirstOrDefault();

if (attr != null && !string.IsNullOrWhiteSpace(attr.Name))
return attr.Name;

return pi.Name;
}
}
}
6 changes: 3 additions & 3 deletions CodeOnlyStoredProcedure/RowFactory/ComplexTypeRowFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static ComplexTypeRowFactory()

var props = implType.GetResultPropertiesBySqlName();
dbColumnNames = props.Keys.ToArray();
requiredColumnNames = props.Where(kv => !kv.Value.GetCustomAttributes(false).OfType<OptionalResultAttribute>().Any())
requiredColumnNames = props.Where(kv => !kv.Value.IsOptional())
.Where(kv => !kv.Value.PropertyType.IsEnumeratedType()) // child collections can't be required
.Select(kv => kv.Key)
.ToArray();
Expand All @@ -40,7 +40,7 @@ static ComplexTypeRowFactory()
{
Name = kv.Key,
Property = kv.Value,
IsOptional = kv.Value.GetCustomAttributes(false).OfType<OptionalResultAttribute>().Any(),
IsOptional = kv.Value.IsOptional(),
AccessorFactory = (AccessorFactoryBase)Activator.CreateInstance(typeof(EnumAccessorFactory<>).MakeGenericType(kv.Value.PropertyType), dataReaderExpression, indexExpression, kv.Value, kv.Key)
};
}
Expand All @@ -49,7 +49,7 @@ static ComplexTypeRowFactory()
{
Name = kv.Key,
Property = kv.Value,
IsOptional = kv.Value.GetCustomAttributes(false).OfType<OptionalResultAttribute>().Any(),
IsOptional = kv.Value.IsOptional(),
AccessorFactory = (AccessorFactoryBase)Activator.CreateInstance(typeof(ValueAccessorFactory<>).MakeGenericType(kv.Value.PropertyType), dataReaderExpression, indexExpression, kv.Value, kv.Key)
};
})
Expand Down
Loading

0 comments on commit 3b22f86

Please sign in to comment.