01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.dml;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.command.ddl.SchemaCommand;
11: import org.h2.constant.ErrorCode;
12: import org.h2.engine.Database;
13: import org.h2.engine.Right;
14: import org.h2.engine.Session;
15: import org.h2.expression.Expression;
16: import org.h2.message.Message;
17: import org.h2.schema.Schema;
18: import org.h2.schema.Sequence;
19: import org.h2.table.Column;
20: import org.h2.table.Table;
21:
22: /**
23: * This class represents the statement
24: * ALTER SEQUENCE
25: */
26: public class AlterSequence extends SchemaCommand {
27:
28: private Table table;
29: private Sequence sequence;
30: private Expression start;
31: private Expression increment;
32:
33: public AlterSequence(Session session, Schema schema) {
34: super (session, schema);
35: }
36:
37: public void setSequence(Sequence sequence) {
38: this .sequence = sequence;
39: }
40:
41: public boolean isTransactional() {
42: return true;
43: }
44:
45: public void setColumn(Column column) throws SQLException {
46: table = column.getTable();
47: sequence = column.getSequence();
48: if (sequence == null) {
49: throw Message.getSQLException(
50: ErrorCode.SEQUENCE_NOT_FOUND_1, column.getSQL());
51: }
52: }
53:
54: public void setStartWith(Expression start) {
55: this .start = start;
56: }
57:
58: public void setIncrement(Expression increment) throws SQLException {
59: this .increment = increment;
60: }
61:
62: public int update() throws SQLException {
63: // TODO rights: what are the rights required for a sequence?
64: Database db = session.getDatabase();
65: if (table != null) {
66: session.getUser().checkRight(table, Right.ALL);
67: }
68: if (start != null) {
69: long startValue = start.optimize(session).getValue(session)
70: .getLong();
71: sequence.setStartValue(startValue);
72: }
73: if (increment != null) {
74: long incrementValue = increment.optimize(session).getValue(
75: session).getLong();
76: if (incrementValue == 0) {
77: throw Message.getSQLException(
78: ErrorCode.INVALID_VALUE_2, new String[] { "0",
79: "INCREMENT" });
80: }
81: sequence.setIncrement(incrementValue);
82: }
83: db.update(session, sequence);
84: return 0;
85: }
86:
87: }
|