/// <summary>
        /// Parses COMMENT ON INDEX.
        /// </summary>
        private static void ParseIndex(Parser parser, PgDatabase database)
        {
            var indexName = parser.ParseIdentifier();

            var objectName = ParserUtils.GetObjectName(indexName);

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

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

            PgSchema schema = database.GetSchema(schemaName);

            PgIndex index = schema.GetIndex(objectName);

            if (index == null)
            {
                PgConstraint primaryKey = schema.GetPrimaryKey(objectName);
                parser.Expect("IS");
                primaryKey.Comment = GetComment(parser);
                parser.Expect(";");
            }
            else
            {
                parser.Expect("IS");
                index.Comment = GetComment(parser);
                parser.Expect(";");
            }
        }
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

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

            var ruleIdentifier = parser.ParseIdentifier();
            var rule           = new PgRule(ParserUtils.GetObjectName(ruleIdentifier));
            var schemaName     = ParserUtils.GetSchemaName(ruleIdentifier, database);

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

            parser.Expect("AS", "ON");
            rule.EventType = parser.ExpectOptionalOneOf("SELECT", "INSERT", "UPDATE", "DELETE");
            parser.Expect("TO");
            var tableIdentifier = parser.ParseIdentifier();

            if (parser.ExpectOptional("WHERE"))
            {
                var endIndex = parser.String.IndexOf("DO");
                rule.Condition = parser.GetSubString(0, endIndex);
            }

            rule.TableName = ParserUtils.GetObjectName(tableIdentifier);

            parser.Expect("DO");
            rule.Do      = parser.ExpectOptionalOneOf("ALSO", "INSTEAD");
            rule.Command = parser.Rest() + ";";

            PgSchema schema = database.GetSchema(schemaName);

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

            // check if the rule is a view
            if (rule.EventType == "SELECT" && rule.Do == "INSTEAD")
            {
                var table = schema.Tables.Single(t => t.Name == rule.TableName);
                schema.Tables.Remove(table);
                schema.Views.Add(new PgView(table.Name)
                {
                    Comment = table.Comment, Query = rule.Command
                });
            }
            else
            {
                schema.Rules.Add(rule);
            }
        }
        /// <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(";");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parses CREATE VIEW statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

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

            var viewName = parser.ParseIdentifier();

            var columnsExist = parser.ExpectOptional("(");

            var columnNames = new List <string>(10);

            if (columnsExist)
            {
                while (!parser.ExpectOptional(")"))
                {
                    columnNames.Add(ParserUtils.GetObjectName(parser.ParseIdentifier()));
                    parser.ExpectOptional(",");
                }
            }

            parser.ExpectOptional("WITH", "(security_barrier='false')");

            parser.Expect("AS");

            var query = parser.Rest();

            var view = new PgView(ParserUtils.GetObjectName(viewName))
            {
                ColumnNames = columnNames,
                Query       = query,
            };
            var schemaName = ParserUtils.GetSchemaName(viewName, database);

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

            PgSchema schema = database.GetSchema(schemaName);

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

            schema.Add(view);
        }
        /// <summary>
        /// Parses ALTER SEQUENCE statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

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

            var sequenceName = parser.ParseIdentifier();

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

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

            var schema = database.GetSchema(schemaName);

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

            var objectName = ParserUtils.GetObjectName(sequenceName);

            PgSequence sequence = schema.GetSequence(objectName);

            if (sequence == null)
            {
                throw new TeamworkParserException($"CannotFindSequence {sequenceName} from statement {statement}");
            }

            while (!parser.ExpectOptional(";"))
            {
                if (parser.ExpectOptional("OWNED", "BY"))
                {
                    if (parser.ExpectOptional("NONE"))
                    {
                        sequence.Owner = null;
                    }
                    else
                    {
                        sequence.Owner = parser.Expression();
                    }
                }
                else
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Parses CREATE INDEX statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

            parser.Expect("CREATE");

            var unique = parser.ExpectOptional("UNIQUE");

            parser.Expect("INDEX");
            parser.ExpectOptional("CONCURRENTLY");

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

            parser.Expect("ON");

            var tableName = parser.ParseIdentifier();

            var definition = parser.Rest();

            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)
            {
                throw new TeamworkParserException($"CannotFindTable {tableName} from {statement}");
            }

            var index = new PgIndex(indexName);

            table.AddIndex(index);
            schema.Add(index);
            index.Definition = definition.Trim();
            index.TableName  = table.Name;
            index.Unique     = unique;
        }
        /// <summary>
        /// Parses COMMENT ON TABLE.
        /// </summary>
        private static void ParseTable(Parser parser, PgDatabase database)
        {
            var tableName = parser.ParseIdentifier();

            var objectName = ParserUtils.GetObjectName(tableName);

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

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

            PgTable table = database.GetSchema(schemaName).GetTable(objectName);

            parser.Expect("IS");
            table.Comment = GetComment(parser);
            parser.Expect(";");
        }
        /// <summary>
        /// Parses COMMENT ON VIEW.
        /// </summary>
        private static void ParseView(Parser parser, PgDatabase database)
        {
            var viewName = parser.ParseIdentifier();

            var objectName = ParserUtils.GetObjectName(viewName);

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

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

            PgView view = database.GetSchema(schemaName).GetView(objectName);

            parser.Expect("IS");
            view.Comment = GetComment(parser);
            parser.Expect(";");
        }
