Exemple #1
0
        private static void ParseColumn(Parser parser, PgTable table)
        {
            var column = new PgColumn(ParserUtils.GetObjectName(parser.ParseIdentifier()));

            table.AddColumn(column);
            column.ParseDefinition(parser.GetExpression());
        }
Exemple #2
0
        private static void ParseConstraint(Parser parser, PgTable table)
        {
            var constraint = new PgConstraint(ParserUtils.GetObjectName(parser.ParseIdentifier()));

            table.AddConstraint(constraint);
            constraint.Definition = parser.GetExpression();
            constraint.TableName  = table.Name;
        }
Exemple #3
0
        private static void ParseSchema(Parser parser, PgDatabase database)
        {
            var schemaName = ParserUtils.GetObjectName(parser.ParseIdentifier());
            var schema     = database.GetSchema(schemaName);

            parser.Expect("IS");
            schema.Comment = GetComment(parser);
            parser.Expect(";");
        }
Exemple #4
0
        private static void ParseTable(Parser parser, PgDatabase database)
        {
            var tableName  = parser.ParseIdentifier();
            var objectName = ParserUtils.GetObjectName(tableName);
            var schemaName = ParserUtils.GetSchemaName(tableName, database);

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

            parser.Expect("IS");
            table.Comment = GetComment(parser);
            parser.Expect(";");
        }
Exemple #5
0
        private static void ParseView(Parser parser, PgDatabase database)
        {
            var viewName   = parser.ParseIdentifier();
            var objectName = ParserUtils.GetObjectName(viewName);
            var schemaName = ParserUtils.GetSchemaName(viewName, database);

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

            parser.Expect("IS");
            view.Comment = GetComment(parser);
            parser.Expect(";");
        }
Exemple #6
0
        private static void ParseInherits(Parser parser, PgTable table)
        {
            parser.Expect("(");

            while (!parser.ExpectOptional(")"))
            {
                table.AddInherits(ParserUtils.GetObjectName(parser.ParseIdentifier()));
                if (parser.ExpectOptional(")"))
                {
                    break;
                }

                parser.Expect(",");
            }
        }
Exemple #7
0
        private static void ParseAddConstraint(Parser parser, PgTable table, PgSchema schema)
        {
            var constraintName = ParserUtils.GetObjectName(parser.ParseIdentifier());
            var constraint     = new PgConstraint(constraintName);

            constraint.TableName = table.Name;
            table.AddConstraint(constraint);

            if (parser.ExpectOptional("PRIMARY", "KEY"))
            {
                schema.AddPrimaryKey(constraint);
                constraint.Definition = "PRIMARY KEY " + parser.GetExpression();
            }
            else
            {
                constraint.Definition = parser.GetExpression();
            }
        }
Exemple #8
0
        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>();

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

            parser.Expect("AS");

            var query = parser.GetRest();

            var view = new PgView(ParserUtils.GetObjectName(viewName));

            view.ColumnNames = columnNames;
            view.Query       = query;

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

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

            schema.AddView(view);
        }
Exemple #9
0
        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.GetRest();
            var schemaName = ParserUtils.GetSchemaName(tableName, database);
            var schema     = database.GetSchema(schemaName);

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

            var objectName = ParserUtils.GetObjectName(tableName);
            var table      = schema.GetTable(objectName);

            if (table == null)
            {
                throw new Exception(string.Format(Resources.CannotFindTable, tableName, statement));
            }

            var index = new PgIndex(indexName);

            table.AddIndex(index);
            schema.AddIndex(index);
            index.Definition = definition.Trim();
            index.TableName  = table.Name;
            index.Unique     = unique;
        }
Exemple #10
0
        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);
            var schema     = database.GetSchema(schemaName);

            var table = schema.GetTable(tableName);

            if (table == null)
            {
                var 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
            {
                var column = table.GetColumn(objectName);

                if (column == null)
                {
                    throw new ParserException(string.Format(Resources.CannotFindColumnInTable, columnName, table.Name));
                }

                parser.Expect("IS");
                column.Comment = GetComment(parser);
                parser.Expect(";");
            }
        }
