Example #1
0
        internal static Collection <PgFunction> GetFunctions(DataTable table)
        {
            Collection <PgFunction> procedures = new Collection <PgFunction>();

            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    PgFunction function = new PgFunction
                    {
                        FunctionOid        = Conversion.TryCastString(row["oid"]),
                        FunctionDefinition = Conversion.TryCastString(row["definition"]),
                        RowNumber          = Conversion.TryCastLong(row["row_number"]),
                        Name         = Conversion.TryCastString(row["function_name"]),
                        SchemaName   = Conversion.TryCastString(row["object_schema"]),
                        Arguments    = Conversion.TryCastString(row["arguments"]),
                        ResultType   = Conversion.TryCastString(row["result_type"]),
                        FunctionType = Conversion.TryCastString(row["function_type"]),
                        Owner        = Conversion.TryCastString(row["owner"]),
                        Description  = Conversion.TryCastString(row["description"])
                    };


                    procedures.Add(function);
                }
            }

            return(procedures);
        }
Example #2
0
        private static void BuildDocumentation(string content, PgFunction function)
        {
            content = content.Replace("[DBName]", Program.Database.ToUpperInvariant());

            content = Parsers.FunctionParser.Parse(content, function);

            string targetPath = System.IO.Path.Combine(OutputPath, function.SchemaName, function.Name + "-" + function.FunctionOid + ".html");

            FileHelper.WriteFile(content, targetPath);
        }
Example #3
0
        internal static string Parse(string content, PgFunction procedure)
        {
            StringBuilder items = new StringBuilder();

            items.Append(content.Replace("[Name]", procedure.Name)
                         .Replace("[TriggerSchema]", procedure.SchemaName)
                         .Replace("[FunctionSchema]", procedure.SchemaName)
                         .Replace("[Arguments]", procedure.Arguments)
                         .Replace("[RowNumber]", procedure.RowNumber.ToString())
                         .Replace("[Owner]", procedure.Owner)
                         .Replace("[ResultType]", procedure.ResultType)
                         .Replace("[FunctionType]", procedure.FunctionType)
                         .Replace("[FunctionOid]", procedure.FunctionOid)
                         .Replace("[Definition]", procedure.FunctionDefinition)
                         .Replace("[Description]", procedure.Description));

            content = content.Replace(content, items.ToString());

            return(content);
        }
Example #4
0
        /// <summary>
        /// Parses CREATE FUNCTION and CREATE OR REPLACE FUNCTION statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

            parser.Expect("CREATE");
            parser.ExpectOptional("OR", "REPLACE");
            parser.Expect("FUNCTION");

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

            PgSchema schema = database.GetSchema(schemaName);

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

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

            var function = new PgFunction()
            {
                Name = ParserUtils.GetObjectName(functionName),
            };

            schema.Add(function);

            parser.Expect("(");

            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.ExpectOptional("=") && !parser.ExpectOptional("DEFAULT"))
                {
                    parser.Position = position;
                    argumentName    = ParserUtils.GetObjectName(parser.ParseIdentifier());
                    dataType        = parser.ParseDataType();
                }
                else
                {
                    parser.Position = position2;
                }

                string defaultExpression;
                if (parser.ExpectOptional("=") || parser.ExpectOptional("DEFAULT"))
                {
                    defaultExpression = parser.Expression();
                }
                else
                {
                    defaultExpression = null;
                }

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

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

            function.Body = parser.Rest();
        }
Example #5
0
        private static void ParseFunction(Parser parser, PgDatabase database)
        {
            var functionName = parser.ParseIdentifier();
            var objectName   = ParserUtils.GetObjectName(functionName);
            var schemaName   = ParserUtils.GetSchemaName(functionName, database);
            var schema       = database.GetSchema(schemaName);

            parser.Expect("(");

            var tmpFunction = new PgFunction();

            tmpFunction.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.GetPosition();
                string argumentName = null;
                var    dataType     = parser.ParseDataType();

                var position2 = parser.GetPosition();

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

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

                if (parser.ExpectOptional(")"))
                {
                    break;
                }

                parser.Expect(",");
            }

            var function =
                schema.GetFunction(tmpFunction.GetSignature());

            parser.Expect("IS");
            function.Comment = GetComment(parser);
            parser.Expect(";");
        }
        /// <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;");
                }
            }
        }
Example #7
0
        internal static void Run(PgFunction function)
        {
            string content = FileHelper.ReadResource(TemplatePath);

            BuildDocumentation(content, function);
        }
Example #8
0
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

            parser.Expect("CREATE");
            parser.ExpectOptional("OR", "REPLACE");
            parser.Expect("FUNCTION");

            var functionName = parser.ParseIdentifier();
            var schemaName   = ParserUtils.GetSchemaName(functionName, database);
            var schema       = database.GetSchema(schemaName);

            if (schema == null)
            {
                throw new Exception(string.Format(Resources.CannotFindSchema, schemaName, statement));
            }

            var function = new PgFunction();

            function.Name = ParserUtils.GetObjectName(functionName);
            schema.AddFunction(function);

            parser.Expect("(");

            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.GetPosition();
                string argumentName = null;
                var    dataType     = parser.ParseDataType();

                var position2 = parser.GetPosition();

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

                string defaultExpression;

                if (parser.ExpectOptional("=") || parser.ExpectOptional("DEFAULT"))
                {
                    defaultExpression = parser.GetExpression();
                }
                else
                {
                    defaultExpression = null;
                }

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

                if (parser.ExpectOptional(")"))
                {
                    break;
                }

                parser.Expect(",");
            }

            function.Body = parser.GetRest();
        }