~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: 2008-09-09 15:42:54 UTC
  • Revision ID: james.westby@ubuntu.com-20080909154254-458sv7ew1rczdal1
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

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