~ubuntu-branches/ubuntu/wily/apgdiff/wily

« 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: 2009-12-02 11:11:33 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091202111133-k5wrh3zb2pzmbv8c
Tags: 1.4-1
* New upstream version. (Without changelog, I am afraid.)
* Test suite still not enabled as it needs network access. (The junit
  version in Debian needs upgrading.)
* Optimize rules files a bit so debhelper doesn't clean twice, and quilt's
  patch target doesn't prevent build-stamp from working.

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
1
package cz.startnet.utils.pgdiff.parsers;
5
2
 
6
3
import cz.startnet.utils.pgdiff.schema.PgDatabase;
9
6
import java.util.regex.Matcher;
10
7
import java.util.regex.Pattern;
11
8
 
12
 
 
13
9
/**
14
10
 * Parses CREATE SEQUENCE commands.
15
11
 *
16
12
 * @author fordfrog
17
 
 * @version $Id: CreateSequenceParser.java 80 2007-09-01 20:25:45Z fordfrog $
18
13
 */
19
14
public class CreateSequenceParser {
 
15
 
20
16
    /**
21
17
     * Pattern for getting sequence name.
22
18
     */
23
 
    private static final Pattern PATTERN_SEQUENCE_NAME =
24
 
        Pattern.compile(
25
 
                "CREATE[\\s]+SEQUENCE[\\s]+\"?([^\\s\"]+)\"?",
26
 
                Pattern.CASE_INSENSITIVE);
27
 
 
 
19
    private static final Pattern PATTERN_SEQUENCE_NAME = Pattern.compile(
 
20
            "CREATE[\\s]+SEQUENCE[\\s]+\"?([^\\s\"]+)\"?",
 
21
            Pattern.CASE_INSENSITIVE);
28
22
    /**
29
23
     * Pattern for getting value of START WITH parameter.
30
24
     */
31
 
    private static final Pattern PATTERN_START_WITH =
32
 
