2
* Copyright 2006 StartNet s.r.o.
4
* Distributed under MIT license
1
6
package cz.startnet.utils.pgdiff.parsers;
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;
7
import java.util.regex.Matcher;
8
import java.util.regex.Pattern;
11
* Parses CREATE INDEX commands.
13
* Parses CREATE INDEX statements.
15
17
public class CreateIndexParser {
18
* Pattern for parsing CREATE INDEX definition.
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);
26
20
* Creates a new instance of CreateIndexParser.
28
22
private CreateIndexParser() {
33
* Parses CREATE INDEX command.
26
* Parses CREATE INDEX statement.
35
28
* @param database database
36
* @param command CREATE INDEX command
38
* @throws ParserException Thrown if problem occured while parsing the
29
* @param statement CREATE INDEX statement
41
public static void parse(final PgDatabase database, final String command) {
42
final Matcher matcher = PATTERN.matcher(command.trim());
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);
50
if ((indexName == null) || (tableName == null) || (def == null)) {
51
throw new ParserException(
52
ParserException.CANNOT_PARSE_COMMAND + command);
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);
64
throw new ParserException(
65
ParserException.CANNOT_PARSE_COMMAND + command);
31
public static void parse(final PgDatabase database,
32
final String statement) {
33
final Parser parser = new Parser(statement);
34
parser.expect("CREATE");
36
final boolean unique = parser.expectOptional("UNIQUE");
38
parser.expect("INDEX");
39
parser.expectOptional("CONCURRENTLY");
41
final String indexName =
42
ParserUtils.getObjectName(parser.parseIdentifier());
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);
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);