~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: 2010-10-11 09:08:18 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20101011090818-sdw8yfemrnxo328k
Tags: 2.2.2-1
* New upstream version.
* Using changelog included in zipfile, thanks Miroslav for providing this.
* Update manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2006 StartNet s.r.o.
 
3
 *
 
4
 * Distributed under MIT license
 
5
 */
1
6
package cz.startnet.utils.pgdiff.parsers;
2
7
 
3
8
import cz.startnet.utils.pgdiff.schema.PgDatabase;
4
9
import cz.startnet.utils.pgdiff.schema.PgSequence;
5
10
 
6
 
import java.util.regex.Matcher;
7
 
import java.util.regex.Pattern;
8
 
 
9
11
/**
10
 
 * Parses CREATE SEQUENCE commands.
 
12
 * Parses CREATE SEQUENCE statements.
11
13
 *
12
14
 * @author fordfrog
13
15
 */
14
16
public class CreateSequenceParser {
15
17
 
16
18
    /**
17
 
     * Pattern for getting sequence name.
18
 
     */
19
 
    private static final Pattern PATTERN_SEQUENCE_NAME = Pattern.compile(
20
 
            "CREATE[\\s]+SEQUENCE[\\s]+\"?([^\\s\"]+)\"?",
21
 
            Pattern.CASE_INSENSITIVE);
22
 
    /**
23
 
     * Pattern for getting value of START WITH parameter.
24
 
     */
25
 
    private static final Pattern PATTERN_START_WITH = Pattern.compile(
26
 
            "START[\\s]+(?:WITH[\\s]+)?([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
27
 
    /**
28
 
     * Pattern for getting value of INCREMENT BY parameter.
29
 
     */
30
 
    private static final Pattern PATTERN_INCREMENT_BY = Pattern.compile(
31
 
            "INCREMENT[\\s]+(?:BY[\\s]+)?([-]?[\\d]+)",
32
 
            Pattern.CASE_INSENSITIVE);
33
 
    /**
34
 
     * Pattern for getting value of MAXVALUE parameter.
35
 
     */
36
 
    private static final Pattern PATTERN_MAXVALUE = Pattern.compile(
37
 
            "MAXVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
38
 
    /**
39
 
     * Pattern for getting value of MINVALUE parameter.
40
 
     */
41
 
    private static final Pattern PATTERN_MINVALUE = Pattern.compile(
42
 
            "MINVALUE[\\s]+([-]?[\\d]+)", Pattern.CASE_INSENSITIVE);
43
 
    /**
44
 
     * Pattern for getting value of CACHE parameter.
45
 
     */
46
 
    private static final Pattern PATTERN_CACHE =
47
 
            Pattern.compile("CACHE[\\s]+([\\d]+)", Pattern.CASE_INSENSITIVE);
48
 
    /**
49
 
     * Pattern for checking whether string contains NO CYCLE string.
50
 
     */
51
 
    private static final Pattern PATTERN_NO_CYCLE =
52
 
            Pattern.compile(".*NO[\\s]+CYCLE.*", Pattern.CASE_INSENSITIVE);
53
 
    /**
54
 
     * Pattern for checking whether string contains CYCLE string.
55
 
     */
56
 
    private static final Pattern PATTERN_CYCLE =
57
 
            Pattern.compile(".*CYCLE.*", Pattern.CASE_INSENSITIVE);
58
 
    /**
59
 
     * Pattern for checking whether string contains NO MAXVALUE string.
60
 
     */
61
 
    private static final Pattern PATTERN_NO_MAXVALUE =
62
 
            Pattern.compile(".*NO[\\s]+MAXVALUE.*", Pattern.CASE_INSENSITIVE);
63
 
    /**
64
 
     * Pattern for checking whether string contains NO MINVALUE string.
65
 
     */
66
 
    private static final Pattern PATTERN_NO_MINVALUE =
67
 
            Pattern.compile(".*NO[\\s]+MINVALUE.*", Pattern.CASE_INSENSITIVE);
68
 
 
69
 
    /**
70
19
     * Creates a new instance of CreateSequenceParser.
71
20
     */
72
21
    private CreateSequenceParser() {
73
 
        super();
74
22
    }
75
23
 
76
24
    /**
77
 
     * Parses CREATE SEQUENCE command.
 
25
     * Parses CREATE SEQUENCE statement.
78
26
     *
79
27
     * @param database database
80
 
     * @param command CREATE SEQUENCE command
81
 
     *
82
 
     * @throws ParserException Thrown if problem occured while parsing the
83
 
     *         command.
 
28
     * @param statement CREATE SEQUENCE statement
84
29
     */
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;
89
 
 
90
 
        if (matcher.find()) {
91
 
            sequenceName = matcher.group(1).trim();
92
 
            line =
93
 
                    ParserUtils.removeSubString(
94
 
                    line,
95
 
                    matcher.start(),
96
 
                    matcher.end());
97
 
        } else {
98
 
            throw new ParserException(
99
 
                    ParserException.CANNOT_PARSE_COMMAND + line);
100
 
        }
101
 
 
 
30
    public static void parse(final PgDatabase database,
 
31
            final String statement) {
 
32
        final Parser parser = new Parser(statement);
 
33
        parser.expect("CREATE", "SEQUENCE");
 
34
 
 
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);
113
 
        line = line.trim();
114
 
 
115
 
        if (line.length() > 0) {
116
 
            throw new ParserException("Cannot parse commmand '" + command
117
 
                    + "', string '" + line + "'");
118
 
        }
119
 
    }
120
 
 
121
 
    /**
122
 
     * Processes CACHE instruction.
123
 
     *
124
 
     * @param sequence sequence
125
 
     * @param command command
126
 
     *
127
 
     * @return command without CACHE instruction
128
 
     */
129
 
    private static String processCache(final PgSequence sequence,
130
 
            final String command) {
131
 
        String line = command;
132
 
        final Matcher matcher = PATTERN_CACHE.matcher(line);
133
 
 
134
 
        if (matcher.find()) {
135
 
            sequence.setCache(matcher.group(1).trim());
136
 
            line = ParserUtils.removeSubString(
137
 
                    line, matcher.start(), matcher.end());
138
 
        }
139
 
 
140
 
        return line;
141
 
    }
142
 
 
143
 
    /**
144
 
     * Processes CYCLE and NO CYCLE instructions.
145
 
     *
146
 
     * @param sequence sequence
147
 
     * @param command command
148
 
     *
149
 
     * @return command without CYCLE instructions
150
 
     */
151
 
    private static String processCycle(final PgSequence sequence,
152
 
            final String command) {
153
 
        String line = command;
154
 
 
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");
161
 
        }
162
 
 
163
 
        return line;
164
 
    }
165
 
 
166
 
