KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > model > Column


1 package org.apache.torque.engine.database.model;
2
3 /*
4  * Copyright 2001-2004 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 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.torque.engine.EngineException;
27 import org.apache.torque.engine.platform.Platform;
28 import org.apache.torque.engine.platform.PlatformDefaultImpl;
29 import org.xml.sax.Attributes JavaDoc;
30
31 /**
32  * A Class for holding data about a column used in an Application.
33  *
34  * @author <a HREF="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
35  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
36  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
37  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
38  * @author <a HREF="mailto:byron_foster@byron_foster@yahoo.com>Byron Foster</a>
39  * @author <a HREF="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
40  * @version $Id: Column.java,v 1.29 2005/06/27 21:34:10 tfischer Exp $
41  */

42 public class Column
43 {
44     private static final SchemaType DEFAULT_TYPE = SchemaType.VARCHAR;
45     /** Logging class from commons.logging */
46     private static Log log = LogFactory.getLog(Column.class);
47     private String JavaDoc name;
48     private String JavaDoc description;
49     private Domain domain = new Domain();
50     private String JavaDoc javaName = null;
51     private String JavaDoc javaNamingMethod;
52     private boolean isNotNull = false;
53     private boolean isProtected = false;
54     private String JavaDoc javaType;
55     private Table parentTable;
56     private int position;
57     private boolean isPrimaryKey = false;
58     private boolean isUnique = false;
59     private boolean isAutoIncrement = false;
60     private List JavaDoc referrers;
61     // only one type is supported currently, which assumes the
62
// column either contains the classnames or a key to
63
// classnames specified in the schema. Others may be
64
// supported later.
65
private String JavaDoc inheritanceType;
66     private boolean isInheritance;
67     private boolean isEnumeratedClasses;
68     private List JavaDoc inheritanceList;
69     private boolean needsTransactionInPostgres;
70
71     /** generate is... setters for boolean columns if true */
72     private boolean correctGetters = false;
73
74     /** class name to do input validation on this column */
75     private String JavaDoc inputValidator = null;
76
77     /**
78      * Creates a new instance with a <code>null</code> name.
79      */

80     public Column()
81     {
82         this(null);
83     }
84
85     /**
86      * Creates a new column and set the name
87      *
88      * @param name column name
89      */

90     public Column(String JavaDoc name)
91     {
92         this.name = name;
93     }
94
95     /**
96      * Return a comma delimited string listing the specified columns.
97      *
98      * @param columns Either a list of <code>Column</code> objects, or
99      * a list of <code>String</code> objects with column names.
100      */

101     public static String JavaDoc makeList(List JavaDoc columns)
102     {
103         Object JavaDoc obj = columns.get(0);
104         boolean isColumnList = (obj instanceof Column);
105         if (isColumnList)
106         {
107             obj = ((Column) obj).getName();
108         }
109         StringBuffer JavaDoc buf = new StringBuffer JavaDoc((String JavaDoc) obj);
110         for (int i = 1; i < columns.size(); i++)
111         {
112             obj = columns.get(i);
113             if (isColumnList)
114             {
115                 obj = ((Column) obj).getName();
116             }
117             buf.append(", ").append(obj);
118         }
119         return buf.toString();
120     }
121
122     /**
123      * Imports a column from an XML specification
124      */

125     public void loadFromXML(Attributes JavaDoc attrib)
126     {
127         String JavaDoc dom = attrib.getValue("domain");
128         if (StringUtils.isNotEmpty(dom))
129         {
130             domain = new Domain(getTable().getDatabase().getDomain(dom));
131         }
132         else
133         {
134             domain = new Domain(getPlatform().getDomainForSchemaType(DEFAULT_TYPE));
135             setType(attrib.getValue("type"));
136         }
137         //Name
138
name = attrib.getValue("name");
139
140         javaName = attrib.getValue("javaName");
141         javaType = attrib.getValue("javaType");
142         if (javaType != null && javaType.length() == 0)
143         {
144             javaType = null;
145         }
146
147         // retrieves the method for converting from specified name to
148
// a java name.
149
javaNamingMethod = attrib.getValue("javaNamingMethod");
150         if (javaNamingMethod == null)
151         {
152             javaNamingMethod
153                     = parentTable.getDatabase().getDefaultJavaNamingMethod();
154         }
155
156         //Primary Key
157
String JavaDoc primaryKey = attrib.getValue("primaryKey");
158         //Avoid NullPointerExceptions on string comparisons.
159
isPrimaryKey = ("true".equals(primaryKey));
160
161         // If this column is a primary key then it can't be null.
162
if ("true".equals(primaryKey))
163         {
164             isNotNull = true;
165         }
166
167         // HELP: Should primary key, index, and/or idMethod="native"
168
// affect isNotNull? If not, please document why here.
169
String JavaDoc notNull = attrib.getValue("required");
170         isNotNull = (notNull != null && "true".equals(notNull));
171
172         //AutoIncrement/Sequences
173
String JavaDoc autoIncrement = attrib.getValue("autoIncrement");
174         // autoincrement is false per default,
175
// except if the column is a primary key
176
// and the idMethod is native
177
// and the platform's default id Method is identity
178
// and autoIncrement is not excplicitly set to false
179
isAutoIncrement = ("true".equals(autoIncrement)
180                 || (isPrimaryKey()
181                     && IDMethod.NATIVE.equals(getTable().getIdMethod())
182                     && Platform.IDENTITY.equals(
183                             getPlatform().getNativeIdMethod())
184                     && (!"false".equals(autoIncrement))));
185         //Default column value.
186
domain.replaceDefaultValue(attrib.getValue("default"));
187
188         domain.replaceSize(attrib.getValue("size"));
189         domain.replaceScale(attrib.getValue("scale"));
190
191         inheritanceType = attrib.getValue("inheritance");
192         isInheritance = (inheritanceType != null
193                 && !inheritanceType.equals("false"));
194
195         this.inputValidator = attrib.getValue("inputValidator");
196         description = attrib.getValue("description");
197         
198         isProtected = ("true".equals(attrib.getValue("protected")));
199     }
200
201     /**
202      * Returns table.column
203      */

204     public String JavaDoc getFullyQualifiedName()
205     {
206         return (parentTable.getName() + '.' + name);
207     }
208
209     /**
210      * Get the name of the column
211      */

212     public String JavaDoc getName()
213     {
214         return name;
215     }
216
217     /**
218      * Set the name of the column
219      */

220     public void setName(String JavaDoc newName)
221     {
222         name = newName;
223     }
224
225     /**
226      * Get the description for the Table
227      */

228     public String JavaDoc getDescription()
229     {
230         return description;
231     }
232
233     /**
234      * Set the description for the Table
235      *
236      * @param newDescription description for the Table
237      */

238     public void setDescription(String JavaDoc newDescription)
239     {
240         description = newDescription;
241     }
242
243     /**
244      * Get name to use in Java sources to build method names.
245      *
246      * @return the capitalised javaName
247      */

248     public String JavaDoc getJavaName()
249     {
250         if (javaName == null)
251         {
252             List JavaDoc inputs = new ArrayList JavaDoc(2);
253             inputs.add(name);
254             inputs.add(javaNamingMethod);
255             try
256             {
257                 javaName = NameFactory.generateName(NameFactory.JAVA_GENERATOR,
258                                                     inputs);
259             }
260             catch (EngineException e)
261             {
262                 log.error(e, e);
263             }
264         }
265         return StringUtils.capitalize(javaName);
266     }
267     
268     /**
269      * Returns the name for the getter method to retrieve the value of this
270      * column
271      *
272      * @return A getter method name for this column.
273      * @since 3.2
274      */

275     public String JavaDoc getGetterName()
276     {
277         if (("boolean".equalsIgnoreCase(getJavaNative()) && isCorrectGetters()))
278         {
279             return "is" + StringUtils.capitalize(getJavaName());
280         }
281         else
282         {
283             return "get" + StringUtils.capitalize(getJavaName());
284         }
285     }
286
287     /**
288      * Returns the name for the setter method to set the value of this
289      * column
290      *
291      * @return A setter method name for this column.
292      * @since 3.2
293      */

294     public String JavaDoc getSetterName()
295     {
296         return "set" + StringUtils.capitalize(getJavaName());
297     }
298
299     /**
300      * Get variable name to use in Java sources (= uncapitalised java name)
301      */

302     public String JavaDoc getUncapitalisedJavaName()
303     {
304         return StringUtils.uncapitalize(getJavaName());
305     }
306
307     /**
308      * Set name to use in Java sources
309      */

310     public void setJavaName(String JavaDoc javaName)
311     {
312         this.javaName = javaName;
313     }
314
315     /**
316      * Get type to use in Java sources
317      */

318     public String JavaDoc getJavaType()
319     {
320         return javaType;
321     }
322
323     /**
324      * Get the location of this column within the table (one-based).
325      * @return value of position.
326      */

327     public int getPosition()
328     {
329         return position;
330     }
331
332     /**
333      * Get the location of this column within the table (one-based).
334      * @param v Value to assign to position.
335      */

336     public void setPosition(int v)
337     {
338         this.position = v;
339     }
340
341     /**
342      * Set the parent Table of the column
343      */

344     public void setTable(Table parent)
345     {
346         parentTable = parent;
347     }
348
349     /**
350      * Get the parent Table of the column
351      */

352     public Table getTable()
353     {
354         return parentTable;
355     }
356
357     /**
358      * Returns the Name of the table the column is in
359      */

360     public String JavaDoc getTableName()
361     {
362         return parentTable.getName();
363     }
364
365     /**
366      * A utility function to create a new column
367      * from attrib and add it to this table.
368      */

369     public Inheritance addInheritance(Attributes JavaDoc attrib)
370     {
371         Inheritance inh = new Inheritance();
372         inh.loadFromXML (attrib);
373         addInheritance(inh);
374
375         return inh;
376     }
377
378     /**
379      * Adds a new inheritance definition to the inheritance list and set the
380      * parent column of the inheritance to the current column
381      */

382     public void addInheritance(Inheritance inh)
383     {
384         inh.setColumn(this);
385         if (inheritanceList == null)
386         {
387             inheritanceList = new ArrayList JavaDoc();
388             isEnumeratedClasses = true;
389         }
390         inheritanceList.add(inh);
391     }
392
393     /**
394      * Get the inheritance definitions.
395      */

396     public List JavaDoc getChildren()
397     {
398         return inheritanceList;
399     }
400
401     /**
402      * Determine if this column is a normal property or specifies a
403      * the classes that are represented in the table containing this column.
404      */

405     public boolean isInheritance()
406     {
407         return isInheritance;
408     }
409
410     /**
411      * Determine if possible classes have been enumerated in the xml file.
412      */

413     public boolean isEnumeratedClasses()
414     {
415         return isEnumeratedClasses;
416     }
417
418     /**
419      * Return the isNotNull property of the column
420      */

421     public boolean isNotNull()
422     {
423         return isNotNull;
424     }
425
426     /**
427      * Set the isNotNull property of the column
428      */

429     public void setNotNull(boolean status)
430     {
431         isNotNull = status;
432     }
433     
434     /**
435      * Return NOT NULL String for this column
436      *
437      * @return "NOT NULL" if null values are not allowed or an empty String.
438      */

439     public String JavaDoc getNotNullString()
440     {
441         return getTable().getDatabase().getPlatform()
442                 .getNullString(this.isNotNull());
443     }
444
445     /**
446      * Return the isProtected property of the column
447      */

448     public boolean isProtected()
449     {
450         return isProtected;
451     }
452     
453     /**
454      * Set the isProtected property of the Column
455      */

456     public void setProtected(boolean prot)
457     {
458         isProtected = prot;
459     }
460     
461     /**
462      * Set if the column is a primary key or not
463      */

464     public void setPrimaryKey(boolean pk)
465     {
466         isPrimaryKey = pk;
467     }
468
469     /**
470      * Return true if the column is a primary key
471      */

472     public boolean isPrimaryKey()
473     {
474         return isPrimaryKey;
475     }
476
477     /**
478      * Set true if the column is UNIQUE
479      */

480     public void setUnique (boolean u)
481     {
482         isUnique = u;
483     }
484
485     /**
486      * Get the UNIQUE property
487      */

488     public boolean isUnique()
489     {
490         return isUnique;
491     }
492
493     /**
494      * Return true if the column requires a transaction in Postgres
495      */

496     public boolean requiresTransactionInPostgres()
497     {
498         return needsTransactionInPostgres;
499     }
500
501     /**
502      * Utility method to determine if this column is a foreign key.
503      */

504     public boolean isForeignKey()
505     {
506         return (getForeignKey() != null);
507     }
508
509     /**
510      * Determine if this column is a foreign key that refers to the
511      * same table as another foreign key column in this table.
512      */

513     public boolean isMultipleFK()
514     {
515         ForeignKey fk = getForeignKey();
516         if (fk != null)
517         {
518             Iterator JavaDoc fks = parentTable.getForeignKeys().iterator();
519             while (fks.hasNext())
520             {
521                 ForeignKey key = (ForeignKey) fks.next();
522                 if (key.getForeignTableName().equals(fk.getForeignTableName())
523                         && !key.getLocalColumns().contains(this.name))
524                 {
525                     return true;
526                 }
527             }
528         }
529
530         // No multiple foreign keys.
531
return false;
532     }
533
534     /**
535      * get the foreign key object for this column
536      * if it is a foreign key or part of a foreign key
537      */

538     public ForeignKey getForeignKey()
539     {
540         return parentTable.getForeignKey(this.name);
541     }
542
543     /**
544      * Utility method to get the related table of this column if it is a foreign
545      * key or part of a foreign key
546      */

547     public String JavaDoc getRelatedTableName()
548     {
549         ForeignKey fk = getForeignKey();
550         return (fk == null ? null : fk.getForeignTableName());
551     }
552
553     /**
554      * Utility method to get the related column of this local column if this
555      * column is a foreign key or part of a foreign key.
556      */

557     public String JavaDoc getRelatedColumnName()
558     {
559         ForeignKey fk = getForeignKey();
560         if (fk == null)
561         {
562             return null;
563         }
564         else
565         {
566             return fk.getLocalForeignMapping().get(this.name).toString();
567         }
568     }
569
570     /**
571      * Adds the foreign key from another table that refers to this column.
572      */

573     public void addReferrer(ForeignKey fk)
574     {
575         if (referrers == null)
576         {
577             referrers = new ArrayList JavaDoc(5);
578         }
579         referrers.add(fk);
580     }
581
582     /**
583      * Get list of references to this column.
584      */

585     public List JavaDoc getReferrers()
586     {
587         if (referrers == null)
588         {
589             referrers = new ArrayList JavaDoc(5);
590         }
591         return referrers;
592     }
593
594     /**
595      * Sets the colunm type
596      */

597     public void setType(String JavaDoc torqueType)
598     {
599         SchemaType type = SchemaType.getEnum(torqueType);
600         if (type == null)
601         {
602             log.warn("SchemaType " + torqueType + " does not exist");
603             type = Column.DEFAULT_TYPE;
604         }
605         setType(type);
606     }
607
608     /**
609      * Sets the colunm type
610      */

611     public void setType(SchemaType torqueType)
612     {
613         domain = new Domain(getPlatform().getDomainForSchemaType(torqueType));
614         if (torqueType.equals(SchemaType.VARBINARY)
615                 || torqueType.equals(SchemaType.BLOB))
616         {
617             needsTransactionInPostgres = true;
618         }
619     }
620     
621     /**
622      * Returns the column jdbc type as an object
623      *
624      * @deprecated the type conversion is handled by the platform package
625      * (since torque 3.2)
626      */

627     public Object JavaDoc getType()
628     {
629         return TypeMap.getJdbcType(domain.getType()).getName();
630     }
631
632     /**
633      * Returns the column type as given in the schema as an object
634      */

635     public Object JavaDoc getTorqueType()
636     {
637         return domain.getType().getName();
638     }
639
640     /**
641      * Utility method to see if the column is a string
642      *
643      * @deprecated will be removed after the 3.2 release
644      */

645     public boolean isString()
646     {
647         return (domain.getType().getName().indexOf ("CHAR") != -1);
648     }
649
650     /**
651      * Utility method to return the value as an element to be usable
652      * in an SQL insert statement. This is used from the SQL loader task
653      */

654     public boolean needEscapedValue()
655     {
656         String JavaDoc torqueType = domain.getType().getName();
657         return (torqueType != null) && (torqueType.equals("VARCHAR")
658                         || torqueType.equals("LONGVARCHAR")
659                         || torqueType.equals("DATE")
660                         || torqueType.equals("DATETIME")
661                         || torqueType.equals("TIMESTAMP")
662                         || torqueType.equals("TIME")
663                         || torqueType.equals("CHAR")
664                         || torqueType.equals("CLOB"));
665     }
666
667     /**
668      * String representation of the column. This is an xml representation.
669      *
670      * @return string representation in xml
671      */

672     public String JavaDoc toString()
673     {
674         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
675         result.append(" <column name=\"").append(name).append('"');
676
677         if (javaName != null)
678         {
679             result.append(" javaName=\"").append(javaName).append('"');
680         }
681
682         if (isPrimaryKey)
683         {
684             result.append(" primaryKey=\"").append(isPrimaryKey).append('"');
685         }
686
687         if (isNotNull)
688         {
689             result.append(" required=\"true\"");
690         }
691         else
692         {
693             result.append(" required=\"false\"");
694         }
695
696         result.append(" type=\"").append(domain.getType().getName()).append('"');
697
698         if (domain.getSize() != null)
699         {
700             result.append(" size=\"").append(domain.getSize()).append('"');
701         }
702
703         if (domain.getScale() != null)
704         {
705             result.append(" scale=\"").append(domain.getScale()).append('"');
706         }
707
708         if (domain.getDefaultValue() != null)
709         {
710             result.append(" default=\"").append(domain.getDefaultValue()).append('"');
711         }
712
713         if (isInheritance())
714         {
715             result.append(" inheritance=\"").append(inheritanceType)
716                 .append('"');
717         }
718
719         // Close the column.
720
result.append(" />\n");
721
722         return result.toString();
723     }
724
725     /**
726      * Returns the size of the column
727      */

728     public String JavaDoc getSize()
729     {
730         return domain.getSize();
731     }
732
733     /**
734      * Set the size of the column
735      */

736     public void setSize(String JavaDoc newSize)
737     {
738         domain.setSize(newSize);
739     }
740
741     /**
742      * Returns the scale of the column
743      */

744     public String JavaDoc getScale()
745     {
746         return domain.getScale();
747     }
748
749     /**
750      * Set the scale of the column
751      */

752     public void setScale(String JavaDoc newScale)
753     {
754         domain.setScale(newScale);
755     }
756
757     /**
758      * Return the size and scale in brackets for use in an sql schema.
759      *
760      * @return size and scale or an empty String if there are no values
761      * available.
762      */

763     public String JavaDoc printSize()
764     {
765         return domain.printSize();
766     }
767
768     /**
769      * Return a string that will give this column a default value.
770      * @deprecated
771      */

772      public String JavaDoc getDefaultSetting()
773      {
774          return domain.getDefaultSetting();
775      }
776
777     /**
778      * Set a string that will give this column a default value.
779      */

780     public void setDefaultValue(String JavaDoc def)
781     {
782         domain.setDefaultValue(def);
783     }
784
785     /**
786      * Get a string that will give this column a default value.
787      */

788     public String JavaDoc getDefaultValue()
789     {
790         return domain.getDefaultValue();
791     }
792
793     /**
794      * Returns the class name to do input validation
795      */

796     public String JavaDoc getInputValidator()
797     {
798        return this.inputValidator;
799     }
800
801     /**
802      * Return auto increment/sequence string for the target database. We need to
803      * pass in the props for the target database!
804      */

805     public boolean isAutoIncrement()
806     {
807         return isAutoIncrement;
808     }
809
810     /**
811      * Set the auto increment value.
812      * Use isAutoIncrement() to find out if it is set or not.
813      */

814     public void setAutoIncrement(boolean value)
815     {
816         isAutoIncrement = value;
817     }
818
819     public String JavaDoc getAutoIncrementString()
820     {
821         if (isAutoIncrement()
822                 && IDMethod.NATIVE.equals(getTable().getIdMethod()))
823         {
824             return getPlatform().getAutoIncrement();
825         }
826         return "";
827     }
828     
829     /**
830      * Set the column type from a string property
831      * (normally a string from an sql input file)
832      */

833     public void setTypeFromString(String JavaDoc typeName, String JavaDoc size)
834     {
835         String JavaDoc tn = typeName.toUpperCase();
836         setType(tn);
837
838         if (size != null)
839         {
840             domain.setSize(size);
841         }
842
843         if (tn.indexOf("CHAR") != -1)
844         {
845             domain.setType(SchemaType.VARCHAR);
846         }
847         else if (tn.indexOf("INT") != -1)
848         {
849             domain.setType(SchemaType.INTEGER);
850         }
851         else if (tn.indexOf("FLOAT") != -1)
852         {
853             domain.setType(SchemaType.FLOAT);
854         }
855         else if (tn.indexOf("DATE") != -1)
856         {
857             domain.setType(SchemaType.DATE);
858         }
859         else if (tn.indexOf("TIME") != -1)
860         {
861             domain.setType(SchemaType.TIMESTAMP);
862         }
863         else if (tn.indexOf("BINARY") != -1)
864         {
865             domain.setType(SchemaType.LONGVARBINARY);
866         }
867         else
868         {
869             domain.setType(SchemaType.VARCHAR);
870         }
871     }
872
873     /**
874      * Return a string representation of the
875      * Java object which corresponds to the JDBC
876      * type of this column. Use in the generation
877      * of MapBuilders.
878      */

879     public String JavaDoc getJavaObject()
880     {
881         return TypeMap.getJavaObject(domain.getType());
882     }
883
884     /**
885      * Return a string representation of the primitive java type which
886      * corresponds to the JDBC type of this column.
887      *
888      * @return string representation of the primitive java type
889      */

890     public String JavaDoc getJavaPrimitive()
891     {
892         return TypeMap.getJavaNative(domain.getType());
893     }
894
895     /**
896      * Return a string representation of the native java type which corresponds
897      * to the JDBC type of this column. Use in the generation of Base objects.
898      * This method is used by torque, so it returns Key types for primaryKey and
899      * foreignKey columns
900      *
901      * @return java datatype used by torque
902      */

903     public String JavaDoc getJavaNative()
904     {
905         String JavaDoc jtype = TypeMap.getJavaNativeObject(domain.getType());
906         if (isUsePrimitive())
907         {
908             jtype = TypeMap.getJavaNative(domain.getType());
909         }
910
911         return jtype;
912     }
913
914     /**
915      * Return Village asX() method which corresponds to the JDBC type
916      * which represents this column.
917      */

918     public String JavaDoc getVillageMethod()
919     {
920         String JavaDoc vmethod = TypeMap.getVillageObjectMethod(domain.getType());
921         if (isUsePrimitive())
922         {
923             vmethod = TypeMap.getVillageMethod(domain.getType());
924         }
925
926         return vmethod;
927     }
928
929     /**
930      * Return ParameterParser getX() method which
931      * corresponds to the JDBC type which represents this column.
932      */

933     public String JavaDoc getParameterParserMethod()
934     {
935         return TypeMap.getPPMethod(domain.getType());
936     }
937
938     /**
939      * Returns true if the column type is boolean in the
940      * java object and a numeric (1 or 0) in the db.
941      */

942     public boolean isBooleanInt()
943     {
944         return TypeMap.isBooleanInt(domain.getType());
945     }
946
947     /**
948      * Returns true if the column type is boolean in the
949      * java object and a String ("Y" or "N") in the db.
950      */

951     public boolean isBooleanChar()
952     {
953         return TypeMap.isBooleanChar(domain.getType());
954     }
955
956     /**
957      * Returns true if the column type is boolean in the
958      * java object and a Bit ("1" or "0") in the db.
959      */

960     public boolean isBit()
961     {
962         return TypeMap.isBit(domain.getType());
963     }
964
965     /**
966      * returns true, if the columns java native type is an
967      * boolean, byte, short, int, long, float, double, char
968      */

969     public boolean isPrimitive()
970     {
971         String JavaDoc t = getJavaNative();
972         return "boolean".equals(t)
973             || "byte".equals(t)
974             || "short".equals(t)
975             || "int".equals(t)
976             || "long".equals(t)
977             || "float".equals(t)
978             || "double".equals(t)
979             || "char".equals(t);
980     }
981
982     public boolean isUsePrimitive()
983     {
984         String JavaDoc s = getJavaType();
985         return (s != null && s.equals("primitive"))
986             || (s == null && !"object".equals(
987                getTable().getDatabase().getDefaultJavaType()));
988     }
989
990     /**
991      * @return Returns the domain.
992      */

993     public Domain getDomain()
994     {
995         return domain;
996     }
997
998     /**
999      * @param domain The domain to set.
1000     */

1001    public void setDomain(Domain domain)
1002    {
1003        this.domain = domain;
1004    }
1005
1006    private Platform getPlatform()
1007    {
1008        try
1009        {
1010            return getTable().getDatabase().getPlatform();
1011        }
1012        catch (Exception JavaDoc ex)
1013        {
1014            log.warn("could not load platform implementation");
1015        }
1016        return new PlatformDefaultImpl();
1017    }
1018    
1019    public String JavaDoc getSqlString()
1020    {
1021        StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1022        sb.append(getName());
1023        sb.append(' ');
1024        sb.append(getDomain().getSqlType());
1025        if (getPlatform().hasSize(getDomain().getSqlType()))
1026        {
1027            sb.append(getDomain().printSize());
1028            sb.append(' ');
1029        }
1030        if (getDomain().getDefaultValue() != null)
1031        {
1032            sb.append("default ");
1033            if (TypeMap.isTextType(getDomain().getType()))
1034            {
1035                // TODO: Properly SQL-escape the text.
1036
sb.append('\'').append(getDefaultValue()).append('\'');
1037            }
1038            else
1039            {
1040                sb.append(getDefaultValue());
1041            }
1042            sb.append(' ');
1043        }
1044        sb.append(getNotNullString());
1045        sb.append(' ');
1046        sb.append(getAutoIncrementString());
1047        return sb.toString();
1048    }
1049    
1050    /**
1051     * Return the correctGetters property of the column
1052     *
1053     * @return The currentValue of the correctGetters property.
1054     * @since 3.2
1055     */

1056    public boolean isCorrectGetters()
1057    {
1058        return correctGetters;
1059    }
1060
1061    /**
1062     * Set the correctGetters property of the column. If set to true, the
1063     * column returns is&lt;xxx&gt; as the getter name which is correct for the
1064     * Bean Specs but incompatible to pre-3.2 releases.
1065     *
1066     * @param correctGetters The new value of the correctGetters property.
1067     * @since 3.2
1068     */

1069    public void setCorrectGetters(boolean correctGetters)
1070    {
1071        this.correctGetters = correctGetters;
1072    }
1073}
1074
Popular Tags