Esempio n. 1
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(";");
        }
Esempio n. 2
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();
        }
Esempio n. 3
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();
        }