        Pattern.compile(
33
 
                "START[\\s]+(?:WITH[\\s]+)?([-]?[\\d]+)",
34
 
                Pattern.CASE_INSENSITIVE);
35
 
 
 
25
    private static final Pattern PATTERN_START_WITH = Pattern.compile(
 
26
            "START[\\s]+(?:WITH[\\s]+)?([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
36
27
    /**
37
28
     * Pattern for getting value of INCREMENT BY parameter.
38
29
     */
39
 
    private static final Pattern PATTERN_INCREMENT_BY =
40
 
        Pattern.compile(
41
 
                "INCREMENT[\\s]+(?:BY[\\s]+)?([-]?[\\d]+)",
42
 
                Pattern.CASE_INSENSITIVE);
43
 
 
 
30
    private static final Pattern PATTERN_INCREMENT_BY = Pattern.compile(
 
31
            "INCREMENT[\\s]+(?:BY[\\s]+)?([-]?[\\d]+)",
 
32
            Pattern.CASE_INSENSITIVE);
44
33
    /**
45
34
     * Pattern for getting value of MAXVALUE parameter.
46
35
     */
47
 
    private static final Pattern PATTERN_MAXVALUE =
48
 
        Pattern.compile("MAXVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
49
 
 
 
36
    private static final Pattern PATTERN_MAXVALUE = Pattern.compile(
 
37
            "MAXVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
50
38
    /**
51
39
     * Pattern for getting value of MINVALUE parameter.
52
40
     */
53
 
    private static final Pattern PATTERN_MINVALUE =
54
 
        Pattern.compile("MINVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
55
 
 
 
41
    private static final Pattern PATTERN_MINVALUE = Pattern.compile(
 
42
            "MINVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
56
43
    /**
57
44
     * Pattern for getting value of CACHE parameter.
58
45
     */
59
46
    private static final Pattern PATTERN_CACHE =
60
 
        Pattern.compile("CACHE[\\s]+([\\d]+)", Pattern.CASE_INSENSITIVE);
61
 
 
 
47
            Pattern.compile("CACHE[\\s]+([\\d]+)", Pattern.CASE_INSENSITIVE);
62
48
    /**
63
49
     * Pattern for checking whether string contains NO CYCLE string.
64
50
     */
65
51
    private static final Pattern PATTERN_NO_CYCLE =
66
 
        Pattern.compile(".*NO[\\s]+CYCLE.*", Pattern.CASE_INSENSITIVE);
67
 
 
 
52
            Pattern.compile(".*NO[\\s]+CYCLE.*", Pattern.CASE_INSENSITIVE);
68
53
    /**
69
54
     * Pattern for checking whether string contains CYCLE string.
70
55
     */
71
56
    private static final Pattern PATTERN_CYCLE =
72
 
        Pattern.compile(".*CYCLE.*", Pattern.CASE_INSENSITIVE);
73
 
 
 
57
            Pattern.compile(".*CYCLE.*", Pattern.CASE_INSENSITIVE);
74
58
    /**
75
59
     * Pattern for checking whether string contains NO MAXVALUE string.
76
60
     */
77
61
    private static final Pattern PATTERN_NO_MAXVALUE =
78
 
        Pattern.compile(".*NO[\\s]+MAXVALUE.*", Pattern.CASE_INSENSITIVE);
79
 
 
 
62
            Pattern.compile(".*NO[\\s]+MAXVALUE.*", Pattern.CASE_INSENSITIVE);
80
63
    /**
81
64
     * Pattern for checking whether string contains NO MINVALUE string.
82
65
     */
83
66
    private static final Pattern PATTERN_NO_MINVALUE =
84
 
        Pattern.compile(".*NO[\\s]+MINVALUE.*", Pattern.CASE_INSENSITIVE);
 
67
            Pattern.compile(".*NO[\\s]+MINVALUE.*", Pattern.CASE_INSENSITIVE);
85
68
 
86
69
    /**
87
70
     * Creates a new instance of CreateSequenceParser.
107
90
        if (matcher.find()) {
108
91
            sequenceName = matcher.group(1).trim();
109
92
            line =
110
 
                ParserUtils.removeSubString(
111
 
                        line,
112
 
                        matcher.start(),
113
 
                        matcher.end());
 
93
                    ParserUtils.removeSubString(
 
94
                    line,
 
95
                    matcher.start(),
 
96
                    matcher.end());
114
97
        } else {
115
98
            throw new ParserException(
116
99
                    ParserException.CANNOT_PARSE_COMMAND + line);
117
100
        }
118
101
 
119
102
        final PgSequence sequence =
120
 
            new PgSequence(ParserUtils.getObjectName(sequenceName));
121
 
        database.getSchema(ParserUtils.getSchemaName(sequenceName, database)).addSequence(
122
 
                sequence);
 
103
                new PgSequence(ParserUtils.getObjectName(sequenceName));
 
104
        database.getSchema(ParserUtils.getSchemaName(
 
105
                sequenceName, database)).addSequence(sequence);
123
106
        line = ParserUtils.removeLastSemicolon(line);
124
107
        line = processMaxValue(sequence, line);
125
108
        line = processMinValue(sequence, line);
130
113
        line = line.trim();
131
114
 
132
115
        if (line.length() > 0) {
133
 
            throw new ParserException(
134
 
                    "Cannot parse commmand '" + command + "', string '" + line
135
 
                    + "'");
 
116
            throw new ParserException("Cannot parse commmand '" + command
 
117
                    + "', string '" + line + "'");
136
118
        }
137
119
    }
138
120
 
144
126
     *
145
127
     * @return command without CACHE instruction
146
128
     */
147
 
    private static String processCache(
148
 
        final PgSequence sequence,
149
 
        final String command) {
 
129
    private static String processCache(final PgSequence sequence,
 
130
            final String command) {
150
131
        String line = command;
151
132
        final Matcher matcher = PATTERN_CACHE.matcher(line);
152
133
 
153
134
        if (matcher.find()) {
154
135
            sequence.setCache(matcher.group(1).trim());
155
 
            line =
156
 
                ParserUtils.removeSubString(
157
 
                        line,
158
 
                        matcher.start(),
159
 
                        matcher.end());
 
136
            line = ParserUtils.removeSubString(
 
137
                    line, matcher.start(), matcher.end());
160
138
        }
161
139
 
162
140
        return line;
170
148
     *
171
149
     * @return command without CYCLE instructions
172
150
     */
173
 
    private static String processCycle(
174
 
        final PgSequence sequence,
175
 
        final String command) {
 
151
    private static String processCycle(final PgSequence sequence,
 
152
            final String command) {
176
153
        String line = command;
177
154
 
178
155
        if (PATTERN_NO_CYCLE.matcher(line).matches()) {
194
171
     *
195
172
     * @return command without INCREMENT BY instruction
196
173
     */
197
 
    private static String processIncrement(
198
 
        final PgSequence sequence,
199
 
        final String command) {
 
174
    private static String processIncrement(final PgSequence sequence,
 
175
            final String command) {
200
176
        String line = command;
201
177
        final Matcher matcher = PATTERN_INCREMENT_BY.matcher(line);
202
178
 
203
179
        if (matcher.find()) {
204
180
            sequence.setIncrement(matcher.group(1).trim());
205
 
            line =
206
 
                ParserUtils.removeSubString(
207
 
                        line,
208
 
                        matcher.start(),
209
 
                        matcher.end());
 
181
            line = ParserUtils.removeSubString(
 
182
                    line, matcher.start(), matcher.end());
210
183
        }
211
184
 
212
185
        return line;
220
193
     *
221
194
     * @return command without MAX VALUE instructions
222
195
     */
223
 
    private static String processMaxValue(
224
 
        final PgSequence sequence,
225
 
        final String command) {
 
196
    private static String processMaxValue(final PgSequence sequence,
 
197
            final String command) {
226
198
        String line = command;
227
199
 
228
200
        if (PATTERN_NO_MAXVALUE.matcher(line).matches()) {
233
205
 
234
206
            if (matcher.find()) {
235
207
                sequence.setMaxValue(matcher.group(1).trim());
236
 
                line =
237
 
                    ParserUtils.removeSubString(
238
 
                            line,
239
 
                            matcher.start(),
240
 
                            matcher.end());
 
208
                line = ParserUtils.removeSubString(
 
209
                        line, matcher.start(), matcher.end());
241
210
            }
242
211
        }
243
212
 
252
221
     *
253
222
     * @return command without MIN VALUE instructions
254
223
     */
255
 
    private static String processMinValue(
256
 
        final PgSequence sequence,
257
 
        final String command) {
 
224
    private static String processMinValue(final PgSequence sequence,
 
225
            final String command) {
258
226
        String line = command;
259
227
 
260
228
        if (PATTERN_NO_MINVALUE.matcher(line).matches()) {
265
233
 
266
234
            if (matcher.find()) {
267
235
                sequence.setMinValue(matcher.group(1).trim());
268
 
                line =
269
 
                    ParserUtils.removeSubString(
270
 
                            line,
271
 
                            matcher.start(),
272
 
                            matcher.end());
 
236
                line = ParserUtils.removeSubString(
 
237
                        line, matcher.start(), matcher.end());
273
238
            }
274
239
        }
275
240
 
284
249
     *
285
250
     * @return command without START WITH instruction
286
251
     */
287
 
    private static String processStartWith(
288
 
        final PgSequence sequence,
289
 
        final String command) {
 
252
    private static String processStartWith(final PgSequence sequence,
 
253
            final String command) {
290
254
        String line = command;
291
255
        final Matcher matcher = PATTERN_START_WITH.matcher(line);
292
256
 
293
257
        if (matcher.find()) {
294
258
            sequence.setStartWith(matcher.group(1).trim());
295
 
            line =
296
 
                ParserUtils.removeSubString(
297
 
                        line,
298
 
                        matcher.start(),
299
 
                        matcher.end());
 
259
            line = ParserUtils.removeSubString(
 
260
                    line, matcher.start(), matcher.end());
300
261
        }
301
262
 
302
263
        return line;