Skip to content

Commit

Permalink
version 2.0.12 #8 done
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfoneil committed May 13, 2020
1 parent df3e8b3 commit b390e3b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ModelSync.Library/Extensions/TypeExtensions.cs
Expand Up @@ -13,5 +13,13 @@ public static bool IsNullableGeneric(this Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}

public static bool IsNullableEnum(this Type type)
{
return
type.IsGenericType &&
type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)) &&
type.GetGenericArguments()[0].IsEnum;
}
}
}
4 changes: 2 additions & 2 deletions ModelSync.Library/ModelSync.Library.csproj
Expand Up @@ -3,15 +3,15 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.11</Version>
<Version>2.0.12</Version>
<Authors>Adam O'Neil</Authors>
<Copyright>Copyright (c) 2020 Adam O'Neil</Copyright>
<RepositoryUrl>https://github.com/adamosoftware/ModelSync</RepositoryUrl>
<PackageProjectUrl>https://github.com/adamosoftware/ModelSync</PackageProjectUrl>
<Description>A database schema diff library for .NET assemblies and SQL Server databases using AO.DbSchema.Attributes</Description>
<PackageId>AO.ModelSync.Library</PackageId>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReleaseNotes>added back some static create methods DataModel.FromAssembly and DataModel.FromSqlServerAsync</PackageReleaseNotes>
<PackageReleaseNotes>nullable enum support https://github.com/adamosoftware/ModelSync/issues/8</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions ModelSync.Library/Models/Column.cs
@@ -1,4 +1,6 @@
using ModelSync.Library.Abstract;
using ModelSync.Library.Extensions;
using ModelSync.Library.Services;
using System;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -162,6 +164,16 @@ string prepExpression(string input)

public override async Task<bool> ExistsAsync(IDbConnection connection, SqlDialect dialect)
{
var sqlServer = dialect as SqlServerDialect;
if (sqlServer != null)
{
return await connection.RowExistsAsync(
@"[sys].[columns] [col]
INNER JOIN [sys].[tables] [t] ON [col].[object_id]=[t].[object_id]
WHERE SCHEMA_NAME([t].[schema_id])=@schema AND [t].[name]=@tableName AND [col].[name]=@columnName",
new { schema = Parent.GetSchema("dbo"), tableName = Parent.GetBaseName(), columnName = Name });
}

throw new NotImplementedException();
}

Expand Down
8 changes: 7 additions & 1 deletion ModelSync.Library/Services/AssemblyModelBuilder.cs
Expand Up @@ -265,7 +265,7 @@ IEnumerable<Column> getColumns(Type type)

var properties = type
.GetProperties().Where(pi =>
(DataTypes.ContainsKey(pi.PropertyType) || pi.PropertyType.IsEnum) &&
(DataTypes.ContainsKey(pi.PropertyType) || pi.PropertyType.IsEnum || pi.PropertyType.IsNullableEnum()) &&
pi.CanWrite &&
!pi.HasAttribute<NotMappedAttribute>(out _))
.Select(pi => GetColumnFromProperty(pi, defaultIdentityColumn))
Expand Down Expand Up @@ -307,6 +307,12 @@ private static Column GetColumnFromProperty(PropertyInfo propertyInfo, string de
result.DataType = "int";
}

if (propertyInfo.PropertyType.IsNullableEnum())
{
result.DataType = "int";
result.IsNullable = true;
}

SetColumnProperties(propertyInfo, result, defaultIdentityColumn);

return result;
Expand Down
2 changes: 2 additions & 0 deletions Testing/CreateObjects.cs
Expand Up @@ -33,6 +33,8 @@ public void CreateIfNotExists()
Assert.IsTrue(ObjectExistsAsync(cn, new Table() { Name = "dbo.ActionItem2" }).Result);
Assert.IsTrue(ObjectExistsAsync(cn, new Table() { Name = "dbo.Employee" }).Result);
Assert.IsTrue(ObjectExistsAsync(cn, new ForeignKey() { Name = "FK_ActionItem2_EmployeeId" }).Result);
Assert.IsTrue(ObjectExistsAsync(cn, new Column() { Parent = new Table() { Name = "dbo.Employee" }, Name = "Status" }).Result);
Assert.IsTrue(ObjectExistsAsync(cn, new Column() { Parent = new Table() { Name = "dbo.Employee" }, Name = "Another" }).Result);
}
}

Expand Down
17 changes: 17 additions & 0 deletions Testing/Models/Employee.cs
Expand Up @@ -4,6 +4,19 @@

namespace Testing.Models
{
public enum Status
{
Hourly,
Exempt
}

public enum AnotherEnum
{
This,
That,
Other
}

[Identity(nameof(Id))]
public class Employee
{
Expand All @@ -25,5 +38,9 @@ public class Employee
public DateTime? HireDate { get; set; }

public DateTime? TermDate { get; set; }

public Status Status { get; set; }

public AnotherEnum? Another { get; set; }
}
}

0 comments on commit b390e3b

Please sign in to comment.