KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querymodel > ColumnNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querymodel;
20
21 import java.util.Collection JavaDoc;
22
23 /**
24  * Represents a column in a SELECT clause
25  */

26 public class ColumnNode extends ColumnItem implements Column {
27
28     // Fields
29

30     private TableNode _table;
31
32     private Identifier _columnName;
33
34     private Identifier _derivedColName; // Column alias
35

36
37     // Constructors
38

39     // Called from a number of places in the editor
40
// First arg is a tableSpec, which may need to be split
41
public ColumnNode(String JavaDoc tableSpec, String JavaDoc columnName) {
42
43         String JavaDoc tableName=null, schemaName=null;
44
45         // See if we've got a schema specified with the table
46
String JavaDoc[] table = tableSpec.split("\\.");
47         if (table.length>1) {
48             schemaName=table[0];
49             tableName=table[1];
50         } else
51             tableName=tableSpec;
52
53         // Note that this will take care of delimiters if necessary
54
_table = new TableNode(tableName, null, schemaName);
55         _columnName=new Identifier(columnName);
56      }
57
58     // Called only from QueryModel.replaceStar
59
public ColumnNode(String JavaDoc tableName, String JavaDoc columnName, String JavaDoc corrName, String JavaDoc schemaName) {
60         _table=new TableNode(tableName, corrName, schemaName);
61         _columnName = new Identifier(columnName);
62
63     }
64
65 // public ColumnNode(String tableName, String columnName, String corrName) {
66
// this(tableName, columnName, corrName, null);
67
// }
68

69
70 // // Used mainly for "*", "?"
71
// public ColumnNode(String columnName) {
72
// this(null, columnName, null, null);
73
// }
74

75
76     // Special case where we already have the table object
77
public ColumnNode(Table table, String JavaDoc columnName) {
78         _table = (TableNode)table;
79         _columnName = new Identifier(columnName);
80     }
81
82     // Ctor used by the make method
83
private ColumnNode() {
84     }
85
86     // Pseudo-constructor
87
// These constructors take Strings, but can't be overloaded with 'Identifier'
88
// because of compiler ambiguity
89
public static ColumnNode make (Identifier tableName, Identifier columnName, Identifier schemaName,
90         Identifier derivedColName)
91     {
92         ColumnNode c = new ColumnNode();
93         c._columnName = columnName;
94         c._derivedColName = derivedColName;
95         c._table= (tableName!=null) ? TableNode.make(tableName, null, schemaName) : null;
96         return c;
97     }
98
99
100     // Methods
101

102     Column getReferencedColumn() {
103         return this;
104     }
105
106     public void getReferencedColumns(Collection JavaDoc columns) {}
107     public void getQueryItems(Collection JavaDoc items) {}
108
109     public boolean matches(String JavaDoc table, String JavaDoc column) {
110         return (table.equals(getTableSpec()) && column.equals(getColumnName()));
111     }
112
113     public boolean matches(String JavaDoc table) {
114         return table.equals(getTableSpec());
115     }
116
117     public boolean equals(Column column) {
118         return column.matches(getTableSpec(), getColumnName());
119     }
120
121     public String JavaDoc genText(boolean select) {
122         return
123             // Table Spec, if any
124
( ((_table!=null) && (_table.getTableSpec()!=null)) ?
125               _table.genText(false)+ "." : // NOI18N
126
"") + // NOI18N
127

128             // Column Name
129
_columnName.genText() +
130
131             // Derived Column Name, if there is one and we're in a SELECT
132
( ((select) && (_derivedColName!=null)) ?
133               " AS " + _derivedColName.genText() : // NOI18N
134
""); // NOI18N
135
}
136
137
138     public String JavaDoc genText() {
139         return genText(false);
140     }
141
142
143     /**
144      * Rename the table part of the column spec
145      */

146     public void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
147         _table.renameTableSpec(oldTableSpec, corrName);
148     }
149
150
151     /**
152      * set table name
153      */

154     public void setTableSpec (String JavaDoc oldTableSpec, String JavaDoc newTableSpec) {
155         if ( _table == null ) {
156             String JavaDoc tableName=null, schemaName=null;
157
158             // See if we've got a schema specified with the table
159
String JavaDoc[] table = newTableSpec.split("\\.");
160             if (table.length>1) {
161                 schemaName=table[0];
162                 tableName=table[1];
163             } else
164                 tableName=newTableSpec;
165
166             // Note that this will take care of delimiters if necessary
167
_table = new TableNode(tableName, null, schemaName);
168         }
169
170         _table.setTableSpec ( oldTableSpec, newTableSpec );
171     }
172
173
174     // Accessors/Mutators
175

176     public String JavaDoc getColumnName() {
177         return _columnName.getName();
178     }
179
180     public String JavaDoc getTableSpec() {
181         return (_table==null) ? null : _table.getTableSpec();
182     }
183
184     public String JavaDoc getFullTableName() {
185         return (_table==null) ? null : _table.getFullTableName();
186     }
187
188     public String JavaDoc getDerivedColName() {
189         return
190             (_derivedColName==null) ? null : _derivedColName.getName();
191     }
192
193     public void setDerivedColName(String JavaDoc derivedColName) {
194         _derivedColName =
195             (derivedColName==null) ? null : new Identifier(derivedColName);
196     }
197
198
199     /**
200      * set column name
201      */

202     public void setColumnName (String JavaDoc oldColumnName, String JavaDoc newColumnName) {
203         if ( _columnName.getName().equals(oldColumnName) ) {
204             _columnName = new Identifier(newColumnName);
205         }
206     }
207
208     public void setColumnTableName (String JavaDoc tableName ) {
209         if ( _table == null ) {
210             // this should never happen.
211
_table = new TableNode();
212         }
213         _table.setTableName (tableName);
214     }
215
216     public void setColumnCorrName (String JavaDoc corrName ) {
217         if ( _table == null ) {
218             // this should never happen.
219
_table = new TableNode();
220         }
221         _table.setCorrName (corrName);
222     }
223
224     public boolean isParameterized() {
225         return false;
226     }
227     
228     public Expression findExpression(String JavaDoc table1, String JavaDoc column1, String JavaDoc table2, String JavaDoc column2) {
229         return null;
230     }
231 }
232
233
234
Popular Tags