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

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/parsers/CreateSequenceParser.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: CreateSequenceParser.java 80 2007-09-01 20:25:45Z 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.PgSequence;
 
8
 
 
9
import java.util.regex.Matcher;
 
10
import java.util.regex.Pattern;
 
11
 
 
12
 
 
13
/**
 
14
 * Parses CREATE SEQUENCE commands.
 
15
 *
 
16
 * @author fordfrog
 
17
 * @version $Id: CreateSequenceParser.java 80 2007-09-01 20:25:45Z fordfrog $
 
18
 */
 
19
public class CreateSequenceParser {
 
20
    /**
 
21
     * Pattern for getting sequence name.
 
22
     */
 
23
    private static final Pattern PATTERN_SEQUENCE_NAME =
 
24
        Pattern.compile(
 
25
                "CREATE[\\s]+SEQUENCE[\\s]+\"?([^\\s\"]+)\"?",
 
26
                Pattern.CASE_INSENSITIVE);
 
27
 
 
28
    /**
 
29
     * Pattern for getting value of START WITH parameter.
 
30
     */
 
31
    private static final Pattern PATTERN_START_WITH =
 
32
        Pattern.compile(
 
33
                "START[\\s]+(?:WITH[\\s]+)?([-]?[\\d]+)",
 
34
                Pattern.CASE_INSENSITIVE);
 
35
 
 
36
    /**
 
37
     * Pattern for getting value of INCREMENT BY parameter.
 
38
     */
 
39
    private static final Pattern PATTERN_INCREMENT_BY =
 
40
        Pattern.compile(
 
41
                "INCREMENT[\\s]+(?:BY[\\s]+)?([-]?[\\d]+)",
 
42
                Pattern.CASE_INSENSITIVE);
 
43
 
 
44
    /**
 
45
     * Pattern for getting value of MAXVALUE parameter.
 
46
     */
 
47
    private static final Pattern PATTERN_MAXVALUE =
 
48
        Pattern.compile("MAXVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
 
49
 
 
50
    /**
 
51
     * Pattern for getting value of MINVALUE parameter.
 
52
     */
 
53
    private static final Pattern PATTERN_MINVALUE =
 
54
        Pattern.compile("MINVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
 
55
 
 
56
    /**
 
57
     * Pattern for getting value of CACHE parameter.
 
58
     */
 
59
    private static final Pattern PATTERN_CACHE =
 
60
        Pattern.compile("CACHE[\\s]+([\\d]+)", Pattern.CASE_INSENSITIVE);
 
61
 
 
62
    /**
 
63
     * Pattern for checking whether string contains NO CYCLE string.
 
64
     */
 
65
    private static final Pattern PATTERN_NO_CYCLE =
 
66
        Pattern.compile(".*NO[\\s]+CYCLE.*", Pattern.CASE_INSENSITIVE);
 
67
 
 
68
    /**
 
69
     * Pattern for checking whether string contains CYCLE string.
 
70
     */
 
71
    private static final Pattern PATTERN_CYCLE =
 
72
        Pattern.compile(".*CYCLE.*", Pattern.CASE_INSENSITIVE);
 
73
 
 
74
    /**
 
75
     * Pattern for checking whether string contains NO MAXVALUE string.
 
76
     */
 
77
    private static final Pattern PATTERN_NO_MAXVALUE =
 
78
        Pattern.compile(".*NO[\\s]+MAXVALUE.*", Pattern.CASE_INSENSITIVE);
 
79
 
 
80
    /**
 
81
     * Pattern for checking whether string contains NO MINVALUE string.
 
82
     */
 
83
    private static final Pattern PATTERN_NO_MINVALUE =
 
84
        Pattern.compile(".*NO[\\s]+MINVALUE.*", Pattern.CASE_INSENSITIVE);
 
85
 
 
86
    /**
 
87
     * Creates a new instance of CreateSequenceParser.
 
88
     */
 
89
    private CreateSequenceParser() {
 
90
        super();
 
91
    }
 
92
 
 
93
    /**
 
94
     * Parses CREATE SEQUENCE command.
 
95
     *
 
96
     * @param database database
 
97
     * @param command CREATE SEQUENCE command
 
98
     *
 
99
     * @throws ParserException Thrown if problem occured while parsing the
 
100
     *         command.
 
101
     */
 
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;
 
106
 
 
107
        if (matcher.find()) {
 
108
            sequenceName = matcher.group(1).trim();
 
109
            line =
 
110
                ParserUtils.removeSubString(
 
111
                        line,
 
112
                        matcher.start(),
 
113
                        matcher.end());
 
114
        } else {
 
115
            throw new ParserException(
 
116
                    ParserException.CANNOT_PARSE_COMMAND + line);
 
117
        }
 
118
 
 
119
        final PgSequence sequence =
 
120
            new PgSequence(ParserUtils.getObjectName(sequenceName));
 
121
        database.getSchema(ParserUtils.getSchemaName(sequenceName, database)).addSequence(
 
122
                sequence);
 
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);
 
130
        line = line.trim();
 
131
 
 
132
        if (line.length() > 0) {
 
133
            throw new ParserException(
 
134
                    "Cannot parse commmand '" + command + "', string '" + line
 
135
                    + "'");
 
136
        }
 
137
    }
 
138
 
 
139
    /**
 
140
     * Processes CACHE instruction.
 
141
     *
 
142
     * @param sequence sequence
 
143
     * @param command command
 
144
     *
 
145
     * @return command without CACHE instruction
 
146
     */
 
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);
 
