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.PgSequence;
6
import java.util.regex.Matcher;
7
import java.util.regex.Pattern;
10
* Parses CREATE SEQUENCE commands.
12
* Parses CREATE SEQUENCE statements.
14
16
public class CreateSequenceParser {
17
* Pattern for getting sequence name.
19
private static final Pattern PATTERN_SEQUENCE_NAME = Pattern.compile(
20
"CREATE[\\s]+SEQUENCE[\\s]+\"?([^\\s\"]+)\"?",
21
Pattern.CASE_INSENSITIVE);
23
* Pattern for getting value of START WITH parameter.
25
private static final Pattern PATTERN_START_WITH = Pattern.compile(
26
"START[\\s]+(?:WITH[\\s]+)?([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
28
* Pattern for getting value of INCREMENT BY parameter.
30
private static final Pattern PATTERN_INCREMENT_BY = Pattern.compile(
31
"INCREMENT[\\s]+(?:BY[\\s]+)?([-]?[\\d]+)",
32
Pattern.CASE_INSENSITIVE);
34
* Pattern for getting value of MAXVALUE parameter.
36
private static final Pattern PATTERN_MAXVALUE = Pattern.compile(
37
"MAXVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
39
* Pattern for getting value of MINVALUE parameter.
41
private static final Pattern PATTERN_MINVALUE = Pattern.compile(
42
"MINVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
44
* Pattern for getting value of CACHE parameter.
46
private static final Pattern PATTERN_CACHE =
47
Pattern.compile("CACHE[\\s]+([\\d]+)", Pattern.CASE_INSENSITIVE);
49
* Pattern for checking whether string contains NO CYCLE string.
51
private static final Pattern PATTERN_NO_CYCLE =
52
Pattern.compile(".*NO[\\s]+CYCLE.*", Pattern.CASE_INSENSITIVE);
54
* Pattern for checking whether string contains CYCLE string.
56
private static final Pattern PATTERN_CYCLE =
57
Pattern.compile(".*CYCLE.*", Pattern.CASE_INSENSITIVE);
59
* Pattern for checking whether string contains NO MAXVALUE string.
61
private static final Pattern PATTERN_NO_MAXVALUE =
62
Pattern.compile(".*NO[\\s]+MAXVALUE.*", Pattern.CASE_INSENSITIVE);
64
* Pattern for checking whether string contains NO MINVALUE string.
66
private static final Pattern PATTERN_NO_MINVALUE =
67
Pattern.compile(".*NO[\\s]+MINVALUE.*", Pattern.CASE_INSENSITIVE);
70
19
* Creates a new instance of CreateSequenceParser.
72
21
private CreateSequenceParser() {
77
* Parses CREATE SEQUENCE command.
25
* Parses CREATE SEQUENCE statement.
79
27
* @param database database
80
* @param command CREATE SEQUENCE command
82
* @throws ParserException Thrown if problem occured while parsing the
28
* @param statement CREATE SEQUENCE statement
85
public static void parse(final PgDatabase database, final String command) {
86
String line = command;
87
final Matcher matcher = PATTERN_SEQUENCE_NAME.matcher(line);
88
final String sequenceName;
91
sequenceName = matcher.group(1).trim();
93
ParserUtils.removeSubString(
98
throw new ParserException(
99
ParserException.CANNOT_PARSE_COMMAND + line);
30
public static void parse(final PgDatabase database,
31
final String statement) {
32
final Parser parser = new Parser(statement);
33
parser.expect("CREATE", "SEQUENCE");
35
final String sequenceName = parser.parseIdentifier();
102
36
final PgSequence sequence =
103
37
new PgSequence(ParserUtils.getObjectName(sequenceName));
104
database.getSchema(ParserUtils.getSchemaName(
105
sequenceName, database)).addSequence(sequence);
106
line = ParserUtils.removeLastSemicolon(line);
107
line = processMaxValue(sequence, line);
108
line = processMinValue(sequence, line);
109
line = processCycle(sequence, line);
110
line = processCache(sequence, line);
111
line = processIncrement(sequence, line);
112
line = processStartWith(sequence, line);
115
if (line.length() > 0) {
116
throw new ParserException("Cannot parse commmand '" + command
117
+ "', string '" + line + "'");
122
* Processes CACHE instruction.
124
* @param sequence sequence
125
* @param command command
127
* @return command without CACHE instruction
129
private static String processCache(final PgSequence sequence,
130
final String command) {
131
String line = command;
132
final Matcher matcher = PATTERN_CACHE.matcher(line);
134
if (matcher.find()) {
135
sequence.setCache(matcher.group(1).trim());
136
line = ParserUtils.removeSubString(
137
line, matcher.start(), matcher.end());
144
* Processes CYCLE and NO CYCLE instructions.
146
* @param sequence sequence
147
* @param command command
149
* @return command without CYCLE instructions
151
private static String processCycle(final PgSequence sequence,
152
final String command) {
153
String line = command;
155
if (PATTERN_NO_CYCLE.matcher(line).matches()) {
156
sequence.setCycle(false);
157
line = ParserUtils.removeSubString(line, "NO CYCLE");
158
} else if (PATTERN_CYCLE.matcher(line).matches()) {
159
sequence.setCycle(true);
160
line = ParserUtils.removeSubString(line, "CYCLE");
167
* Processes INCREMENT BY instruction.
169
* @param sequence sequence
170
* @param command command
172
* @return command without INCREMENT BY instruction
174
private static String processIncrement(final PgSequence sequence,
175
final String command) {
176
String line = command;
177
final Matcher matcher = PATTERN_INCREMENT_BY.matcher(line);
179
if (matcher.find()) {
180
sequence.setIncrement(matcher.group(1).trim());
181
line = ParserUtils.removeSubString(
182
line, matcher.start(), matcher.end());
189
* Processes MAX VALUE and NO MAXVALUE instructions.
191
* @param sequence sequence
192
* @param command command
194
* @return command without MAX VALUE instructions
196
private static String processMaxValue(final PgSequence sequence,
197
final String command) {
198
String line = command;
200
if (PATTERN_NO_MAXVALUE.matcher(line).matches()) {
201
sequence.setMaxValue(null);
202
line = ParserUtils.removeSubString(line, "NO MAXVALUE");
204
final Matcher matcher = PATTERN_MAXVALUE.matcher(line);
206
if (matcher.find()) {
207
sequence.setMaxValue(matcher.group(1).trim());
208
line = ParserUtils.removeSubString(
209
line, matcher.start(), matcher.end());
217
* Processes MIN VALUE and NO MINVALUE instructions.
219
* @param sequence sequence
220
* @param command command
222
* @return command without MIN VALUE instructions
224
private static String processMinValue(final PgSequence sequence,
225
final String command) {
226
String line = command;
228
if (PATTERN_NO_MINVALUE.matcher(line).matches()) {
229
sequence.setMinValue(null);
230
line = ParserUtils.removeSubString(line, "NO MINVALUE");
232
final Matcher matcher = PATTERN_MINVALUE.matcher(line);
234
if (matcher.find()) {
235
sequence.setMinValue(matcher.group(1).trim());
236
line = ParserUtils.removeSubString(
237
line, matcher.start(), matcher.end());
245
* Processes START WITH instruction.
247
* @param sequence sequence
248
* @param command command
250
* @return command without START WITH instruction
252
private static String processStartWith(final PgSequence sequence,
253
final String command) {
254
String line = command;
255
final Matcher matcher = PATTERN_START_WITH.matcher(line);
257
if (matcher.find()) {
258
sequence.setStartWith(matcher.group(1).trim());
259
line = ParserUtils.removeSubString(
260
line, matcher.start(), matcher.end());
38
final String schemaName =
39
ParserUtils.getSchemaName(sequenceName, database);
40
database.getSchema(schemaName).addSequence(sequence);
42
while (!parser.expectOptional(";")) {
43
if (parser.expectOptional("INCREMENT")) {
44
parser.expectOptional("BY");
45
sequence.setIncrement(parser.parseString());
46
} else if (parser.expectOptional("MINVALUE")) {
47
sequence.setMinValue(parser.parseString());
48
} else if (parser.expectOptional("MAXVALUE")) {
49
sequence.setMaxValue(parser.parseString());
50
} else if (parser.expectOptional("START")) {
51
parser.expectOptional("WITH");
52
sequence.setStartWith(parser.parseString());
53
} else if (parser.expectOptional("CACHE")) {
54
sequence.setCache(parser.parseString());
55
} else if (parser.expectOptional("CYCLE")) {
56
sequence.setCycle(true);
57
} else if (parser.expectOptional("OWNED", "BY")) {
58
if (parser.expectOptional("NONE")) {
59
sequence.setOwnedBy(null);
61
sequence.setOwnedBy(ParserUtils.getObjectName(
62
parser.parseIdentifier()));
64
} else if (parser.expectOptional("NO")) {
65
if (parser.expectOptional("MINVALUE")) {
66
sequence.setMinValue(null);
67
} else if (parser.expectOptional("MAXVALUE")) {
68
sequence.setMaxValue(null);
69
} else if (parser.expectOptional("CYCLE")) {
70
sequence.setCycle(false);
72
parser.throwUnsupportedCommand();
75
parser.throwUnsupportedCommand();