1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.table; |
7 | |
8 | import org.h2.result.SortOrder; |
9 | |
10 | /** |
11 | * This represents a column item of an index. This is required because some |
12 | * indexes support descending sorted columns. |
13 | */ |
14 | public class IndexColumn { |
15 | |
16 | /** |
17 | * The column name. |
18 | */ |
19 | public String columnName; |
20 | |
21 | /** |
22 | * The column, or null if not set. |
23 | */ |
24 | public Column column; |
25 | |
26 | /** |
27 | * The sort type. Ascending (the default) and descending are supported; |
28 | * nulls can be sorted first or last. |
29 | */ |
30 | public int sortType = SortOrder.ASCENDING; |
31 | |
32 | /** |
33 | * Get the SQL snippet for this index column. |
34 | * |
35 | * @return the SQL snippet |
36 | */ |
37 | public String getSQL() { |
38 | StringBuilder buff = new StringBuilder(column.getSQL()); |
39 | if ((sortType & SortOrder.DESCENDING) != 0) { |
40 | buff.append(" DESC"); |
41 | } |
42 | if ((sortType & SortOrder.NULLS_FIRST) != 0) { |
43 | buff.append(" NULLS FIRST"); |
44 | } else if ((sortType & SortOrder.NULLS_LAST) != 0) { |
45 | buff.append(" NULLS LAST"); |
46 | } |
47 | return buff.toString(); |
48 | } |
49 | |
50 | /** |
51 | * Create an array of index columns from a list of columns. The default sort |
52 | * type is used. |
53 | * |
54 | * @param columns the column list |
55 | * @return the index column array |
56 | */ |
57 | public static IndexColumn[] wrap(Column[] columns) { |
58 | IndexColumn[] list = new IndexColumn[columns.length]; |
59 | for (int i = 0; i < list.length; i++) { |
60 | list[i] = new IndexColumn(); |
61 | list[i].column = columns[i]; |
62 | } |
63 | return list; |
64 | } |
65 | |
66 | /** |
67 | * Map the columns using the column names and the specified table. |
68 | * |
69 | * @param indexColumns the column list with column names set |
70 | * @param table the table from where to map the column names to columns |
71 | */ |
72 | public static void mapColumns(IndexColumn[] indexColumns, Table table) { |
73 | for (IndexColumn col : indexColumns) { |
74 | col.column = table.getColumn(col.columnName); |
75 | } |
76 | } |
77 | } |