| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://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.engine.SessionLocal; |
| 10 |
import org.h2.expression.Expression; |
| 11 |
import org.h2.expression.ValueExpression; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.schema.Sequence; |
| 14 |
import org.h2.value.TypeInfo; |
| 15 |
import org.h2.value.Value; |
| 16 |
import org.h2.value.ValueBigint; |
| 17 |
import org.h2.value.ValueNull; |
| 18 |
|
| 19 |
/** |
| 20 |
* Sequence options. |
| 21 |
*/ |
| 22 |
public class SequenceOptions { |
| 23 |
|
| 24 |
private TypeInfo dataType; |
| 25 |
|
| 26 |
private Expression start; |
| 27 |
|
| 28 |
private Expression restart; |
| 29 |
|
| 30 |
private Expression increment; |
| 31 |
|
| 32 |
private Expression maxValue; |
| 33 |
|
| 34 |
private Expression minValue; |
| 35 |
|
| 36 |
private Sequence.Cycle cycle; |
| 37 |
|
| 38 |
private Expression cacheSize; |
| 39 |
|
| 40 |
private long[] bounds; |
| 41 |
|
| 42 |
private final Sequence oldSequence; |
| 43 |
|
| 44 |
private static Long getLong(SessionLocal session, Expression expr) { |
| 45 |
if (expr != null) { |
| 46 |
Value value = expr.optimize(session).getValue(session); |
| 47 |
if (value != ValueNull.INSTANCE) { |
| 48 |
return value.getLong(); |
| 49 |
} |
| 50 |
} |
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates new instance of sequence options. |
| 56 |
*/ |
| 57 |
public SequenceOptions() { |
| 58 |
oldSequence = null; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Creates new instance of sequence options. |
| 63 |
* |
| 64 |
* @param oldSequence |
| 65 |
* the sequence to copy options from |
| 66 |
* @param dataType |
| 67 |
* the new data type |
| 68 |
*/ |
| 69 |
public SequenceOptions(Sequence oldSequence, TypeInfo dataType) { |
| 70 |
this.oldSequence = oldSequence; |
| 71 |
this.dataType = dataType; |
| 72 |
// Check data type correctness immediately |
| 73 |
getBounds(); |
| 74 |
} |
| 75 |
|
| 76 |
public TypeInfo getDataType() { |
| 77 |
if (oldSequence != null) { |
| 78 |
synchronized (oldSequence) { |
| 79 |
copyFromOldSequence(); |
| 80 |
} |
| 81 |
} |
| 82 |
return dataType; |
| 83 |
} |
| 84 |
|
| 85 |
private void copyFromOldSequence() { |
| 86 |
long bounds[] = getBounds(); |
| 87 |
long min = Math.max(oldSequence.getMinValue(), bounds[0]); |
| 88 |
long max = Math.min(oldSequence.getMaxValue(), bounds[1]); |
| 89 |
if (max < min) { |
| 90 |
min = bounds[0]; |
| 91 |
max = bounds[1]; |
| 92 |
} |
| 93 |
minValue = ValueExpression.get(ValueBigint.get(min)); |
| 94 |
maxValue = ValueExpression.get(ValueBigint.get(max)); |
| 95 |
long v = oldSequence.getStartValue(); |
| 96 |
if (v >= min && v <= max) { |
| 97 |
start = ValueExpression.get(ValueBigint.get(v)); |
| 98 |
} |
| 99 |
v = oldSequence.getBaseValue(); |
| 100 |
if (v >= min && v <= max) { |
| 101 |
restart = ValueExpression.get(ValueBigint.get(v)); |
| 102 |
} |
| 103 |
increment = ValueExpression.get(ValueBigint.get(oldSequence.getIncrement())); |
| 104 |
cycle = oldSequence.getCycle(); |
| 105 |
cacheSize = ValueExpression.get(ValueBigint.get(oldSequence.getCacheSize())); |
| 106 |
} |
| 107 |
|
| 108 |
public void setDataType(TypeInfo dataType) { |
| 109 |
this.dataType = dataType; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Gets start value. |
| 114 |
* |
| 115 |
* @param session The session to calculate the value. |
| 116 |
* @return start value or {@code null} if value is not defined. |
| 117 |
*/ |
| 118 |
public Long getStartValue(SessionLocal session) { |
| 119 |
return check(getLong(session, start)); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Sets start value expression. |
| 124 |
* |
| 125 |
* @param start START WITH value expression. |
| 126 |
*/ |
| 127 |
public void setStartValue(Expression start) { |
| 128 |
this.start = start; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Gets restart value. |
| 133 |
* |
| 134 |
* @param session |
| 135 |
* the session to calculate the value |
| 136 |
* @param startValue |
| 137 |
* the start value to use if restart without value is specified |
| 138 |
* @return restart value or {@code null} if value is not defined. |
| 139 |
*/ |
| 140 |
public Long getRestartValue(SessionLocal session, long startValue) { |
| 141 |
return check(restart == ValueExpression.DEFAULT ? (Long) startValue : getLong(session, restart)); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Sets restart value expression, or {@link ValueExpression#DEFAULT}. |
| 146 |
* |
| 147 |
* @param restart |
| 148 |
* RESTART WITH value expression, or |
| 149 |
* {@link ValueExpression#DEFAULT} for simple RESTART |
| 150 |
*/ |
| 151 |
public void setRestartValue(Expression restart) { |
| 152 |
this.restart = restart; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Gets increment value. |
| 157 |
* |
| 158 |
* @param session The session to calculate the value. |
| 159 |
* @return increment value or {@code null} if value is not defined. |
| 160 |
*/ |
| 161 |
public Long getIncrement(SessionLocal session) { |
| 162 |
return check(getLong(session, increment)); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Sets increment value expression. |
| 167 |
* |
| 168 |
* @param increment INCREMENT BY value expression. |
| 169 |
*/ |
| 170 |
public void setIncrement(Expression increment) { |
| 171 |
this.increment = increment; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Gets max value. |
| 176 |
* |
| 177 |
* @param sequence the sequence to get default max value. |
| 178 |
* @param session The session to calculate the value. |
| 179 |
* @return max value when the MAXVALUE expression is set, otherwise returns default max value. |
| 180 |
*/ |
| 181 |
public Long getMaxValue(Sequence sequence, SessionLocal session) { |
| 182 |
Long v; |
| 183 |
if (maxValue == ValueExpression.NULL && sequence != null) { |
| 184 |
v = Sequence.getDefaultMaxValue(getCurrentStart(sequence, session), |
| 185 |
increment != null ? getIncrement(session) : sequence.getIncrement(), getBounds()); |
| 186 |
} else { |
| 187 |
v = getLong(session, maxValue); |
| 188 |
} |
| 189 |
return check(v); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Sets max value expression. |
| 194 |
* |
| 195 |
* @param maxValue MAXVALUE expression. |
| 196 |
*/ |
| 197 |
public void setMaxValue(Expression maxValue) { |
| 198 |
this.maxValue = maxValue; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Gets min value. |
| 203 |
* |
| 204 |
* @param sequence the sequence to get default min value. |
| 205 |
* @param session The session to calculate the value. |
| 206 |
* @return min value when the MINVALUE expression is set, otherwise returns default min value. |
| 207 |
*/ |
| 208 |
public Long getMinValue(Sequence sequence, SessionLocal session) { |
| 209 |
Long v; |
| 210 |
if (minValue == ValueExpression.NULL && sequence != null) { |
| 211 |
v = Sequence.getDefaultMinValue(getCurrentStart(sequence, session), |
| 212 |
increment != null ? getIncrement(session) : sequence.getIncrement(), getBounds()); |
| 213 |
} else { |
| 214 |
v = getLong(session, minValue); |
| 215 |
} |
| 216 |
return check(v); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Sets min value expression. |
| 221 |
* |
| 222 |
* @param minValue MINVALUE expression. |
| 223 |
*/ |
| 224 |
public void setMinValue(Expression minValue) { |
| 225 |
this.minValue = minValue; |
| 226 |
} |
| 227 |
|
| 228 |
private Long check(Long value) { |
| 229 |
if (value == null) { |
| 230 |
return null; |
| 231 |
} else { |
| 232 |
long[] bounds = getBounds(); |
| 233 |
long v = value; |
| 234 |
if (v < bounds[0] || v > bounds[1]) { |
| 235 |
throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, Long.toString(v)); |
| 236 |
} |
| 237 |
} |
| 238 |
return value; |
| 239 |
} |
| 240 |
|
| 241 |
public long[] getBounds() { |
| 242 |
long[] bounds = this.bounds; |
| 243 |
if (bounds == null) { |
| 244 |
this.bounds = bounds = getBounds(dataType); |
| 245 |
} |
| 246 |
return bounds; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Get the bounds (min, max) of a data type. |
| 251 |
* |
| 252 |
* @param dataType the data type |
| 253 |
* @return the bounds (an array with 2 elements) |
| 254 |
*/ |
| 255 |
public static long[] getBounds(TypeInfo dataType) { |
| 256 |
long min, max; |
| 257 |
switch (dataType.getValueType()) { |
| 258 |
case Value.TINYINT: |
| 259 |
min = Byte.MIN_VALUE; |
| 260 |
max = Byte.MAX_VALUE; |
| 261 |
break; |
| 262 |
case Value.SMALLINT: |
| 263 |
min = Short.MIN_VALUE; |
| 264 |
max = Short.MAX_VALUE; |
| 265 |
break; |
| 266 |
case Value.INTEGER: |
| 267 |
min = Integer.MIN_VALUE; |
| 268 |
max = Integer.MAX_VALUE; |
| 269 |
break; |
| 270 |
case Value.BIGINT: |
| 271 |
min = Long.MIN_VALUE; |
| 272 |
max = Long.MAX_VALUE; |
| 273 |
break; |
| 274 |
case Value.REAL: |
| 275 |
min = -0x100_0000; |
| 276 |
max = 0x100_0000; |
| 277 |
break; |
| 278 |
case Value.DOUBLE: |
| 279 |
min = -0x20_0000_0000_0000L; |
| 280 |
max = 0x20_0000_0000_0000L; |
| 281 |
break; |
| 282 |
case Value.NUMERIC: { |
| 283 |
if (dataType.getScale() != 0) { |
| 284 |
throw DbException.getUnsupportedException(dataType.getTraceSQL()); |
| 285 |
} |
| 286 |
long p = (dataType.getPrecision() - dataType.getScale()); |
| 287 |
if (p <= 0) { |
| 288 |
throw DbException.getUnsupportedException(dataType.getTraceSQL()); |
| 289 |
} else if (p > 18) { |
| 290 |
min = Long.MIN_VALUE; |
| 291 |
max = Long.MAX_VALUE; |
| 292 |
} else { |
| 293 |
max = 10; |
| 294 |
for (int i = 1; i < p; i++) { |
| 295 |
max *= 10; |
| 296 |
} |
| 297 |
min = - --max; |
| 298 |
} |
| 299 |
break; |
| 300 |
} |
| 301 |
case Value.DECFLOAT: { |
| 302 |
long p = dataType.getPrecision(); |
| 303 |
if (p > 18) { |
| 304 |
min = Long.MIN_VALUE; |
| 305 |
max = Long.MAX_VALUE; |
| 306 |
} else { |
| 307 |
max = 10; |
| 308 |
for (int i = 1; i < p; i++) { |
| 309 |
max *= 10; |
| 310 |
} |
| 311 |
min = -max; |
| 312 |
} |
| 313 |
break; |
| 314 |
} |
| 315 |
default: |
| 316 |
throw DbException.getUnsupportedException(dataType.getTraceSQL()); |
| 317 |
} |
| 318 |
long bounds[] = { min, max }; |
| 319 |
return bounds; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Gets cycle option. |
| 324 |
* |
| 325 |
* @return cycle option value or {@code null} if is not defined. |
| 326 |
*/ |
| 327 |
public Sequence.Cycle getCycle() { |
| 328 |
return cycle; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Sets cycle option. |
| 333 |
* |
| 334 |
* @param cycle option value. |
| 335 |
*/ |
| 336 |
public void setCycle(Sequence.Cycle cycle) { |
| 337 |
this.cycle = cycle; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Gets cache size. |
| 342 |
* |
| 343 |
* @param session The session to calculate the value. |
| 344 |
* @return cache size or {@code null} if value is not defined. |
| 345 |
*/ |
| 346 |
public Long getCacheSize(SessionLocal session) { |
| 347 |
return getLong(session, cacheSize); |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Sets cache size. |
| 352 |
* |
| 353 |
* @param cacheSize cache size. |
| 354 |
*/ |
| 355 |
public void setCacheSize(Expression cacheSize) { |
| 356 |
this.cacheSize = cacheSize; |
| 357 |
} |
| 358 |
|
| 359 |
private long getCurrentStart(Sequence sequence, SessionLocal session) { |
| 360 |
return start != null ? getStartValue(session) : sequence.getBaseValue(); |
| 361 |
} |
| 362 |
} |