Ejemplo n.º 9
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(",");
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Parses CREATE SEQUENCE statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

            parser.Expect("CREATE", "SEQUENCE");

            var sequenceName = parser.ParseIdentifier();

            var sequence = new PgSequence(ParserUtils.GetObjectName(sequenceName));

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

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

            PgSchema schema = database.GetSchema(schemaName);

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

            schema.Add(sequence);

            while (!parser.ExpectOptional(";"))
            {
                if (parser.ExpectOptional("INCREMENT"))
                {
                    parser.ExpectOptional("BY");
#pragma warning disable CS0618 // Type or member is obsolete
                    sequence.Increment = parser.ParseStringCompat();
#pragma warning restore CS0618 // Type or member is obsolete
                }
                else if (parser.ExpectOptional("MINVALUE"))
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    sequence.MinValue = parser.ParseStringCompat();
#pragma warning restore CS0618 // Type or member is obsolete
                }
                else if (parser.ExpectOptional("MAXVALUE"))
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    sequence.MaxValue = parser.ParseStringCompat();
#pragma warning restore CS0618 // Type or member is obsolete
                }
                else if (parser.ExpectOptional("START"))
                {
                    parser.ExpectOptional("WITH");
#pragma warning disable CS0618 // Type or member is obsolete
                    sequence.StartWith = parser.ParseStringCompat();
#pragma warning restore CS0618 // Type or member is obsolete
                }
                else if (parser.ExpectOptional("CACHE"))
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    sequence.Cache = parser.ParseStringCompat();
#pragma warning restore CS0618 // Type or member is obsolete
                }
                else if (parser.ExpectOptional("CYCLE"))
                {
                    sequence.Cycle = true;
                }
                else if (parser.ExpectOptional("OWNED", "BY"))
                {
                    if (parser.ExpectOptional("NONE"))
                    {
                        sequence.Owner = null;
                    }
                    else
                    {
                        sequence.Owner = ParserUtils.GetObjectName(parser.ParseIdentifier());
                    }
                }
                else if (parser.ExpectOptional("NO"))
                {
                    if (parser.ExpectOptional("MINVALUE"))
                    {
                        sequence.MinValue = null;
                    }
                    else if (parser.ExpectOptional("MAXVALUE"))
                    {
                        sequence.MaxValue = null;
                    }
                    else if (parser.ExpectOptional("CYCLE"))
                    {
                        sequence.Cycle = false;
                    }
                    else
                    {
                        throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                    }
                }
                else
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }
            }
        }
        /// <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");
                }
            }
        }
        /// <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(";");
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Parses CREATE TABLE statement.
        /// </summary>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

            parser.Expect("CREATE", "TABLE");

            // Optional IF NOT EXISTS, irrelevant for our purposes
            parser.ExpectOptional("IF", "NOT", "EXISTS");

            var tableIdentifier = parser.ParseIdentifier();
            var table           = new PgTable(ParserUtils.GetObjectName(tableIdentifier));
            var schemaName      = ParserUtils.GetSchemaName(tableIdentifier, database);

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

            var schema = database.GetSchema(schemaName);

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

            schema.Add(table);

            parser.Expect("(");

            while (!parser.ExpectOptional(")"))
            {
                if (parser.ExpectOptional("CONSTRAINT"))
                {
                    ParseConstraint(parser, table);
                }
                else
                {
                    ParseColumn(parser, table);
                }

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

            while (!parser.ExpectOptional(";"))
            {
                if (parser.ExpectOptional("INHERITS"))
                {
                    ParseInherits(parser, table);
                }
                else if (parser.ExpectOptional("WITHOUT"))
                {
                    table.With = "OIDS=false";
                }
                else if (parser.ExpectOptional("WITH"))
                {
                    if (parser.ExpectOptional("OIDS") || parser.ExpectOptional("OIDS=true"))
                    {
                        table.With = "OIDS=true";
                    }
                    else if (parser.ExpectOptional("OIDS=false"))
                    {
                        table.With = "OIDS=false";
                    }
                    else
                    {
                        table.With = parser.Expression();
                    }
                }
                else if (parser.ExpectOptional("TABLESPACE"))
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    table.Tablespace = parser.ParseStringCompat();
#pragma warning restore CS0618 // Type or member is obsolete
                }
                else
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }
            }
        }
        /// <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();
        }
