File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\schema\Sequence.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.schema;
8
9 import org.h2.engine.DbObject;
10 import org.h2.engine.Session;
11 import org.h2.message.DbException;
12 import org.h2.message.Trace;
13 import org.h2.table.Table;
14
15 /**
16 *A sequence is created using the statement
17 * CREATE SEQUENCE
18 */
19 public class Sequence extends SchemaObjectBase {
20
21 /**
22 * The default cache size for sequences.
23 */
24 public static final int DEFAULT_CACHE_SIZE = 32;
25
26 private long value = 1;
27 private long valueWithMargin;
28 private long increment = 1;
29 private long cacheSize = DEFAULT_CACHE_SIZE;
30 private boolean belongsToTable;
31
32 public Sequence(Schema schema, int id, String name, boolean belongsToTable) {
33 initSchemaObjectBase(schema, id, name, Trace.SEQUENCE);
34 this.belongsToTable = belongsToTable;
35 }
36
37 public synchronized void setStartValue(long value) {
38 this.value = value;
39 this.valueWithMargin = value;
40 }
41
42 public boolean getBelongsToTable() {
43 return belongsToTable;
44 }
45
46 public long getIncrement() {
47 return increment;
48 }
49
50 public void setIncrement(long inc) {
51 if (inc == 0) {
52 throw DbException.getInvalidValueException("INCREMENT", 0);
53 }
54 this.increment = inc;
55 }
56
57 public String getDropSQL() {
58 if (getBelongsToTable()) {
59 return null;
60 }
61 return "DROP SEQUENCE IF EXISTS " + getSQL();
62 }
63
64 public String getCreateSQLForCopy(Table table, String quotedName) {
65 throw DbException.throwInternalError();
66 }
67
68 public synchronized String getCreateSQL() {
69 StringBuilder buff = new StringBuilder("CREATE SEQUENCE ");
70 buff.append(getSQL()).append(" START WITH ").append(value);
71 if (increment != 1) {
72 buff.append(" INCREMENT BY ").append(increment);
73 }
74 if (cacheSize != DEFAULT_CACHE_SIZE) {
75 buff.append(" CACHE ").append(cacheSize);
76 }
77 if (belongsToTable) {
78 buff.append(" BELONGS_TO_TABLE");
79 }
80 return buff.toString();
81 }
82
83 /**
84 * Get the next value for this sequence.
85 *
86 * @param session the session
87 * @return the next value
88 */
89 public synchronized long getNext(Session session) {
90 if ((increment > 0 && value >= valueWithMargin) || (increment < 0 && value <= valueWithMargin)) {
91 valueWithMargin += increment * cacheSize;
92 flush(session);
93 }
94 long v = value;
95 value += increment;
96 return v;
97 }
98
99 /**
100 * Flush the current value to disk.
101 */
102 public void flushWithoutMargin() {
103 if (valueWithMargin != value) {
104 valueWithMargin = value;
105 flush(null);
106 }
107 }
108
109 /**
110 * Flush the current value, including the margin, to disk.
111 *
112 * @param session the session
113 */
114 public synchronized void flush(Session session) {
115 Session sysSession = database.getSystemSession();
116 if (session == null || !database.isSysTableLocked()) {
117 // this session may not lock the sys table (except if it already has locked it)
118 // because it must be committed immediately
119 // otherwise other threads can not access the sys table.
120 session = sysSession;
121 }
122 synchronized (session) {
123 // just for this case, use the value with the margin for the script
124 long realValue = value;
125 try {
126 value = valueWithMargin;
127 database.update(session, this);
128 } finally {
129 value = realValue;
130 }
131 if (session == sysSession) {
132 // if the system session is used,
133 // the transaction must be committed immediately
134 sysSession.commit(false);
135 }
136 }
137 }
138
139 /**
140 * Flush the current value to disk and close this object.
141 */
142 public void close() {
143 flushWithoutMargin();
144 }
145
146 public int getType() {
147 return DbObject.SEQUENCE;
148 }
149
150 public void removeChildrenAndResources(Session session) {
151 database.removeMeta(session, getId());
152 invalidate();
153 }
154
155 public void checkRename() {
156 // nothing to do
157 }
158
159 public synchronized long getCurrentValue() {
160 return value - increment;
161 }
162
163 public void setBelongsToTable(boolean b) {
164 this.belongsToTable = b;
165 }
166
167 public void setCacheSize(long cacheSize) {
168 this.cacheSize = Math.max(1, cacheSize);
169 }
170
171 public long getCacheSize() {
172 return cacheSize;
173 }
174
175 }
176