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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/parsers/CreateViewParser.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
 */
1
6
package cz.startnet.utils.pgdiff.parsers;
2
7
 
3
8
import cz.startnet.utils.pgdiff.schema.PgDatabase;
4
9
import cz.startnet.utils.pgdiff.schema.PgSchema;
5
10
import cz.startnet.utils.pgdiff.schema.PgView;
6
 
 
7
 
import java.util.regex.Matcher;
8
 
import java.util.regex.Pattern;
 
11
import java.util.ArrayList;
 
12
import java.util.List;
9
13
 
10
14
/**
11
 
 * Parses CREATE VIEW commands.
 
15
 * Parses CREATE VIEW statements.
12
16
 *
13
17
 * @author fordfrog
14
18
 */
15
19
public class CreateViewParser {
16
20
 
17
21
    /**
18
 
     * Pattern for parsing CREATE VIEW definition.
19
 
     */
20
 
    private static final Pattern PATTERN = Pattern.compile(
21
 
            "CREATE[\\s]+(?:OR[\\s]+REPLACE[\\s]+)?VIEW[\\s]+"
22
 
            + "\"?([^\\s\"]+)\"?[\\s]+(?:\\(([^)]+)\\)[\\s]+)?"
23
 
            + "AS[\\s]+(.+)?(?:;)", Pattern.CASE_INSENSITIVE);
24
 
 
25
 
    /**
26
22
     * Creates a new instance of CreateViewParser.
27
23
     */
28
24
    private CreateViewParser() {
29
 
        super();
30
25
    }
31
26
 
32
27
    /**
33
 
     * Parses CREATE VIEW command.
 
28
     * Parses CREATE VIEW statement.
34
29
     *
35
30
     * @param database database
36
 
     * @param command CREATE VIEW command
37
 
     *
38
 
     * @throws ParserException Thrown if problem occured while parsing the
39
 
     *         command.
 
31
     * @param statement CREATE VIEW statement
40
32
     */
41
 
    public static void parse(final PgDatabase database, final String command) {
42
 
        final Matcher matcher = PATTERN.matcher(command.trim());
43
 
 
44
 
        if (matcher.matches()) {
45
 
            final String viewName = matcher.group(1);
46
 
            final String columnNames = matcher.group(2);
47
 
            final String query = matcher.group(3);
48
 
 
49
 
            if ((viewName == null) || (query == null)) {
50
 
                throw new ParserException(
51
 
                        ParserException.CANNOT_PARSE_COMMAND + command);
 
33
    public static void parse(final PgDatabase database,
 
34
            final String statement) {
 
35
        final Parser parser = new Parser(statement);
 
36
        parser.expect("CREATE");
 
37
        parser.expectOptional("OR", "REPLACE");
 
38
        parser.expect("VIEW");
 
39
 
 
40
        final String viewName = parser.parseIdentifier();
 
41
 
 
42
        final boolean columnsExist = parser.expectOptional("(");
 
43
        final List<String> columnNames = new ArrayList<String>(10);
 
44
 
 
45
        if (columnsExist) {
 
46
            while (!parser.expectOptional(")")) {
 
47
                columnNames.add(
 
48
                        ParserUtils.getObjectName(parser.parseIdentifier()));
 
49
                parser.expectOptional(",");
52
50
            }
53
 
 
54
 
            final PgView view = new PgView(ParserUtils.getObjectName(viewName));
55
 
            view.setColumnNames(columnNames);
56
 
            view.setQuery(query);
57
 
 
58
 
            final PgSchema schema =
59
 
                    database.getSchema(
60
 
                    ParserUtils.getSchemaName(viewName, database));
61
 
            schema.addView(view);
62
 
        } else {
63
 
            throw new ParserException(
64
 
                    ParserException.CANNOT_PARSE_COMMAND + command);
65
51
        }
 
52
 
 
53
        parser.expect("AS");
 
54
 
 
55
        final String query = parser.getRest();
 
56
 
 
57
        final PgView view = new PgView(ParserUtils.getObjectName(viewName));
 
58
        view.setColumnNames(columnNames);
 
59
        view.setQuery(query);
 
60
 
 
61
        final PgSchema schema = database.getSchema(
 
62
                ParserUtils.getSchemaName(viewName, database));
 
63
        schema.addView(view);
66
64
    }
67
65
}