Ejemplo n.º 15
0
        public static void Parse(PgDatabase database, string statement, PgPrivilegeCommand command)
        {
            var parser = new Parser(statement);

            parser.Expect(command.ToString().ToUpper());
            var privilegeKind = parser.ExpectOptionalOneOf(enumEntries.Select(k => k.ToString().ToUpper()).ToArray());
            var kind          = enumEntries.Single(k => k.ToString().ToUpper() == privilegeKind);

            parser.Expect("ON");

            var type           = parser.ParseIdentifier().ToUpper();
            var name           = parser.ParseIdentifier();
            var nameWithSchema = name;

            if (parser.ExpectOptional("("))
            {
                name += "(";
                while (!parser.ExpectOptional(")"))
                {
                    var argumentName = ParserUtils.GetObjectName(parser.ParseIdentifier());
                    var dataType     = parser.ParseDataType();
                    name += $"{argumentName} {dataType}";

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

                name += ")";
            }

            switch (command)
            {
            case PgPrivilegeCommand.Grant:
                parser.Expect("TO");
                break;

            case PgPrivilegeCommand.Revoke:
                parser.Expect("FROM");
                break;

            default:
                throw new ArgumentException($"The postgres privilege command {command} is unknown.");
            }

            var role = parser.ParseIdentifier();

            parser.Expect(";");

            var privilege = new PgPrivilege()
            {
                Command   = command,
                Privilege = kind,
                OnType    = type,
                OnName    = name,
                Role      = role,
            };

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

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

            var schema = database.GetSchema(schemaName);

            if (schema == null)
            {
                throw new Exception($"Cannot find schema {schemaName} for statement {statement}.");
            }

            schema.Add(privilege);
        }
Ejemplo n.º 16
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();
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Parses the given statement and adds the <see cref="PgType"/> to the <see cref="PgDatabase"/>.
        /// </summary>
        /// <param name="database">The <see cref="PgDatabase"/> to which the <see cref="PgType"/> is added.</param>
        /// <param name="statement">The SQL statement of the <see cref="PgType"/>.</param>
        public static void Parse(PgDatabase database, string statement)
        {
            var parser = new Parser(statement);

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

            var typeName = parser.ParseIdentifier();

            parser.Expect("AS");

            // check if type is enum
            if (parser.ExpectOptional("ENUM"))
            {
                var enumEntries = new List <string>();

                var columnsExist = parser.ExpectOptional("(");

                if (columnsExist)
                {
                    while (!parser.ExpectOptional(")"))
                    {
#pragma warning disable CS0618 // Type or member is obsolete
                        var entry = parser.ParseStringCompat();
#pragma warning restore CS0618 // Type or member is obsolete
                        enumEntries.Add(ParserUtils.GetObjectName(entry));
                        parser.ExpectOptional(",");
                    }
                }

                var type = new PgType(ParserUtils.GetObjectName(typeName), true)
                {
                    EnumEntries = enumEntries,
                };
                var schemaName = ParserUtils.GetSchemaName(typeName, database);

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

                PgSchema schema = database.GetSchema(schemaName);
                if (schema == null)
                {
                    throw new Exception(string.Format("Cannot find schema {0}. Statement {1}", schemaName, statement));
                }

                schema.Add(type);
            }
            else
            {
                var type = new PgType(ParserUtils.GetObjectName(typeName), false);

                var arguments = new List <PgArgument>();

                // type is no enum
                parser.Expect("(");

                var attributeType = new StringBuilder();

                while (true)
                {
                    var name     = parser.ParseIdentifier();
                    var datatype = parser.ParseDataType();

                    arguments.Add(new PgArgument(name, datatype));

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

                type.AttributeArguments = arguments;

                parser.Expect(";");

                var      schemaName = ParserUtils.GetSchemaName(typeName, database);
                PgSchema schema     = database.GetSchema(schemaName);
                if (schema == null)
                {
                    throw new Exception(string.Format("Cannot find schema {0}. Statement {1}", schemaName, statement));
                }

                schema.Add(type);
            }
        }