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.command.dml; |
7 | |
8 | import org.h2.expression.Expression; |
9 | |
10 | /** |
11 | * Describes one element of the ORDER BY clause of a query. |
12 | */ |
13 | public class SelectOrderBy { |
14 | |
15 | /** |
16 | * The order by expression. |
17 | */ |
18 | public Expression expression; |
19 | |
20 | /** |
21 | * The column index expression. This can be a column index number (1 meaning |
22 | * the first column of the select list) or a parameter (the parameter is a |
23 | * number representing the column index number). |
24 | */ |
25 | public Expression columnIndexExpr; |
26 | |
27 | /** |
28 | * If the column should be sorted descending. |
29 | */ |
30 | public boolean descending; |
31 | |
32 | /** |
33 | * If NULL should be appear first. |
34 | */ |
35 | public boolean nullsFirst; |
36 | |
37 | /** |
38 | * If NULL should be appear at the end. |
39 | */ |
40 | public boolean nullsLast; |
41 | |
42 | public String getSQL() { |
43 | StringBuilder buff = new StringBuilder(); |
44 | if (expression != null) { |
45 | buff.append('=').append(expression.getSQL()); |
46 | } else { |
47 | buff.append(columnIndexExpr.getSQL()); |
48 | } |
49 | if (descending) { |
50 | buff.append(" DESC"); |
51 | } |
52 | if (nullsFirst) { |
53 | buff.append(" NULLS FIRST"); |
54 | } else if (nullsLast) { |
55 | buff.append(" NULLS LAST"); |
56 | } |
57 | return buff.toString(); |
58 | } |
59 | |
60 | } |