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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/parsers/CreateIndexParser.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.PgIndex;
5
10
import cz.startnet.utils.pgdiff.schema.PgTable;
6
11
 
7
 
import java.util.regex.Matcher;
8
 
import java.util.regex.Pattern;
9
 
 
10
12
/**
11
 
 * Parses CREATE INDEX commands.
 
13
 * Parses CREATE INDEX statements.
12
14
 *
13
15
 * @author fordfrog
14
16
 */
15
17
public class CreateIndexParser {
16
18
 
17
19
    /**
18
 
     * Pattern for parsing CREATE INDEX definition.
19
 
     */
20
 
    private static final Pattern PATTERN = Pattern.compile(
21
 
            "CREATE[\\s](UNIQUE[\\s]+)?+INDEX[\\s]+\"?([^\\s\"]+)\"?[\\s]+"
22
 
            + "ON[\\s]+\"?([^\\s\"(]+)\"?[\\s]*([^;]+)[;]?",
23
 
            Pattern.CASE_INSENSITIVE);
24
 
 
25
 
    /**
26
20
     * Creates a new instance of CreateIndexParser.
27
21
     */
28
22
    private CreateIndexParser() {
29
 
        super();
30
23
    }
31
24
 
32
25
    /**
33
 
     * Parses CREATE INDEX command.
 
26
     * Parses CREATE INDEX statement.
34
27
     *
35
28
     * @param database database
36
 
     * @param command CREATE INDEX command
37
 
     *
38
 
     * @throws ParserException Thrown if problem occured while parsing the
39
 
     *         command.
 
29
     * @param statement CREATE INDEX statement
40
30
     */
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 boolean unique = matcher.group(1) != null;
46
 
            final String indexName = matcher.group(2);
47
 
            final String tableName = matcher.group(3);
48
 
            final String def = matcher.group(4);
49
 
 
50
 
            if ((indexName == null) || (tableName == null) || (def == null)) {
51
 
                throw new ParserException(
52
 
                        ParserException.CANNOT_PARSE_COMMAND + command);
53
 
            }
54
 
 
55
 
            final PgTable table = database.getSchema(
56
 
                    ParserUtils.getSchemaName(
57
 
                    tableName.trim(), database)).getTable(tableName.trim());
58
 
            final PgIndex index = new PgIndex(indexName);
59
 
            table.addIndex(index);
60
 
            index.setDefinition(def.trim());
61
 
            index.setTableName(table.getName());
62
 
            index.setUnique(unique);
63
 
        } else {
64
 
            throw new ParserException(
65
 
                    ParserException.CANNOT_PARSE_COMMAND + command);
66
 
        }
 
31
    public static void parse(final PgDatabase database,
 
32
            final String statement) {
 
33
        final Parser parser = new Parser(statement);
 
34
        parser.expect("CREATE");
 
35
 
 
36
        final boolean unique = parser.expectOptional("UNIQUE");
 
37
 
 
38
        parser.expect("INDEX");
 
39
        parser.expectOptional("CONCURRENTLY");
 
40
 
 
41
        final String indexName =
 
42
                ParserUtils.getObjectName(parser.parseIdentifier());
 
43
 
 
44
        parser.expect("ON");
 
45
 
 
46
        final String tableName = parser.parseIdentifier();
 
47
        final String definition = parser.getRest();
 
48
        final String schemaName =
 
49
                ParserUtils.getSchemaName(tableName, database);
 
50
        final String objectName = ParserUtils.getObjectName(tableName);
 
51
 
 
52
        final PgTable table =
 
53
                database.getSchema(schemaName).getTable(objectName);
 
54
        final PgIndex index = new PgIndex(indexName);
 
55
        table.addIndex(index);
 
56
        index.setDefinition(definition.trim());
 
57
        index.setTableName(table.getName());
 
58
        index.setUnique(unique);
67
59
    }
68
60
}