提交 7a212e63 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix NO MINVALUE|MAXVALUE in ALTER SEQUENCE

上级 f28d45c5
......@@ -6201,20 +6201,20 @@ public class Parser {
} else if (readIf("MINVALUE")) {
options.setMinValue(readExpression());
} else if (readIf("NOMINVALUE")) {
options.setMinValue(null);
options.setMinValue(ValueExpression.getNull());
} else if (readIf("MAXVALUE")) {
options.setMaxValue(readExpression());
} else if (readIf("NOMAXVALUE")) {
options.setMaxValue(null);
options.setMaxValue(ValueExpression.getNull());
} else if (readIf("CYCLE")) {
options.setCycle(true);
} else if (readIf("NOCYCLE")) {
options.setCycle(false);
} else if (readIf("NO")) {
if (readIf("MINVALUE")) {
options.setMinValue(null);
options.setMinValue(ValueExpression.getNull());
} else if (readIf("MAXVALUE")) {
options.setMaxValue(null);
options.setMaxValue(ValueExpression.getNull());
} else if (readIf("CYCLE")) {
options.setCycle(false);
} else if (readIf("CACHE")) {
......
......@@ -84,8 +84,8 @@ public class AlterSequence extends SchemaCommand {
sequence.setCacheSize(cache);
}
if (options.isRangeSet()) {
sequence.modify(options.getStartValue(session), options.getMinValue(session), options.getMaxValue(session),
options.getIncrement(session));
sequence.modify(options.getStartValue(session), options.getMinValue(sequence, session),
options.getMaxValue(sequence, session), options.getIncrement(session));
}
db.updateMeta(session, sequence);
return 0;
......
......@@ -54,8 +54,8 @@ public class CreateSequence extends SchemaCommand {
}
int id = getObjectId();
Sequence sequence = new Sequence(getSchema(), id, sequenceName, options.getStartValue(session),
options.getIncrement(session), options.getCacheSize(session), options.getMinValue(session),
options.getMaxValue(session), Boolean.TRUE.equals(options.getCycle()), belongsToTable);
options.getIncrement(session), options.getCacheSize(session), options.getMinValue(null, session),
options.getMaxValue(null, session), Boolean.TRUE.equals(options.getCycle()), belongsToTable);
db.addSchemaObject(session, sequence);
return 0;
}
......
......@@ -7,6 +7,8 @@ package org.h2.command.ddl;
import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.expression.ValueExpression;
import org.h2.schema.Sequence;
/**
* Sequence options.
......@@ -45,7 +47,11 @@ public class SequenceOptions {
this.increment = increment;
}
public Long getMaxValue(Session session) {
public Long getMaxValue(Sequence sequence, Session session) {
if (maxValue == ValueExpression.getNull() && sequence != null) {
return Sequence.getDefaultMaxValue(getCurrentStart(sequence, session),
increment != null ? getIncrement(session) : sequence.getIncrement());
}
return getLong(session, maxValue);
}
......@@ -53,10 +59,18 @@ public class SequenceOptions {
this.maxValue = maxValue;
}
public Long getMinValue(Session session) {
public Long getMinValue(Sequence sequence, Session session) {
if (minValue == ValueExpression.getNull() && sequence != null) {
return Sequence.getDefaultMinValue(getCurrentStart(sequence, session),
increment != null ? getIncrement(session) : sequence.getIncrement());
}
return getLong(session, minValue);
}
public long getCurrentStart(Sequence sequence, Session session) {
return start != null ? getStartValue(session) : sequence.getCurrentValue() + sequence.getIncrement();
}
public void setMinValue(Expression minValue) {
this.minValue = minValue;
}
......
......@@ -148,7 +148,7 @@ public class Sequence extends SchemaObjectBase {
Math.abs(increment) + Long.MIN_VALUE <= maxValue - minValue + Long.MIN_VALUE;
}
private static long getDefaultMinValue(Long startValue, long increment) {
public static long getDefaultMinValue(Long startValue, long increment) {
long v = increment >= 0 ? 1 : Long.MIN_VALUE;
if (startValue != null && increment >= 0 && startValue < v) {
v = startValue;
......@@ -156,7 +156,7 @@ public class Sequence extends SchemaObjectBase {
return v;
}
private static long getDefaultMaxValue(Long startValue, long increment) {
public static long getDefaultMaxValue(Long startValue, long increment) {
long v = increment >= 0 ? Long.MAX_VALUE : -1;
if (startValue != null && increment < 0 && startValue > v) {
v = startValue;
......
......@@ -456,7 +456,7 @@ public class Column {
} while (schema.findSequence(sequenceName) != null);
Sequence seq = new Sequence(schema, id, sequenceName, autoIncrementOptions.getStartValue(session),
autoIncrementOptions.getIncrement(session), autoIncrementOptions.getCacheSize(session),
autoIncrementOptions.getMinValue(session), autoIncrementOptions.getMaxValue(session),
autoIncrementOptions.getMinValue(null, session), autoIncrementOptions.getMaxValue(null, session),
Boolean.TRUE.equals(autoIncrementOptions.getCycle()), true);
seq.setTemporary(temporary);
session.getDatabase().addSchemaObject(session, seq);
......
......@@ -45,3 +45,36 @@ DROP SEQUENCE SEQ;
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY -9223372036854775808 MINVALUE 0 MAXVALUE 9223372036854775807;
> exception SEQUENCE_ATTRIBUTES_INVALID
CREATE SEQUENCE SEQ START WITH 0 MINVALUE -10 MAXVALUE 10;
> ok
SELECT SEQUENCE_NAME, CURRENT_VALUE, INCREMENT, CACHE, MIN_VALUE, MAX_VALUE, IS_CYCLE
FROM INFORMATION_SCHEMA.SEQUENCES;
> SEQUENCE_NAME CURRENT_VALUE INCREMENT CACHE MIN_VALUE MAX_VALUE IS_CYCLE
> ------------- ------------- --------- ----- --------- --------- --------
> SEQ -1 1 32 -10 10 FALSE
> rows: 1
ALTER SEQUENCE SEQ NO MINVALUE NO MAXVALUE;
> ok
SELECT SEQUENCE_NAME, CURRENT_VALUE, INCREMENT, CACHE, MIN_VALUE, MAX_VALUE, IS_CYCLE
FROM INFORMATION_SCHEMA.SEQUENCES;
> SEQUENCE_NAME CURRENT_VALUE INCREMENT CACHE MIN_VALUE MAX_VALUE IS_CYCLE
> ------------- ------------- --------- ----- --------- ------------------- --------
> SEQ -1 1 32 0 9223372036854775807 FALSE
> rows: 1
ALTER SEQUENCE SEQ MINVALUE -100 MAXVALUE 100;
> ok
SELECT SEQUENCE_NAME, CURRENT_VALUE, INCREMENT, CACHE, MIN_VALUE, MAX_VALUE, IS_CYCLE
FROM INFORMATION_SCHEMA.SEQUENCES;
> SEQUENCE_NAME CURRENT_VALUE INCREMENT CACHE MIN_VALUE MAX_VALUE IS_CYCLE
> ------------- ------------- --------- ----- --------- --------- --------
> SEQ -1 1 32 -100 100 FALSE
> rows: 1
DROP SEQUENCE SEQ;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论