File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\result\SortOrder.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.result;
8
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.Comparator;
12 import org.h2.constant.SysProperties;
13 import org.h2.engine.Database;
14 import org.h2.expression.Expression;
15 import org.h2.util.StatementBuilder;
16 import org.h2.util.StringUtils;
17 import org.h2.value.Value;
18 import org.h2.value.ValueNull;
19
20 /**
21 * A sort order represents an ORDER BY clause in a query.
22 */
23 public class SortOrder implements Comparator<Value[]> {
24
25 /**
26 * This bit mask means the values should be sorted in ascending order.
27 */
28 public static final int ASCENDING = 0;
29
30 /**
31 * This bit mask means the values should be sorted in descending order.
32 */
33 public static final int DESCENDING = 1;
34
35 /**
36 * This bit mask means NULLs should be sorted before other data, no matter
37 * if ascending or descending order is used.
38 */
39 public static final int NULLS_FIRST = 2;
40
41 /**
42 * This bit mask means NULLs should be sorted after other data, no matter
43 * if ascending or descending order is used.
44 */
45 public static final int NULLS_LAST = 4;
46
47 /**
48 * The default sort order for NULL.
49 */
50 private static final int DEFAULT_NULL_SORT = SysProperties.SORT_NULLS_HIGH ? 1 : -1;
51
52 private final Database database;
53 private final int[] indexes;
54 private final int[] sortTypes;
55
56 /**
57 * Construct a new sort order object.
58 *
59 * @param database the database
60 * @param index the column index list
61 * @param sortType the sort order bit masks
62 */
63 public SortOrder(Database database, int[] index, int[] sortType) {
64 this.database = database;
65 this.indexes = index;
66 this.sortTypes = sortType;
67 }
68
69 /**
70 * Create the SQL snippet that describes this sort order.
71 * This is the SQL snippet that usually appears after the ORDER BY clause.
72 *
73 * @param list the expression list
74 * @param visible the number of columns in the select list
75 * @return the SQL snippet
76 */
77 public String getSQL(Expression[] list, int visible) {
78 StatementBuilder buff = new StatementBuilder();
79 int i = 0;
80 for (int idx : indexes) {
81 buff.appendExceptFirst(", ");
82 if (idx < visible) {
83 buff.append(idx + 1);
84 } else {
85 buff.append('=').append(StringUtils.unEnclose(list[idx].getSQL()));
86 }
87 int type = sortTypes[i++];
88 if ((type & DESCENDING) != 0) {
89 buff.append(" DESC");
90 }
91 if ((type & NULLS_FIRST) != 0) {
92 buff.append(" NULLS FIRST");
93 } else if ((type & NULLS_LAST) != 0) {
94 buff.append(" NULLS LAST");
95 }
96 }
97 return buff.toString();
98 }
99
100 /**
101 * Compare two expressions where one of them is NULL.
102 *
103 * @param aNull whether the first expression is null
104 * @param sortType the sort bit mask to use
105 * @return the result of the comparison (-1 meaning the first expression
106 * should appear before the second, 0 if they are equal)
107 */
108 public static int compareNull(boolean aNull, int sortType) {
109 if ((sortType & NULLS_FIRST) != 0) {
110 return aNull ? -1 : 1;
111 } else if ((sortType & NULLS_LAST) != 0) {
112 return aNull ? 1 : -1;
113 } else {
114 // see also JdbcDatabaseMetaData.nullsAreSorted*
115 int comp = aNull ? DEFAULT_NULL_SORT : -DEFAULT_NULL_SORT;
116 return (sortType & DESCENDING) == 0 ? comp : -comp;
117 }
118 }
119
120 /**
121 * Compare two expression lists.
122 *
123 * @param a the first expression list
124 * @param b the second expression list
125 * @return the result of the comparison
126 */
127 public int compare(Value[] a, Value[] b) {
128 for (int i = 0, len = indexes.length; i < len; i++) {
129 int idx = indexes[i];
130 int type = sortTypes[i];
131 Value ao = a[idx];
132 Value bo = b[idx];
133 boolean aNull = ao == ValueNull.INSTANCE, bNull = bo == ValueNull.INSTANCE;
134 if (aNull || bNull) {
135 if (aNull == bNull) {
136 continue;
137 }
138 return compareNull(aNull, type);
139 }
140 int comp = database.compare(ao, bo);
141 if (comp != 0) {
142 return (type & DESCENDING) == 0 ? comp : -comp;
143 }
144 }
145 return 0;
146 }
147
148 /**
149 * Sort a list of rows.
150 *
151 * @param rows the list of rows
152 */
153 public void sort(ArrayList<Value[]> rows) {
154 Collections.sort(rows, this);
155 }
156
157 /**
158 * Get the column index list.
159 *
160 * @return the list
161 */
162 public int[] getIndexes() {
163 return indexes;
164 }
165
166 /**
167 * Get the sort order bit masks.
168 *
169 * @return the list
170 */
171 public int[] getSortTypes() {
172 return sortTypes;
173 }
174
175 }
176