2
* $Id: CreateSequenceParser.java 80 2007-09-01 20:25:45Z fordfrog $
4
package cz.startnet.utils.pgdiff.parsers;
6
import cz.startnet.utils.pgdiff.schema.PgDatabase;
7
import cz.startnet.utils.pgdiff.schema.PgSequence;
9
import java.util.regex.Matcher;
10
import java.util.regex.Pattern;
14
* Parses CREATE SEQUENCE commands.
17
* @version $Id: CreateSequenceParser.java 80 2007-09-01 20:25:45Z fordfrog $
19
public class CreateSequenceParser {
21
* Pattern for getting sequence name.
23
private static final Pattern PATTERN_SEQUENCE_NAME =
25
"CREATE[\\s]+SEQUENCE[\\s]+\"?([^\\s\"]+)\"?",
26
Pattern.CASE_INSENSITIVE);
29
* Pattern for getting value of START WITH parameter.
31
private static final Pattern PATTERN_START_WITH =
33
"START[\\s]+(?:WITH[\\s]+)?([-]?[\\d]+)",
34
Pattern.CASE_INSENSITIVE);
37
* Pattern for getting value of INCREMENT BY parameter.
39
private static final Pattern PATTERN_INCREMENT_BY =
41
"INCREMENT[\\s]+(?:BY[\\s]+)?([-]?[\\d]+)",
42
Pattern.CASE_INSENSITIVE);
45
* Pattern for getting value of MAXVALUE parameter.
47
private static final Pattern PATTERN_MAXVALUE =
48
Pattern.compile("MAXVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
51
* Pattern for getting value of MINVALUE parameter.
53
private static final Pattern PATTERN_MINVALUE =
54
Pattern.compile("MINVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
57
* Pattern for getting value of CACHE parameter.
59
private static final Pattern PATTERN_CACHE =
60
Pattern.compile("CACHE[\\s]+([\\d]+)", Pattern.CASE_INSENSITIVE);
63
* Pattern for checking whether string contains NO CYCLE string.
65
private static final Pattern PATTERN_NO_CYCLE =
66
Pattern.compile(".*NO[\\s]+CYCLE.*", Pattern.CASE_INSENSITIVE);
69
* Pattern for checking whether string contains CYCLE string.
71
private static final Pattern PATTERN_CYCLE =
72
Pattern.compile(".*CYCLE.*", Pattern.CASE_INSENSITIVE);
75
* Pattern for checking whether string contains NO MAXVALUE string.
77
private static final Pattern PATTERN_NO_MAXVALUE =
78
Pattern.compile(".*NO[\\s]+MAXVALUE.*", Pattern.CASE_INSENSITIVE);
81
* Pattern for checking whether string contains NO MINVALUE string.
83
private static final Pattern PATTERN_NO_MINVALUE =
84
Pattern.compile(".*NO[\\s]+MINVALUE.*", Pattern.CASE_INSENSITIVE);
87
* Creates a new instance of CreateSequenceParser.
89
private CreateSequenceParser() {
94
* Parses CREATE SEQUENCE command.
96
* @param database database
97
* @param command CREATE SEQUENCE command
99
* @throws ParserException Thrown if problem occured while parsing the
102
public static void parse(final PgDatabase database, final String command) {
103
String line = command;
104
final Matcher matcher = PATTERN_SEQUENCE_NAME.matcher(line);
105
final String sequenceName;
107
if (matcher.find()) {
108
sequenceName = matcher.group(1).trim();
110
ParserUtils.removeSubString(
115
throw new ParserException(
116
ParserException.CANNOT_PARSE_COMMAND + line);
119
final PgSequence sequence =
120
new PgSequence(ParserUtils.getObjectName(sequenceName));
121
database.getSchema(ParserUtils.getSchemaName(sequenceName, database)).addSequence(
123
line = ParserUtils.removeLastSemicolon(line);
124
line = processMaxValue(sequence, line);
125
line = processMinValue(sequence, line);
126
line = processCycle(sequence, line);
127
line = processCache(sequence, line);
128
line = processIncrement(sequence, line);
129
line = processStartWith(sequence, line);
132
if (line.length() > 0) {
133
throw new ParserException(
134
"Cannot parse commmand '" + command + "', string '" + line
140
* Processes CACHE instruction.
142
* @param sequence sequence
143
* @param command command
145
* @return command without CACHE instruction
147
private static String processCache(
148
final PgSequence sequence,
149
final String command) {
150
String line = command;
151
final Matcher matcher = PATTERN_CACHE.matcher(line);
153
if (matcher.find()) {
154
sequence.setCache(matcher.group(1).trim());
156
ParserUtils.removeSubString(
166
* Processes CYCLE and NO CYCLE instructions.
168
* @param sequence sequence
169
* @param command command
171
* @return command without CYCLE instructions
173
private static String processCycle(
174
final PgSequence sequence,
175
final String command) {
176
String line = command;
178
if (PATTERN_NO_CYCLE.matcher(line).matches()) {
179
sequence.setCycle(false);
180
line = ParserUtils.removeSubString(line, "NO CYCLE");
181
} else if (PATTERN_CYCLE.matcher(line).matches()) {
182
sequence.setCycle(true);
183
line = ParserUtils.removeSubString(line, "CYCLE");
190
* Processes INCREMENT BY instruction.
192
* @param sequence sequence
193
* @param command command
195
* @return command without INCREMENT BY instruction
197
private static String processIncrement(
198
final PgSequence sequence,
199
final String command) {
200
String line = command;
201
final Matcher matcher = PATTERN_INCREMENT_BY.matcher(line);
203
if (matcher.find()) {
204
sequence.setIncrement(matcher.group(1).trim());
206
ParserUtils.removeSubString(
216
* Processes MAX VALUE and NO MAXVALUE instructions.
218
* @param sequence sequence
219
* @param command command
221
* @return command without MAX VALUE instructions
223
private static String processMaxValue(
224
final PgSequence sequence,
225
final String command) {
226
String line = command;
228
if (PATTERN_NO_MAXVALUE.matcher(line).matches()) {
229
sequence.setMaxValue(null);
230
line = ParserUtils.removeSubString(line, "NO MAXVALUE");
232
final Matcher matcher = PATTERN_MAXVALUE.matcher(line);
234
if (matcher.find()) {
235
sequence.setMaxValue(matcher.group(1).trim());
237
ParserUtils.removeSubString(
248
* Processes MIN VALUE and NO MINVALUE instructions.
250
* @param sequence sequence
251
* @param command command
253
* @return command without MIN VALUE instructions
255
private static String processMinValue(
256
final PgSequence sequence,
257
final String command) {
258
String line = command;
260
if (PATTERN_NO_MINVALUE.matcher(line).matches()) {
261
sequence.setMinValue(null);
262
line = ParserUtils.removeSubString(line, "NO MINVALUE");
264
final Matcher matcher = PATTERN_MINVALUE.matcher(line);
266
if (matcher.find()) {
267
sequence.setMinValue(matcher.group(1).trim());
269
ParserUtils.removeSubString(
280
* Processes START WITH instruction.
282
* @param sequence sequence
283
* @param command command
285
* @return command without START WITH instruction
287
private static String processStartWith(
288
final PgSequence sequence,
289
final String command) {
290
String line = command;
291
final Matcher matcher = PATTERN_START_WITH.matcher(line);
293
if (matcher.find()) {
294
sequence.setStartWith(matcher.group(1).trim());
296
ParserUtils.removeSubString(