KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > exp > ColumnExp


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.exp;
13
14 import com.versant.core.jdbc.metadata.JdbcColumn;
15 import com.versant.core.jdbc.metadata.JdbcField;
16 import com.versant.core.jdbc.sql.SqlDriver;
17 import com.versant.core.util.CharBuf;
18 import com.versant.core.metadata.ClassMetaData;
19 import com.versant.core.jdo.query.ParamNode;
20 import com.versant.core.common.Debug;
21
22 import java.util.Map JavaDoc;
23
24 import com.versant.core.common.BindingSupportImpl;
25
26 import javax.jdo.JDOFatalInternalException;
27
28 /**
29  * The name of a column.
30  */

31 public class ColumnExp extends LeafExp {
32
33     /**
34      * This may be null in which case this expression refers to all columns
35      * (i.e. a 'select *').
36      */

37     public JdbcColumn col;
38     /**
39      * The table this column belongs to. This provides the correct alias.
40      */

41     public SelectExp selectExp;
42     /**
43      * The field this expression is for (null if none).
44      */

45     public JdbcField jdbcField;
46     /**
47      * The class this expression is for (-1 if none).
48      */

49     public ClassMetaData cmd;
50
51     private String JavaDoc asValue;
52
53     public ColumnExp(JdbcColumn col, SelectExp selectExp, JdbcField jdbcField) {
54         if (Debug.DEBUG) {
55             if (selectExp == null) {
56                 throw BindingSupportImpl.getInstance().internal("");
57             }
58             if (col.table != selectExp.table) {
59                 throw BindingSupportImpl.getInstance().internal("The col for the table is not the same as the select exp");
60             }
61         }
62         this.col = col;
63         this.selectExp = selectExp;
64         this.jdbcField = jdbcField;
65     }
66
67     public ColumnExp() {
68     }
69
70     public ColumnExp(String JavaDoc asValue) {
71         if (asValue != null && asValue.length() > 0) {
72             this.asValue = asValue;
73         } else {
74             this.asValue = null;
75         }
76     }
77
78     public SqlExp createInstance() {
79         return new ColumnExp();
80     }
81
82     public SqlExp getClone(SqlExp columnExp, Map JavaDoc cloneMap) {
83         super.getClone(columnExp, cloneMap);
84         ColumnExp cst = (ColumnExp) columnExp;
85
86         cst.col = col;
87         cst.jdbcField = jdbcField;
88         cst.cmd = cmd;
89         if (selectExp != null) cst.selectExp = (SelectExp) createClone(selectExp, cloneMap);
90
91         return columnExp;
92     }
93
94     public ClassMetaData getCmd() {
95         return cmd;
96     }
97
98     public void setCmd(ClassMetaData cmd) {
99         this.cmd = cmd;
100     }
101
102     public String JavaDoc toString() {
103         return super.toString() + " " + col + " " + selectExp;
104     }
105
106     /**
107      * Append SQL for this node to s.
108      *
109      * @param driver The driver being used
110      * @param s Append the SQL here
111      * @param leftSibling
112      */

113     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
114         if (isAliasedColumn() && jdbcField == null) {
115             s.append(" " + asValue + " ");
116         } else {
117             driver.appendSqlColumn(col, selectExp.alias, s);
118             if (isAliasedColumn()) {
119                 s.append(driver.getAliasPrepend() + asValue);
120             }
121         }
122     }
123
124     /**
125      * What is the JDBC type of this expression (0 if unknown)?
126      */

127     public int getJdbcType() {
128         return col.jdbcType;
129     }
130
131     /**
132      * What is the java type code of this expression (0 if unknown)?
133      */

134     public int getJavaTypeCode() {
135         return col.javaTypeCode;
136     }
137
138     /**
139      * What is the class index for this expression (-1 if unknown)?
140      * @see ParamNode
141      */

142     public int getClassIndex() {
143         if (cmd == null)
144             return -1;
145         else
146             return cmd.index;
147     }
148
149     /**
150      * Replace any references to old with nw. This is used when redundant
151      * joins are removed.
152      */

153     public void replaceSelectExpRef(SelectExp old, SelectExp nw) {
154         if (nw == null) {
155             throw new JDOFatalInternalException();
156         }
157         if (selectExp == old) {
158             selectExp = nw;
159         } else {
160             SelectExp other = nw.findTableRecursive(selectExp.table,
161                     selectExp.jdbcField);
162             if (other != null) selectExp = other;
163         }
164     }
165
166     /**
167      * If this expression involves a single table only then return the
168      * SelectExp for the table (e.g. a.col1 == 10 returns a, a.col1 = b.col2
169      * returns null). This is used to detect expressions that can be moved
170      * into the ON list for the join to the table involved.
171      * @param exclude
172      */

173     public SelectExp getSingleSelectExp(SelectExp exclude) {
174         return selectExp;
175     }
176
177     /**
178      * Create an aliases for any subtables we may have.
179      */

180     public int createAlias(int index) {
181         if ( selectExp == null )
182             return 0;
183         else
184             return selectExp.createAlias(index);
185     }
186
187     public boolean isAliasedColumn() {
188         return asValue != null;
189     }
190
191     public String JavaDoc getAsValue() {
192         return asValue;
193     }
194
195     public void setColAlias(String JavaDoc columnAlias) {
196         if (columnAlias != null && columnAlias.length() > 0) {
197             this.asValue = columnAlias;
198         } else {
199             this.asValue = null;
200         }
201     }
202 }
203
Popular Tags