View Javadoc

1   package com.explosion.expfmodules.rdbmsconn.dbom.sql;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.sql.DatabaseMetaData;
24  import java.sql.SQLException;
25  import java.util.Iterator;
26  
27  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
28  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityBuffer;
29  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityColumn;
30  import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataUtils;
31  import com.explosion.utilities.exception.ExceptionManagerFactory;
32  
33  /***
34   * This class represents an order by clause in an SQL statement
35   * 
36   * @author Stephen Created on Apr 22, 2004
37   */
38  public class OrderByClause
39  {
40  
41      private DBEntity entity;
42      public static final String ASCENDING_ORDER = " ASC ";
43      public static final String DESCENDING_ORDER = " DESC ";
44      private MetadataUtils utils = new MetadataUtils();
45      
46      /***
47       * Constructs an order by object which will use the sort order defined in the DBEntity buffer 
48       * to sort the columns.
49       * @param entity
50       */
51      public OrderByClause(DBEntity entity)
52      {
53          this.entity = entity;
54      }
55      
56  	/***
57  	 * Returns an order by statement
58  	 * @return
59  	 * @throws SQLException
60  	 * @throws MaxColumnsInOrderByExceededException
61  	 */
62      public String getOrderByString(DatabaseMetaData meta) throws MaxColumnsInOrderByExceededException, SQLException
63      {
64          int size = entity.getDBEntityBuffer().getSortedColumnsList().size();
65          if (size == 0)
66              return "";
67          else
68          {
69              if (meta.getMaxColumnsInOrderBy() != 0 && size > meta.getMaxColumnsInOrderBy())
70              {
71                  throw new MaxColumnsInOrderByExceededException("Sorry, your database does not support order by statements that contain more than " + meta.getMaxColumnsInOrderBy() + " columns.");
72              }
73              
74              String clause = " order by ";
75              for (Iterator it = entity.getDBEntityBuffer().getSortedColumnsList().iterator(); it.hasNext();)
76              {
77                  String name = (String) it.next();
78                  DBEntityColumn column = entity.getColumn(name);
79                  boolean ascending = entity.getDBEntityBuffer().getSortDirection(name) == DBEntityBuffer.ASCENDING;
80                  try
81                  {
82                      clause += utils.getFullColumnName(name, null, meta) + " " + (ascending ? ASCENDING_ORDER : DESCENDING_ORDER) + (it.hasNext() ? "," : "");
83                  }
84                  catch (Exception e)
85                  {
86                      ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while createing order by clause.");
87                  }
88              }
89              return clause;
90          }
91      }
92  
93  }