~ubuntu-branches/ubuntu/vivid/apgdiff/vivid

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2010-10-02 19:35:17 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101002193517-9wesve8sksxcktkc
Tags: 2.2-1
* New upstream version.
* Update homepage location.
* Finally enable test suite, yay!

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
 
6
 
import java.util.regex.Matcher;
7
 
import java.util.regex.Pattern;
8
 
 
9
11
/**
10
 
 * Parses CREATE SCHEMA commands.
 
12
 * Parses CREATE SCHEMA statements.
11
13
 *
12
14
 * @author fordfrog
13
15
 */
14
16
public class CreateSchemaParser {
15
17
 
16
18
    /**
17
 
     * Pattern for parsing CREATE SCHEMA ... AUTHORIZATION ...
18
 
     */
19
 
    private static final Pattern PATTERN_CREATE_SCHEMA = Pattern.compile(
20
 
            "^CREATE[\\s]+SCHEMA[\\s]+([^\\s;]+)"
21
 
            + "(?:[\\s]+AUTHORIZATION[\\s]+([^\\s;]+))?;$",
22
 
            Pattern.CASE_INSENSITIVE);
23
 
    /**
24
 
     * Pattern for parsing CREATE SCHEMA AUTHORIZATION ...
25
 
     */
26
 
    private static final Pattern PATTERN_CREATE_SCHEMA_AUTHORIZATION =
27
 
            Pattern.compile(
28
 
            "^CREATE[\\s]+SCHEMA[\\s]+AUTHORIZATION[\\s]+([^\\s;]+);$",
29
 
            Pattern.CASE_INSENSITIVE);
30
 
 
31
 
    /**
32
19
     * Creates a new CreateSchemaParser object.
33
20
     */
34
21
    private CreateSchemaParser() {
35
 
        super();
36
22
    }
37
23
 
38
24
    /**
39
 
     * Parses CREATE SCHEMA command.
 
25
     * Parses CREATE SCHEMA statement.
40
26
     *
41
27
     * @param database database
42
 
     * @param command CREATE SCHEMA command
43
 
     *
44
 
     * @throws ParserException Thrown if problem occured while parsing the
45
 
     *         command.
 
28
     * @param statement CREATE SCHEMA statement
46
29
     */
47
 
    public static void parse(final PgDatabase database, final String command) {
48
 
        final Matcher matcher = PATTERN_CREATE_SCHEMA.matcher(command);
49
 
 
50
 
        if (matcher.matches()) {
51
 
            final PgSchema schema = new PgSchema(matcher.group(1));
52
 
            final String authorization = matcher.group(2);
53
 
 
54
 
            if (authorization != null) {
55
 
                schema.setAuthorization(authorization);
 
30
    public static void parse(final PgDatabase database,
 
31
            final String statement) {
 
32
        final Parser parser = new Parser(statement);
 
33
        parser.expect("CREATE", "SCHEMA");
 
34
 
 
35
        if (parser.expectOptional("AUTHORIZATION")) {
 
36
            final PgSchema schema = new PgSchema(
 
37
                    ParserUtils.getObjectName(parser.parseIdentifier()));
 
38
            database.addSchema(schema);
 
39
            schema.setAuthorization(schema.getName());
 
40
 
 
41
            final String definition = parser.getRest();
 
42
 
 
43
            if (definition != null && !definition.isEmpty()) {
 
44
                schema.setDefinition(definition);
56
45
            }
57
 
 
58
 
            database.addSchema(schema);
59
46
        } else {
60
 
            final Matcher matcher2 =
61
 
                    PATTERN_CREATE_SCHEMA_AUTHORIZATION.matcher(command);
62
 
 
63
 
            if (matcher2.matches()) {
64
 
                final PgSchema schema = new PgSchema(matcher.group(1));
65
 
                schema.setAuthorization(schema.getName());
66
 
                database.addSchema(schema);
67
 
            } else {
68
 
                throw new ParserException(
69
 
                        ParserException.CANNOT_PARSE_COMMAND + command);
 
47
            final PgSchema schema = new PgSchema(
 
48
                    ParserUtils.getObjectName(parser.parseIdentifier()));
 
49
            database.addSchema(schema);
 
50
 
 
51
            if (parser.expectOptional("AUTHORIZATION")) {
 
52
                schema.setAuthorization(
 
53
                        ParserUtils.getObjectName(parser.parseIdentifier()));
 
54
            }
 
55
 
 
56
            final String definition = parser.getRest();
 
57
 
 
58
            if (definition != null && !definition.isEmpty()) {
 
59
                schema.setDefinition(definition);
70
60
            }
71
61
        }
72
62
    }