1 | package org.sqlorm.querybuilder; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import org.sqlorm.querybuilder.OrderByExpr.Ordering; |
7 | |
8 | class OrderByList implements IPositionOrderBy { |
9 | List<OrderByExpr> orderBys = new ArrayList<OrderByExpr>(); |
10 | |
11 | public OrderByList add(final String tableName, final Ordering ordering) { |
12 | final OrderByExpr obExpr = new OrderByExpr(tableName, ordering); |
13 | if(orderBys.contains(obExpr) == false) { |
14 | orderBys.add(obExpr); |
15 | } |
16 | return this; |
17 | } |
18 | |
19 | public void toSql(final StringBuilder sb, final String indent) { |
20 | if(orderBys.size() == 0) |
21 | return; |
22 | sb.append("\n"); |
23 | sb.append(indent); |
24 | sb.append("ORDER BY "); |
25 | |
26 | for(final OrderByExpr ob : orderBys) { |
27 | ob.toSql(sb, indent); |
28 | sb.append(","); |
29 | } |
30 | sb.deleteCharAt(sb.length() - 1); // delete last comma |
31 | |
32 | } |
33 | } |