Coverage report

  %line %branch
org.apache.torque.engine.database.model.Column
48% 
88% 

 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;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 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;
 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  40
     private static final SchemaType DEFAULT_TYPE = SchemaType.VARCHAR;
 45  
     /** Logging class from commons.logging */
 46  80
     private static Log log = LogFactory.getLog(Column.class);
 47  
     private String name;
 48  
     private String description;
 49  7132
     private Domain domain = new Domain();
 50  7132
     private String javaName = null;
 51  
     private String javaNamingMethod;
 52  7132
     private boolean isNotNull = false;
 53  7132
     private boolean isProtected = false;
 54  
     private String javaType;
 55  
     private Table parentTable;
 56  
     private int position;
 57  7132
     private boolean isPrimaryKey = false;
 58  7132
     private boolean isUnique = false;
 59  7132
     private boolean isAutoIncrement = false;
 60  
     private List 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 inheritanceType;
 66  
     private boolean isInheritance;
 67  
     private boolean isEnumeratedClasses;
 68  
     private List inheritanceList;
 69  
     private boolean needsTransactionInPostgres;
 70  
 
 71  
     /** generate is... setters for boolean columns if true */
 72  7132
     private boolean correctGetters = false;
 73  
 
 74  
     /** class name to do input validation on this column */
 75  7132
     private String inputValidator = null;
 76  
 
 77  
     /**
 78  
      * Creates a new instance with a <code>null</code> name.
 79  
      */
 80  
     public Column()
 81  
     {
 82  7124
         this(null);
 83  7124
     }
 84  
 
 85  
     /**
 86  
      * Creates a new column and set the name
 87  
      *
 88  
      * @param name column name
 89  
      */
 90  
     public Column(String name)
 91  7132
     {
 92  7132
         this.name = name;
 93  7132
     }
 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 makeList(List columns)
 102  
     {
 103  0
         Object obj = columns.get(0);
 104  0
         boolean isColumnList = (obj instanceof Column);
 105  0
         if (isColumnList)
 106  
         {
 107  0
             obj = ((Column) obj).getName();
 108  
         }
 109  0
         StringBuffer buf = new StringBuffer((String) obj);
 110  0
         for (int i = 1; i < columns.size(); i++)
 111  
         {
 112  0
             obj = columns.get(i);
 113  0
             if (isColumnList)
 114  
             {
 115  0
                 obj = ((Column) obj).getName();
 116  
             }
 117  0
             buf.append(", ").append(obj);
 118  
         }
 119  0
         return buf.toString();
 120  
     }
 121  
 
 122  
     /**
 123  
      * Imports a column from an XML specification
 124  
      */
 125  
     public void loadFromXML(Attributes attrib)
 126  
     {
 127  7112
         String dom = attrib.getValue("domain");
 128  7112
         if (StringUtils.isNotEmpty(dom)) 
 129  
         {
 130  304
             domain = new Domain(getTable().getDatabase().getDomain(dom));
 131  
         } 
 132  
         else
 133  
         {
 134  6808
             domain = new Domain(getPlatform().getDomainForSchemaType(DEFAULT_TYPE));
 135  6808
             setType(attrib.getValue("type"));
 136  
         }
 137  
         //Name
 138  7112
         name = attrib.getValue("name");
 139  
 
 140  7112
         javaName = attrib.getValue("javaName");
 141  7112
         javaType = attrib.getValue("javaType");
 142  7112
         if (javaType != null && javaType.length() == 0)
 143  
         {
 144  0
             javaType = null;
 145  
         }
 146  
 
 147  
         // retrieves the method for converting from specified name to
 148  
         // a java name.
 149  7112
         javaNamingMethod = attrib.getValue("javaNamingMethod");
 150  7112
         if (javaNamingMethod == null)
 151  
         {
 152  7112
             javaNamingMethod
 153  
                     = parentTable.getDatabase().getDefaultJavaNamingMethod();
 154  
         }
 155  
 
 156  
         //Primary Key
 157  7112
         String primaryKey = attrib.getValue("primaryKey");
 158  
         //Avoid NullPointerExceptions on string comparisons.
 159  7112
         isPrimaryKey = ("true".equals(primaryKey));
 160  
 
 161  
         // If this column is a primary key then it can't be null.
 162  7112
         if ("true".equals(primaryKey))
 163  
         {
 164  788
             isNotNull = true;
 165  
         }
 166  
 
 167  
         // HELP: Should primary key, index, and/or idMethod="native"
 168  
         // affect isNotNull?  If not, please document why here.
 169  7112
         String notNull = attrib.getValue("required");
 170  7112
         isNotNull = (notNull != null && "true".equals(notNull));
 171  
 
 172  
         //AutoIncrement/Sequences
 173  7112
         String 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  7112
         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  7112
         domain.replaceDefaultValue(attrib.getValue("default"));
 187  
 
 188  7112
         domain.replaceSize(attrib.getValue("size"));
 189  7112
         domain.replaceScale(attrib.getValue("scale"));
 190  
 
 191  7112
         inheritanceType = attrib.getValue("inheritance");
 192  7112
         isInheritance = (inheritanceType != null
 193  
                 && !inheritanceType.equals("false"));
 194  
 
 195  7112
         this.inputValidator = attrib.getValue("inputValidator");
 196  7112
         description = attrib.getValue("description");
 197  
         
 198  7112
         isProtected = ("true".equals(attrib.getValue("protected")));
 199  7112
     }
 200  
 
 201  
     /**
 202  
      * Returns table.column
 203  
      */
 204  
     public String getFullyQualifiedName()
 205  
     {
 206  0
         return (parentTable.getName() + '.' + name);
 207  
     }
 208  
 
 209  
     /**
 210  
      * Get the name of the column
 211  
      */
 212  
     public String getName()
 213  
     {
 214  7308
         return name;
 215  
     }
 216  
 
 217  
     /**
 218  
      * Set the name of the column
 219  
      */
 220  
     public void setName(String newName)
 221  
     {
 222  0
         name = newName;
 223  0
     }
 224  
 
 225  
     /**
 226  
      * Get the description for the Table
 227  
      */
 228  
     public String getDescription()
 229  
     {
 230  0
         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 newDescription)
 239  
     {
 240  0
         description = newDescription;
 241  0
     }
 242  
 
 243  
     /**
 244  
      * Get name to use in Java sources to build method names.
 245  
      * 
 246  
      * @return the capitalised javaName
 247  
      */
 248  
     public String getJavaName()
 249  
     {
 250  7120
         if (javaName == null)
 251  
         {
 252  7120
             List inputs = new ArrayList(2);
 253  7120
             inputs.add(name);
 254  7120
             inputs.add(javaNamingMethod);
 255  
             try
 256  
             {
 257  7120
                 javaName = NameFactory.generateName(NameFactory.JAVA_GENERATOR,
 258  
                                                     inputs);
 259  
             }
 260  0
             catch (EngineException e)
 261  
             {
 262  0
                 log.error(e, e);
 263  7120
             }
 264  
         }
 265  7120
         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 getGetterName()
 276  
     {
 277  0
         if (("boolean".equalsIgnoreCase(getJavaNative()) && isCorrectGetters()))
 278  
         {
 279  0
             return "is" + StringUtils.capitalize(getJavaName());
 280  
         }
 281  
         else
 282  
         {
 283  0
             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 getSetterName()
 295  
     {
 296  0
         return "set" + StringUtils.capitalize(getJavaName());
 297  
     }
 298  
 
 299  
     /**
 300  
      * Get variable name to use in Java sources (= uncapitalised java name)
 301  
      */    
 302  
     public String getUncapitalisedJavaName()
 303  
     {
 304  0
         return StringUtils.uncapitalize(getJavaName());
 305  
     }
 306  
 
 307  
     /**
 308  
      * Set name to use in Java sources
 309  
      */
 310  
     public void setJavaName(String javaName)
 311  
     {
 312  0
         this.javaName = javaName;
 313  0
     }
 314  
 
 315  
     /**
 316  
      * Get type to use in Java sources
 317  
      */
 318  
     public String getJavaType()
 319  
     {
 320  0
         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  0
         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  7120
         this.position = v;
 339  7120
     }
 340  
 
 341  
     /**
 342  
      * Set the parent Table of the column
 343  
      */
 344  
     public void setTable(Table parent)
 345  
     {
 346  14232
         parentTable = parent;
 347  14232
     }
 348  
 
 349  
     /**
 350  
      * Get the parent Table of the column
 351  
      */
 352  
     public Table getTable()
 353  
     {
 354  15028
         return parentTable;
 355  
     }
 356  
 
 357  
     /**
 358  
      * Returns the Name of the table the column is in
 359  
      */
 360  
     public String getTableName()
 361  
     {
 362  0
         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 attrib)
 370  
     {
 371  0
         Inheritance inh = new Inheritance();
 372  0
         inh.loadFromXML (attrib);
 373  0
         addInheritance(inh);
 374  
 
 375  0
         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  0
         inh.setColumn(this);
 385  0
         if (inheritanceList == null)
 386  
         {
 387  0
             inheritanceList = new ArrayList();
 388  0
             isEnumeratedClasses = true;
 389  
         }
 390  0
         inheritanceList.add(inh);
 391  0
     }
 392  
 
 393  
     /**
 394  
      * Get the inheritance definitions.
 395  
      */
 396  
     public List getChildren()
 397  
     {
 398  0
         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  7120
         return isInheritance;
 408  
     }
 409  
 
 410  
     /**
 411  
      * Determine if possible classes have been enumerated in the xml file.
 412  
      */
 413  
     public boolean isEnumeratedClasses()
 414  
     {
 415  0
         return isEnumeratedClasses;
 416  
     }
 417  
 
 418  
     /**
 419  
      * Return the isNotNull property of the column
 420  
      */
 421  
     public boolean isNotNull()
 422  
     {
 423  168
         return isNotNull;
 424  
     }
 425  
 
 426  
     /**
 427  
      * Set the isNotNull property of the column
 428  
      */
 429  
     public void setNotNull(boolean status)
 430  
     {
 431  4
         isNotNull = status;
 432  4
     }
 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 getNotNullString()
 440  
     {
 441  168
         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  0
         return isProtected;
 451  
     }
 452  
     
 453  
     /**
 454  
      * Set the isProtected property of the Column
 455  
      */
 456  
     public void setProtected(boolean prot)
 457  
     {
 458  0
         isProtected = prot;
 459  0
     }
 460  
     
 461  
     /**
 462  
      * Set if the column is a primary key or not
 463  
      */
 464  
     public void setPrimaryKey(boolean pk)
 465  
     {
 466  4
         isPrimaryKey = pk;
 467  4
     }
 468  
 
 469  
     /**
 470  
      * Return true if the column is a primary key
 471  
      */
 472  
     public boolean isPrimaryKey()
 473  
     {
 474  7180
         return isPrimaryKey;
 475  
     }
 476  
 
 477  
     /**
 478  
      * Set true if the column is UNIQUE
 479  
      */
 480  
     public void setUnique (boolean u)
 481  
     {
 482  0
         isUnique = u;
 483  0
     }
 484  
 
 485  
     /**
 486  
      * Get the UNIQUE property
 487  
      */
 488  
     public boolean isUnique()
 489  
     {
 490  0
         return isUnique;
 491  
     }
 492  
 
 493  
     /**
 494  
      * Return true if the column requires a transaction in Postgres
 495  
      */
 496  
     public boolean requiresTransactionInPostgres()
 497  
     {
 498  7132
         return needsTransactionInPostgres;
 499  
     }
 500  
 
 501  
     /**
 502  
      * Utility method to determine if this column is a foreign key.
 503  
      */
 504  
     public boolean isForeignKey()
 505  
     {
 506  0
         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  0
         ForeignKey fk = getForeignKey();
 516  0
         if (fk != null)
 517  
         {
 518  0
             Iterator fks = parentTable.getForeignKeys().iterator();
 519  0
             while (fks.hasNext())
 520  
             {
 521  0
                 ForeignKey key = (ForeignKey) fks.next();
 522  0
                 if (key.getForeignTableName().equals(fk.getForeignTableName())
 523  
                         && !key.getLocalColumns().contains(this.name))
 524  
                 {
 525  0
                     return true;
 526  
                 }
 527  
             }
 528  
         }
 529  
 
 530  
         // No multiple foreign keys.
 531  0
         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  0
         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 getRelatedTableName()
 548  
     {
 549  0
         ForeignKey fk = getForeignKey();
 550  0
         return (fk == null ? class="keyword">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 getRelatedColumnName()
 558  
     {
 559  0
         ForeignKey fk = getForeignKey();
 560  0
         if (fk == null)
 561  
         {
 562  0
             return null;
 563  
         }
 564  
         else
 565  
         {
 566  0
             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  144
         if (referrers == null)
 576  
         {
 577  144
             referrers = new ArrayList(5);
 578  
         }
 579  144
         referrers.add(fk);
 580  144
     }
 581  
 
 582  
     /**
 583  
      * Get list of references to this column.
 584  
      */
 585  
     public List getReferrers()
 586  
     {
 587  0
         if (referrers == null)
 588  
         {
 589  0
             referrers = new ArrayList(5);
 590  
         }
 591  0
         return referrers;
 592  
     }
 593  
 
 594  
     /**
 595  
      * Sets the colunm type
 596  
      */
 597  
     public void setType(String torqueType)
 598  
     {
 599  6828
         SchemaType type = SchemaType.getEnum(torqueType);
 600  6828
         if (type == null)
 601  
         {
 602  456
             log.warn("SchemaType " + torqueType + " does not exist");
 603  456
             type = Column.DEFAULT_TYPE;
 604  
         }
 605  6828
         setType(type);
 606  6828
     }
 607  
 
 608  
     /**
 609  
      * Sets the colunm type
 610  
      */
 611  
     public void setType(SchemaType torqueType)
 612  
     {
 613  6828
         domain = new Domain(getPlatform().getDomainForSchemaType(torqueType));
 614  6828
         if (torqueType.equals(SchemaType.VARBINARY)
 615  
                 || torqueType.equals(SchemaType.BLOB))
 616  
         {
 617  312
             needsTransactionInPostgres = true;
 618  
         }
 619  6828
     }
 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 getType()
 628  
     {
 629  24
         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 getTorqueType()
 636  
     {
 637  108
         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  0
         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  0
         String torqueType = domain.getType().getName();
 657  0
         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 toString()
 673  
     {
 674  0
         StringBuffer result = new StringBuffer();
 675  0
         result.append("    <column name=\"").append(name).append('"');
 676  
 
 677  0
         if (javaName != null)
 678  
         {
 679  0
             result.append(" javaName=\"").append(javaName).append('"');
 680  
         }
 681  
 
 682  0
         if (isPrimaryKey)
 683  
         {
 684  0
             result.append(" primaryKey=\"").append(isPrimaryKey).append('"');
 685  
         }
 686  
 
 687  0
         if (isNotNull)
 688  
         {
 689  0
             result.append(" required=\"true\"");
 690  
         }
 691  
         else
 692  
         {
 693  0
             result.append(" required=\"false\"");
 694  
         }
 695  
 
 696  0
         result.append(" type=\"").append(domain.getType().getName()).append('"');
 697  
 
 698  0
         if (domain.getSize() != null)
 699  
         {
 700  0
             result.append(" size=\"").append(domain.getSize()).append('"');
 701  
         }
 702  
 
 703  0
         if (domain.getScale() != null)
 704  
         {
 705  0
             result.append(" scale=\"").append(domain.getScale()).append('"');
 706  
         }
 707  
 
 708  0
         if (domain.getDefaultValue() != null)
 709  
         {
 710  0
             result.append(" default=\"").append(domain.getDefaultValue()).append('"');
 711  
         }
 712  
 
 713  0
         if (isInheritance())
 714  
         {
 715  0
             result.append(" inheritance=\"").append(inheritanceType)
 716  
                 .append('"');
 717  
         }
 718  
 
 719  
         // Close the column.
 720  0
         result.append(" />\n");
 721  
 
 722  0
         return result.toString();
 723  
     }
 724  
 
 725  
     /**
 726  
      * Returns the size of the column
 727  
      */
 728  
     public String getSize()
 729  
     {
 730  80
         return domain.getSize();
 731  
     }
 732  
 
 733  
     /**
 734  
      * Set the size of the column
 735  
      */
 736  
     public void setSize(String newSize)
 737  
     {
 738  0
         domain.setSize(newSize);
 739  0
     }
 740  
 
 741  
     /**
 742  
      * Returns the scale of the column
 743  
      */
 744  
     public String getScale()
 745  
     {
 746  60
         return domain.getScale();
 747  
     }
 748  
 
 749  
     /**
 750  
      * Set the scale of the column
 751  
      */
 752  
     public void setScale(String newScale)
 753  
     {
 754  0
         domain.setScale(newScale);
 755  0
     }
 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 printSize()
 764  
     {
 765  80
         return domain.printSize();
 766  
     }
 767  
 
 768  
     /**
 769  
      * Return a string that will give this column a default value.
 770  
      * @deprecated
 771  
      */
 772  
      public String getDefaultSetting()
 773  
      {
 774  0
          return domain.getDefaultSetting();
 775  
      }
 776  
 
 777  
     /**
 778  
      * Set a string that will give this column a default value.
 779  
      */
 780  
     public void setDefaultValue(String def)
 781  
     {
 782  0
         domain.setDefaultValue(def);
 783  0
     }
 784  
 
 785  
     /**
 786  
      * Get a string that will give this column a default value.
 787  
      */
 788  
     public String getDefaultValue()
 789  
     {
 790  80
         return domain.getDefaultValue();
 791  
     }
 792  
 
 793  
     /**
 794  
      * Returns the class name to do input validation
 795  
      */
 796  
     public String getInputValidator()
 797  
     {
 798  0
        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  276
         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  0
         isAutoIncrement = value;
 817  0
     }
 818  
 
 819  
     public String getAutoIncrementString()
 820  
     {
 821  276
         if (isAutoIncrement() 
 822  
                 && IDMethod.NATIVE.equals(getTable().getIdMethod()))
 823  
         {
 824  40
             return getPlatform().getAutoIncrement();
 825  
         }
 826  236
         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 typeName, String size)
 834  
     {
 835  8
         String tn = typeName.toUpperCase();
 836  8
         setType(tn);
 837  
 
 838  8
         if (size != null)
 839  
         {
 840  4
             domain.setSize(size);
 841  
         }
 842  
 
 843  8
         if (tn.indexOf("CHAR") != -1)
 844  
         {
 845  4
             domain.setType(SchemaType.VARCHAR);
 846  
         }
 847  4
         else if (tn.indexOf("INT") != -1)
 848  
         {
 849  4
             domain.setType(SchemaType.INTEGER);
 850  
         }
 851  0
         else if (tn.indexOf("FLOAT") != -1)
 852  
         {
 853  0
             domain.setType(SchemaType.FLOAT);
 854  
         }
 855  0
         else if (tn.indexOf("DATE") != -1)
 856  
         {
 857  0
             domain.setType(SchemaType.DATE);
 858  
         }
 859  0
         else if (tn.indexOf("TIME") != -1)
 860  
         {
 861  0
             domain.setType(SchemaType.TIMESTAMP);
 862  
         }
 863  0
         else if (tn.indexOf("BINARY") != -1)
 864  
         {
 865  0
             domain.setType(SchemaType.LONGVARBINARY);
 866  
         }
 867  
         else
 868  
         {
 869  0
             domain.setType(SchemaType.VARCHAR);
 870  
         }
 871  8
     }
 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 getJavaObject()
 880  
     {
 881  0
         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 getJavaPrimitive()
 891  
     {
 892  0
         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 getJavaNative()
 904  
     {
 905  0
         String jtype = TypeMap.getJavaNativeObject(domain.getType());
 906  0
         if (isUsePrimitive())
 907  
         {
 908  0
             jtype = TypeMap.getJavaNative(domain.getType());
 909  
         }
 910  
 
 911  0
         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 getVillageMethod()
 919  
     {
 920  0
         String vmethod = TypeMap.getVillageObjectMethod(domain.getType());
 921  0
         if (isUsePrimitive())
 922  
         {
 923  0
             vmethod = TypeMap.getVillageMethod(domain.getType());
 924  
         }
 925  
 
 926  0
         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 getParameterParserMethod()
 934  
     {
 935  0
         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  0
         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  0
         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  0
         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  0
         String t = getJavaNative();
 972  0
         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  0
         String s = getJavaType();
 985  0
         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  836
         return domain;
 996  
     }
 997  
 
 998  
     /**
 999  
      * @param domain The domain to set.
 1000  
      */
 1001  
     public void setDomain(Domain domain)
 1002  
     {
 1003  0
         this.domain = domain;
 1004  0
     }
 1005  
 
 1006  
     private Platform getPlatform()
 1007  
     {
 1008  
         try
 1009  
         {
 1010  13880
             return getTable().getDatabase().getPlatform();
 1011  
         }
 1012  20
         catch (Exception ex)
 1013  
         {
 1014  20
             log.warn("could not load platform implementation");
 1015  
         }
 1016  20
         return new PlatformDefaultImpl();
 1017  
     }
 1018  
     
 1019  
     public String getSqlString()
 1020  
     {
 1021  168
         StringBuffer sb = new StringBuffer();
 1022  168
         sb.append(getName());
 1023  168
         sb.append(' ');
 1024  168
         sb.append(getDomain().getSqlType());
 1025  168
         if (getPlatform().hasSize(getDomain().getSqlType()))
 1026  
         {
 1027  164
             sb.append(getDomain().printSize());
 1028  164
             sb.append(' ');
 1029  
         }
 1030  168
         if (getDomain().getDefaultValue() != null)
 1031  
         {
 1032  40
             sb.append("default ");
 1033  40
             if (TypeMap.isTextType(getDomain().getType()))
 1034  
             {
 1035  
                 // TODO: Properly SQL-escape the text.
 1036  0
                 sb.append('\'').append(getDefaultValue()).append('\'');
 1037  
             }
 1038  
             else
 1039  
             {
 1040  40
                 sb.append(getDefaultValue());
 1041  
             }
 1042  40
             sb.append(' ');
 1043  
         }
 1044  168
         sb.append(getNotNullString());
 1045  168
         sb.append(' ');
 1046  168
         sb.append(getAutoIncrementString());
 1047  168
         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  0
         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  7112
         this.correctGetters = correctGetters;
 1072  7112
     }
 1073  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.