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