152
 
 
153
        if (matcher.find()) {
 
154
            sequence.setCache(matcher.group(1).trim());
 
155
            line =
 
156
                ParserUtils.removeSubString(
 
157
                        line,
 
158
                        matcher.start(),
 
159
                        matcher.end());
 
160
        }
 
161
 
 
162
        return line;
 
163
    }
 
164
 
 
165
    /**
 
166
     * Processes CYCLE and NO CYCLE instructions.
 
167
     *
 
168
     * @param sequence sequence
 
169
     * @param command command
 
170
     *
 
171
     * @return command without CYCLE instructions
 
172
     */
 
173
    private static String processCycle(
 
174
        final PgSequence sequence,
 
175
        final String command) {
 
176
        String line = command;
 
177
 
 
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");
 
184
        }
 
185
 
 
186
        return line;
 
187
    }
 
188
 
 
189
    /**
 
190
     * Processes INCREMENT BY instruction.
 
191
     *
 
192
     * @param sequence sequence
 
193
     * @param command command
 
194
     *
 
195
     * @return command without INCREMENT BY instruction
 
196
     */
 
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);
 
202
 
 
203
        if (matcher.find()) {
 
204
            sequence.setIncrement(matcher.group(1).trim());
 
205
            line =
 
206
                ParserUtils.removeSubString(
 
207
                        line,
 
208
                        matcher.start(),
 
209
                        matcher.end());
 
210
        }
 
211
 
 
212
        return line;
 
213
    }
 
214
 
 
215
    /**
 
216
     * Processes MAX VALUE and NO MAXVALUE instructions.
 
217
     *
 
218
     * @param sequence sequence
 
219
     * @param command command
 
220
     *
 
221
     * @return command without MAX VALUE instructions
 
222
     */
 
223
    private static String processMaxValue(
 
224
        final PgSequence sequence,
 
225
        final String command) {
 
226
        String line = command;
 
227
 
 
228
        if (PATTERN_NO_MAXVALUE.matcher(line).matches()) {
 
229
            sequence.setMaxValue(null);
 
230
            line = ParserUtils.removeSubString(line, "NO MAXVALUE");
 
231
        } else {
 
232
            final Matcher matcher = PATTERN_MAXVALUE.matcher(line);
 
233
 
 
234
            if (matcher.find()) {
 
235
                sequence.setMaxValue(matcher.group(1).trim());
 
236
                line =
 
237
                    ParserUtils.removeSubString(
 
238
                            line,
 
239
                            matcher.start(),
 
240
                            matcher.end());
 
241
            }
 
242
        }
 
243
 
 
244
        return line;
 
245
    }
 
246
 
 
247
    /**
 
248
     * Processes MIN VALUE and NO MINVALUE instructions.
 
249
     *
 
250
     * @param sequence sequence
 
251
     * @param command command
 
252
     *
 
253
     * @return command without MIN VALUE instructions
 
254
     */
 
255
    private static String processMinValue(
 
256
        final PgSequence sequence,
 
257
        final String command) {
 
258
        String line = command;
 
259
 
 
260
        if (PATTERN_NO_MINVALUE.matcher(line).matches()) {
 
261
            sequence.setMinValue(null);
 
262
            line = ParserUtils.removeSubString(line, "NO MINVALUE");
 
263
        } else {
 
264
            final Matcher matcher = PATTERN_MINVALUE.matcher(line);
 
265
 
 
266
            if (matcher.find()) {
 
267
                sequence.setMinValue(matcher.group(1).trim());
 
268
                line =
 
269
                    ParserUtils.removeSubString(
 
270
                            line,
 
271
                            matcher.start(),
 
272
                            matcher.end());
 
273
            }
 
274
        }
 
275
 
 
276
        return line;
 
277
    }
 
278
 
 
279
    /**
 
280
     * Processes START WITH instruction.
 
281
     *
 
282
     * @param sequence sequence
 
283
     * @param command command
 
284
     *
 
285
     * @return command without START WITH instruction
 
286
     */
 
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);
 
292
 
 
293
        if (matcher.find()) {
 
294
            sequence.setStartWith(matcher.group(1).trim());
 
295
            line =
 
296
                ParserUtils.removeSubString(
 
297
                        line,
 
298
                        matcher.start(),
 
299
                        matcher.end());
 
300
        }
 
301
 
 
302
        return line;
 
303
    }
 
304
}