KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.util.Collection JavaDoc;
23
24 /**
25  * Represents a generalized table
26  */

27 public class TableNode implements Table {
28
29     // Fields
30

31     private Identifier _tableName;
32
33     private Identifier _corrName;
34
35     private Identifier _schemaName;
36
37
38     // Constructors
39

40     public TableNode(String JavaDoc tableName, String JavaDoc corrName, String JavaDoc schemaName) {
41         _tableName = new Identifier(tableName);
42         _corrName = corrName==null ? null : new Identifier(corrName);
43         _schemaName = schemaName==null ? null : new Identifier(schemaName);
44     }
45
46     public TableNode(String JavaDoc tableName, String JavaDoc corrName) {
47         this(tableName, corrName, null);
48     }
49
50     public TableNode(String JavaDoc tableName) {
51         this(tableName, null, null);
52     }
53
54     public TableNode() {
55     }
56
57     // PseudoConstructor
58
// Use static methods because we can't overload Identier, String
59
public static TableNode make(Identifier tableName, Identifier corrName, Identifier schemaName) {
60         TableNode t = new TableNode();
61         t._tableName = tableName;
62         t._corrName = corrName;
63         t._schemaName = schemaName;
64         return t;
65     }
66
67
68     // Methods
69
public String JavaDoc genText() {
70         return genText(false);
71     }
72
73     // Return the SQL string that corresponds to this Table
74
// This was originally called only in FROM clauses, but is now used as
75
// part of column specifications.
76
// For now, assume no joins
77
public String JavaDoc genText(boolean from) {
78         if (from) // Calling from within a FROM clause
79
return
80                 ((_schemaName==null) ? "" : _schemaName.genText()+".") + // NOI18N
81
_tableName.genText() +
82                 // remove AS to fix CR5097412
83
((_corrName==null) ? "" : " " + _corrName.genText()); // NOI18N
84
else // Calling from within a column
85
return
86                 ((_corrName!=null)
87                  ? _corrName.genText()
88                  : ((_schemaName==null) ? "" : _schemaName.genText()+".") + // NOI18N
89
_tableName.genText());
90     }
91
92
93     // Accessors/Mutators
94

95     public String JavaDoc getTableName() {
96         return _tableName.getName();
97     }
98
99     public String JavaDoc getFullTableName() {
100         return
101             ((_schemaName==null) ? "" : _schemaName.getName()+".") + // NOI18N
102
_tableName.getName();
103     }
104
105     public String JavaDoc getCorrName() {
106         return (_corrName==null) ? null : _corrName.getName();
107     }
108
109     public String JavaDoc getSchemaName() {
110         return (_schemaName==null) ? null : _schemaName.getName();
111     }
112
113     public String JavaDoc getTableSpec() {
114         return (_corrName!=null) ?
115                _corrName.getName() :
116                getFullTableName();
117     }
118
119     // if oldTableSpec is null, just replace the table schema and name.
120
public void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
121         // If we really have a correlation name, there cannot be any schema specified to split
122
if (oldTableSpec == null) {
123             String JavaDoc[] table = corrName.split("\\."); // NOI18N
124
if (table.length>1) {
125                 _schemaName= new Identifier(table[0]);
126                 _tableName = new Identifier(table[1]);
127             } else
128                 _tableName= new Identifier(table[0]);
129         }
130         else if (getTableSpec().equals(oldTableSpec))
131             _corrName=(corrName==null) ? null : new Identifier(corrName);
132     }
133
134     void setTableName (String JavaDoc tableName) {
135         String JavaDoc[] table = tableName.split("\\."); // NOI18N
136
if (table.length>1) {
137             _schemaName= new Identifier(table[0]);
138             _tableName = new Identifier(table[1]);
139         } else
140             _tableName=new Identifier(table[0]);
141     }
142
143     void setCorrName (String JavaDoc corrName) {
144         _corrName=new Identifier(corrName);
145     }
146
147     void setTableSpec(String JavaDoc oldTableSpec, String JavaDoc newTableSpec) {
148         if (getTableSpec().equals(oldTableSpec)) {
149             String JavaDoc[] table = newTableSpec.split("\\."); // NOI18N
150
if (table.length>1) {
151                 _schemaName= new Identifier(table[0]);
152                 _tableName = new Identifier(table[1]);
153             } else
154                 _tableName=new Identifier(table[0]);
155         }
156
157     }
158
159     public void getReferencedColumns(Collection JavaDoc columns) {}
160     public void getQueryItems(Collection JavaDoc items) {}
161 }
162
Popular Tags