1 package com.explosion.expfmodules.rdbmsconn.dbom.sql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
72
73
74 if (entity.getColumns().size() <= 0)
75 clause += " * ";
76
77
78 String fullEntityName = utils.getFullEntityName(entity, meta);
79
80
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 }