~ubuntu-branches/ubuntu/raring/apgdiff/raring

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/parsers/AlterViewParser.java

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2010-10-11 09:08:18 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20101011090818-sdw8yfemrnxo328k
Tags: 2.2.2-1
* New upstream version.
* Using changelog included in zipfile, thanks Miroslav for providing this.
* Update manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2006 StartNet s.r.o.
 
3
 *
 
4
 * Distributed under MIT license
 
5
 */
 
6
package cz.startnet.utils.pgdiff.parsers;
 
7
 
 
8
import cz.startnet.utils.pgdiff.schema.PgDatabase;
 
9
import cz.startnet.utils.pgdiff.schema.PgView;
 
10
 
 
11
/**
 
12
 * Parses ALTER VIEW statements.
 
13
 * 
 
14
 * @author fordfrog
 
15
 */
 
16
public class AlterViewParser {
 
17
 
 
18
    /**
 
19
     * Creates new instance of AlterViewParser.
 
20
     */
 
21
    private AlterViewParser() {
 
22
    }
 
23
 
 
24
    /**
 
25
     * Parses ALTER VIEW statement.
 
26
     * 
 
27
     * @param database database
 
28
     * @param statement ALTER VIEW statement
 
29
     * @param outputIgnoredStatements whether ignored statements should be
 
30
     * output in the diff
 
31
     */
 
32
    public static void parse(final PgDatabase database,
 
33
            final String statement, final boolean outputIgnoredStatements) {
 
34
        final Parser parser = new Parser(statement);
 
35
        parser.expect("ALTER", "VIEW");
 
36
 
 
37
        final String viewName = parser.parseIdentifier();
 
38
        final String schemaName = ParserUtils.getSchemaName(viewName, database);
 
39
        final String objectName = ParserUtils.getObjectName(viewName);
 
40
 
 
41
        final PgView view = database.getSchema(schemaName).getView(objectName);
 
42
 
 
43
        while (!parser.expectOptional(";")) {
 
44
            if (parser.expectOptional("ALTER")) {
 
45
                parser.expectOptional("COLUMN");
 
46
 
 
47
                final String columnName =
 
48
                        ParserUtils.getObjectName(parser.parseIdentifier());
 
49
 
 
50
                if (parser.expectOptional("SET", "DEFAULT")) {
 
51
                    final String expression = parser.getExpression();
 
52
                    view.addColumnDefaultValue(columnName, expression);
 
53
                } else if (parser.expectOptional("DROP", "DEFAULT")) {
 
54
                    view.removeColumnDefaultValue(columnName);
 
55
                } else {
 
56
                    parser.throwUnsupportedCommand();
 
57
                }
 
58
            } else if (parser.expectOptional("OWNER", "TO")) {
 
59
                // we do not parse this one so we just consume the identifier
 
60
                if (outputIgnoredStatements) {
 
61
                    database.addIgnoredStatement("ALTER TABLE " + viewName
 
62
                            + " OWNER TO " + parser.parseIdentifier() + ';');
 
63
                } else {
 
64
                    parser.parseIdentifier();
 
65
                }
 
66
            } else {
 
67
                parser.throwUnsupportedCommand();
 
68
            }
 
69
        }
 
70
    }
 
71
}