/// <summary>
        /// Parses COMMENT ON CONSTRAINT.
        /// </summary>
        private static void ParseConstraint(Parser parser, PgDatabase database)
        {
            var constraintName = ParserUtils.GetObjectName(parser.ParseIdentifier());

            parser.Expect("ON");

            var tableName  = parser.ParseIdentifier();
            var objectName = ParserUtils.GetObjectName(tableName);
            var schemaName = ParserUtils.GetSchemaName(constraintName, database);

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

            PgConstraint constraint = database.GetSchema(schemaName).GetTable(objectName).GetConstraint(constraintName);

            parser.Expect("IS");
            constraint.Comment = GetComment(parser);
            parser.Expect(";");
        }
Example #2
0
        /// <summary>
        /// Parses ADD CONSTRAINT action.
        /// </summary>
        private static void ParseAddConstraint(Parser parser, PgTable table, PgSchema schema)
        {
            var constraintName = ParserUtils.GetObjectName(parser.ParseIdentifier());

            var constraint = new PgConstraint(constraintName)
            {
                TableName = table.Name,
            };

            table.AddConstraint(constraint);

            if (parser.ExpectOptional("PRIMARY", "KEY"))
            {
                schema.Add(constraint);
                constraint.Definition = "PRIMARY KEY " + parser.Expression();
            }
            else
            {
                constraint.Definition = parser.Expression();
            }
        }
        /// <summary>
        /// Parses COMMENT ON TRIGGER.
        /// </summary>
        private static void ParseTrigger(Parser parser, PgDatabase database)
        {
            var triggerName = ParserUtils.GetObjectName(parser.ParseIdentifier());

            parser.Expect("ON");

            var tableName = parser.ParseIdentifier();

            var objectName = ParserUtils.GetObjectName(tableName);

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

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

            PgTrigger trigger = database.GetSchema(schemaName).GetTable(objectName).GetTrigger(triggerName);

            parser.Expect("IS");
            trigger.Comment = GetComment(parser);
            parser.Expect(";");
        }
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
        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);
        }
Example #6
0
        /// <summary>
        /// Parses ALTER COLUMN action.
        /// </summary>
        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"))
                {
                    PgColumn column = table.GetColumn(columnName);

                    if (column == null)
                    {
                        throw new TeamworkParserException($"CannotFindTableColumn {columnName} in table {table.Name} from {parser.String}");
                    }

                    column.Statistics = parser.ParseInteger();
                }
                else if (parser.ExpectOptional("DEFAULT"))
                {
                    var defaultValue = parser.Expression();

                    if (table.ContainsColumn(columnName))
                    {
                        PgColumn column = table.GetColumn(columnName);

                        if (column == null)
                        {
                            throw new TeamworkParserException($"CannotFindTableColumn {columnName} in table {table.Name} from {parser.String}");
                        }

                        column.DefaultValue = defaultValue;
                    }
                    else
                    {
                        throw new TeamworkParserException($"CannotFindColumnInTable {columnName} in table {table.Name}");
                    }
                }
                else if (parser.ExpectOptional("STORAGE"))
                {
                    PgColumn column = table.GetColumn(columnName);

                    if (column == null)
                    {
                        throw new TeamworkParserException($"CannotFindTableColumn {columnName} in table {table.Name} from {parser.String}");
                    }

                    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
                    {
                        throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                    }
                }
                else
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }
            }
            else
            {
                throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
            }
        }
Example #7
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(",");
                }
            }
        }
Example #8
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(";");
        }
Example #11
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();
        }
Example #13
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);
            }
        }
        /// <summary>
        /// Parses CREATE TRIGGER statement.
        /// </summary>
        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()
            {
                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;
                }
                else 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
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }

                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
                {
                    throw new TeamworkParserException("CannotParseStringUnsupportedCommand");
                }
            }

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

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

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

            if (!ignoreSlonyTrigger)
            {
                PgSchema tableSchema = database.GetSchema(ParserUtils.GetSchemaName(tableName, database));
                tableSchema.GetTable(trigger.TableName).AddTrigger(trigger);
            }
        }