コード例 #1
0
ファイル: AddColumns.cs プロジェクト: vebin/Postulate
        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);
            }
        }
コード例 #2
0
 private IEnumerable <string> ModelColumnNames()
 {
     return(_modelType.GetProperties().Where(pi =>
                                             !pi.HasAttribute <CalculatedAttribute>() &&
                                             !pi.HasAttribute <NotMappedAttribute>() &&
                                             CreateTable.IsSupportedType(pi.PropertyType)).Select(pi => pi.SqlColumnName()));
 }
コード例 #3
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
         };
     }));
 }
コード例 #4
0
        public static IEnumerable <PropertyInfo> GetForeignKeys(Type modelType)
        {
            foreach (var pi in modelType.GetProperties().Where(pi => pi.HasAttribute <ForeignKeyAttribute>()))
            {
                yield return(pi);
            }

            foreach (var attr in modelType.GetCustomAttributes <ForeignKeyAttribute>()
                     .Where(attr => CreateTable.HasColumnName(modelType, attr.ColumnName)))
            {
                PropertyInfo pi = modelType.GetProperty(attr.ColumnName);
                if (pi != null)
                {
                    yield return(pi);
                }
            }
        }
コード例 #5
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);
        }