From b390e3b67346762d0e99d2257880b41ad120facb Mon Sep 17 00:00:00 2001 From: Adam ONeil Date: Wed, 13 May 2020 12:10:21 -0400 Subject: [PATCH] version 2.0.12 #8 done --- ModelSync.Library/Extensions/TypeExtensions.cs | 8 ++++++++ ModelSync.Library/ModelSync.Library.csproj | 4 ++-- ModelSync.Library/Models/Column.cs | 12 ++++++++++++ .../Services/AssemblyModelBuilder.cs | 8 +++++++- Testing/CreateObjects.cs | 2 ++ Testing/Models/Employee.cs | 17 +++++++++++++++++ 6 files changed, 48 insertions(+), 3 deletions(-) diff --git a/ModelSync.Library/Extensions/TypeExtensions.cs b/ModelSync.Library/Extensions/TypeExtensions.cs index 772d75a..4818f87 100644 --- a/ModelSync.Library/Extensions/TypeExtensions.cs +++ b/ModelSync.Library/Extensions/TypeExtensions.cs @@ -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; + } } } diff --git a/ModelSync.Library/ModelSync.Library.csproj b/ModelSync.Library/ModelSync.Library.csproj index f8a417c..b7d116d 100644 --- a/ModelSync.Library/ModelSync.Library.csproj +++ b/ModelSync.Library/ModelSync.Library.csproj @@ -3,7 +3,7 @@ netstandard2.0 true - 2.0.11 + 2.0.12 Adam O'Neil Copyright (c) 2020 Adam O'Neil https://github.com/adamosoftware/ModelSync @@ -11,7 +11,7 @@ A database schema diff library for .NET assemblies and SQL Server databases using AO.DbSchema.Attributes AO.ModelSync.Library LICENSE - added back some static create methods DataModel.FromAssembly and DataModel.FromSqlServerAsync + nullable enum support https://github.com/adamosoftware/ModelSync/issues/8 diff --git a/ModelSync.Library/Models/Column.cs b/ModelSync.Library/Models/Column.cs index 4ea32e3..47bf526 100644 --- a/ModelSync.Library/Models/Column.cs +++ b/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; @@ -162,6 +164,16 @@ string prepExpression(string input) public override async Task 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(); } diff --git a/ModelSync.Library/Services/AssemblyModelBuilder.cs b/ModelSync.Library/Services/AssemblyModelBuilder.cs index e2574dd..1c73652 100644 --- a/ModelSync.Library/Services/AssemblyModelBuilder.cs +++ b/ModelSync.Library/Services/AssemblyModelBuilder.cs @@ -265,7 +265,7 @@ IEnumerable 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(out _)) .Select(pi => GetColumnFromProperty(pi, defaultIdentityColumn)) @@ -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; diff --git a/Testing/CreateObjects.cs b/Testing/CreateObjects.cs index f44fe45..30df1fa 100644 --- a/Testing/CreateObjects.cs +++ b/Testing/CreateObjects.cs @@ -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); } } diff --git a/Testing/Models/Employee.cs b/Testing/Models/Employee.cs index 5012b0b..f5e5763 100644 --- a/Testing/Models/Employee.cs +++ b/Testing/Models/Employee.cs @@ -4,6 +4,19 @@ namespace Testing.Models { + public enum Status + { + Hourly, + Exempt + } + + public enum AnotherEnum + { + This, + That, + Other + } + [Identity(nameof(Id))] public class Employee { @@ -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; } } }