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