1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.command.ddl; |
7 | |
8 | import org.h2.api.ErrorCode; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.engine.Database; |
11 | import org.h2.engine.Session; |
12 | import org.h2.expression.Expression; |
13 | import org.h2.message.DbException; |
14 | import org.h2.schema.Schema; |
15 | import org.h2.schema.Sequence; |
16 | |
17 | /** |
18 | * This class represents the statement |
19 | * CREATE SEQUENCE |
20 | */ |
21 | public class CreateSequence extends SchemaCommand { |
22 | |
23 | private String sequenceName; |
24 | private boolean ifNotExists; |
25 | private boolean cycle; |
26 | private Expression minValue; |
27 | private Expression maxValue; |
28 | private Expression start; |
29 | private Expression increment; |
30 | private Expression cacheSize; |
31 | private boolean belongsToTable; |
32 | |
33 | public CreateSequence(Session session, Schema schema) { |
34 | super(session, schema); |
35 | } |
36 | |
37 | public void setSequenceName(String sequenceName) { |
38 | this.sequenceName = sequenceName; |
39 | } |
40 | |
41 | public void setIfNotExists(boolean ifNotExists) { |
42 | this.ifNotExists = ifNotExists; |
43 | } |
44 | |
45 | public void setCycle(boolean cycle) { |
46 | this.cycle = cycle; |
47 | } |
48 | |
49 | @Override |
50 | public int update() { |
51 | session.commit(true); |
52 | Database db = session.getDatabase(); |
53 | if (getSchema().findSequence(sequenceName) != null) { |
54 | if (ifNotExists) { |
55 | return 0; |
56 | } |
57 | throw DbException.get(ErrorCode.SEQUENCE_ALREADY_EXISTS_1, sequenceName); |
58 | } |
59 | int id = getObjectId(); |
60 | Long startValue = getLong(start); |
61 | Long inc = getLong(increment); |
62 | Long cache = getLong(cacheSize); |
63 | Long min = getLong(minValue); |
64 | Long max = getLong(maxValue); |
65 | Sequence sequence = new Sequence(getSchema(), id, sequenceName, startValue, inc, |
66 | cache, min, max, cycle, belongsToTable); |
67 | db.addSchemaObject(session, sequence); |
68 | return 0; |
69 | } |
70 | |
71 | private Long getLong(Expression expr) { |
72 | if (expr == null) { |
73 | return null; |
74 | } |
75 | return expr.optimize(session).getValue(session).getLong(); |
76 | } |
77 | |
78 | public void setStartWith(Expression start) { |
79 | this.start = start; |
80 | } |
81 | |
82 | public void setIncrement(Expression increment) { |
83 | this.increment = increment; |
84 | } |
85 | |
86 | public void setMinValue(Expression minValue) { |
87 | this.minValue = minValue; |
88 | } |
89 | |
90 | public void setMaxValue(Expression maxValue) { |
91 | this.maxValue = maxValue; |
92 | } |
93 | |
94 | public void setBelongsToTable(boolean belongsToTable) { |
95 | this.belongsToTable = belongsToTable; |
96 | } |
97 | |
98 | public void setCacheSize(Expression cacheSize) { |
99 | this.cacheSize = cacheSize; |
100 | } |
101 | |
102 | @Override |
103 | public int getType() { |
104 | return CommandInterface.CREATE_SEQUENCE; |
105 | } |
106 | |
107 | } |