Exemplo n.º 1
0
        public static DbObject FromType(Type modelType, IDbConnection connection)
        {
            DbObject obj = FromType(modelType);

            SetObjectId(connection, obj);
            return(obj);
        }
Exemplo n.º 2
0
        public static string SqlServerName(Type modelType, bool squareBrackets = true)
        {
            DbObject obj = FromType(modelType);

            obj.SquareBraces = squareBrackets;
            return(obj.ToString());
        }
Exemplo n.º 3
0
        public static DbObject Parse(string objectName, IDbConnection connection = null)
        {
            DbObject result = null;

            string[] parts = objectName.Split('.');
            switch (parts.Length)
            {
            case 1:
                result = new DbObject("dbo", objectName);
                break;

            case 2:
                result = new DbObject(parts[0], parts[1]);
                break;

            default:
                throw new InvalidOperationException($"Too many name parts in {objectName}");
            }

            if (connection != null)
            {
                SetObjectId(connection, result);
            }

            return(result);
        }
Exemplo n.º 4
0
        public override bool Equals(object obj)
        {
            DbObject test = obj as DbObject;

            if (test != null)
            {
                return(test.Schema.ToLower().Equals(this.Schema.ToLower()) && test.Name.ToLower().Equals(this.Name.ToLower()));
            }

            Type testType = obj as Type;

            if (testType != null)
            {
                return(Equals(FromType(testType)));
            }

            RenameFromAttribute rename = obj as RenameFromAttribute;

            if (rename != null)
            {
                DbObject renamedObj = Parse(rename.OldName);
                return(renamedObj.Equals(this));
            }

            return(false);
        }
Exemplo n.º 5
0
        public override bool Equals(object obj)
        {
            ColumnRef test = obj as ColumnRef;

            if (test != null)
            {
                return
                    (test.Schema.ToLower().Equals(this.Schema.ToLower()) &&
                     test.TableName.ToLower().Equals(this.TableName.ToLower()) &&
                     test.ColumnName.ToLower().Equals(this.ColumnName.ToLower()));
            }

            PropertyInfo pi = obj as PropertyInfo;

            if (pi != null)
            {
                DbObject dbo = DbObject.FromType(pi.ReflectedType);
                return
                    (dbo.Schema.ToLower().Equals(this.Schema.ToLower()) &&
                     dbo.Name.ToLower().Equals(this.TableName.ToLower()) &&
                     pi.SqlColumnName().ToLower().Equals(this.ColumnName.ToLower()));
            }

            return(false);
        }
Exemplo n.º 6
0
        private IEnumerable <MergeAction> AddColumnsWithTableAlter(IDbConnection connection, IEnumerable <PropertyInfo> newColumns)
        {
            // tables with data may only have columns added
            var alterColumns = newColumns
                               .GroupBy(pi => DbObject.FromType(pi.ReflectedType, connection))
                               .Where(obj => connection.TableExists(obj.Key.Schema, obj.Key.Name) && !connection.IsTableEmpty(obj.Key.Schema, obj.Key.Name))
                               .SelectMany(tbl => tbl);

            // adding primary key columns requires containing PKs to be rebuilt
            var pkColumns = alterColumns.Where(pi => pi.HasAttribute <PrimaryKeyAttribute>());
            var pkTables  = pkColumns.GroupBy(item => DbObject.FromType(item.ReflectedType)).Select(grp => grp.Key);

            foreach (var pk in pkTables)
            {
                yield return(new DropPrimaryKey(pk));
            }

            // todo: same thing with unique keys -- they must be rebuilt if column additions impact them

            foreach (var col in alterColumns)
            {
                yield return(new AddColumn(col));
            }

            foreach (var pk in pkTables)
            {
                yield return(new CreatePrimaryKey(pk));
            }
        }
Exemplo n.º 7
0
 internal Type FindModelType(DbObject @object)
 {
     return(_modelTypes.FirstOrDefault(t =>
     {
         DbObject obj = DbObject.FromType(t);
         return obj.Schema.Equals(@object.Schema) && obj.Name.Equals(@object.Name);
     }));
 }
Exemplo n.º 8
0
 internal Type FindModelType(IEnumerable <Type> modelTypes)
 {
     return(modelTypes.FirstOrDefault(t =>
     {
         DbObject obj = DbObject.FromType(t);
         return obj.Schema.Equals(Schema) && obj.Name.Equals(TableName);
     }));
 }
Exemplo n.º 9
0
 private IEnumerable <string> SchemaColumnNames(IDbConnection connection, DbObject dbObject)
 {
     return(connection.Query <string>(
                @"SELECT
             LOWER([col].[name]) AS [LoweredName]
         FROM
             [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]=@name
         ORDER BY
             [col].[name]", new { schema = dbObject.Schema, name = dbObject.Name }));
 }
Exemplo n.º 10
0
 internal static IEnumerable <ForeignKeyRef> GetModelReferencingForeignKeys(this Type modelType, IEnumerable <Type> allTypes)
 {
     return(allTypes.SelectMany(t => GetModelForeignKeys(t).Where(pi =>
     {
         ForeignKeyAttribute fk = pi.GetForeignKeyAttribute();
         return (fk.PrimaryTableType.Equals(modelType));
     }).Select(pi =>
               new ForeignKeyRef()
     {
         ConstraintName = pi.ForeignKeyName(), ChildObject = DbObject.FromType(pi.DeclaringType), PropertyInfo = pi
     }
               )));
 }
Exemplo n.º 11
0
        private IEnumerable <MergeAction> RenameColumns(IDbConnection connection)
        {
            var renamedColumns = _modelTypes.SelectMany(t =>
                                                        t.GetProperties().Where(pi =>
            {
                RenameFromAttribute attr;
                if (pi.HasAttribute(out attr))
                {
                    DbObject obj = DbObject.FromType(pi.DeclaringType);
                    return(connection.ColumnExists(obj.Schema, obj.Name, attr.OldName));
                }
                return(false);
            })
                                                        );

            List <RenameColumn> results = new List <RenameColumn>();

            results.AddRange(renamedColumns.Select(pi => new RenameColumn(pi)));
            return(results);
        }
Exemplo n.º 12
0
        public ColumnRef(PropertyInfo pi)
        {
            PropertyInfo = pi;
            DbObject obj = DbObject.FromType(pi.ReflectedType);

            Schema     = obj.Schema;
            TableName  = obj.Name;
            ColumnName = pi.SqlColumnName();
            DataType   = pi.SqlDataType();
            CollateAttribute collate;

            if (pi.HasAttribute(out collate))
            {
                Collation = collate.Collation;
            }
            DbObject     = obj;
            IsNullable   = pi.AllowSqlNull();
            ModelType    = pi.ReflectedType;
            IsCalculated = pi.HasAttribute <CalculatedAttribute>();
        }
Exemplo n.º 13
0
 public static void SetObjectId(IDbConnection connection, DbObject obj)
 {
     obj.ObjectId = connection.QueryFirstOrDefault <int>("SELECT [object_id] FROM [sys].[tables] WHERE SCHEMA_NAME([schema_id])=@schema AND [name]=@name", new { schema = obj.Schema, name = obj.Name });
 }
Exemplo n.º 14
0
        public static string ConstraintName(Type modelType)
        {
            DbObject obj = FromType(modelType);

            return(obj.ConstraintName());
        }
Exemplo n.º 15
0
 public static DbObject FromTempName(DbObject obj)
 {
     return(new DbObject(obj.Schema, obj.Name.Substring(0, obj.Name.IndexOf(_tempSuffix))));
 }