    /**
167
 
     * Processes INCREMENT BY instruction.
168
 
     *
169
 
     * @param sequence sequence
170
 
     * @param command command
171
 
     *
172
 
     * @return command without INCREMENT BY instruction
173
 
     */
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);
178
 
 
179
 
        if (matcher.find()) {
180
 
            sequence.setIncrement(matcher.group(1).trim());
181
 
            line = ParserUtils.removeSubString(
182
 
                    line, matcher.start(), matcher.end());
183
 
        }
184
 
 
185
 
        return line;
186
 
    }
187
 
 
188
 
    /**
189
 
     * Processes MAX VALUE and NO MAXVALUE instructions.
190
 
     *
191
 
     * @param sequence sequence
192
 
     * @param command command
193
 
     *
194
 
     * @return command without MAX VALUE instructions
195
 
     */
196
 
    private static String processMaxValue(final PgSequence sequence,
197
 
            final String command) {
198
 
        String line = command;
199
 
 
200
 
        if (PATTERN_NO_MAXVALUE.matcher(line).matches()) {
201
 
            sequence.setMaxValue(null);
202
 
            line = ParserUtils.removeSubString(line, "NO MAXVALUE");
203
 
        } else {
204
 
            final Matcher matcher = PATTERN_MAXVALUE.matcher(line);
205
 
 
206
 
            if (matcher.find()) {
207
 
                sequence.setMaxValue(matcher.group(1).trim());
208
 
                line = ParserUtils.removeSubString(
209
 
                        line, matcher.start(), matcher.end());
210
 
            }
211
 
        }
212
 
 
213
 
        return line;
214
 
    }
215
 
 
216
 
    /**
217
 
     * Processes MIN VALUE and NO MINVALUE instructions.
218
 
     *
219
 
     * @param sequence sequence
220
 
     * @param command command
221
 
     *
222
 
     * @return command without MIN VALUE instructions
223
 
     */
224
 
    private static String processMinValue(final PgSequence sequence,
225
 
            final String command) {
226
 
        String line = command;
227
 
 
228
 
        if (PATTERN_NO_MINVALUE.matcher(line).matches()) {
229
 
            sequence.setMinValue(null);
230
 
            line = ParserUtils.removeSubString(line, "NO MINVALUE");
231
 
        } else {
232
 
            final Matcher matcher = PATTERN_MINVALUE.matcher(line);
233
 
 
234
 
            if (matcher.find()) {
235
 
                sequence.setMinValue(matcher.group(1).trim());
236
 
                line = ParserUtils.removeSubString(
237
 
                        line, matcher.start(), matcher.end());
238
 
            }
239
 
        }
240
 
 
241
 
        return line;
242
 
    }
243
 
 
244
 
    /**
245
 
     * Processes START WITH instruction.
246
 
     *
247
 
     * @param sequence sequence
248
 
     * @param command command
249
 
     *
250
 
     * @return command without START WITH instruction
251
 
     */
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);
256
 
 
257
 
        if (matcher.find()) {
258
 
            sequence.setStartWith(matcher.group(1).trim());
259
 
            line = ParserUtils.removeSubString(
260
 
                    line, matcher.start(), matcher.end());
261
 
        }
262
 
 
263
 
        return line;
 
38
        final String schemaName =
 
39
                ParserUtils.getSchemaName(sequenceName, database);
 
40
        database.getSchema(schemaName).addSequence(sequence);
 
41
 
 
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);
 
60
                } else {
 
61
                    sequence.setOwnedBy(ParserUtils.getObjectName(
 
62
                            parser.parseIdentifier()));
 
63
                }
 
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);
 
71
                } else {
 
72
                    parser.throwUnsupportedCommand();
 
73
                }
 
74
            } else {
 
75
                parser.throwUnsupportedCommand();
 
76
            }
 
77
        }
264
78
    }
265
79
}