/// <summary>
        /// Parses COMMENT ON COLUMN.
        /// </summary>
        private static void ParseColumn(Parser parser, PgDatabase database)
        {
            var columnName = parser.ParseIdentifier();

            var objectName = ParserUtils.GetObjectName(columnName);

            var tableName = ParserUtils.GetSecondObjectName(columnName);

            var schemaName = ParserUtils.GetThirdObjectName(columnName);

            if (database.SchemaIsIgnored(schemaName))
            {
                return;
            }

            PgSchema schema = database.GetSchema(schemaName);

            PgTable table = schema.GetTable(tableName);

            if (table == null)
            {
                PgView view = schema.GetView(tableName);
                parser.Expect("IS");

                var comment = GetComment(parser);

                if (comment == null)
                {
                    view.RemoveColumnComment(objectName);
                }
                else
                {
                    view.AddColumnComment(objectName, comment);
                }

                parser.Expect(";");
            }
            else
            {
                PgColumn column = table.GetColumn(objectName);

                if (column == null)
                {
                    throw new TeamworkParserException($"CannotFindColumnInTable {columnName} from table {table.Name}");
                }

                parser.Expect("IS");
                column.Comment = GetComment(parser);
                parser.Expect(";");
            }
        }
Пример #2
0
 public static void CreateViews(TextWriter writer, PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
 {
     foreach (var newView in newSchema.GetViews())
     {
         if (oldSchema == null ||
             !oldSchema.ContainsView(newView.Name) ||
             IsViewModified(
                 oldSchema.GetView(newView.Name), newView))
         {
             searchPathHelper.OutputSearchPath(writer);
             writer.WriteLine();
             writer.WriteLine(newView.GetCreationSql());
         }
     }
 }
Пример #3
0
        public static void DropViews(TextWriter writer, PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (var oldView in oldSchema.GetViews())
            {
                var newView = newSchema.GetView(oldView.Name);

                if (newView == null || IsViewModified(oldView, newView))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.WriteLine(oldView.GetDropSql());
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Outputs statements for dropping views.
        /// </summary>
        public static void Drop(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgView oldView in oldSchema.Views)
            {
                PgView newView = newSchema.GetView(oldView.Name);

                if (newView == null || IsViewModified(oldView, newView))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.WriteLine(oldView.DropSQL);
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Parses ALTER TABLE statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement, bool outputIgnoredStatements)
        {
            var parser = new Parser(statement);

            parser.Expect("ALTER", "TABLE");
            parser.ExpectOptional("ONLY");

            var tableName = parser.ParseIdentifier();

            var schemaName = ParserUtils.GetSchemaName(tableName, database);

            if (database.SchemaIsIgnored(schemaName))
            {
                return;
            }

            PgSchema schema = database.GetSchema(schemaName);

            if (schema == null)
            {
                throw new TeamworkParserException($"CannotFindSchema {schemaName} from {statement}");
            }

            var objectName = ParserUtils.GetObjectName(tableName);

            PgTable table = schema.GetTable(objectName);

            if (table == null)
            {
                PgView view = schema.GetView(objectName);

                if (view != null)
                {
                    ParseView(parser, view, outputIgnoredStatements, tableName, database);
                    return;
                }

                PgSequence sequence = schema.GetSequence(objectName);

                if (sequence != null)
                {
                    ParseSequence(parser, sequence, outputIgnoredStatements, tableName, database);
                    return;
                }

                throw new TeamworkParserException($"CannotFindObject in {tableName} from {statement}");
            }

            while (!parser.ExpectOptional(";"))
            {
                if (parser.ExpectOptional("ALTER"))
                {
                    ParseAlterColumn(parser, table);
                }
                else if (parser.ExpectOptional("CLUSTER", "ON"))
                {
                    table.ClusterIndexName = ParserUtils.GetObjectName(parser.ParseIdentifier());
                }
                else if (parser.ExpectOptional("OWNER", "TO"))
                {
                    // we do not parse this one so we just consume the identifier
                    if (outputIgnoredStatements)
                    {
                        database.AddIgnoredStatement("ALTER TABLE " + tableName + " OWNER TO " + parser.ParseIdentifier() + ';');
                    }
                    else
                    {
                        parser.ParseIdentifier();
                    }
                }
                else if (parser.ExpectOptional("ADD"))
                {
                    if (parser.ExpectOptional("FOREIGN", "KEY"))
                    {
                        ParseAddForeignKey(parser, table);
                    }
                    else if (parser.ExpectOptional("CONSTRAINT"))
                    {
                        ParseAddConstraint(parser, table, schema);
                    }
                    else
                    {
                        throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                    }
                }
                else if (parser.ExpectOptional("ENABLE"))
                {
                    ParseEnable(parser, outputIgnoredStatements, tableName, database);
                }
                else if (parser.ExpectOptional("DISABLE"))
                {
                    ParseDisable(parser, outputIgnoredStatements, tableName, database);
                }
                else if (parser.ExpectOptional("REPLICA IDENTITY"))
                {
                    parser.Expect("NOTHING");
                }
                else
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }

                if (parser.ExpectOptional(";"))
                {
                    break;
                }
                else
                {
                    parser.Expect(",");
                }
            }
        }
        /// <summary>
        /// Parses ALTER VIEW statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement, bool outputIgnoredStatements)
        {
            var parser = new Parser(statement);

            parser.Expect("ALTER", "VIEW");

            var viewName = parser.ParseIdentifier();

            var schemaName = ParserUtils.GetSchemaName(viewName, database);

            if (database.SchemaIsIgnored(schemaName))
            {
                return;
            }

            PgSchema schema = database.GetSchema(schemaName);

            if (schema == null)
            {
                throw new TeamworkParserException($"CannotFindSchema {schemaName} from {statement}");
            }

            var objectName = ParserUtils.GetObjectName(viewName);

            PgView view = schema.GetView(objectName);

            if (view == null)
            {
                throw new TeamworkParserException($"CannotFindView {viewName} from {statement}");
            }

            while (!parser.ExpectOptional(";"))
            {
                if (parser.ExpectOptional("ALTER"))
                {
                    parser.ExpectOptional("COLUMN");

                    var columnName = ParserUtils.GetObjectName(parser.ParseIdentifier());

                    if (parser.ExpectOptional("SET", "DEFAULT"))
                    {
                        var expression = parser.Expression();
                        view.AddColumnDefaultValue(columnName, expression);
                    }
                    else if (parser.ExpectOptional("DROP", "DEFAULT"))
                    {
                        view.RemoveColumnDefaultValue(columnName);
                    }
                    else
                    {
                        throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                    }
                }
                else if (parser.ExpectOptional("OWNER", "TO"))
                {
                    // we do not parse this one so we just consume the identifier
                    if (outputIgnoredStatements)
                    {
                        database.AddIgnoredStatement("ALTER TABLE " + viewName + " OWNER TO " + parser.ParseIdentifier() + ';');
                    }
                    else
                    {
                        parser.ParseIdentifier();
                    }
                }
                else
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }
            }
        }
Пример #7
0
        public static void AlterViews(TextWriter writer, PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (var oldView in oldSchema.GetViews())
            {
                var newView = newSchema.GetView(oldView.Name);
                if (newView == null)
                {
                    continue;
                }

                DiffDefaultValues(writer, oldView, newView, searchPathHelper);

                if (oldView.Comment == null &&
                    newView.Comment != null ||
                    oldView.Comment != null &&
                    newView.Comment != null &&
                    !oldView.Comment.Equals(
                        newView.Comment))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON VIEW ");
                    writer.Write(PgDiffUtils.GetQuotedName(newView.Name));
                    writer.Write(" IS ");
                    writer.Write(newView.Comment);
                    writer.WriteLine(';');
                }
                else if (oldView.Comment != null && newView.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON VIEW ");
                    writer.Write(PgDiffUtils.GetQuotedName(newView.Name));
                    writer.WriteLine(" IS NULL;");
                }

                var columnNames = newView.ColumnComments.Select(c => c.ColumnName).ToList();

                foreach (var columnComment in oldView.ColumnComments)
                {
                    if (!columnNames.Contains(columnComment.ColumnName))
                    {
                        columnNames.Add(columnComment.ColumnName);
                    }
                }

                foreach (var columnName in columnNames)
                {
                    var oldColumnComment = oldView.ColumnComments.FirstOrDefault(cc => columnName.Equals(cc.ColumnName));
                    var newColumnComment = newView.ColumnComments.FirstOrDefault(cc => columnName.Equals(cc.ColumnName));

                    if (oldColumnComment == null && newColumnComment != null ||
                        oldColumnComment != null && newColumnComment != null &&
                        !oldColumnComment.Comment.Equals(
                            newColumnComment.Comment))
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON COLUMN ");
                        writer.Write(PgDiffUtils.GetQuotedName(newView.Name));
                        writer.Write('.');
                        writer.Write(PgDiffUtils.GetQuotedName(newColumnComment.ColumnName));
                        writer.Write(" IS ");
                        writer.Write(newColumnComment.Comment);
                        writer.WriteLine(';');
                    }
                    else if (oldColumnComment != null &&
                             newColumnComment == null)
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON COLUMN ");
                        writer.Write(PgDiffUtils.GetQuotedName(newView.Name));
                        writer.Write('.');
                        writer.Write(PgDiffUtils.GetQuotedName(oldColumnComment.ColumnName));
                        writer.WriteLine(" IS NULL;");
                    }
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Outputs statements for altering view default values.
        /// </summary>
        public static void Alter(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgView oldView in oldSchema.Views)
            {
                PgView newView = newSchema.GetView(oldView.Name);

                if (newView == null)
                {
                    continue;
                }

                DiffDefaultValues(writer, oldView, newView, searchPathHelper);

                if ((oldView.Comment == null && newView.Comment != null) ||
                    (oldView.Comment != null &&
                     newView.Comment != null &&
                     !oldView.Comment.Equals(newView.Comment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON VIEW ");
                    writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                    writer.Write(" IS ");
                    writer.Write(newView.Comment);
                    writer.WriteLine(';');
                }
                else if (oldView.Comment != null && newView.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON VIEW ");
                    writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                    writer.WriteLine(" IS NULL;");
                }

                IList <string> columnNames = new List <string>(newView.ColumnComments.Count);

                foreach (PgView.ColumnComment columnComment in newView.ColumnComments)
                {
                    columnNames.Add(columnComment.ColumnName);
                }

                foreach (PgView.ColumnComment columnComment in oldView.ColumnComments)
                {
                    if (!columnNames.Contains(columnComment.ColumnName))
                    {
                        columnNames.Add(columnComment.ColumnName);
                    }
                }

                foreach (var columnName in columnNames)
                {
                    PgView.ColumnComment oldColumnComment = null;
                    PgView.ColumnComment newColumnComment = null;

                    foreach (PgView.ColumnComment columnComment in oldView.ColumnComments)
                    {
                        if (columnName.Equals(columnComment.ColumnName))
                        {
                            oldColumnComment = columnComment;
                            break;
                        }
                    }

                    foreach (PgView.ColumnComment columnComment in newView.ColumnComments)
                    {
                        if (columnName.Equals(columnComment.ColumnName))
                        {
                            newColumnComment = columnComment;
                            break;
                        }
                    }

                    if ((oldColumnComment == null && newColumnComment != null) ||
                        (oldColumnComment != null &&
                         newColumnComment != null &&
                         !oldColumnComment.Comment.Equals(newColumnComment.Comment)))
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON COLUMN ");
                        writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                        writer.Write('.');
                        writer.Write(PgDiffStringExtension.QuoteName(newColumnComment.ColumnName));
                        writer.Write(" IS ");
                        writer.Write(newColumnComment.Comment);
                        writer.WriteLine(';');
                    }
                    else if (oldColumnComment != null && newColumnComment == null)
                    {
                        searchPathHelper.OutputSearchPath(writer);
                        writer.WriteLine();
                        writer.Write("COMMENT ON COLUMN ");
                        writer.Write(PgDiffStringExtension.QuoteName(newView.Name));
                        writer.Write('.');
                        writer.Write(PgDiffStringExtension.QuoteName(oldColumnComment.ColumnName));
                        writer.WriteLine(" IS NULL;");
                    }
                }
            }
        }
Пример #9
0
 /// <summary>
 /// Outputs statements for creation of views.
 /// </summary>
 public static void Create(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
 {
     foreach (PgView newView in newSchema.Views)
     {
         if (oldSchema == null || !oldSchema.ContainsView(newView.Name) || IsViewModified(oldSchema.GetView(newView.Name), newView))
         {
             searchPathHelper.OutputSearchPath(writer);
             writer.WriteLine();
             writer.WriteLine(newView.CreationSQL);
         }
     }
 }