Exemple #11
0
 private static void ParseView(Parser parser, PgView view, bool outputIgnoredStatements, string viewName, PgDatabase database)
 {
     while (!parser.ExpectOptional(";"))
     {
         if (parser.ExpectOptional("ALTER"))
         {
             parser.ExpectOptional("COLUMN");
             var columnName = ParserUtils.GetObjectName(parser.ParseIdentifier());
             if (parser.ExpectOptional("SET", "DEFAULT"))
             {
                 var expression = parser.GetExpression();
                 view.AddColumnDefaultValue(columnName, expression);
             }
             else if (parser.ExpectOptional("DROP", "DEFAULT"))
             {
                 view.RemoveColumnDefaultValue(columnName);
             }
             else
             {
                 parser.ThrowUnsupportedCommand();
             }
         }
         else if (parser.ExpectOptional("OWNER", "TO"))
         {
             // we do not parse this one so we just consume the identifier
             if (outputIgnoredStatements)
             {
                 database.IgnoredStatements.Add($"ALTER TABLE {viewName} OWNER TO {parser.ParseIdentifier()};");
             }
             else
             {
                 parser.ParseIdentifier();
             }
         }
         else
         {
             parser.ThrowUnsupportedCommand();
         }
     }
 }
Exemple #12
0
        public static void Parse(PgDatabase database, string statement, bool outputIgnoredStatements)
        {
            var parser = new Parser(statement);

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

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

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

            var objectName = ParserUtils.GetObjectName(sequenceName);
            var sequence   = schema.GetSequence(objectName);

            if (sequence == null)
            {
                throw new Exception(string.Format(Resources.CannotFindSequence, sequenceName, statement));
            }


            while (!parser.ExpectOptional(";"))
            {
                if (parser.ExpectOptional("OWNED", "BY"))
                {
                    sequence.OwnedBy = parser.ExpectOptional("NONE")
                        ? null
                        : parser.GetExpression();
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }
            }
        }
Exemple #13
0
        private static void ParseIndex(Parser parser, PgDatabase database)
        {
            var indexName  = parser.ParseIdentifier();
            var objectName = ParserUtils.GetObjectName(indexName);
            var schemaName = ParserUtils.GetSchemaName(indexName, database);
            var schema     = database.GetSchema(schemaName);

            var index = schema.GetIndex(objectName);

            if (index == null)
            {
                var 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", "SCHEMA");

            if (parser.ExpectOptional("AUTHORIZATION"))
            {
                var schema = new PgSchema(ParserUtils.GetObjectName(parser.ParseIdentifier()));
                database.Schemas.Add(schema);
                schema.Authorization = schema.Name;

                var definition = parser.GetRest();

                if (!string.IsNullOrEmpty(definition))
                {
                    schema.Definition = definition;
                }
            }
            else
            {
                var schema = new PgSchema(ParserUtils.GetObjectName(parser.ParseIdentifier()));
                database.Schemas.Add(schema);

                if (parser.ExpectOptional("AUTHORIZATION"))
                {
                    schema.Authorization = ParserUtils.GetObjectName(parser.ParseIdentifier());
                }

                var definition = parser.GetRest();

                if (!string.IsNullOrEmpty(definition))
                {
                    schema.Definition = definition;
                }
            }
        }
Exemple #15
0
        private static void ParseAddForeignKey(Parser parser, PgTable table)
        {
            var columnNames = new List <string>();

            parser.Expect("(");

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

                parser.Expect(",");
            }

            var constraintName = ParserUtils.GenerateName(table.Name + "_", columnNames, "_fkey");
            var constraint     = new PgConstraint(constraintName);

            table.AddConstraint(constraint);
            constraint.Definition = parser.GetExpression();
            constraint.TableName  = table.Name;
        }
