| 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.mode; |
| 7 |
|
| 8 |
import static org.h2.result.SortOrder.DESCENDING; |
| 9 |
import static org.h2.result.SortOrder.NULLS_FIRST; |
| 10 |
import static org.h2.result.SortOrder.NULLS_LAST; |
| 11 |
|
| 12 |
/** |
| 13 |
* Default ordering of NULL values. |
| 14 |
*/ |
| 15 |
public enum DefaultNullOrdering { |
| 16 |
|
| 17 |
/** |
| 18 |
* NULL values are considered as smaller than other values during sorting. |
| 19 |
*/ |
| 20 |
LOW(NULLS_FIRST, NULLS_LAST), |
| 21 |
|
| 22 |
/** |
| 23 |
* NULL values are considered as larger than other values during sorting. |
| 24 |
*/ |
| 25 |
HIGH(NULLS_LAST, NULLS_FIRST), |
| 26 |
|
| 27 |
/** |
| 28 |
* NULL values are sorted before other values, no matter if ascending or |
| 29 |
* descending order is used. |
| 30 |
*/ |
| 31 |
FIRST(NULLS_FIRST, NULLS_FIRST), |
| 32 |
|
| 33 |
/** |
| 34 |
* NULL values are sorted after other values, no matter if ascending or |
| 35 |
* descending order is used. |
| 36 |
*/ |
| 37 |
LAST(NULLS_LAST, NULLS_LAST); |
| 38 |
|
| 39 |
private static final DefaultNullOrdering[] VALUES = values(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns default ordering of NULL values for the specified ordinal number. |
| 43 |
* |
| 44 |
* @param ordinal |
| 45 |
* ordinal number |
| 46 |
* @return default ordering of NULL values for the specified ordinal number |
| 47 |
* @see #ordinal() |
| 48 |
*/ |
| 49 |
public static DefaultNullOrdering valueOf(int ordinal) { |
| 50 |
return VALUES[ordinal]; |
| 51 |
} |
| 52 |
|
| 53 |
private final int defaultAscNulls, defaultDescNulls; |
| 54 |
|
| 55 |
private final int nullAsc, nullDesc; |
| 56 |
|
| 57 |
private DefaultNullOrdering(int defaultAscNulls, int defaultDescNulls) { |
| 58 |
this.defaultAscNulls = defaultAscNulls; |
| 59 |
this.defaultDescNulls = defaultDescNulls; |
| 60 |
nullAsc = defaultAscNulls == NULLS_FIRST ? -1 : 1; |
| 61 |
nullDesc = defaultDescNulls == NULLS_FIRST ? -1 : 1; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns a sort type bit mask with {@link org.h2.result.SortOrder#NULLS_FIRST} or |
| 66 |
* {@link org.h2.result.SortOrder#NULLS_LAST} explicitly set |
| 67 |
* |
| 68 |
* @param sortType |
| 69 |
* sort type bit mask |
| 70 |
* @return bit mask with {@link org.h2.result.SortOrder#NULLS_FIRST} or {@link org.h2.result.SortOrder#NULLS_LAST} |
| 71 |
* explicitly set |
| 72 |
*/ |
| 73 |
public int addExplicitNullOrdering(int sortType) { |
| 74 |
if ((sortType & (NULLS_FIRST | NULLS_LAST)) == 0) { |
| 75 |
sortType |= ((sortType & DESCENDING) == 0 ? defaultAscNulls : defaultDescNulls); |
| 76 |
} |
| 77 |
return sortType; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Compare two expressions where one of them is NULL. |
| 82 |
* |
| 83 |
* @param aNull |
| 84 |
* whether the first expression is null |
| 85 |
* @param sortType |
| 86 |
* the sort bit mask to use |
| 87 |
* @return the result of the comparison (-1 meaning the first expression |
| 88 |
* should appear before the second, 0 if they are equal) |
| 89 |
*/ |
| 90 |
public int compareNull(boolean aNull, int sortType) { |
| 91 |
if ((sortType & NULLS_FIRST) != 0) { |
| 92 |
return aNull ? -1 : 1; |
| 93 |
} else if ((sortType & NULLS_LAST) != 0) { |
| 94 |
return aNull ? 1 : -1; |
| 95 |
} else if ((sortType & DESCENDING) == 0) { |
| 96 |
return aNull ? nullAsc : -nullAsc; |
| 97 |
} else { |
| 98 |
return aNull ? nullDesc : -nullDesc; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
} |