db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mpoes...@apache.org
Subject cvs commit: db-torque/src/generator/src/java/org/apache/torque/engine/database/model Column.java
Date Wed, 03 Dec 2003 19:05:08 GMT
mpoeschl    2003/12/03 11:05:08

  Modified:    src/generator/src/test/org/apache/torque/engine/database/model
                        domaintest-schema.xml DomainTest.java
               src/generator/src/java/org/apache/torque/engine/database/model
                        Column.java
  Log:
  get sql type from platform impl when loading columns
  
  Revision  Changes    Path
  1.4       +3 -0      db-torque/src/generator/src/test/org/apache/torque/engine/database/model/domaintest-schema.xml
  
  Index: domaintest-schema.xml
  ===================================================================
  RCS file: /home/cvs/db-torque/src/generator/src/test/org/apache/torque/engine/database/model/domaintest-schema.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- domaintest-schema.xml	18 Nov 2003 17:59:59 -0000	1.3
  +++ domaintest-schema.xml	3 Dec 2003 19:05:08 -0000	1.4
  @@ -14,6 +14,9 @@
       <column name="article_id" required="true" primaryKey="true" type="INTEGER" />
       <column name="name" size="40" />
       <column name="price" domain="amount" size="12" default="1000" />
  +    <column name="note" type="VARCHAR" size="50" default="default note" />
  +    <column name="decimal_col" type="DECIMAL" size="10" scale="3" />
  +    <column name="date_col" type="DATE"/>
     </table>
     
   </database>
  
  
  
  1.5       +26 -1     db-torque/src/generator/src/test/org/apache/torque/engine/database/model/DomainTest.java
  
  Index: DomainTest.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/generator/src/test/org/apache/torque/engine/database/model/DomainTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DomainTest.java	18 Nov 2003 17:59:59 -0000	1.4
  +++ DomainTest.java	3 Dec 2003 19:05:08 -0000	1.5
  @@ -95,6 +95,7 @@
       {
           Domain amount = db.getDomain("amount");
           assertEquals(SchemaType.NUMERIC, amount.getType());
  +        assertEquals("DECIMAL", amount.getSqlType());
           assertEquals("10", amount.getSize());
           assertEquals("2", amount.getScale());
           assertEquals("0", amount.getDefaultValue());
  @@ -109,10 +110,12 @@
           Table table = db.getTable("product");
           Column name = table.getColumn("name");
           assertEquals("VARCHAR", name.getType());
  +        assertEquals("VARCHAR", name.getDomain().getSqlType());
           assertEquals("40", name.getSize());
           Column price = table.getColumn("price");
           assertEquals("NUMERIC", price.getTorqueType());
           assertEquals("NUMERIC", price.getType());
  +        assertEquals("DECIMAL", price.getDomain().getSqlType());
           assertEquals("10", price.getSize());
           assertEquals("2", price.getScale());
           assertEquals("0", price.getDefaultValue());
  @@ -128,10 +131,32 @@
           Column price = table.getColumn("price");
           assertEquals("NUMERIC", price.getTorqueType());
           assertEquals("NUMERIC", price.getType());
  +        assertEquals("DECIMAL", price.getDomain().getSqlType());
           assertEquals("12", price.getSize());
           assertEquals("2", price.getScale());
           assertEquals("1000", price.getDefaultValue());
           assertEquals("(12,2)", price.printSize());
       }
       
  +    public void testDecimalColumn() throws Exception
  +    {
  +        Table table = db.getTable("article");
  +        Column col = table.getColumn("decimal_col");
  +        assertEquals("DECIMAL", col.getTorqueType());
  +        assertEquals("DECIMAL", col.getType());
  +        assertEquals("DECIMAL", col.getDomain().getSqlType());
  +        assertEquals("10", col.getSize());
  +        assertEquals("3", col.getScale());
  +        assertEquals("(10,3)", col.printSize());
  +    }
  +
  +    public void testDateColumn() throws Exception
  +    {
  +        Table table = db.getTable("article");
  +        Column col = table.getColumn("date_col");
  +        assertEquals("DATE", col.getTorqueType());
  +        assertEquals("DATE", col.getType());
  +        assertEquals("DATETIME", col.getDomain().getSqlType());
  +        assertEquals("", col.printSize());
  +    }    
   }
  
  
  
  1.16      +55 -30    db-torque/src/generator/src/java/org/apache/torque/engine/database/model/Column.java
  
  Index: Column.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/generator/src/java/org/apache/torque/engine/database/model/Column.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- Column.java	1 Dec 2003 16:56:30 -0000	1.15
  +++ Column.java	3 Dec 2003 19:05:08 -0000	1.16
  @@ -59,12 +59,11 @@
   import java.util.List;
   
   import org.apache.commons.lang.StringUtils;
  -
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -
   import org.apache.torque.engine.EngineException;
  -
  +import org.apache.torque.engine.platform.Platform;
  +import org.apache.torque.engine.platform.PlatformDefaultImpl;
   import org.xml.sax.Attributes;
   
   /**
  @@ -80,7 +79,7 @@
    */
   public class Column
   {
  -    private static final String DEFAULT_TYPE = "VARCHAR";
  +    private static final SchemaType DEFAULT_TYPE = SchemaType.VARCHAR;
       /** Logging class from commons.logging */
       private static Log log = LogFactory.getLog(Column.class);
       private String name;
  @@ -166,8 +165,8 @@
           } 
           else
           {
  -            domain = new Domain();
  -            domain.setType(DEFAULT_TYPE);
  +            domain = new Domain(getPlatform().getDomainForSchemaType(DEFAULT_TYPE));
  +            setType(attrib.getValue("type"));
           }
           //Name
           name = attrib.getValue("name");
  @@ -214,8 +213,6 @@
           domain.replaceSize(attrib.getValue("size"));
           domain.replaceScale(attrib.getValue("scale"));
   
  -        domain.replaceType(attrib.getValue("type"));
  -
           inheritanceType = attrib.getValue("inheritance");
           isInheritance = (inheritanceType != null
                   && !inheritanceType.equals("false"));
  @@ -571,18 +568,33 @@
       }
   
       /**
  -     * Returns the colunm type
  +     * Sets the colunm type
        */
       public void setType(String torqueType)
       {
  -        domain.setType(torqueType);
  -        if (torqueType.equals("VARBINARY") || torqueType.equals("BLOB"))
  +        SchemaType type = SchemaType.getEnum(torqueType);
  +        if (type == null)
           {
  -            needsTransactionInPostgres = true;
  +            log.warn("SchemaType " + torqueType + " does not exist");
  +            type = this.DEFAULT_TYPE;
           }
  +        setType(type);
       }
   
       /**
  +     * Sets the colunm type
  +     */
  +    public void setType(SchemaType torqueType)
  +    {
  +        domain = new Domain(getPlatform().getDomainForSchemaType(torqueType));
  +        if (torqueType.equals(SchemaType.VARBINARY)
  +                || torqueType.equals(SchemaType.BLOB))
  +        {
  +            needsTransactionInPostgres = true;
  +        }
  +    }
  +    
  +    /**
        * Returns the column jdbc type as an object
        */
       public Object getType()
  @@ -728,26 +740,10 @@
   
       /**
        * Return a string that will give this column a default value.
  -     * <p>
  -     * TODO: Properly SQL-escape text values.
        */
        public String getDefaultSetting()
        {
  -         StringBuffer dflt = new StringBuffer(0);
  -         if (domain.getDefaultValue() != null)
  -         {
  -             dflt.append("default ");
  -             if (TypeMap.isTextType(domain.getType()))
  -             {
  -                 // TODO: Properly SQL-escape the text.
  -                 dflt.append('\'').append(domain.getDefaultValue()).append('\'');
  -             }
  -             else
  -             {
  -                 dflt.append(domain.getDefaultValue());
  -             }
  -         }
  -         return dflt.toString();
  +         return domain.getDefaultSetting();
        }
   
       /**
  @@ -942,5 +938,34 @@
           return (s != null && s.equals("primitive"))
               || (s == null && !"object".equals(
                  getTable().getDatabase().getDefaultJavaType()));
  +    }
  +    
  +    /**
  +     * @return Returns the domain.
  +     */
  +    public Domain getDomain()
  +    {
  +        return domain;
  +    }
  +
  +    /**
  +     * @param domain The domain to set.
  +     */
  +    public void setDomain(Domain domain)
  +    {
  +        this.domain = domain;
  +    }
  +
  +    private Platform getPlatform()
  +    {
  +        try
  +        {
  +            return getTable().getDatabase().getPlatform();
  +        }
  +        catch (Exception ex)
  +        {
  +            log.warn("could not load platform implementation");
  +        }
  +        return new PlatformDefaultImpl();
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message