提交 48a8b9c7 authored 作者: noelgrandin's avatar noelgrandin

Fix bug in calculating default MIN and MAX values for SEQUENCE

上级 c11011ec
...@@ -20,6 +20,7 @@ Change Log ...@@ -20,6 +20,7 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Storing LOBs in separate files (outside of the database) is no longer supported for new databases. <ul><li>Storing LOBs in separate files (outside of the database) is no longer supported for new databases.
</li><li>Lucene 2 is no longer supported. </li><li>Lucene 2 is no longer supported.
</li><li>Fix bug in calculating default MIN and MAX values for SEQUENCE
</li></ul> </li></ul>
<h2>Version 1.3.175 (2014-01-18)</h2> <h2>Version 1.3.175 (2014-01-18)</h2>
......
...@@ -66,8 +66,8 @@ public class Sequence extends SchemaObjectBase { ...@@ -66,8 +66,8 @@ public class Sequence extends SchemaObjectBase {
Long minValue, Long maxValue, boolean cycle, boolean belongsToTable) { Long minValue, Long maxValue, boolean cycle, boolean belongsToTable) {
initSchemaObjectBase(schema, id, name, Trace.SEQUENCE); initSchemaObjectBase(schema, id, name, Trace.SEQUENCE);
this.increment = increment != null ? increment : 1; this.increment = increment != null ? increment : 1;
this.minValue = minValue != null ? minValue : getDefaultMinValue(this.increment); this.minValue = minValue != null ? minValue : getDefaultMinValue(startValue, this.increment);
this.maxValue = maxValue != null ? maxValue : getDefaultMaxValue(this.increment); this.maxValue = maxValue != null ? maxValue : getDefaultMaxValue(startValue, this.increment);
this.value = startValue != null ? startValue : getDefaultStartValue(this.increment); this.value = startValue != null ? startValue : getDefaultStartValue(this.increment);
this.valueWithMargin = value; this.valueWithMargin = value;
this.cacheSize = cacheSize != null ? Math.max(1, cacheSize) : DEFAULT_CACHE_SIZE; this.cacheSize = cacheSize != null ? Math.max(1, cacheSize) : DEFAULT_CACHE_SIZE;
...@@ -136,12 +136,20 @@ public class Sequence extends SchemaObjectBase { ...@@ -136,12 +136,20 @@ public class Sequence extends SchemaObjectBase {
BigInteger.valueOf(maxValue).subtract(BigInteger.valueOf(minValue))) < 0; BigInteger.valueOf(maxValue).subtract(BigInteger.valueOf(minValue))) < 0;
} }
private static long getDefaultMinValue(long increment) { private static long getDefaultMinValue(Long startValue, long increment) {
return increment >= 0 ? 1 : Long.MIN_VALUE; long v = increment >= 0 ? 1 : Long.MIN_VALUE;
if (startValue != null && increment >= 0 && startValue < v) {
v = startValue;
}
return v;
} }
private static long getDefaultMaxValue(long increment) { private static long getDefaultMaxValue(Long startValue, long increment) {
return increment >= 0 ? Long.MAX_VALUE : -1; long v = increment >= 0 ? Long.MAX_VALUE : -1;
if (startValue != null && increment < 0 && startValue > v) {
v = startValue;
}
return v;
} }
private long getDefaultStartValue(long increment) { private long getDefaultStartValue(long increment) {
...@@ -192,10 +200,10 @@ public class Sequence extends SchemaObjectBase { ...@@ -192,10 +200,10 @@ public class Sequence extends SchemaObjectBase {
if (increment != 1) { if (increment != 1) {
buff.append(" INCREMENT BY ").append(increment); buff.append(" INCREMENT BY ").append(increment);
} }
if (minValue != getDefaultMinValue(increment)) { if (minValue != getDefaultMinValue(value, increment)) {
buff.append(" MINVALUE ").append(minValue); buff.append(" MINVALUE ").append(minValue);
} }
if (maxValue != getDefaultMaxValue(increment)) { if (maxValue != getDefaultMaxValue(value, increment)) {
buff.append(" MAXVALUE ").append(maxValue); buff.append(" MAXVALUE ").append(maxValue);
} }
if (cycle) { if (cycle) {
......
...@@ -41,6 +41,7 @@ public class TestSequence extends TestBase { ...@@ -41,6 +41,7 @@ public class TestSequence extends TestBase {
testCreateWithMaxValue(); testCreateWithMaxValue();
testCreationErrors(); testCreationErrors();
testCreateSql(); testCreateSql();
testDefaultMinMax();
deleteDb("sequence"); deleteDb("sequence");
} }
...@@ -252,6 +253,16 @@ public class TestSequence extends TestBase { ...@@ -252,6 +253,16 @@ public class TestSequence extends TestBase {
conn.close(); conn.close();
} }
private void testDefaultMinMax() throws SQLException {
// test that we calculate default MIN and MAX values correctly
deleteDb("sequence");
Connection conn = getConnection("sequence");
Statement stat = conn.createStatement();
stat.execute("create sequence a START WITH -7320917853639540658");
stat.execute("create sequence b START WITH 7320917853639540658 INCREMENT -1");
conn.close();
}
private void testTwo() throws SQLException { private void testTwo() throws SQLException {
deleteDb("sequence"); deleteDb("sequence");
Connection conn = getConnection("sequence"); Connection conn = getConnection("sequence");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论