提交 36fc00c4 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Rename containsNullAndAllowMultipleNull() to mayHaveDuplicates()

Previous name was incorrect.
上级 9ba6f143
......@@ -87,17 +87,16 @@ public class Mode {
public boolean systemColumns;
/**
* For unique indexes, NULL is distinct. That means only one row with NULL
* in one of the columns is allowed.
* <ul>
* <li>If {@code 0}, multiple identical indexed columns with at least one
* {@code NULL} value are allowed in unique index.</li>
* <li>If {@code 1}, multiple identical indexed columns with all {@code NULL}
* values are allowed in unique index.</li>
* <li>If {@code 2}, multiple identical indexed columns are not allowed in
* unique index.</li>
* </ul>
*/
public boolean uniqueIndexSingleNull;
/**
* When using unique indexes, multiple rows with NULL in all columns
* are allowed, however it is not allowed to have multiple rows with the
* same values otherwise.
*/
public boolean uniqueIndexSingleNullExceptAllColumnsAreNull;
public byte uniquieIndexNullsHandling;
/**
* Empty strings are treated like NULL values. Useful for Oracle emulation.
......@@ -208,7 +207,7 @@ public class Mode {
mode = new Mode(ModeEnum.Derby.name());
mode.aliasColumnName = true;
mode.uniqueIndexSingleNull = true;
mode.uniquieIndexNullsHandling = 2;
mode.supportOffsetFetch = true;
mode.sysDummy1 = true;
mode.isolationLevelInSelectOrInsertStatement = true;
......@@ -220,7 +219,7 @@ public class Mode {
mode.aliasColumnName = true;
mode.convertOnlyToSmallerScale = true;
mode.nullConcatIsNull = true;
mode.uniqueIndexSingleNull = true;
mode.uniquieIndexNullsHandling = 2;
mode.allowPlusForStringConcat = true;
// HSQLDB does not support client info properties. See
// http://hsqldb.org/doc/apidocs/
......@@ -232,7 +231,7 @@ public class Mode {
mode = new Mode(ModeEnum.MSSQLServer.name());
mode.aliasColumnName = true;
mode.squareBracketQuotedNames = true;
mode.uniqueIndexSingleNull = true;
mode.uniquieIndexNullsHandling = 2;
mode.allowPlusForStringConcat = true;
mode.swapConvertFunctionParameters = true;
mode.supportPoundSymbolForColumnNames = true;
......@@ -260,7 +259,7 @@ public class Mode {
mode = new Mode(ModeEnum.Oracle.name());
mode.aliasColumnName = true;
mode.convertOnlyToSmallerScale = true;
mode.uniqueIndexSingleNullExceptAllColumnsAreNull = true;
mode.uniquieIndexNullsHandling = 1;
mode.treatEmptyStringsAsNull = true;
mode.regexpReplaceBackslashReferences = true;
mode.supportPoundSymbolForColumnNames = true;
......
......@@ -9,7 +9,6 @@ import java.util.HashSet;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.DbObject;
import org.h2.engine.Mode;
import org.h2.engine.Session;
import org.h2.message.DbException;
import org.h2.message.Trace;
......@@ -302,33 +301,43 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
}
/**
* Check if one of the columns is NULL and multiple rows with NULL are
* allowed using the current compatibility mode for unique indexes. Note:
* NULL behavior is complicated in SQL.
* Check if this row may have duplicates with the same indexed values in the
* current compatibility mode.
*
* @param newRow the row to check
* @return true if one of the columns is null and multiple nulls in unique
* indexes are allowed
* @param searchRow
* the row to check
* @return {@code true} if specified row may have duplicates,
* {@code false otherwise}
*/
protected boolean containsNullAndAllowMultipleNull(SearchRow newRow) {
Mode mode = database.getMode();
if (mode.uniqueIndexSingleNull) {
protected boolean mayHaveDuplicates(SearchRow searchRow) {
int mode = database.getMode().uniquieIndexNullsHandling;
/*
* Multiple identical indexed columns with at least one NULL value are allowed
* in unique index.
*/
if (mode == 0) {
for (int index : columnIds) {
if (searchRow.getValue(index) == ValueNull.INSTANCE) {
return true;
}
}
return false;
} else if (mode.uniqueIndexSingleNullExceptAllColumnsAreNull) {
}
/*
* Multiple identical indexed columns with all {@code NULL} values are allowed
* in unique index.
*/
if (mode == 1) {
for (int index : columnIds) {
Value v = newRow.getValue(index);
if (v != ValueNull.INSTANCE) {
if (searchRow.getValue(index) != ValueNull.INSTANCE) {
return false;
}
}
return true;
}
for (int index : columnIds) {
Value v = newRow.getValue(index);
if (v == ValueNull.INSTANCE) {
return true;
}
}
/*
* Multiple identical indexed columns are not allowed in unique index.
*/
return false;
}
......
......@@ -116,7 +116,7 @@ public abstract class PageBtree extends Page {
comp = index.compareRows(row, compare);
if (comp == 0) {
if (add && index.indexType.isUnique()) {
if (!index.containsNullAndAllowMultipleNull(compare)) {
if (!index.mayHaveDuplicates(compare)) {
throw index.getDuplicateKeyException(compare.toString());
}
}
......
......@@ -66,7 +66,7 @@ public class TreeIndex extends BaseIndex {
int compare = compareRows(row, r);
if (compare == 0) {
if (indexType.isUnique()) {
if (!containsNullAndAllowMultipleNull(row)) {
if (!mayHaveDuplicates(row)) {
throw getDuplicateKeyException(row.toString());
}
}
......
......@@ -208,7 +208,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
if (compareRows(row, r2) != 0) {
break;
}
if (containsNullAndAllowMultipleNull(r2)) {
if (mayHaveDuplicates(r2)) {
// this is allowed
continue;
}
......@@ -233,7 +233,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
break;
}
if (map.get(k) != null) {
if (!containsNullAndAllowMultipleNull(r2)) {
if (!mayHaveDuplicates(r2)) {
throw getDuplicateKeyException(k.toString());
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论