Exemplo n.º 1
0
        public override IEnumerable <string> SqlCommands()
        {
            var    obj             = DbObject.FromType(_modelType, _connection);
            var    temp            = obj.GetTemp();
            string sourceTableName = obj.ToString();
            string tempTableName   = temp.ToString();

            yield return(SelectInto(obj, tempTableName));

            DropTable drop = new DropTable(obj, _connection);

            foreach (var cmd in drop.SqlCommands())
            {
                yield return(cmd);
            }

            CreateTable create = new CreateTable(_modelType);

            foreach (var cmd in create.SqlCommands())
            {
                yield return(cmd);
            }

            RestoreTempTable rt = new RestoreTempTable(temp, _modelType, _connection,
                                                       _columns.Where(cr => !cr.PropertyInfo.HasAttribute <CalculatedAttribute>()).ToDictionary(
                                                           item => item.PropertyInfo.SqlColumnName(),
                                                           item => item.PropertyInfo.SqlDefaultExpression(forCreateTable: false)));

            foreach (var cmd in rt.SqlCommands())
            {
                yield return(cmd);
            }
        }
Exemplo n.º 2
0
            public ColumnRef(PropertyInfo pi)
            {
                PropertyInfo = pi;
                DbObject obj = DbObject.FromType(pi.ReflectedType);

                Schema     = obj.Schema;
                TableName  = obj.Name;
                ColumnName = pi.SqlColumnName();
            }
Exemplo n.º 3
0
 internal static IEnumerable <ForeignKeyRef> GetReferencingForeignKeys(Type modelType, IEnumerable <Type> allTypes)
 {
     return(allTypes.SelectMany(t => GetForeignKeys(t).Where(pi =>
     {
         ForeignKeyAttribute fk = pi.GetForeignKeyAttribute();
         return (fk.PrimaryTableType.Equals(modelType));
     }).Select(pi =>
               new ForeignKeyRef()
     {
         ConstraintName = pi.ForeignKeyName(), ReferencingTable = DbObject.FromType(pi.DeclaringType)
     }
               )));
 }
Exemplo n.º 4
0
        public override IEnumerable <string> SqlCommands()
        {
            Type     modelType = _modelColumn.ReflectedType;
            DbObject obj       = DbObject.FromType(modelType);

            if (_modelColumn.HasAttribute <PrimaryKeyAttribute>())
            {
                // issue #7, table must be dropped and rebuilt
            }

            yield return($"ALTER TABLE [{obj.Schema}].[{obj.Name}] ALTER COLUMN [{_modelColumn.SqlColumnName()}] {_modelColumn.SqlColumnType()}");

            // restore PK
        }
Exemplo n.º 5
0
 private static IEnumerable <PrimaryKeyRef> GetModelPrimaryKeys(IEnumerable <Type> types, IDbConnection connection)
 {
     return(types.Select(t =>
     {
         DbObject obj = DbObject.FromType(t, connection);
         return new PrimaryKeyRef()
         {
             Schema = obj.Schema,
             TableName = obj.Name,
             ColumnList = string.Join(",", CreateTable.PrimaryKeyColumns(t)),
             ObjectId = obj.ObjectID,
             ModelType = t
         };
     }));
 }
Exemplo n.º 6
0
        private IEnumerable <Action> GetNewTables(IEnumerable <Type> modelTypes, IDbConnection connection)
        {
            List <Action> actions = new List <Action>();

            foreach (var type in modelTypes)
            {
                DbObject obj = DbObject.FromType(type);
                if (!connection.Exists("[sys].[tables] WHERE SCHEMA_NAME([schema_id])=@schema AND [name]=@name", new { schema = obj.Schema, name = obj.Name }))
                {
                    if (!_createdTables.Contains(obj))
                    {
                        _createdTables.Add(obj);
                        actions.Add(new CreateTable(type));
                    }
                }
            }

            return(actions);
        }
Exemplo n.º 7
0
 public CreateTable(Type modelType) : base(MergeObjectType.Table, MergeActionType.Create, DbObject.FromType(modelType).QualifiedName())
 {
     _modelType = modelType;
 }
Exemplo n.º 8
0
        private IEnumerable <Action> GetNewColumns(IEnumerable <Type> modelTypes, IDbConnection connection)
        {
            List <Action> results = new List <Action>();

            var schemaColumns = GetSchemaColumns(connection);

            var dbObjects = schemaColumns.GroupBy(item => new DbObject(item.Schema, item.TableName)
            {
                ObjectID = item.ObjectID
            });
            var dcObjectIDs = dbObjects.ToDictionary(obj => obj.Key, obj => obj.Key.ObjectID);

            Dictionary <DbObject, Type> dcModelTypes = new Dictionary <DbObject, Type>();

            var modelColumns = modelTypes.SelectMany(mt => mt.GetProperties()
                                                     .Where(pi => CreateTable.IsSupportedType(pi.PropertyType) && !pi.HasAttribute <NotMappedAttribute>())
                                                     .Select(pi =>
            {
                DbObject obj = DbObject.FromType(mt);
                if (!dcModelTypes.ContainsKey(obj))
                {
                    dcModelTypes.Add(obj, mt);
                }
                return(new ColumnRef()
                {
                    Schema = obj.Schema,
                    TableName = obj.Name,
                    ColumnName = pi.SqlColumnName(),
                    PropertyInfo = pi
                });
            }));

            var newColumns = modelColumns.Where(mcol =>
                                                !_createdTables.Contains(new DbObject(mcol.Schema, mcol.TableName)) &&
                                                !schemaColumns.Any(scol => mcol.Equals(scol)));

            foreach (var colGroup in newColumns.GroupBy(item => new DbObject(item.Schema, item.TableName)))
            {
                if (!_deletedTables.Contains(colGroup.Key))
                {
                    _deletedTables.Add(colGroup.Key);
                }
                if (!_createdTables.Contains(colGroup.Key))
                {
                    _createdTables.Add(colGroup.Key);
                }

                if (IsTableEmpty(connection, colGroup.Key.Schema, colGroup.Key.Name) || dcModelTypes[colGroup.Key].HasAttribute <AllowDropAttribute>())
                {
                    colGroup.Key.ObjectID = dcObjectIDs[colGroup.Key];
                    results.Add(new DropTable(colGroup.Key, connection));
                    results.Add(new CreateTable(dcModelTypes[colGroup.Key]));
                }
                else
                {
                    results.Add(new AddColumns(dcModelTypes[colGroup.Key], colGroup, connection));
                }
            }

            return(results);
        }
Exemplo n.º 9
0
        private IEnumerable <Action> GetRestorableTempTables(IEnumerable <Type> modelTypes, IDbConnection connection)
        {
            List <Action> results = new List <Action>();

            var tempTables = connection.Query <TempTableRef>(
                @"SELECT 
					SCHEMA_NAME([schema_id]) AS [Schema], [name] AS [TempName], 
					SUBSTRING([name], 0, LEN([name]) - LEN('_temp') + 1) AS [ModelName],
					[object_id] AS [TempObjectId]
				FROM 
					[sys].[tables] WHERE [name] LIKE '%_temp'"                    );

            var restoreTables = from tt in tempTables
                                join mt in modelTypes on new DbObject(tt.Schema, tt.ModelName) equals DbObject.FromType(mt)
                                select new { TempObject = new DbObject(tt.Schema, tt.TempName)
                                             {
                                                 ObjectID = tt.TempObjectId
                                             }, ModelType = mt };

            results.AddRange(restoreTables.Select(rt => new RestoreTempTable(rt.TempObject, rt.ModelType, connection)));

            return(results);
        }