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.DBEntityColumn;
29  import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataUtils;
30  import com.explosion.utilities.exception.ExceptionManagerFactory;
31  
32  /***
33   * This class represents an SQL select statement from one table
34   * 
35   * @author Stephen Created on Apr 22, 2004
36   */
37  public class SingleTableSelectStatement
38  {
39  
40      private DBEntity entity;
41      private MetadataUtils utils = new MetadataUtils();
42      
43      public SingleTableSelectStatement(DBEntity entity)
44      {
45          this.entity = entity;
46      }
47  
48      /***
49       * Builds up a select statement and returns it
50       * @see java.lang.Object#toString()
51       * @return
52       */
53      public String getSelectString(DatabaseMetaData meta) throws SQLException
54      {
55          try
56          {
57              String clause = "SELECT ";
58  
59              /* Get column info for this entity fromthe metaDataManager */
60              Iterator it = entity.getColumns().iterator();
61              int i = 0;
62              while (it.hasNext())
63              {
64                  DBEntityColumn column = (DBEntityColumn) it.next();
65                  String columnName = utils.getFullColumnName(column.getColumnName(), null, meta);
66                  clause += columnName + (i == entity.getColumns().size() - 1 ? "" : ", ");
67                  i++;
68              }
69  
70              /*
71               * There is a possibility that there are no column descriptions
72               * available
73               */
74              if (entity.getColumns().size() <= 0)
75                  clause += " * ";
76  
77              /* Obtain a proper entity name */
78              String fullEntityName = utils.getFullEntityName(entity, meta);
79  
80              /* Add them all together */
81              clause += (" from " + fullEntityName);
82  
83              clause += (new OrderByClause(entity)).getOrderByString(meta);
84  
85              return clause;
86          } catch (Exception e)
87          {
88              ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while building select statement.");
89              return null;
90          }
91  
92      }
93  
94  }