Debian Codegraph @

src/main/org/h2/table/IndexColumn.java

raw
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.table;
7
8 import org.h2.result.SortOrder;
9 import org.h2.util.HasSQL;
10 import org.h2.util.ParserUtil;
11
12 /**
13 * This represents a column item of an index. This is required because some
14 * indexes support descending sorted columns.
15 */
16 public class IndexColumn {
17
18 /**
19 * Do not append ordering.
20 */
21 public static final int SQL_NO_ORDER = 0x8000_0000;
22
23 /**
24 * The column name.
25 */
26 public final String columnName;
27
28 /**
29 * The column, or null if not set.
30 */
31 public Column column;
32
33 /**
34 * The sort type. Ascending (the default) and descending are supported;
35 * nulls can be sorted first or last.
36 */
37 public int sortType = SortOrder.ASCENDING;
38
39 /**
40 * Appends the specified columns to the specified builder.
41 *
42 * @param builder
43 * string builder
44 * @param columns
45 * index columns
46 * @param sqlFlags
47 * formatting flags
48 * @return the specified string builder
49 */
50 public static StringBuilder writeColumns(StringBuilder builder, IndexColumn[] columns, int sqlFlags) {
51 return writeColumns(builder, columns, 0, columns.length, sqlFlags);
52 }
53
54 /**
55 * Appends the specified columns to the specified builder.
56 *
57 * @param builder
58 * string builder
59 * @param startOffset
60 * start offset, inclusive
61 * @param endOffset
62 * end offset, exclusive
63 * @param columns
64 * index columns
65 * @param sqlFlags
66 * formatting flags
67 * @return the specified string builder
68 */
69 public static StringBuilder writeColumns(StringBuilder builder, IndexColumn[] columns, int startOffset,
70 int endOffset, int sqlFlags) {
71 for (int i = startOffset; i < endOffset; i++) {
72 if (i > startOffset) {
73 builder.append(", ");
74 }
75 columns[i].getSQL(builder, sqlFlags);
76 }
77 return builder;
78 }
79
80 /**
81 * Appends the specified columns to the specified builder.
82 *
83 * @param builder
84 * string builder
85 * @param columns
86 * index columns
87 * @param separator
88 * separator
89 * @param suffix
90 * additional SQL to append after each column
91 * @param sqlFlags
92 * formatting flags
93 * @return the specified string builder
94 */
95 public static StringBuilder writeColumns(StringBuilder builder, IndexColumn[] columns, String separator,
96 String suffix, int sqlFlags) {
97 for (int i = 0, l = columns.length; i < l; i++) {
98 if (i > 0) {
99 builder.append(separator);
100 }
101 columns[i].getSQL(builder, sqlFlags).append(suffix);
102 }
103 return builder;
104 }
105
106 /**
107 * Creates a new instance with the specified name.
108 *
109 * @param columnName
110 * the column name
111 */
112 public IndexColumn(String columnName) {
113 this.columnName = columnName;
114 }
115
116 /**
117 * Creates a new instance with the specified name.
118 *
119 * @param columnName
120 * the column name
121 * @param sortType
122 * the sort type
123 */
124 public IndexColumn(String columnName, int sortType) {
125 this.columnName = columnName;
126 this.sortType = sortType;
127 }
128
129 /**
130 * Creates a new instance with the specified column.
131 *
132 * @param column
133 * the column
134 */
135 public IndexColumn(Column column) {
136 columnName = null;
137 this.column = column;
138 }
139
140 /**
141 * Appends the SQL snippet for this index column to the specified string builder.
142 *
143 * @param builder
144 * string builder
145 * @param sqlFlags
146 * formatting flags
147 * @return the specified string builder
148 */
149 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
150 if (column != null) {
151 column.getSQL(builder, sqlFlags);
152 } else {
153 ParserUtil.quoteIdentifier(builder, columnName, sqlFlags);
154 }
155 if ((sqlFlags & SQL_NO_ORDER) == 0) {
156 SortOrder.typeToString(builder, sortType);
157 }
158 return builder;
159 }
160
161 /**
162 * Create an array of index columns from a list of columns. The default sort
163 * type is used.
164 *
165 * @param columns the column list
166 * @return the index column array
167 */
168 public static IndexColumn[] wrap(Column[] columns) {
169 IndexColumn[] list = new IndexColumn[columns.length];
170 for (int i = 0; i < list.length; i++) {
171 list[i] = new IndexColumn(columns[i]);
172 }
173 return list;
174 }
175
176 /**
177 * Map the columns using the column names and the specified table.
178 *
179 * @param indexColumns the column list with column names set
180 * @param table the table from where to map the column names to columns
181 */
182 public static void mapColumns(IndexColumn[] indexColumns, Table table) {
183 for (IndexColumn col : indexColumns) {
184 col.column = table.getColumn(col.columnName);
185 }
186 }
187
188 @Override
189 public String toString() {
190 return getSQL(new StringBuilder("IndexColumn "), HasSQL.TRACE_SQL_FLAGS).toString();
191 }
192 }