/// <summary>
        /// Generates create table commands for the supplied type
        /// </summary>
        internal static void CreateTable <T>(StringBuilder tables, Type type, StringBuilder constraints, StringBuilder manyToManyTables)
        {
            tables.AppendLine("CREATE TABLE [" + type.Name + "] ( ");

            foreach (var pi in DwarfHelper.GetDBProperties(type))
            {
                tables.AppendLine(TypeToColumnName(pi) + " " + TypeToColumnConstruction(type, pi.ContainedProperty));
            }

            foreach (var pi in DwarfHelper.GetGemListProperties(type))
            {
                tables.AppendLine(TypeToColumnName(pi) + " " + TypeToColumnConstruction(type, pi.ContainedProperty));
            }

            foreach (var manyToManyProperty in DwarfHelper.GetManyToManyProperties(type))
            {
                CreateManyToManyTable(type, manyToManyProperty, manyToManyTables, constraints);
            }

            var keys = String.Empty;

            foreach (var propertyInfo in DwarfHelper.GetPKProperties(type))
            {
                keys += TypeToColumnName(propertyInfo) + ", ";
            }

            if (!string.IsNullOrEmpty(keys))
            {
                keys = keys.TruncateEnd(2);

                keys = "CONSTRAINT [PK_" + type.Name + "] PRIMARY KEY (" + keys + ")";

                tables.AppendLine(keys);
            }

            foreach (var pi in DwarfHelper.GetFKProperties <T>(type))
            {
                var constraintName = "FK_" + type.Name + "_" + pi.Name;

                var alterTable = "ALTER TABLE [" + type.Name + "] ADD CONSTRAINT [" + constraintName + "Id] FOREIGN KEY (" +
                                 pi.Name + "Id) REFERENCES [" + pi.PropertyType.Name + "] (Id)";

                if (!DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty).DisableDeleteCascade)
                {
                    alterTable += " ON DELETE CASCADE ";
                }

                constraints.AppendLine(alterTable);
            }
            CreateUniqueConstraint <T>(constraints, type);

            tables.AppendLine(") ");
            tables.AppendLine();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns true if the object contains faulty Foreign Key values
        /// </summary>
        private static IEnumerable <string> FaultyForeignKeys(IDwarf obj)
        {
            foreach (var pi in Cfg.FKProperties[DwarfHelper.DeProxyfy(obj)])
            {
                var att = DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty);

                if (!att.IsNullable && (pi.GetValue(obj) == null || !((IDwarf)pi.GetValue(obj)).IsSaved))
                {
                    yield return(pi.Name);
                }
            }
        }
