Author: tfischer
Date: Fri May 4 11:13:01 2012
New Revision: 1333879
URL: http://svn.apache.org/viewvc?rev=1333879&view=rev
Log:
Make domains work
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/typemapping/SqlType.java
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java?rev=1333879&r1=1333878&r2=1333879&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/SchemaTypeHelper.java
Fri May 4 11:13:01 2012
@@ -19,11 +19,15 @@ package org.apache.torque.templates.tran
* under the License.
*/
+import java.util.List;
+
+import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.generator.control.ControllerState;
import org.apache.torque.generator.source.SourceElement;
import org.apache.torque.generator.source.transform.SourceTransformerException;
import org.apache.torque.templates.TemplateOptionName;
import org.apache.torque.templates.TorqueSchemaAttributeName;
+import org.apache.torque.templates.TorqueSchemaElementName;
import org.apache.torque.templates.platform.Platform;
import org.apache.torque.templates.platform.PlatformFactory;
import org.apache.torque.templates.typemapping.SchemaType;
@@ -71,22 +75,37 @@ public final class SchemaTypeHelper
+ columnElement.getName()
+ " is null");
}
+
SchemaType schemaType = null;
- String schemaTypeString = (String) columnElement.getAttribute(
- TorqueSchemaAttributeName.TYPE.getName());
- if (schemaTypeString == null)
- {
- throw new SourceTransformerException("type attribute not set"
- + " in Column "
- + columnNameFromSchema);
- }
- schemaType = SchemaType.valueOf(schemaTypeString);
- if (schemaType == null)
- {
- throw new SourceTransformerException("Unknown type "
- + schemaTypeString
- + " in Column "
- + columnNameFromSchema);
+ SqlType domain = getDomain(columnElement, controllerState);
+ if (domain != null && domain.getSqlTypeName() != null)
+ {
+ schemaType = SchemaType.valueOf(domain.getSqlTypeName());
+ if (schemaType == null)
+ {
+ throw new SourceTransformerException("Unknown type "
+ + domain.getSqlTypeName()
+ + " in Domain definition");
+ }
+ }
+ else
+ {
+ String schemaTypeString = (String) columnElement.getAttribute(
+ TorqueSchemaAttributeName.TYPE.getName());
+ if (schemaTypeString == null)
+ {
+ throw new SourceTransformerException("type attribute not set"
+ + " in Column "
+ + columnNameFromSchema);
+ }
+ schemaType = SchemaType.valueOf(schemaTypeString);
+ if (schemaType == null)
+ {
+ throw new SourceTransformerException("Unknown type "
+ + schemaTypeString
+ + " in Column "
+ + columnNameFromSchema);
+ }
}
return schemaType;
}
@@ -96,13 +115,23 @@ public final class SchemaTypeHelper
*
* @param schemaType the schema type for which the SQL type should be
* determined, not null.
+ * @param domainType the domain type which overrides the schema type,
+ * or null if no domain is defined.
* @param controllerState the controller state, not null.
+ * @param size overrides the size from schemaType and/or domainType,
+ * or null to use the default from domainType or schemaType.
+ * @param scale overrides the scale from schemaType and/or domainType,
+ * or null to use the default from domainType or schemaType.
+ * @param defaultValue overrides the defaultValue from schemaType
+ * and/or domainType, or null to use the default from domainType
+ * or schemaType.
*
* @return the the SQL type for the schema type, or null if no SQL type
* exists for the schema type.
*/
public static SqlType getSqlType(
SchemaType schemaType,
+ SqlType domainType,
ControllerState controllerState,
String size,
String scale,
@@ -111,8 +140,79 @@ public final class SchemaTypeHelper
Platform platform = PlatformFactory.getPlatformFor(
controllerState.getStringOption(TemplateOptionName.DATABASE));
SqlType platformSqlType = platform.getSqlTypeForSchemaType(schemaType);
+ if (domainType != null)
+ {
+ if (size == null)
+ {
+ size = domainType.getSize();
+ }
+ if (scale == null)
+ {
+ scale = domainType.getScale();
+ }
+ if (defaultValue == null)
+ {
+ defaultValue = domainType.getDefaultValue();
+ }
+ }
SqlType result
= new SqlType(platformSqlType, size, scale, defaultValue);
return result;
}
+
+ public static SqlType getDomain(
+ SourceElement columnElement,
+ ControllerState controllerState)
+ throws SourceTransformerException
+ {
+ String domainNameFromSchema
+ = (String) columnElement.getAttribute(
+ TorqueSchemaAttributeName.DOMAIN);
+ if (domainNameFromSchema == null)
+ {
+ // no domain specified
+ return null;
+ }
+ SourceElement domainElement = null;
+ {
+ SourceElement databaseElement
+ = columnElement.getParent().getParent();
+ List<SourceElement> domainElementList
+ = databaseElement.getChildren(
+ TorqueSchemaElementName.DOMAIN);
+ for (SourceElement candidate : domainElementList)
+ {
+ if (domainNameFromSchema.equals(candidate.getAttribute(
+ TorqueSchemaAttributeName.NAME)))
+ {
+ domainElement = candidate;
+ break;
+ }
+ }
+ }
+ if (domainElement == null)
+ {
+ throw new SourceTransformerException("The domain named "
+ + domainNameFromSchema
+ + " referenced by the column "
+ + columnElement.getParent().getAttribute(
+ TorqueSchemaAttributeName.NAME)
+ + " in the table "
+ +
columnElement.getAttribute(TorqueSchemaAttributeName.NAME)
+ + " was not found in this schema");
+ }
+ String sqlType = ObjectUtils.toString(
+ domainElement.getAttribute(TorqueSchemaAttributeName.TYPE),
+ null);
+ String defaultValue = ObjectUtils.toString(
+ domainElement.getAttribute(TorqueSchemaAttributeName.DEFAULT),
+ null);
+ String size = ObjectUtils.toString(
+ domainElement.getAttribute(TorqueSchemaAttributeName.SIZE),
+ null);
+ String scale = ObjectUtils.toString(
+ domainElement.getAttribute(TorqueSchemaAttributeName.SCALE),
+ null);
+ return new SqlType(sqlType, size, scale, defaultValue);
+ }
}
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java?rev=1333879&r1=1333878&r2=1333879&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
Fri May 4 11:13:01 2012
@@ -37,6 +37,7 @@ import org.apache.torque.templates.trans
import org.apache.torque.templates.typemapping.JavaType;
import org.apache.torque.templates.typemapping.ResultSetGetter;
import org.apache.torque.templates.typemapping.SchemaType;
+import org.apache.torque.templates.typemapping.SqlType;
import org.apache.torque.templates.typemapping.TypeMap;
/**
@@ -88,6 +89,7 @@ public class OMColumnTransformer
columnElement,
controllerState);
columnElement.setAttribute("schemaType", schemaType);
+ setDomainAttributes(columnElement, controllerState);
JavaType fieldJavaType = getFieldJavaType(columnElement, schemaType);
columnElement.setAttribute(
@@ -150,7 +152,7 @@ public class OMColumnTransformer
*
* @param columnElement the column element, not null.
*/
- private void setAttributeDefaultValues(SourceElement columnElement)
+ protected void setAttributeDefaultValues(SourceElement columnElement)
{
// set autoincrement attribute
if
(columnElement.getAttribute(TorqueSchemaAttributeName.AUTO_INCREMENT)
@@ -216,6 +218,50 @@ public class OMColumnTransformer
}
}
+ protected void setDomainAttributes(
+ SourceElement columnElement,
+ ControllerState controllerState)
+ throws SourceTransformerException
+ {
+ SqlType domain = SchemaTypeHelper.getDomain(
+ columnElement,
+ controllerState);
+ if (domain == null)
+ {
+ return;
+ }
+ if (columnElement.getAttribute(TorqueSchemaAttributeName.TYPE)
+ == null
+ && domain.getSqlTypeName() != null)
+ {
+ columnElement.setAttribute(
+ TorqueSchemaAttributeName.TYPE,
+ domain.getSqlTypeName());
+ }
+ if (columnElement.getAttribute(TorqueSchemaAttributeName.DEFAULT)
+ == null
+ && domain.getDefaultValue() != null)
+ {
+ columnElement.setAttribute(
+ TorqueSchemaAttributeName.DEFAULT,
+ domain.getDefaultValue());
+ }
+ if (columnElement.getAttribute(TorqueSchemaAttributeName.SIZE) == null
+ && domain.getSize() != null)
+ {
+ columnElement.setAttribute(
+ TorqueSchemaAttributeName.SIZE,
+ domain.getSize());
+ }
+ if (columnElement.getAttribute(TorqueSchemaAttributeName.SCALE) == null
+ && domain.getScale() != null)
+ {
+ columnElement.setAttribute(
+ TorqueSchemaAttributeName.SCALE,
+ domain.getScale());
+ }
+ }
+
/**
* Sets the attributes getterAccessModifer and setterAccessModifer
* on the column element.
@@ -552,7 +598,7 @@ public class OMColumnTransformer
return;
}
String defaultValue = (String) columnElement.getAttribute(
- TorqueSchemaAttributeName.DEFAULT.getName());
+ TorqueSchemaAttributeName.DEFAULT);
boolean primitiveFieldType = javaType.isPrimitive();
String fieldDefaultValue;
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java?rev=1333879&r1=1333878&r2=1333879&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
Fri May 4 11:13:01 2012
@@ -265,6 +265,9 @@ public class SQLTransformer implements S
SchemaType schemaType = SchemaTypeHelper.getSchemaType(
columnElement,
controllerState);
+ SqlType domainType = SchemaTypeHelper.getDomain(
+ columnElement,
+ controllerState);
Object size = columnElement.getAttribute(
TorqueSchemaAttributeName.SIZE);
Object scale = columnElement.getAttribute(
@@ -273,6 +276,7 @@ public class SQLTransformer implements S
TorqueSchemaAttributeName.DEFAULT);
SqlType sqlType = SchemaTypeHelper.getSqlType(
schemaType,
+ domainType,
controllerState,
ObjectUtils.toString(size, null),
ObjectUtils.toString(scale, null),
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/typemapping/SqlType.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/typemapping/SqlType.java?rev=1333879&r1=1333878&r2=1333879&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/typemapping/SqlType.java
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/typemapping/SqlType.java
Fri May 4 11:13:01 2012
@@ -51,25 +51,22 @@ import org.apache.commons.lang.StringUti
public class SqlType
{
/**
- * The default size for the columns with this type
- * (can be overridden in the column definition).
+ * The default size for the columns with this type.
*/
private String size;
/**
- * The default scale for the columns with this type
- * (can be overridden in the column definition).
+ * The default scale for the columns with this type.
*/
private String scale;
/**
- * The default "default value" for the columns with this type
- * (can be overridden in the column definition).
- */
+ * The default "default value" for the columns with this type.
+. */
private String defaultValue;
/**
- * The SQL expression for the type name, not null.
+ * The SQL expression for the type name, or null if unknown.
*/
private String sqlTypeName;
@@ -77,16 +74,10 @@ public class SqlType
* Creates a new SqlType with the given SQL Type.
* Size, scale and defaultValue are set to null.
*
- * @param sqlType the SQL name of the SQL type, not null.
- *
- * @throws NullPointerException if sqlTypeName is null.
+ * @param sqlType the SQL name of the SQL type, or null.
*/
public SqlType(String sqlTypeName)
{
- if (sqlTypeName == null)
- {
- throw new NullPointerException("sqlTypeName must not be null");
- }
this.sqlTypeName = sqlTypeName;
}
@@ -219,7 +210,10 @@ public class SqlType
}
/**
- * @return Returns the SQL type name for this column type.
+ * Returns the SQL type name.
+ *
+ * @return The SQL type name for this column type, or null if the type
+ * is not set.
*/
public String getSqlTypeName()
{
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd?rev=1333879&r1=1333878&r2=1333879&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
Fri May 4 11:13:01 2012
@@ -439,10 +439,10 @@ The column name
</xs:documentation>
</xs:annotation>
</xs:attribute>
- <xs:attribute name="type" use="required" type="sqlDataType">
+ <xs:attribute name="type" use="optional" type="sqlDataType">
<xs:annotation>
<xs:documentation xml:lang="en">
-The SQL data type for the column
+The SQL data type for the column. Is required unless a domain is used.
</xs:documentation>
</xs:annotation>
</xs:attribute>
Modified:
db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml?rev=1333879&r1=1333878&r2=1333879&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml
(original)
+++
db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml
Fri May 4 11:13:01 2012
@@ -23,8 +23,9 @@
package="org.apache.torque.test.ext"
xmlns="http://db.apache.org/torque/4.0/templates/database">
+ <domain name="myTimestamp" type="TIMESTAMP" default="CURRENT_TIMESTAMP"/>
+
<table name="default_values">
-
<column
name="id"
required="true"
@@ -128,8 +129,7 @@
<column
name="CURRENT_TIMESTAMP_VALUE"
required="true"
- type="TIMESTAMP"
- default="CURRENT_TIMESTAMP"
+ domain="myTimestamp"
useDatabaseDefaultValue="true"
/>
</table>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]