View Javadoc

1   package org.apache.torque.map;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  
28  import org.apache.torque.Column;
29  
30  /**
31   * ColumnMap is used to model a column of a table in a database.
32   * <p>
33   * Note that this information should be set via the <Table>MapBuilder class and
34   * not changed by applications. The set methods are only public because this
35   * class needs them.
36   *
37   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
38   * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
39   * @version $Id: ColumnMap.java 1374908 2012-08-20 03:35:49Z tfischer $
40   */
41  public class ColumnMap implements Column, Serializable
42  {
43      /**
44       * Serial version.
45       */
46      private static final long serialVersionUID = 1L;
47  
48      /** A sample object having the same java Type as the column. */
49      private Object type = null;
50  
51      /** The name of the Torque Type of the column. */
52      private String torqueType = null;
53  
54      /** Should object type be converted to primitive. */
55      private boolean usePrimitive = true;
56  
57      /** Size of the column. */
58      private int size = 0;
59  
60      /** Scale of the column */
61      private int scale = 0;
62  
63      /** Is it a primary key? */
64      private boolean pk = false;
65  
66      /** Is null value allowed ? */
67      private boolean notNull = false;
68  
69      /** The TableMap for this column. */
70      private final TableMap table;
71  
72      /** The name of the column. */
73      private final String columnName;
74  
75      /**
76       * The Java Name of this column as defined in XML or created by the
77       * generator code.
78       */
79      private String javaName;
80  
81      /** Is this column an autoincrement column ? */
82      private boolean autoIncrement = false;
83  
84      /** Column description info (if any). */
85      private String description = "";
86  
87      /** is Column protected ? */
88      private boolean isProtected = false;
89  
90      /**
91       * String representing the default value defined for field. Note that
92       * default is a keyword, so defaultValue is used to store the value for the
93       * get/setDefault() methods.
94       */
95      private String defaultValue = null;
96  
97      /** Inheritance type used. */
98      private String inheritance = "false";
99  
100     /**
101      * Does column uses Inheritance subclasses? Note that this is tied to the
102      * TableMap useInheritance thru the set function.
103      */
104     private boolean useInheritance;
105 
106     /** Associated of inheritance maps. */
107     private final Map<String, InheritanceMap> inheritanceMaps
108             = Collections.synchronizedMap(
109                    new LinkedHashMap<String, InheritanceMap>());
110 
111     /** Java naming method the generator used. */
112     private String javaNamingMethod;
113 
114     /** Java type string specified in XML. */
115     private String javaType;
116 
117     /** Column position in the table (one based). */
118     private int position = -1;
119 
120     /** Associated of inheritance maps. */
121     private final Map<String, String> optionsMap = Collections
122             .synchronizedMap(new LinkedHashMap<String, String>());
123 
124     /**
125      * Constructor.
126      *
127      * @param name The name of the column.
128      * @param containingTable TableMap of the table this column is in.
129      */
130     public ColumnMap(String name, TableMap containingTable)
131     {
132         table = containingTable;
133         this.columnName = normalizeName(name);
134     }
135 
136     /**
137      * Makes sure that the column names don't include table prefixes. E.g.,
138      * SCARAB_PROJECT.PROJECT_ID should be PROJECT_ID.
139      *
140      * @param name The name to check
141      * @return The corrected name if needed or the same name if not.
142      */
143     protected String normalizeName(String name)
144     {
145         if (name.indexOf('.') > 0)
146         {
147             return name.substring(name.lastIndexOf('.') + 1);
148         }
149         return name;
150     }
151 
152     /**
153      * Get the name of a column.
154      *
155      * @return A String with the column name.
156      */
157     public String getColumnName()
158     {
159         return columnName;
160     }
161 
162     /**
163      * Get the table name + column name.
164      *
165      * @return A String with the full column name.
166      *
167      * @deprecated use getSqlExpression() instead.
168      */
169     @Deprecated
170     public String getFullyQualifiedName()
171     {
172         return table.getName() + "." + columnName;
173     }
174 
175     /**
176      * Get the name of the table this column is in.
177      *
178      * @return A String with the table name.
179      */
180     public String getTableName()
181     {
182         return table.getName();
183     }
184 
185     /**
186      * Get the name of the table this column is in.
187      *
188      * @return A String with the table name.
189      */
190     public String getFullTableName()
191     {
192         String schema = table.getSchemaName();
193         if (schema != null)
194         {
195             return schema + '.' + table.getName();
196         }
197         return table.getName();
198     }
199 
200     /**
201      * Get the name of the schema of the table this column is in.
202      *
203      * @return A String with the schema name, or null if no schema is given.
204      */
205     public String getSchemaName()
206     {
207         return table.getSchemaName();
208     }
209 
210     /**
211      * Set the type of this column.
212      *
213      * @param type An Object specifying the type.
214      */
215     public void setType(Object type)
216     {
217         this.type = type;
218     }
219 
220     /**
221      * Set the Torque type of this column.
222      *
223      * @param torqueType the Torque type of the column.
224      */
225     public void setTorqueType(String torqueType)
226     {
227         this.torqueType = torqueType;
228     }
229 
230     /**
231      * Set the size of this column.
232      *
233      * @param size An int specifying the size.
234      */
235     public void setSize(int size)
236     {
237         this.size = size;
238     }
239 
240     /**
241      * Set if this column is a primary key or not.
242      *
243      * @param pk True if column is a primary key.
244      */
245     public void setPrimaryKey(boolean pk)
246     {
247         this.pk = pk;
248     }
249 
250     /**
251      * Set if this column may be null.
252      *
253      * @param nn True if column may be null.
254      */
255     public void setNotNull(boolean nn)
256     {
257         this.notNull = nn;
258     }
259 
260 
261     /**
262      * Get the type of this column. Note that if usePrimitive is true, this may
263      * need to be converted.
264      *
265      * @return An Object specifying the type.
266      */
267     public Object getType()
268     {
269         return type;
270     }
271 
272     /**
273      * Get the name of the Torque type of this column.
274      *
275      * @return The name of the Torque type of this column.
276      */
277     public String getTorqueType()
278     {
279         return torqueType;
280     }
281 
282     /**
283      * The "precision" value from the XML
284      * size="&lt;precision&gt;[,&lt;scale&gt;]"
285      * attribute. Where [,&lt;scale&gt;] is optional.
286      *
287      * If the size attribute has not been set in the XML, it will return 0.
288      * <p>
289      *
290      * Note that the size="P,S" format should be replaced with size="P"
291      * scale="S".
292      *
293      * @return An int specifying the size.
294      */
295     public int getSize()
296     {
297         return size;
298     }
299 
300     /**
301      * Is this column a primary key?
302      *
303      * @return True if column is a primary key.
304      */
305     public boolean isPrimaryKey()
306     {
307         return pk;
308     }
309 
310     /**
311      * Is null value allowed ?
312      *
313      * @return True if column may be null.
314      */
315     public boolean isNotNull()
316     {
317         return (notNull || isPrimaryKey());
318     }
319 
320     /**
321      * Gets the scale set for this column (if any) as set in the XML database
322      * definition. E.g., the value of the scale attribute or the scale portion
323      * of a size="P,S" attribute. (Note: size="P,S" format is being
324      * deprecated!).
325      *
326      * @return Returns the scale.
327      */
328     public int getScale()
329     {
330         return scale;
331     }
332 
333     /**
334      * @param scale The scale to set.
335      */
336     public void setScale(int scale)
337     {
338         this.scale = scale;
339     }
340 
341     /**
342      * Gets the Java Name for this column as defined in XML or created by
343      * generator code.
344      *
345      * @return the Java Name.
346      */
347     public String getJavaName()
348     {
349         return this.javaName;
350     }
351 
352     /**
353      * Sets the Java Name for this column.
354      *
355      * @param name the Java Name.
356      */
357     public void setJavaName(String name)
358     {
359         this.javaName = name;
360     }
361 
362     /**
363      * Returns whether this column is an autoincrement column.
364      *
365      * @return true if this column is an autoIncrement column, false otherwise.
366      */
367     public boolean isAutoIncrement()
368     {
369         return autoIncrement;
370     }
371 
372     /**
373      * Sets whether this column is an autoincrement column.
374      *
375      * @param autoIncrement whether this colimn is an autoincrement column.
376      */
377     public void setAutoIncrement(boolean autoIncrement)
378     {
379         this.autoIncrement = autoIncrement;
380     }
381 
382     /**
383      * A string representing the default value defined for this column.
384      *
385      * @return The default value of this column, if any.
386      */
387     public String getDefault()
388     {
389         return defaultValue;
390     }
391 
392     /**
393      * Sets the default value for this column.
394      *
395      * @param defaultValue The defaultValue to set.
396      */
397     public void setDefault(String defaultValue)
398     {
399         this.defaultValue = defaultValue;
400     }
401 
402     /**
403      * Returns the column description info.
404      *
405      * @return the description, if any.
406      */
407     public String getDescription()
408     {
409         return description;
410     }
411 
412     /**
413      * Sets the description for this column.
414      *
415      * @param description The description to set.
416      */
417     public void setDescription(String description)
418     {
419         this.description = description;
420     }
421 
422     /**
423      * Get the inheritance information associated with this column,
424      *
425      * @return Returns an array of associated inheritanceMap.
426      *         The array is in XML order.
427      */
428     public InheritanceMap[] getInheritanceMaps()
429     {
430         InheritanceMap[] iMaps = new InheritanceMap[inheritanceMaps.size()];
431         synchronized (inheritanceMaps)
432         {
433             Iterator<InheritanceMap> it = inheritanceMaps.values().iterator();
434             int i = 0;
435             while (it.hasNext())
436             {
437                 iMaps[i++] = it.next();
438             }
439         }
440         return iMaps;
441     }
442 
443     /**
444      * Add an associated inheritance mapping.
445      *
446      * @param map The inheritanceMap to associate with this column.
447      */
448     public void addInheritanceMap(InheritanceMap map)
449     {
450         setUseInheritance(true);
451         this.inheritanceMaps.put(map.getKey(), map);
452     }
453 
454     /**
455      * Gets the inheritance type used.
456      *
457      * @return the inheritance type used.
458      */
459     public String getInheritance()
460     {
461         return inheritance;
462     }
463 
464     /**
465      * Sets the inheritance type.
466      *
467      * @param inheritanceType The inheritance type to set.
468      */
469     public void setInheritance(String inheritanceType)
470     {
471         this.inheritance = inheritanceType;
472     }
473 
474     /**
475      * Returns whether getters and setters are generated with the
476      * access modifier "protected" rather than "public".
477      *
478      * @return whether the accessors should be protected rather than public.
479      */
480     public boolean isProtected()
481     {
482         return isProtected;
483     }
484 
485     /**
486      * Sets whether getters and setters should be generated with the
487      * access modifier "protected" rather than "public".
488      *
489      * @param isProtected whether getters and setters for this column
490      *        are protected.
491      */
492     public void setProtected(boolean isProtected)
493     {
494         this.isProtected = isProtected;
495     }
496 
497     /**
498      * Returns whether this column is a primary key.
499      *
500      * @return whether this column is a primary key.
501      */
502     public boolean isPk()
503     {
504         return pk;
505     }
506 
507     /**
508      * Sets whether this column is a primary key.
509      *
510      * @param pk whether this column is a primary key.
511      */
512     public void setPk(boolean pk)
513     {
514         this.pk = pk;
515     }
516 
517     /**
518      * Returns whether this column uses inheritance subclasses.
519      *
520      * @return true if inheritance subclasses are used, false otherwise.
521      */
522     public boolean isUseInheritance()
523     {
524         return useInheritance;
525     }
526 
527     /**
528      * Sets whether this column uses inheritance subclasses.
529      *
530      * @param useInheritance whether this column uses Inheritance subclasses.
531      */
532     public void setUseInheritance(boolean useInheritance)
533     {
534         this.useInheritance = useInheritance;
535     }
536 
537     /**
538      * Get the inheritance map with the specified key.
539      *
540      * @param key the key of the inheritance map.
541      * @return the inheritance map with the specified key, or null if no
542      *         inheritance map with the specified key exists in this column.
543      */
544     public InheritanceMap getInheritanceMap(String key)
545     {
546         return inheritanceMaps.get(key);
547     }
548 
549     /**
550      * Returns whether this colum uses primitive values rather than objects.
551      *
552      * @return true if this colum uses primitive values, false if it uses
553      *         objects.
554      */
555     public boolean isUsePrimitive()
556     {
557         return usePrimitive;
558     }
559 
560     /**
561      * Sets whether this colum uses primitive values rather than objects.
562      *
563      * @param usePrimitive whether primitive objects are used
564      *        rather than objects.
565      */
566     public void setUsePrimitive(boolean usePrimitive)
567     {
568         this.usePrimitive = usePrimitive;
569     }
570 
571     /**
572      * Returns the Java naming method for this column.
573      *
574      * @return the javaNamingMethod for this column.
575      */
576     public String getJavaNamingMethod()
577     {
578         return javaNamingMethod;
579     }
580 
581     /**
582      * Sets the java naming method for this column.
583      *
584      * @param javaNamingMethod The javaNamingMethod to set.
585      */
586     public void setJavaNamingMethod(String javaNamingMethod)
587     {
588         this.javaNamingMethod = javaNamingMethod;
589     }
590 
591     /**
592      * Returns the map for the table this column belongs to.
593      *
594      * @return the table map for this column.
595      */
596     public TableMap getTable()
597     {
598         return table;
599     }
600 
601     /**
602      * Returns the position (one based) of this column in the table.
603      * XML order is preserved.
604      *
605      * @return The position of this column, one-based.
606      */
607     public int getPosition()
608     {
609         return position;
610     }
611 
612     /**
613      * Sets the position (one based) of this column in the table.
614      *
615      * @param position The position to set.
616      */
617     public void setPosition(int position)
618     {
619         this.position = position;
620     }
621 
622     /**
623      * Returns the java type of this column.
624      *
625      * @return the javaType.
626      */
627     public String getJavaType()
628     {
629         return javaType;
630     }
631 
632     /**
633      * Sets the java type of this column.
634      *
635      * @param javaType The javaType to set.
636      */
637     public void setJavaType(String javaType)
638     {
639         this.javaType = javaType;
640     }
641 
642     /**
643      * Returns an unmodifiable map of all options.
644      *
645      * @return A map containing all options, not null.
646      */
647     public Map<String, String> getOptions()
648     {
649         return Collections.unmodifiableMap(optionsMap);
650     }
651 
652     /**
653      * Sets an option.
654      *
655      * @param key the key of the option
656      * @param value the value of the option.
657      */
658     public void setOption(String key, String value)
659     {
660         optionsMap.put(key, value);
661     }
662 
663     /**
664      * Returns the value of an option.
665      *
666      * @param key the key of the option.
667      *
668      * @return the value of the option, or null if not set.
669      */
670     public String getOption(String key)
671     {
672         return optionsMap.get(key);
673     }
674 
675     /**
676      * Returns the SQL expression for the fully qualified column name.
677      * This is tableName.columnName if the database for this column has no
678      * schema and defaultSchema is null,
679      * or schemaName.tableName.columnName with a schema
680      * (the database schema overrides the default schema if both are given).
681      *
682      * @return the SQL expression for the column, not null.
683      */
684     public String getSqlExpression()
685     {
686         return table.getName() + '.' + columnName;
687     }
688 
689     @Override
690     public String toString()
691     {
692         return getSqlExpression();
693     }
694 }