Exemple #16
0
        private static void ParseAlterColumn(Parser parser, PgTable table)
        {
            parser.ExpectOptional("COLUMN");

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

            if (parser.ExpectOptional("SET"))
            {
                if (parser.ExpectOptional("STATISTICS"))
                {
                    var column = table.GetColumn(columnName);
                    if (column == null)
                    {
                        throw new Exception(string.Format(Resources.CannotFindTableColumn, columnName, table.Name, parser.GetString()));
                    }

                    column.Statistics = parser.ParseInteger();
                }
                else if (parser.ExpectOptional("DEFAULT"))
                {
                    var defaultValue = parser.GetExpression();
                    if (table.ContainsColumn(columnName))
                    {
                        var column = table.GetColumn(columnName);
                        if (column == null)
                        {
                            throw new Exception(string.Format(Resources.CannotFindTableColumn, columnName, table.Name, parser.GetString()));
                        }

                        column.DefaultValue = defaultValue;
                    }
                    else
                    {
                        throw new ParserException(string.Format(Resources.CannotFindColumnInTable, columnName, table.Name));
                    }
                }
                else if (parser.ExpectOptional("STORAGE"))
                {
                    var column = table.GetColumn(columnName);

                    if (column == null)
                    {
                        throw new Exception(string.Format(Resources.CannotFindTableColumn, columnName, table.Name,
                                                          parser.GetString()));
                    }

                    if (parser.ExpectOptional("PLAIN"))
                    {
                        column.Storage = "PLAIN";
                    }
                    else if (parser.ExpectOptional("EXTERNAL"))
                    {
                        column.Storage = "EXTERNAL";
                    }
                    else if (parser.ExpectOptional("EXTENDED"))
                    {
                        column.Storage = "EXTENDED";
                    }
                    else if (parser.ExpectOptional("MAIN"))
                    {
                        column.Storage = "MAIN";
                    }
                    else
                    {
                        parser.ThrowUnsupportedCommand();
                    }
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }
            }
            else
            {
                parser.ThrowUnsupportedCommand();
            }
        }
        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);
            var schema       = database.GetSchema(schemaName);

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

            schema.AddSequence(sequence);

            while (!parser.ExpectOptional(";"))
            {
                if (parser.ExpectOptional("INCREMENT"))
                {
                    parser.ExpectOptional("BY");
                    sequence.Increment = parser.ParseString();
                }
                else if (parser.ExpectOptional("MINVALUE"))
                {
                    sequence.MinValue = parser.ParseString();
                }
                else if (parser.ExpectOptional("MAXVALUE"))
                {
                    sequence.MaxValue = parser.ParseString();
                }
                else if (parser.ExpectOptional("START"))
                {
                    parser.ExpectOptional("WITH");
                    sequence.StartWith = parser.ParseString();
                }
                else if (parser.ExpectOptional("CACHE"))
                {
                    sequence.Cache = parser.ParseString();
                }
                else if (parser.ExpectOptional("CYCLE"))
                {
                    sequence.Cycle = true;
                }
                else if (parser.ExpectOptional("OWNED", "BY"))
                {
                    sequence.OwnedBy = parser.ExpectOptional("NONE")
                        ? null
                        : 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
                    {
                        parser.ThrowUnsupportedCommand();
                    }
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }
            }
        }
