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.dml; |
7 | |
8 | import org.h2.api.ErrorCode; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.command.ddl.SchemaCommand; |
11 | import org.h2.engine.Database; |
12 | import org.h2.engine.Right; |
13 | import org.h2.engine.Session; |
14 | import org.h2.expression.Expression; |
15 | import org.h2.message.DbException; |
16 | import org.h2.schema.Schema; |
17 | import org.h2.schema.Sequence; |
18 | import org.h2.table.Column; |
19 | import org.h2.table.Table; |
20 | |
21 | /** |
22 | * This class represents the statement |
23 | * ALTER SEQUENCE |
24 | */ |
25 | public class AlterSequence extends SchemaCommand { |
26 | |
27 | private Table table; |
28 | private Sequence sequence; |
29 | private Expression start; |
30 | private Expression increment; |
31 | private Boolean cycle; |
32 | private Expression minValue; |
33 | private Expression maxValue; |
34 | private Expression cacheSize; |
35 | |
36 | public AlterSequence(Session session, Schema schema) { |
37 | super(session, schema); |
38 | } |
39 | |
40 | public void setSequence(Sequence sequence) { |
41 | this.sequence = sequence; |
42 | } |
43 | |
44 | @Override |
45 | public boolean isTransactional() { |
46 | return true; |
47 | } |
48 | |
49 | public void setColumn(Column column) { |
50 | table = column.getTable(); |
51 | sequence = column.getSequence(); |
52 | if (sequence == null) { |
53 | throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, column.getSQL()); |
54 | } |
55 | } |
56 | |
57 | public void setStartWith(Expression start) { |
58 | this.start = start; |
59 | } |
60 | |
61 | public void setIncrement(Expression increment) { |
62 | this.increment = increment; |
63 | } |
64 | |
65 | public void setCycle(Boolean cycle) { |
66 | this.cycle = cycle; |
67 | } |
68 | |
69 | public void setMinValue(Expression minValue) { |
70 | this.minValue = minValue; |
71 | } |
72 | |
73 | public void setMaxValue(Expression maxValue) { |
74 | this.maxValue = maxValue; |
75 | } |
76 | |
77 | public void setCacheSize(Expression cacheSize) { |
78 | this.cacheSize = cacheSize; |
79 | } |
80 | |
81 | @Override |
82 | public int update() { |
83 | Database db = session.getDatabase(); |
84 | if (table != null) { |
85 | session.getUser().checkRight(table, Right.ALL); |
86 | } |
87 | if (cycle != null) { |
88 | sequence.setCycle(cycle); |
89 | } |
90 | if (cacheSize != null) { |
91 | long size = cacheSize.optimize(session).getValue(session).getLong(); |
92 | sequence.setCacheSize(size); |
93 | } |
94 | if (start != null || minValue != null || |
95 | maxValue != null || increment != null) { |
96 | Long startValue = getLong(start); |
97 | Long min = getLong(minValue); |
98 | Long max = getLong(maxValue); |
99 | Long inc = getLong(increment); |
100 | sequence.modify(startValue, min, max, inc); |
101 | } |
102 | // need to use the system session, so that the update |
103 | // can be committed immediately - not committing it |
104 | // would keep other transactions from using the sequence |
105 | Session sysSession = db.getSystemSession(); |
106 | synchronized (sysSession) { |
107 | synchronized (db) { |
108 | db.updateMeta(sysSession, sequence); |
109 | sysSession.commit(true); |
110 | } |
111 | } |
112 | return 0; |
113 | } |
114 | |
115 | private Long getLong(Expression expr) { |
116 | if (expr == null) { |
117 | return null; |
118 | } |
119 | return expr.optimize(session).getValue(session).getLong(); |
120 | } |
121 | |
122 | @Override |
123 | public int getType() { |
124 | return CommandInterface.ALTER_SEQUENCE; |
125 | } |
126 | |
127 | } |