View Javadoc

1   package org.apache.torque.map;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  /***
20   * ColumnMap is used to model a column of a table in a database.
21   *
22   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
23   * @version $Id: ColumnMap.java 326224 2005-10-18 20:35:33Z tfischer $
24   */
25  public class ColumnMap implements java.io.Serializable
26  {
27      /*** Type of the column. */
28      private Object type = null;
29  
30      /*** Size of the column. */
31      private int size = 0;
32      
33      /*** Scale of the column */
34      private int scale = 0;
35  
36      /*** Is it a primary key? */
37      private boolean pk = false;
38  
39      /*** Is null value allowed ?*/
40      private boolean notNull = false;
41  
42      /*** Name of the table that this column is related to. */
43      private String relatedTableName = "";
44  
45      /*** Name of the column that this column is related to. */
46      private String relatedColumnName = "";
47  
48      /*** The TableMap for this column. */
49      private TableMap table;
50  
51      /*** The name of the column. */
52      private String columnName;
53  
54  
55      /***
56       * Constructor.
57       *
58       * @param name The name of the column.
59       * @param containingTable TableMap of the table this column is in.
60       */
61      public ColumnMap(String name, TableMap containingTable)
62      {
63          this.columnName = name;
64          table = containingTable;
65      }
66  
67      /***
68       * Get the name of a column.
69       *
70       * @return A String with the column name.
71       */
72      public String getColumnName()
73      {
74          return columnName;
75      }
76  
77      /***
78       * Get the table name + column name.
79       *
80       * @return A String with the full column name.
81       */
82      public String getFullyQualifiedName()
83      {
84          return table.getName() + "." + columnName;
85      }
86  
87      /***
88       * Get the name of the table this column is in.
89       *
90       * @return A String with the table name.
91       */
92      public String getTableName()
93      {
94          return table.getName();
95      }
96  
97      /***
98       * Set the type of this column.
99       *
100      * @param type An Object specifying the type.
101      */
102     public void setType (Object type)
103     {
104         this.type = type;
105     }
106 
107     /***
108      * Set the size of this column.
109      *
110      * @param size An int specifying the size.
111      */
112     public void setSize(int size)
113     {
114         this.size = size;
115     }
116 
117     /***
118      * Set if this column is a primary key or not.
119      *
120      * @param pk True if column is a primary key.
121      */
122     public void setPrimaryKey(boolean pk)
123     {
124         this.pk = pk;
125     }
126 
127     /***
128      * Set if this column may be null.
129      *
130      * @param nn True if column may be null.
131      */
132     public void setNotNull(boolean nn)
133     {
134         this.notNull = nn;
135     }
136 
137     /***
138      * Set the foreign key for this column.
139      *
140      * @param fullyQualifiedName The name of the table.column that is
141      * foreign.
142      */
143     public void setForeignKey(String fullyQualifiedName)
144     {
145         if (fullyQualifiedName != null && fullyQualifiedName.length() > 0)
146         {
147             relatedTableName = fullyQualifiedName.substring(
148                     0, fullyQualifiedName.indexOf('.'));
149             relatedColumnName = fullyQualifiedName.substring(
150                     fullyQualifiedName.indexOf('.') + 1);
151         }
152         else
153         {
154             relatedTableName = "";
155             relatedColumnName = "";
156         }
157     }
158 
159     /***
160      * Set the foreign key for this column.
161      *
162      * @param tableName The name of the table that is foreign.
163      * @param columnName The name of the column that is foreign.
164      */
165     public void setForeignKey(String tableName, String columnName)
166     {
167         if (tableName != null && tableName.length() > 0 && columnName != null
168                 && columnName.length() > 0)
169         {
170             relatedTableName = tableName;
171             relatedColumnName = columnName;
172         }
173         else
174         {
175             relatedTableName = "";
176             relatedColumnName = "";
177         }
178     }
179 
180     /***
181      * Get the type of this column.
182      *
183      * @return An Object specifying the type.
184      */
185     public Object getType()
186     {
187         return type;
188     }
189 
190     /***
191      * The "precision" value from the XML 
192      * size="&lt;precision&gt;[,&lt;scale&gt;]"
193      * attribute. Where [,&lt;scale&gt;] is optional. 
194      * 
195      * If the size attribute has not been set in the XML, it will return 0.<p>
196      * 
197      * Note that the size="P,S" format should be replaced with 
198      * size="P" scale="S".
199      *
200      * @return An int specifying the size.
201      */
202     public int getSize()
203     {
204         return size;
205     }
206 
207     /***
208      * Is this column a primary key?
209      *
210      * @return True if column is a primary key.
211      */
212     public boolean isPrimaryKey()
213     {
214         return pk;
215     }
216 
217     /***
218      * Is null value allowed ?
219      *
220      * @return True if column may be null.
221      */
222     public boolean isNotNull()
223     {
224         return (notNull || isPrimaryKey());
225     }
226 
227     /***
228      * Is this column a foreign key?
229      *
230      * @return True if column is a foreign key.
231      */
232     public boolean isForeignKey()
233     {
234         return (relatedTableName != null && relatedTableName.length() > 0);
235     }
236 
237     /***
238      * Get the table.column that this column is related to.
239      *
240      * @return A String with the full name for the related column.
241      */
242     public String getRelatedName()
243     {
244         return relatedTableName + "." + relatedColumnName;
245     }
246 
247     /***
248      * Get the table name that this column is related to.
249      *
250      * @return A String with the name for the related table.
251      */
252     public String getRelatedTableName()
253     {
254         return relatedTableName;
255     }
256 
257     /***
258      * Get the column name that this column is related to.
259      *
260      * @return A String with the name for the related column.
261      */
262     public String getRelatedColumnName()
263     {
264         return relatedColumnName;
265     }
266 
267     /***
268      * Gets the scale set for this column (if any) as set in the XML database
269      * definition.  E.g., the value of the scale attribute or the scale portion
270      * of a size="P,S" attribute. 
271      * (Note: size="P,S" format is being deprecated!).
272      *
273      * @return Returns the scale.
274      */
275     public int getScale() 
276     {
277         return scale;
278     }
279 
280     /***
281      * @param scale The scale to set.
282      */
283     public void setScale(int scale) 
284     {
285         this.scale = scale;
286     }
287 }