File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\AlterSequence.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import org.h2.command.CommandInterface;
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.DbException;
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) {
46 table = column.getTable();
47 sequence = column.getSequence();
48 if (sequence == null) {
49 throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, column.getSQL());
50 }
51 }
52
53 public void setStartWith(Expression start) {
54 this.start = start;
55 }
56
57 public void setIncrement(Expression increment) {
58 this.increment = increment;
59 }
60
61 public int update() {
62 Database db = session.getDatabase();
63 if (table != null) {
64 session.getUser().checkRight(table, Right.ALL);
65 }
66 if (start != null) {
67 long startValue = start.optimize(session).getValue(session).getLong();
68 sequence.setStartValue(startValue);
69 }
70 if (increment != null) {
71 long incrementValue = increment.optimize(session).getValue(session).getLong();
72 if (incrementValue == 0) {
73 throw DbException.getInvalidValueException("INCREMENT", 0);
74 }
75 sequence.setIncrement(incrementValue);
76 }
77 // need to use the system session, so that the update
78 // can be committed immediately - not committing it
79 // would keep other transactions from using the sequence
80 Session sysSession = db.getSystemSession();
81 synchronized (sysSession) {
82 db.update(sysSession, sequence);
83 sysSession.commit(true);
84 }
85 return 0;
86 }
87
88 public int getType() {
89 return CommandInterface.ALTER_SEQUENCE;
90 }
91
92 }
93