Exemplo n.º 3
0
        internal static string GetOrderByDirection(Type type)
        {
            DeProxyfy(ref type);

            foreach (var pi in PropertyHelper.GetProperties(type))
            {
                var att = DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty);

                if (att != null && att.IsDefaultSort)
                {
                    return(att.SortDirection == SortDirection.Ascending ? string.Empty : "DESC");
                }
            }

            return(string.Empty);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the first property with the DefaultSort property enabled
        /// </summary>
        internal static ExpressionProperty GetOrderByProperty(Type type)
        {
            DeProxyfy(ref type);

            foreach (var ep in PropertyHelper.GetProperties(type))
            {
                var att = DwarfPropertyAttribute.GetAttribute(ep.ContainedProperty);

                if (att != null && att.IsDefaultSort)
                {
                    return(ep);
                }
            }

            return(null);
        }
        /// <summary>
        /// Returns true if the base type of supplied property info is nullable
        /// </summary>
        internal static bool IsColumnNullable(Type type, PropertyInfo pi)
        {
            var att = DwarfPropertyAttribute.GetAttribute(pi);

            if (att == null)
            {
                return(true);
            }

            if (att.IsPrimaryKey)
            {
                return(false);
            }

            if (pi.Name.Equals("Id"))
            {
                return(false);
            }

            if (DwarfPropertyAttribute.IsFK(pi))
            {
                return(att.IsNullable);
            }

            if (pi.PropertyType == typeof(string))
            {
                return(true);
            }

            if (att.IsNullable)
            {
                return(true);
            }

            return(pi.PropertyType.IsGenericType);
        }
 /// <summary>
 /// Extracts the column name from the supplied property
 /// </summary>
 internal static object TypeToColumnName(PropertyInfo pi)
 {
     return(DwarfPropertyAttribute.RequiresAppendedId(pi) ? "[" + pi.Name + "Id]" : "[" + pi.Name + "]");
 }
        /// <summary>
        /// Returns the sql needed to construct the supplied property as a column
        /// </summary>
        internal static string TypeToColumnConstruction(Type type, PropertyInfo pi, bool skipConstraint = false)
        {
            var value = TypeToColumnType(pi);

            var att = DwarfPropertyAttribute.GetAttribute(pi);

            if (pi.Name.Equals("Id"))
            {
                value += " NOT NULL";
            }
            else if (DwarfPropertyAttribute.IsFK(pi))
            {
                value += att.IsNullable ? string.Empty : " NOT NULL";
            }
            else if (pi.PropertyType == typeof(string))
            {
                value += att.UseMaxLength ? "(max)" : "(255)";
            }
            else if (pi.PropertyType.Implements <IGem>())
            {
                value += "(255)" + (att.IsNullable ? string.Empty : " NOT NULL");
            }
            else if (pi.PropertyType.Implements <IGemList>())
            {
                value += "(max)";
            }
            else if (pi.PropertyType.Implements <byte[]>())
            {
                value += "(max)";
            }
            else if (pi.PropertyType.Implements <Type>())
            {
                value += "(255)";
            }
            else if (pi.PropertyType.Implements <decimal?>())
            {
                value += "(28, 8)";
            }
            else if (pi.PropertyType.IsEnum())
            {
                value += "(255)" + (pi.PropertyType.IsGenericType ? string.Empty : " NOT NULL");
            }
            else if (!pi.PropertyType.IsGenericType && !att.IsNullable)
            {
                value += " NOT NULL";
            }

            if (att != null && att.IsUnique)
            {
                if (!skipConstraint)
                {
                    value += string.Format(" CONSTRAINT [UQ_{0}_{1}{2}] UNIQUE", type.Name, pi.Name, DwarfPropertyAttribute.RequiresAppendedId(pi) ? "Id" : string.Empty);
                }
                else
                {
                    value += "/* WARNING! You might manually have to drop and recreate any Unique Constraint*/";
                }
            }

            if (string.IsNullOrEmpty(value))
            {
                throw new InvalidOperationException(type.Name + "." + pi.Name + "'s type (" + pi.PropertyType + ") isn't supported.");
            }

            return(value + ", ");
        }
        /// <summary>
        /// Returns the base sql type for the supplied property
        /// </summary>
        internal static string TypeToColumnType(PropertyInfo pi)
        {
            var value = String.Empty;

            if (pi.Name.Equals("Id"))
            {
                value = "uniqueidentifier";
            }
            else if (DwarfPropertyAttribute.IsFK(pi))
            {
                value = "uniqueidentifier";
            }
            else if (pi.PropertyType.Implements <int?>())
            {
                value = "int";
            }
            else if (pi.PropertyType.Implements <decimal?>())
            {
                value = "decimal";
            }
            else if (pi.PropertyType.Implements <double?>())
            {
                value = "float";
            }
            else if (pi.PropertyType.Implements <DateTime?>())
            {
                value = "datetime";
            }
            else if (pi.PropertyType == typeof(string))
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.IsEnum())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <bool?>())
            {
                value = "bit";
            }
            else if (pi.PropertyType.Implements <Guid?>())
            {
                value = "uniqueidentifier";
            }
            else if (pi.PropertyType.Implements <IGem>())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <IGemList>())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <Type>())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <byte[]>())
            {
                value = "varbinary";
            }

            if (string.IsNullOrEmpty(value))
            {
                throw new InvalidOperationException(pi.Name + "'s type (" + pi.PropertyType + ") isn't supported.");
            }

            return(value);
        }
        /// <summary>
        /// Analyses the current domain model and database and generates update scripts in an attempt to synchronize them.
        /// Note that a few places might need manual coding, such as when columns become not nullable, when target types changes, etc. Look for "Warning!!"
        /// in the generated code
        /// </summary>
        internal static string GetUpdateScript <T>(Assembly assembly = null)
        {
            var database = Cfg.Databases[assembly ?? typeof(T).Assembly];

            var dropPKConstraints   = new StringBuilder();
            var dropFKConstraints   = new StringBuilder();
            var dropColumns         = new StringBuilder();
            var dropTables          = new StringBuilder();
            var addTables           = new StringBuilder();
            var addManyToManyTables = new StringBuilder();
            var addColumns          = new StringBuilder();
            var addConstraints      = new StringBuilder();

            var allCurrentDomainTypes = DwarfHelper.GetValidTypes <T>().ToList();

            var currentManyToManyTables = GetCurrentManyToManyTables(allCurrentDomainTypes).Distinct().ToList();

            var existingDatabaseTables = DwarfContext <T> .GetConfiguration().Database.ExecuteQuery("SELECT t.name FROM sys.tables t JOIN sys.schemas s ON s.schema_id = t.schema_id ").Select(x => x.name).ToList();

            foreach (var existingTable in existingDatabaseTables.ToArray())
            {
                if (!allCurrentDomainTypes.Select(x => x.Name).Any(x => x.Equals(existingTable)) && !currentManyToManyTables.Any(x => x.Equals(existingTable)))
                {
                    DropDeadTableConstraints <T>(dropPKConstraints, dropFKConstraints, existingTable);
                    dropTables.AppendLine("IF EXISTS (SELECT * FROM dbo.sysobjects WHERE Id = OBJECT_ID(N'[" + existingTable + "]') AND OBJECTPROPERTY(Id, N'IsUserTable') = 1) DROP Table [" + existingTable + "] ");
                    existingDatabaseTables.Remove(existingTable);

                    var constraints = DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("select CONSTRAINT_NAME from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '" + existingTable + "' ");

                    foreach (var constraint in constraints)
                    {
                        AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + constraint.CONSTRAINT_NAME + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + existingTable + "]')) ALTER TABLE [" + existingTable + "] DROP CONSTRAINT [" + constraint.CONSTRAINT_NAME + "]");
                    }
                }
            }

            foreach (var type in allCurrentDomainTypes)
            {
                if (!existingDatabaseTables.Contains(type.Name))
                {
                    CreateTable <T>(addTables, type, addConstraints, addManyToManyTables);
                }

                foreach (var pi in DwarfHelper.GetManyToManyProperties(type))
                {
                    if (!existingDatabaseTables.Contains(ManyToManyAttribute.GetTableName(type, pi.ContainedProperty)))
                    {
                        CreateManyToManyTable(type, pi, addManyToManyTables, addConstraints);
                    }
                }
            }

            foreach (var existingTable in existingDatabaseTables)
            {
                if (!allCurrentDomainTypes.Any(x => x.Name.Equals(existingTable)))
                {
                    continue;
                }

                var existingColumns = (List <dynamic>) DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("SELECT c.name as name1, t.name as name2, c.max_length, c.is_nullable FROM sys.columns c inner join sys.types t on t.user_type_id = c.user_type_id WHERE object_id = OBJECT_ID('dbo." + existingTable + "') ");

                var type  = allCurrentDomainTypes.First(x => x.Name.Equals(existingTable));
                var props = DwarfHelper.GetGemListProperties(type).Union(DwarfHelper.GetDBProperties(type)).ToList();

                foreach (var existingColumn in existingColumns)
                {
                    string columnName = existingColumn.name1.ToString();

                    var pi = props.FirstOrDefault(x => x.Name.Equals(existingColumn.name2.ToString().Equals("uniqueidentifier") && !x.Name.Equals("Id") ? (columnName.EndsWith("Id") ? columnName.TruncateEnd(2) : columnName) : columnName));

                    if (pi == null)
                    {
                        dropColumns.AppendLine("IF EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[" + existingTable + "]') AND Name = '" + columnName + "') ALTER TABLE dbo.[" + existingTable + "] DROP COLUMN " + columnName);

                        if (existingColumn.name2.Equals("uniqueidentifier"))
                        {
                            var fkName = "FK_" + existingTable + "_" + columnName;
                            AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + fkName + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + existingTable + "]')) ALTER TABLE [" + existingTable + "] DROP CONSTRAINT [" + fkName + "]");
                        }
                    }
                }

                foreach (var pi in props)
                {
                    if (DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty) == null && !pi.PropertyType.Implements <IGemList>())
                    {
                        continue;
                    }

                    var existingColumn = existingColumns.FirstOrDefault(x => (pi.PropertyType.Implements <IDwarf>() ? pi.Name + "Id" : pi.Name).Equals(x.name1));

                    if (existingColumn != null)
                    {
                        var typeChanged     = !existingColumn.name2.Equals(TypeToColumnType(pi.ContainedProperty));
                        var lengthChanged   = pi.PropertyType == typeof(string) && (existingColumn.name2.ToString().Equals(DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty).UseMaxLength ? "-1" : "255"));
                        var nullableChanged = (bool.Parse(existingColumn.is_nullable.ToString()) != IsColumnNullable(type, pi.ContainedProperty));

                        if (typeChanged | nullableChanged | lengthChanged)
                        {
                            addColumns.AppendLine("-- WARNING! TYPE CONVERSION MIGHT FAIL!!! ");
                            addColumns.AppendLine("ALTER TABLE [" + type.Name + "] ALTER COLUMN " + TypeToColumnName(pi) + " " + TypeToColumnConstruction(type, pi.ContainedProperty, true).TruncateEnd(2));
                            addColumns.AppendLine("GO ");
                        }

                        if (pi.PropertyType.Implements <IDwarf>())
                        {
                            var fkName           = "FK_" + type.Name + "_" + pi.Name + "Id";
                            var constraintExists = DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("SELECT t.name FROM sys.objects obj inner join sys.foreign_key_columns fk on obj.object_id = fk.constraint_object_id inner join sys.columns c on fk.referenced_object_id = c.object_id and fk.referenced_column_id = c.column_id inner join sys.tables t on t.object_id = c.object_id inner join sys.tables t2 on fk.parent_object_id = t2.object_id WHERE obj.type = 'F' and t2.name = '" + type.Name + "' and obj.name = '" + fkName + "' and t.name = '" + pi.PropertyType.Name + "'");

                            if (!constraintExists.Any())
                            {
                                dropColumns.AppendLine("IF EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[" + existingTable + "]') AND Name = '" + existingColumn.name1 + "') ALTER TABLE dbo.[" + existingTable + "] DROP COLUMN " + existingColumn.name1);

                                AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + fkName + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + existingTable + "]')) ALTER TABLE [" + existingTable + "] DROP CONSTRAINT [" + fkName + "]");

                                if (IsColumnNullable(type, pi.ContainedProperty))
                                {
                                    addColumns.AppendLine("ALTER TABLE [" + type.Name + "] ADD " + TypeToColumnName(pi) + " " + TypeToColumnConstruction(type, pi.ContainedProperty).TruncateEnd(2));
                                    addColumns.AppendLine("GO ");
                                }
                                else
                                {
                                    object value = null;

                                    try { value = Activator.CreateInstance(pi.PropertyType); }
                                    catch { }

                                    addColumns.AppendLine("GO ");
                                    addColumns.AppendLine("ALTER TABLE [" + type.Name + "] ADD " + TypeToColumnName(pi) + " " + TypeToColumnType(pi.ContainedProperty));
                                    addColumns.AppendLine("GO ");
                                    addColumns.AppendLine("-- WARNING! Value is probably wrong. Correct before you execute! ");
                                    addColumns.AppendLine("UPDATE [" + type.Name + "] SET " + TypeToColumnName(pi) + " = " + database.ValueToSqlString(value) + " ");
                                    addColumns.AppendLine("GO ");
                                    addColumns.AppendLine("ALTER TABLE [" + type.Name + "] ALTER COLUMN " + TypeToColumnName(pi) + " " + TypeToColumnConstruction(type, pi.ContainedProperty, true).TruncateEnd(2));
                                    addColumns.AppendLine("GO ");
                                }

                                var alterTable = "ALTER TABLE [" + type.Name + "] ADD CONSTRAINT [" + fkName + "] FOREIGN KEY (" + pi.Name + "Id) REFERENCES [" + pi.PropertyType.Name + "] (Id)";

                                if (!DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty).DisableDeleteCascade)
                                {
                                    alterTable += " ON DELETE CASCADE ";
                                }

                                addConstraints.AppendLine(alterTable);
                                addConstraints.AppendLine("GO ");
                            }
                        }
                    }
                    else
                    {
                        if (IsColumnNullable(type, pi.ContainedProperty))
                        {
                            addColumns.AppendLine("ALTER TABLE [" + type.Name + "] ADD " + TypeToColumnName(pi) + " " + TypeToColumnConstruction(type, pi.ContainedProperty).TruncateEnd(2));
                            addColumns.AppendLine("GO ");
                        }
                        else
                        {
                            object value = null;

                            try { value = Activator.CreateInstance(pi.PropertyType); }
                            catch { }

                            addColumns.AppendLine("GO ");
                            addColumns.AppendLine("ALTER TABLE [" + type.Name + "] ADD " + TypeToColumnName(pi) + " " + " " + TypeToColumnConstruction(type, pi.ContainedProperty).TruncateEnd(2).Replace("NOT NULL", string.Empty));
                            addColumns.AppendLine("GO ");
                            addColumns.AppendLine("-- WARNING! Value is probably wrong. Correct before you execute! ");
                            addColumns.AppendLine("UPDATE [" + type.Name + "] SET " + TypeToColumnName(pi) + " = " + database.ValueToSqlString(value) + " ");
                            addColumns.AppendLine("GO ");
                            addColumns.AppendLine("ALTER TABLE [" + type.Name + "] ALTER COLUMN " + TypeToColumnName(pi) + " " + TypeToColumnConstruction(type, pi.ContainedProperty, true).TruncateEnd(2));
                            addColumns.AppendLine("GO ");
                        }

                        if (pi.PropertyType.Implements <IDwarf>())
                        {
                            var constraintName = "FK_" + type.Name + "_" + pi.Name;

                            var alterTable = "ALTER TABLE [" + type.Name + "] ADD CONSTRAINT [" + constraintName + "Id] FOREIGN KEY (" + pi.Name + "Id) REFERENCES [" + pi.PropertyType.Name + "] (Id)";

                            if (!DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty).DisableDeleteCascade)
                            {
                                alterTable += " ON DELETE CASCADE ";
                            }

                            addConstraints.AppendLine(alterTable);
                            addColumns.AppendLine("GO ");
                        }
                    }
                }
            }

            foreach (var existingTable in existingDatabaseTables)
            {
                var uqConstraints = ((List <dynamic>)DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("SELECT obj.name FROM sys.objects obj inner join sys.tables t on obj.parent_object_id = t.object_id WHERE obj.type = 'UQ' and t.name = '" + existingTable + "'")).Select(x => x.name);

                foreach (var uqConstraint in uqConstraints)
                {
                    var uqParts = uqConstraint.Split('_');

                    var type = allCurrentDomainTypes.FirstOrDefault(x => x.Name.Equals(uqParts[1]));

                    if (type != null)
                    {
                        var columns = (List <dynamic>) DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("select COLUMN_NAME from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where CONSTRAINT_NAME = '" + uqConstraint + "'");

                        //Not a unique combination, but a unique column (right?)
                        if (columns.Count == 1)
                        {
                            var pi = ColumnToProperty(type, columns.First().COLUMN_NAME);

                            if (pi != null)
                            {
                                var att = DwarfPropertyAttribute.GetAttribute(pi);

                                if (att != null && !att.IsUnique)
                                {
                                    AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + uqConstraint + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + type.Name + "]')) ALTER TABLE [" + type.Name + "] DROP CONSTRAINT [" + uqConstraint + "]");
                                }
                            }
                        }
                        else
                        {
                            var uqProperties = (IEnumerable <ExpressionProperty>)DwarfHelper.GetUniqueGroupProperties <T>(type, uqParts[2]);

                            if (!uqProperties.Any())
                            {
                                AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + uqConstraint + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + type.Name + "]')) ALTER TABLE [" + type.Name + "] DROP CONSTRAINT [" + uqConstraint + "]");
                            }
                            else
                            {
                                var difference = uqProperties.Select(x => x.Name).Except(columns.Select(x => x.COLUMN_NAME));

                                if (difference.Any())
                                {
                                    AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + uqConstraint + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + type.Name + "]')) ALTER TABLE [" + type.Name + "] DROP CONSTRAINT [" + uqConstraint + "]");
                                    CreateUniqueConstraint <T>(addConstraints, type);
                                }
                            }
                        }
                    }
                }
            }

            foreach (var type in allCurrentDomainTypes)
            {
                foreach (var pi in DwarfHelper.GetUniqueDBProperties <T>(type))
                {
                    var piName = pi.Name + (pi.PropertyType.Implements <IDwarf>() ? "Id" : string.Empty);
                    var uqName = "UQ_" + type.Name + "_" + piName;

                    var columns = DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("select COLUMN_NAME from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where CONSTRAINT_NAME = '" + uqName + "'");

                    if (columns.Count == 0 && !addColumns.ToString().Contains(uqName) && !addTables.ToString().Contains(uqName))
                    {
                        addConstraints.AppendLine("ALTER TABLE [" + type.Name + "] ADD CONSTRAINT [" + uqName + "] UNIQUE ([" + pi.Name + "]) ");
                        addConstraints.AppendLine("GO ");
                    }
                }

                foreach (var uniqueGroupName in DwarfHelper.GetUniqueGroupNames <T>(type))
                {
                    var pis = DwarfHelper.GetUniqueGroupProperties <T>(type, uniqueGroupName);

                    var columns = DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("select COLUMN_NAME from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where CONSTRAINT_NAME = 'UQ_" + type.Name + "_" + uniqueGroupName + "'").Select(x => x.COLUMN_NAME).ToList();

                    if (columns.Any())
                    {
                        var differnce = pis.Select(x => x.Name).Except(columns);

                        if (differnce.Any())
                        {
                            AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + uniqueGroupName + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + type.Name + "]')) ALTER TABLE [" + type.Name + "] DROP CONSTRAINT [" + uniqueGroupName + "]");
                            CreateUniqueConstraint <T>(addConstraints, type, uniqueGroupName);
                            addConstraints.AppendLine("GO ");
                        }
                    }
                    else
                    {
                        CreateUniqueConstraint <T>(addConstraints, type, uniqueGroupName);
                        addConstraints.AppendLine("GO ");
                    }
                }

                var uniqueColumns = DwarfContext <T> .GetDatabase().ExecuteCustomQuery <T>("select COLUMN_NAME, CONSTRAINT_NAME from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where CONSTRAINT_NAME like 'UQ_%' and CONSTRAINT_NAME like '%_' + COLUMN_NAME and TABLE_NAME = '" + type.Name + "' ");

                foreach (var uniqueColumn in uniqueColumns)
                {
                    var pi = ColumnToProperty(type, uniqueColumn.COLUMN_NAME.ToString());

                    if (pi == null)
                    {
                        AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + uniqueColumn.CONSTRAINT_NAME + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + type.Name + "]')) ALTER TABLE [" + type.Name + "] DROP CONSTRAINT [" + uniqueColumn.CONSTRAINT_NAME + "]");
                        continue;
                    }

                    var att = DwarfPropertyAttribute.GetAttribute(pi);

                    if (att == null)
                    {
                        AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + uniqueColumn.CONSTRAINT_NAME + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + type.Name + "]')) ALTER TABLE [" + type.Name + "] DROP CONSTRAINT [" + uniqueColumn.CONSTRAINT_NAME + "]");
                        continue;
                    }

                    if (!att.IsUnique)
                    {
                        AddDropConstraint(dropPKConstraints, dropFKConstraints, "IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[" + uniqueColumn.CONSTRAINT_NAME + "]') AND PARENT_OBJECT_ID = OBJECT_ID('[" + type.Name + "]')) ALTER TABLE [" + type.Name + "] DROP CONSTRAINT [" + uniqueColumn.CONSTRAINT_NAME + "]");
                    }
                }
            }


            var result = AppendSection(dropFKConstraints) +
                         AppendSection(dropPKConstraints) +
                         AppendSection(dropColumns) +
                         AppendSection(dropTables) +
                         AppendSection(addTables) +
                         AppendSection(addManyToManyTables) +
                         AppendSection(addColumns) +
                         AppendSection(addConstraints);


            if (!string.IsNullOrEmpty(result.Trim()))
            {
                return("--WARNING--\r\n" +
                       "--Use these scripts with caution as there's no guarantee that all model changes are --\r\n" +
                       "--reflected here nor that any previously persisted data will remain intact post execution. --\r\n" +
                       "\r\n" +
                       result);
            }
            return(string.Empty);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Converts an SqlDataReader row into an object of the type T
        /// </summary>
        protected T TupleToObject <T>(DbDataReader sdr) where T : Dwarf <T>, new()
        {
            var type = typeof(T);

            var obj = DwarfHelper.CreateInstance <T>();

            for (var i = 0; i < sdr.FieldCount; i++)
            {
                var propertyName  = sdr.GetName(i);
                var propertyValue = sdr.GetValue(i);

                var pi = PropertyHelper.GetProperty(type, propertyName) ?? PropertyHelper.GetProperty(type, propertyName.Replace("Id", string.Empty));

                if (pi == null)
                {
                    continue;
                }

                if (propertyValue is DBNull)
                {
                    propertyValue = pi.ContainedProperty.PropertyType == typeof(string) ? string.Empty : null;
                }
                else if (pi.PropertyType.Implements <IGem>())
                {
                    propertyValue = Cfg.LoadGem[pi.PropertyType](propertyValue.ToString());
                }
                else if (pi.PropertyType.IsEnum() && propertyValue != null)
                {
                    propertyValue = Enum.Parse(pi.PropertyType.GetTrueEnumType(), propertyValue.ToString());
                }
                else if (pi.PropertyType.Implements <Type>())
                {
                    propertyValue = typeof(T).Assembly.GetType(propertyValue.ToString());
                }
                else if (pi.PropertyType.Implements <IDwarf>())
                {
                    var att = DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty);

                    if (att != null)
                    {
                        if (DwarfPropertyAttribute.IsFK(pi))
                        {
                            obj.SetOriginalValue(pi.Name, propertyValue);

                            if (att.EagerLoad)
                            {
                                var targetType = pi.PropertyType;

                                if (propertyValue != null)
                                {
                                    PropertyHelper.SetValue(obj, pi.Name, Cfg.LoadExpressions[targetType]((Guid)propertyValue));
                                }
                            }

                            continue;
                        }
                    }
                }

                if (pi.PropertyType == typeof(string) && propertyValue != null)
                {
                    propertyValue = propertyValue.ToString().Replace("\\r\\n", "\r\n");
                }

                PropertyHelper.SetValue(obj, pi.Name, propertyValue);
                obj.SetOriginalValue(pi.Name, propertyValue);
            }

            obj.IsSaved = true;

            return(obj);
        }