Exemple #18
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();
        }
        public static void Parse(PgDatabase database, string statement, bool ignoreSlonyTriggers)
        {
            var parser = new Parser(statement);

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

            var triggerName = parser.ParseIdentifier();
            var objectName  = ParserUtils.GetObjectName(triggerName);

            var trigger = new PgTrigger();

            trigger.Name = objectName;

            if (parser.ExpectOptional("BEFORE"))
            {
                trigger.Before = true;
            }
            else if (parser.ExpectOptional("AFTER"))
            {
                trigger.Before = false;
            }

            var first = true;

            while (true)
            {
                if (!first && !parser.ExpectOptional("OR"))
                {
                    break;
                }

                if (parser.ExpectOptional("INSERT"))
                {
                    trigger.OnInsert = true;
                }
                else if (parser.ExpectOptional("UPDATE"))
                {
                    trigger.OnUpdate = true;

                    if (parser.ExpectOptional("OF"))
                    {
                        do
                        {
                            trigger.AddUpdateColumn(parser.ParseIdentifier());
                        }while (parser.ExpectOptional(","));
                    }
                }
                else if (parser.ExpectOptional("DELETE"))
                {
                    trigger.OnDelete = true;
                }
                else if (parser.ExpectOptional("TRUNCATE"))
                {
                    trigger.OnTruncate = true;
                }
                else if (first)
                {
                    break;
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }

                first = false;
            }

            parser.Expect("ON");

            var tableName = parser.ParseIdentifier();

            trigger.TableName = ParserUtils.GetObjectName(tableName);

            if (parser.ExpectOptional("FOR"))
            {
                parser.ExpectOptional("EACH");

                if (parser.ExpectOptional("ROW"))
                {
                    trigger.ForEachRow = true;
                }
                else if (parser.ExpectOptional("STATEMENT"))
                {
                    trigger.ForEachRow = false;
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }
            }

            if (parser.ExpectOptional("WHEN"))
            {
                parser.Expect("(");
                trigger.When = parser.GetExpression();
                parser.Expect(")");
            }

            parser.Expect("EXECUTE", "PROCEDURE");
            trigger.Function = parser.GetRest();

            var ignoreSlonyTrigger = ignoreSlonyTriggers &&
                                     ("_slony_logtrigger".Equals(trigger.Name) ||
                                      "_slony_denyaccess".Equals(trigger.Name));

            if (!ignoreSlonyTrigger)
            {
                var tableSchema = database.GetSchema(ParserUtils.GetSchemaName(tableName, database));
                tableSchema.GetTable(trigger.TableName).AddTrigger(trigger);
            }
        }
Exemple #20
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(";");
        }
Exemple #21
0
        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 tableName  = parser.ParseIdentifier();
            var table      = new PgTable(ParserUtils.GetObjectName(tableName));
            var schemaName = ParserUtils.GetSchemaName(tableName, database);
            var schema     = database.GetSchema(schemaName);

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

            schema.AddTable(table);

            parser.Expect("(");

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

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

                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.GetExpression();
                    }
                }
                else if (parser.ExpectOptional("TABLESPACE"))
                {
                    table.Tablespace = parser.ParseString();
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }
            }
        }
Exemple #22
0
        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);
            var schema     = database.GetSchema(schemaName);

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


            var objectName = ParserUtils.GetObjectName(viewName);
            var view       = schema.GetView(objectName);

            if (view == null)
            {
                throw new Exception(string.Format(Resources.CannotFindView, viewName, 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.GetExpression();
                        view.AddColumnDefaultValue(columnName, expression);
                    }
                    else if (parser.ExpectOptional("DROP", "DEFAULT"))
                    {
                        view.RemoveColumnDefaultValue(columnName);
                    }
                    else
                    {
                        parser.ThrowUnsupportedCommand();
                    }
                }
                else if (parser.ExpectOptional("OWNER", "TO"))
                {
                    // we do not parse this one so we just consume the identifier
                    if (outputIgnoredStatements)
                    {
                        database.IgnoredStatements.Add($"ALTER TABLE {viewName} OWNER TO {parser.ParseIdentifier()};");
                    }
                    else
                    {
                        parser.ParseIdentifier();
                    }
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }
            }
        }
Exemple #23
0
        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);
            var schema     = database.GetSchema(schemaName);

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

            var objectName = ParserUtils.GetObjectName(tableName);
            var table      = schema.GetTable(objectName);

            if (table == null)
            {
                var view = schema.GetView(objectName);

                if (view != null)
                {
                    ParseView(parser, view, outputIgnoredStatements, tableName, database);
                    return;
                }

                var sequence = schema.GetSequence(objectName);

                if (sequence != null)
                {
                    ParseSequence(parser, sequence, outputIgnoredStatements, tableName, database);
                    return;
                }

                throw new Exception(string.Format(Resources.CannotFindObject, tableName, 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"))
                {
                    if (outputIgnoredStatements)
                    {
                        database.IgnoredStatements.Add($"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
                    {
                        parser.ThrowUnsupportedCommand();
                    }
                }
                else if (parser.ExpectOptional("ENABLE"))
                {
                    ParseEnable(parser, outputIgnoredStatements, tableName, database);
                }
                else if (parser.ExpectOptional("DISABLE"))
                {
                    ParseDisable(parser, outputIgnoredStatements, tableName, database);
                }
                else
                {
                    parser.ThrowUnsupportedCommand();
                }

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