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

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