예제 #1
0
        public static void CreateFunctions(TextWriter writer, PgDiffArguments arguments,
                                           PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            // Add new functions and replace modified functions
            foreach (var newFunction in newSchema.GetFunctions())
            {
                var oldFunction = oldSchema?.GetFunction(newFunction.GetSignature());

                if (oldFunction == null || !newFunction.Equals(oldFunction, arguments.IgnoreFunctionWhitespace))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.WriteLine(newFunction.GetCreationSql());
                }
            }
        }
        /// <summary>
        /// Outputs statements for new or modified functions.
        /// </summary>
        public static void Create(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper, bool ignoreFunctionWhitespace)
        {
            // Add new functions and replace modified functions
            foreach (PgFunction newFunction in newSchema.Functions)
            {
                PgFunction oldFunction;

                if (oldSchema == null)
                {
                    oldFunction = null;
                }
                else
                {
                    oldFunction = oldSchema.GetFunction(newFunction.Signature);
                }

                if ((oldFunction == null) || !newFunction.Equals(oldFunction, ignoreFunctionWhitespace))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.WriteLine(newFunction.CreationSQL);
                }
            }
        }
        /// <summary>
        /// Outputs statements for function comments that have changed.
        /// </summary>
        public static void AlterComments(StreamWriter writer, [NullGuard.AllowNull] PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (PgFunction oldfunction in oldSchema.Functions)
            {
                PgFunction newFunction = newSchema.GetFunction(oldfunction.Signature);

                if (newFunction == null)
                {
                    continue;
                }

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

                    writer.Write("COMMENT ON FUNCTION ");
                    writer.Write(PgDiffStringExtension.QuoteName(newFunction.Name));
                    writer.Write('(');

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

                        writer.Write(argument.GetDeclaration(false));
                    }

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

                    writer.Write("COMMENT ON FUNCTION ");
                    writer.Write(PgDiffStringExtension.QuoteName(newFunction.Name));
                    writer.Write('(');

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

                        writer.Write(argument.GetDeclaration(false));
                    }

                    writer.WriteLine(") IS NULL;");
                }
            }
        }
        /// <summary>
        /// Parses COMMENT ON FUNCTION.
        /// </summary>
        private static void ParseFunction(Parser parser, PgDatabase database)
        {
            var functionName = parser.ParseIdentifier();

            var objectName = ParserUtils.GetObjectName(functionName);

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

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

            PgSchema schema = database.GetSchema(schemaName);

            parser.Expect("(");

            var tmpFunction = new PgFunction()
            {
                Name = objectName,
            };

            while (!parser.ExpectOptional(")"))
            {
                string mode;

                if (parser.ExpectOptional("IN"))
                {
                    mode = "IN";
                }
                else if (parser.ExpectOptional("OUT"))
                {
                    mode = "OUT";
                }
                else if (parser.ExpectOptional("INOUT"))
                {
                    mode = "INOUT";
                }
                else if (parser.ExpectOptional("VARIADIC"))
                {
                    mode = "VARIADIC";
                }
                else
                {
                    mode = null;
                }

                var    position     = parser.Position;
                string argumentName = null;
                var    dataType     = parser.ParseDataType();

                var position2 = parser.Position;

                if (!parser.ExpectOptional(")") && !parser.ExpectOptional(","))
                {
                    parser.Position = position;
                    argumentName    = ParserUtils.GetObjectName(parser.ParseIdentifier());
                    dataType        = parser.ParseDataType();
                }
                else
                {
                    parser.Position = position2;
                }

                var argument = new PgFunction.Argument()
                {
                    DataType = dataType,
                    Mode     = mode,
                    Name     = argumentName,
                };
                tmpFunction.AddArgument(argument);

                if (parser.ExpectOptional(")"))
                {
                    break;
                }
                else
                {
                    parser.Expect(",");
                }
            }

            PgFunction function = schema.GetFunction(tmpFunction.Signature);

            parser.Expect("IS");
            function.Comment = GetComment(parser);
            parser.Expect(";");
        }
예제 #5
0
        public static void AlterComments(TextWriter writer, PgSchema oldSchema, PgSchema newSchema, SearchPathHelper searchPathHelper)
        {
            if (oldSchema == null)
            {
                return;
            }

            foreach (var oldFun in oldSchema.GetFunctions())
            {
                var newFunction = newSchema.GetFunction(oldFun.GetSignature());

                if (newFunction == null)
                {
                    continue;
                }

                if (oldFun.Comment == null &&
                    newFunction.Comment != null ||
                    oldFun.Comment != null &&
                    newFunction.Comment != null &&
                    !oldFun.Comment.Equals(
                        newFunction.Comment))
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON FUNCTION ");
                    writer.Write(PgDiffUtils.GetQuotedName(newFunction.Name));
                    writer.Write('(');

                    var addComma = false;

                    foreach (var argument in newFunction.GetArguments())
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.GetDeclaration(false));
                    }

                    writer.Write(") IS ");
                    writer.Write(newFunction.Comment);
                    writer.WriteLine(';');
                }
                else if (oldFun.Comment != null && newFunction.Comment == null)
                {
                    searchPathHelper.OutputSearchPath(writer);
                    writer.WriteLine();
                    writer.Write("COMMENT ON FUNCTION ");
                    writer.Write(PgDiffUtils.GetQuotedName(newFunction.Name));
                    writer.Write('(');

                    var addComma = false;

                    foreach (var argument in newFunction.GetArguments())
                    {
                        if (addComma)
                        {
                            writer.Write(", ");
                        }
                        else
                        {
                            addComma = true;
                        }

                        writer.Write(argument.GetDeclaration(false));
                    }

                    writer.WriteLine(") IS NULL;");
                }
            }
        }