예제 #1
0
        /// <summary>
        /// Outputs statements for aggregate comments that have changed.
        /// </summary>
        public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgAggregate oldAggregate in oldSchema.Aggregates)
            {
                PgAggregate newAggregate = newSchema.GetAggregate(oldAggregate.Signature);

                if (newAggregate == null)
                {
                    continue;
                }

                if ((oldAggregate.Comment == null && newAggregate.Comment != null) ||
                    (oldAggregate.Comment != null && newAggregate.Comment != null && !oldAggregate.Comment.Equals(newAggregate.Comment)))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();

                    writer.Write("COMMENT ON AGGREGATE ");
                    writer.Write(PgDiffStringExtension.QuoteName(newAggregate.Name));
                    writer.Write('(');

                    var addComma = false;

                    foreach (PgAggregate.Argument argument in newAggregate.Arguments)
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.DataType);
                    }

                    writer.Write(") IS ");
                    writer.Write(newAggregate.Comment);
                    writer.WriteLine(';');
                }
                else if (oldAggregate.Comment != null && newAggregate.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();

                    writer.Write("COMMENT ON AGGREGATE ");
                    writer.Write(PgDiffStringExtension.QuoteName(newAggregate.Name));
                    writer.Write('(');

                    var addComma = false;
                    foreach (PgAggregate.Argument argument in newAggregate.Arguments)
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.DataType);
                    }

                    writer.WriteLine(") IS NULL;");
                }
            }
        }
        /// <summary>
        /// Parses CREATE AGGREGATE and CREATE OR REPLACE AGGREGATE statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

            parser.Expect("CREATE");
            parser.Expect("AGGREGATE");

            var aggregateName = parser.ParseIdentifier();

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

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

            PgSchema schema = database.GetSchema(schemaName);

            if (schema == null)
            {
                throw new Exception(string.Format("CannotFindSchema {0} for {1}", schemaName, statement));
            }

            var aggregate = new PgAggregate()
            {
                Name = ParserUtils.GetObjectName(aggregateName),
            };

            schema.Add(aggregate);

            parser.Expect("(");

            var arguments = new List <PgAggregate.Argument>();

            var first = true;

            while (!parser.ExpectOptional(")"))
            {
                if (!first)
                {
                    try
                    {
                        parser.Expect(",");
                    }
                    catch (TeamworkParserException ex)
                    {
                        var tmpArgument = arguments.Last();

                        // last data type was a name
                        Log.Warn(string.Format("Aggregate {0} had a parameter which was not fully a data type (could be because it contained the name). {1} was ignored!", statement, tmpArgument.DataType), ex);
                        arguments.Remove(tmpArgument);
                    }
                }

                var dataType = parser.ParseDataType();

                var argument = new PgAggregate.Argument()
                {
                    DataType = dataType,
                };
                arguments.Add(argument);
                first = false;
            }

            foreach (var argument in arguments)
            {
                aggregate.Arguments.Add(argument);
            }

            aggregate.Body = parser.Rest();
        }