diff --git a/jOOQ-codegen/src/main/java/org/jooq/util/DefaultGenerator.java b/jOOQ-codegen/src/main/java/org/jooq/util/DefaultGenerator.java index 58a8fdf053..dc833fe71e 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/util/DefaultGenerator.java +++ b/jOOQ-codegen/src/main/java/org/jooq/util/DefaultGenerator.java @@ -2742,6 +2742,11 @@ public class DefaultGenerator implements Generator { type = strategy.getFullJavaClassName(db.getUDT(schema, u), Mode.RECORD); } + // Check for custom types + else if (db.getConfiguredCustomType(u) != null) { + type = u; + } + // Try finding a basic standard SQL type according to the current dialect else { try { @@ -2818,6 +2823,12 @@ public class DefaultGenerator implements Generator { sb.append(SQLDataType.class.getCanonicalName()); sb.append("."); sb.append(FieldTypeHelper.normalise(sqlDataType.getTypeName())); + + if (db.getConfiguredCustomType(u) != null) { + sb.append(".asConvertedDataType(new "); + sb.append(db.getConfiguredCustomType(u).getConverter()); + sb.append("())"); + } } // Otherwise, reference the dialect-specific DataType itself. diff --git a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java index 7aa5107919..75eadb2cca 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java +++ b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java @@ -243,6 +243,11 @@ public class GenerationTool { log.info("Use GenerationTool to migrate your .properties file to XML (printed on System.out) :"); log.info("Usage : GenerationTool migrate"); log.info(""); + log.info("XML output:"); + log.info(""); + JAXB.marshal(configuration, System.out); + log.info(""); + log.info(""); main(configuration); } @@ -319,6 +324,7 @@ public class GenerationTool { database.setIncludes(defaultString(g.getDatabase().getIncludes()).split(",")); database.setExcludes(defaultString(g.getDatabase().getExcludes()).split(",")); database.setConfiguredMasterDataTables(g.getDatabase().getMasterDataTables()); + database.setConfiguredCustomTypes(g.getDatabase().getCustomTypes()); database.setConfiguredEnumTypes(g.getDatabase().getEnumTypes()); database.setConfiguredForcedTypes(g.getDatabase().getForcedTypes()); diff --git a/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java b/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java index 690a8d056f..7354d3cb16 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/AbstractDatabase.java @@ -46,6 +46,7 @@ import java.util.List; import org.jooq.SQLDialect; import org.jooq.tools.JooqLogger; import org.jooq.tools.csv.CSVReader; +import org.jooq.util.jaxb.CustomType; import org.jooq.util.jaxb.EnumType; import org.jooq.util.jaxb.ForcedType; import org.jooq.util.jaxb.MasterDataTable; @@ -71,6 +72,7 @@ public abstract class AbstractDatabase implements Database { private boolean dateAsTimestamp; private List configuredSchemata; private List configuredMasterDataTables; + private List configuredCustomTypes; private List configuredEnumTypes; private List configuredForcedTypes; @@ -198,6 +200,27 @@ public abstract class AbstractDatabase implements Database { return configuredEnumTypes; } + @Override + public final void setConfiguredCustomTypes(List configuredCustomTypes) { + this.configuredCustomTypes = configuredCustomTypes; + } + + @Override + public final List getConfiguredCustomTypes() { + return configuredCustomTypes; + } + + @Override + public final CustomType getConfiguredCustomType(String name) { + for (CustomType type : configuredCustomTypes) { + if (type.getName().equals(name)) { + return type; + } + } + + return null; + } + @Override public final void setConfiguredForcedTypes(List configuredForcedTypes) { this.configuredForcedTypes = configuredForcedTypes; diff --git a/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java b/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java index 53e8196e52..d410839caa 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/AbstractTypedElementDefinition.java @@ -37,6 +37,7 @@ package org.jooq.util; import static org.jooq.impl.FieldTypeHelper.getDialectDataType; +import java.sql.Types; import java.util.ArrayList; import java.util.List; @@ -130,7 +131,7 @@ abstract class AbstractTypedElementDefinition } catch (SQLDialectNotSupportedException ignore) {} if (dataType != null) { - if (dataType.getSQLDataType() == SQLDataType.DATE) { + if (dataType.getSQLType() == Types.DATE) { DataType forcedDataType = getDialectDataType(db.getDialect(), SQLDataType.TIMESTAMP.getTypeName(), 0, 0); type = new DefaultDataTypeDefinition(db, getSchema(), forcedDataType.getTypeName(), 0, 0); } diff --git a/jOOQ-meta/src/main/java/org/jooq/util/Database.java b/jOOQ-meta/src/main/java/org/jooq/util/Database.java index 6bb89e53e0..001c66eac5 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/Database.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/Database.java @@ -41,6 +41,7 @@ import java.util.List; import org.jooq.SQLDialect; import org.jooq.impl.Factory; +import org.jooq.util.jaxb.CustomType; import org.jooq.util.jaxb.EnumType; import org.jooq.util.jaxb.ForcedType; import org.jooq.util.jaxb.MasterDataTable; @@ -186,6 +187,23 @@ public interface Database { */ List getConfiguredMasterDataTables(); + /** + * Database objects matching any of these field names will be generated as + * custom types + */ + void setConfiguredCustomTypes(List types); + + /** + * Database objects matching any of these field names will be generated as + * custom types + */ + List getConfiguredCustomTypes(); + + /** + * Get a specific configured custom type by its name + */ + CustomType getConfiguredCustomType(String name); + /** * Database objects matching any of these field names will be generated as * enum types diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Configuration.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Configuration.java index 7e158299a7..30f4564684 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Configuration.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Configuration.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/CustomType.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/CustomType.java new file mode 100644 index 0000000000..890eb75de5 --- /dev/null +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/CustomType.java @@ -0,0 +1,106 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs +// See http://java.sun.com/xml/jaxb +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2012.03.04 at 05:58:26 PM MEZ +// + + +package org.jooq.util.jaxb; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for CustomType complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType name="CustomType">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <all>
+ *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *         <element name="converter" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *       </all>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "CustomType", propOrder = { + +}) +public class CustomType { + + @XmlElement(required = true) + protected String name; + @XmlElement(required = true) + protected String converter; + + /** + * Gets the value of the name property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getName() { + return name; + } + + /** + * Sets the value of the name property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setName(String value) { + this.name = value; + } + + /** + * Gets the value of the converter property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getConverter() { + return converter; + } + + /** + * Sets the value of the converter property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setConverter(String value) { + this.converter = value; + } + + public CustomType withName(String value) { + setName(value); + return this; + } + + public CustomType withConverter(String value) { + setConverter(value); + return this; + } + +} diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/CustomTypes.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/CustomTypes.java new file mode 100644 index 0000000000..4c284eac81 --- /dev/null +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/CustomTypes.java @@ -0,0 +1,91 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs +// See http://java.sun.com/xml/jaxb +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2012.03.04 at 05:58:26 PM MEZ +// + + +package org.jooq.util.jaxb; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for CustomTypes complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType name="CustomTypes">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="customType" type="{http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd}CustomType" maxOccurs="unbounded" minOccurs="0"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "CustomTypes", propOrder = { + "customType" +}) +public class CustomTypes { + + protected List customType; + + /** + * Gets the value of the customType property. + * + *

+ * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a set method for the customType property. + * + *

+ * For example, to add a new item, do as follows: + *

+     *    getCustomType().add(newItem);
+     * 
+ * + * + *

+ * Objects of the following type(s) are allowed in the list + * {@link CustomType } + * + * + */ + public List getCustomType() { + if (customType == null) { + customType = new ArrayList(); + } + return this.customType; + } + + public CustomTypes withCustomType(CustomType... values) { + if (values!= null) { + for (CustomType value: values) { + getCustomType().add(value); + } + } + return this; + } + + public CustomTypes withCustomType(Collection values) { + if (values!= null) { + getCustomType().addAll(values); + } + return this; + } + +} diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Database.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Database.java index b450ff07d3..43d479560d 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Database.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Database.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // @@ -37,6 +37,7 @@ import javax.xml.bind.annotation.XmlType; * <element name="outputSchema" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/> * <element name="schemata" type="{http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd}Schemata" minOccurs="0"/> * <element name="masterDataTables" type="{http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd}MasterDataTables" minOccurs="0"/> + * <element name="customTypes" type="{http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd}CustomTypes" minOccurs="0"/> * <element name="enumTypes" type="{http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd}EnumTypes" minOccurs="0"/> * <element name="forcedTypes" type="{http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd}ForcedTypes" minOccurs="0"/> * </all> @@ -73,6 +74,9 @@ public class Database { @XmlElementWrapper(name = "masterDataTables") @XmlElement(name = "masterDataTable") protected List masterDataTables; + @XmlElementWrapper(name = "customTypes") + @XmlElement(name = "customType") + protected List customTypes; @XmlElementWrapper(name = "enumTypes") @XmlElement(name = "enumType") protected List enumTypes; @@ -262,6 +266,13 @@ public class Database { return masterDataTables; } + public List getCustomTypes() { + if (customTypes == null) { + customTypes = new ArrayList(); + } + return customTypes; + } + public List getEnumTypes() { if (enumTypes == null) { enumTypes = new ArrayList(); @@ -343,6 +354,22 @@ public class Database { return this; } + public Database withCustomTypes(CustomType... values) { + if (values!= null) { + for (CustomType value: values) { + getCustomTypes().add(value); + } + } + return this; + } + + public Database withCustomTypes(Collection values) { + if (values!= null) { + getCustomTypes().addAll(values); + } + return this; + } + public Database withEnumTypes(EnumType... values) { if (values!= null) { for (EnumType value: values) { diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/EnumType.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/EnumType.java index 88497d0f3b..8129a88687 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/EnumType.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/EnumType.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/ForcedType.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/ForcedType.java index fed142693d..9c0649fcf3 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/ForcedType.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/ForcedType.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generate.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generate.java index e7c32d8524..3ac8fabcdd 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generate.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generate.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generator.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generator.java index 0df28d0ff3..1ede47f753 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generator.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Generator.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Jdbc.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Jdbc.java index 5cdf01bde1..3678600819 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Jdbc.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Jdbc.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/MasterDataTable.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/MasterDataTable.java index 84325c2530..1764a459bc 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/MasterDataTable.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/MasterDataTable.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Schema.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Schema.java index 2bea0693be..c767107d95 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Schema.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Schema.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Strategy.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Strategy.java index b201105176..78f5723ba7 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Strategy.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Strategy.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Target.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Target.java index 20fd14f8a4..a76756384e 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Target.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/Target.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // diff --git a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/package-info.java b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/package-info.java index 7fdf0a845d..39ff746520 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/jaxb/package-info.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/jaxb/package-info.java @@ -2,7 +2,7 @@ // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0.5-b02-fcs // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. -// Generated on: 2012.03.02 at 04:30:18 PM MEZ +// Generated on: 2012.03.04 at 05:58:26 PM MEZ // @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) diff --git a/jOOQ-meta/src/main/resources/xsd/jooq-codegen-2.1.0.xsd b/jOOQ-meta/src/main/resources/xsd/jooq-codegen-2.1.0.xsd index d476f099e5..aefb5fbeea 100644 --- a/jOOQ-meta/src/main/resources/xsd/jooq-codegen-2.1.0.xsd +++ b/jOOQ-meta/src/main/resources/xsd/jooq-codegen-2.1.0.xsd @@ -161,6 +161,13 @@ --> + + + + + + + @@ -230,6 +244,16 @@ + + + + + + + + + + diff --git a/jOOQ-test/configuration/org/jooq/configuration/lukas/hsqldb/library.xml b/jOOQ-test/configuration/org/jooq/configuration/lukas/hsqldb/library.xml new file mode 100644 index 0000000000..48ec662b88 --- /dev/null +++ b/jOOQ-test/configuration/org/jooq/configuration/lukas/hsqldb/library.xml @@ -0,0 +1,145 @@ + + + + org.hsqldb.jdbcDriver + jdbc:hsqldb:hsql://localhost + PUBLIC + sa + + + + org.jooq.util.DefaultGenerator + + org.jooq.util.hsqldb.HSQLDBDatabase + .* + T_BOOK_DETAILS,S_TRIGGERS_SEQUENCE + false + true + + + T_LANGUAGE + CD + DESCRIPTION + + + T_658_11 + + + T_658_21 + + + T_658_31 + + + T_658_12 + CD + + + T_658_22 + CD + + + T_658_32 + CD + + + + + org.jooq.test._.StringEnum + org.jooq.test._.StringEnumMapper + + + org.jooq.test._.OrdinalEnum + org.jooq.test._.OrdinalEnumMapper + + + + + BOOLEAN_YN_LC + y,"n" + + + BOOLEAN_YN_UC + "Y",N + + + BOOLEAN_TRUE_FALSE_LC + true,false + + + BOOLEAN_TRUE_FALSE_UC + TRUE,FALSE + + + BOOLEAN_10 + 1,0 + + + BOOLEAN_YES_NO_LC + yes,no + + + BOOLEAN_YES_NO_UC + "YES","NO" + + + + + org.jooq.test._.StringEnum + (?i:(.*?\.)?T_MAPPED_TYPES.DEFAULT_ENUM_NAME) + + + org.jooq.test._.OrdinalEnum + (?i:(.*?\.)?T_MAPPED_TYPES.DEFAULT_ENUM_ORDINAL) + + + + BOOLEAN_YES_NO_LC + (?i:(.*?.)?T_BOOLEANS.YES_NO_LC) + + + BOOLEAN_YES_NO_UC + (?i:(.*?.)?T_BOOLEANS.YES_NO_UC) + + + BOOLEAN + (?i:(.*?.)?T_BOOLEANS.(VC|C|N)_BOOLEAN) + + + BOOLEAN_YN_LC + (?i:(.*?.)?T_BOOLEANS.Y_N_LC) + + + BOOLEAN_YN_UC + (?i:(.*?.)?T_BOOLEANS.Y_N_UC) + + + BOOLEAN_TRUE_FALSE_LC + (?i:(.*?.)?T_BOOLEANS.TRUE_FALSE_LC) + + + BOOLEAN_TRUE_FALSE_UC + (?i:(.*?.)?T_BOOLEANS.TRUE_FALSE_UC) + + + BOOLEAN_10 + (?i:(.*?.)?T_BOOLEANS.ONE_ZERO) + + + + + true + true + true + true + true + true + false + false + + + org.jooq.test.hsqldb.generatedclasses + ./src + + + \ No newline at end of file diff --git a/jOOQ-test/launch/GenerationTool HSQLDB test.launch b/jOOQ-test/launch/GenerationTool HSQLDB test.launch index da0d199980..21cf06ff61 100644 --- a/jOOQ-test/launch/GenerationTool HSQLDB test.launch +++ b/jOOQ-test/launch/GenerationTool HSQLDB test.launch @@ -27,7 +27,7 @@ - + diff --git a/jOOQ-test/src/org/jooq/test/_/OrdinalEnum.java b/jOOQ-test/src/org/jooq/test/_/OrdinalEnum.java new file mode 100644 index 0000000000..d84b09262b --- /dev/null +++ b/jOOQ-test/src/org/jooq/test/_/OrdinalEnum.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.test._; + +public enum OrdinalEnum { + + A, B, C +} diff --git a/jOOQ-test/src/org/jooq/test/_/MyEnumNumericMapper.java b/jOOQ-test/src/org/jooq/test/_/OrdinalEnumMapper.java similarity index 83% rename from jOOQ-test/src/org/jooq/test/_/MyEnumNumericMapper.java rename to jOOQ-test/src/org/jooq/test/_/OrdinalEnumMapper.java index 032d98987f..58b2e82c24 100644 --- a/jOOQ-test/src/org/jooq/test/_/MyEnumNumericMapper.java +++ b/jOOQ-test/src/org/jooq/test/_/OrdinalEnumMapper.java @@ -38,19 +38,19 @@ package org.jooq.test._; import org.jooq.Converter; -public class MyEnumNumericMapper implements Converter { +public class OrdinalEnumMapper implements Converter { /** * Generated UID */ private static final long serialVersionUID = -4252074829213730476L; - public static final MyEnumNumericMapper INSTANCE = new MyEnumNumericMapper(); + public static final OrdinalEnumMapper INSTANCE = new OrdinalEnumMapper(); @Override - public MyEnum from(Integer t) { + public OrdinalEnum from(Integer t) { try { - return MyEnum.values()[t]; + return OrdinalEnum.values()[t]; } catch (Exception e) { return null; @@ -58,7 +58,7 @@ public class MyEnumNumericMapper implements Converter { } @Override - public Integer to(MyEnum u) { + public Integer to(OrdinalEnum u) { return u == null ? null : u.ordinal(); } @@ -68,7 +68,7 @@ public class MyEnumNumericMapper implements Converter { } @Override - public Class toType() { - return MyEnum.class; + public Class toType() { + return OrdinalEnum.class; } } diff --git a/jOOQ-test/src/org/jooq/test/_/MyEnum.java b/jOOQ-test/src/org/jooq/test/_/StringEnum.java similarity index 96% rename from jOOQ-test/src/org/jooq/test/_/MyEnum.java rename to jOOQ-test/src/org/jooq/test/_/StringEnum.java index d54c4a45f9..19a8f6d0af 100644 --- a/jOOQ-test/src/org/jooq/test/_/MyEnum.java +++ b/jOOQ-test/src/org/jooq/test/_/StringEnum.java @@ -35,7 +35,7 @@ */ package org.jooq.test._; -public enum MyEnum { +public enum StringEnum { A, B, C } diff --git a/jOOQ-test/src/org/jooq/test/_/MyEnumStringMapper.java b/jOOQ-test/src/org/jooq/test/_/StringEnumMapper.java similarity index 81% rename from jOOQ-test/src/org/jooq/test/_/MyEnumStringMapper.java rename to jOOQ-test/src/org/jooq/test/_/StringEnumMapper.java index 901d12e341..44d0aede15 100644 --- a/jOOQ-test/src/org/jooq/test/_/MyEnumStringMapper.java +++ b/jOOQ-test/src/org/jooq/test/_/StringEnumMapper.java @@ -37,19 +37,19 @@ package org.jooq.test._; import org.jooq.Converter; -public class MyEnumStringMapper implements Converter { +public class StringEnumMapper implements Converter { /** * Generated UID */ - private static final long serialVersionUID = -4252074829213730476L; + private static final long serialVersionUID = -4252074829213730476L; - public static final MyEnumStringMapper INSTANCE = new MyEnumStringMapper(); + public static final StringEnumMapper INSTANCE = new StringEnumMapper(); @Override - public MyEnum from(String t) { + public StringEnum from(String t) { try { - return MyEnum.valueOf(t); + return StringEnum.valueOf(t); } catch (Exception e) { return null; @@ -57,7 +57,7 @@ public class MyEnumStringMapper implements Converter { } @Override - public String to(MyEnum u) { + public String to(StringEnum u) { return u == null ? null : u.name(); } @@ -67,7 +67,7 @@ public class MyEnumStringMapper implements Converter { } @Override - public Class toType() { - return MyEnum.class; + public Class toType() { + return StringEnum.class; } } diff --git a/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/TMappedTypes.java b/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/TMappedTypes.java index 27856f4add..53a675ff23 100644 --- a/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/TMappedTypes.java +++ b/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/TMappedTypes.java @@ -10,7 +10,7 @@ package org.jooq.test.hsqldb.generatedclasses.tables; comments = "This class is generated by jOOQ") public class TMappedTypes extends org.jooq.impl.UpdatableTableImpl { - private static final long serialVersionUID = -597946068; + private static final long serialVersionUID = 1206625875; /** * The singleton instance of PUBLIC.T_MAPPED_TYPES @@ -50,12 +50,12 @@ public class TMappedTypes extends org.jooq.impl.UpdatableTableImpl DEFAULT_ENUM_ORDINAL = createField("DEFAULT_ENUM_ORDINAL", org.jooq.impl.SQLDataType.INTEGER, this); + public final org.jooq.TableField DEFAULT_ENUM_ORDINAL = createField("DEFAULT_ENUM_ORDINAL", org.jooq.impl.SQLDataType.INTEGER.asConvertedDataType(new org.jooq.test._.OrdinalEnumMapper()), this); /** * An uncommented item */ - public final org.jooq.TableField DEFAULT_ENUM_NAME = createField("DEFAULT_ENUM_NAME", org.jooq.impl.SQLDataType.VARCHAR, this); + public final org.jooq.TableField DEFAULT_ENUM_NAME = createField("DEFAULT_ENUM_NAME", org.jooq.impl.SQLDataType.VARCHAR.asConvertedDataType(new org.jooq.test._.StringEnumMapper()), this); /** * An uncommented item diff --git a/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/records/TMappedTypesRecord.java b/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/records/TMappedTypesRecord.java index 269ce9af44..e1444b824d 100644 --- a/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/records/TMappedTypesRecord.java +++ b/jOOQ-test/src/org/jooq/test/hsqldb/generatedclasses/tables/records/TMappedTypesRecord.java @@ -10,7 +10,7 @@ package org.jooq.test.hsqldb.generatedclasses.tables.records; comments = "This class is generated by jOOQ") public class TMappedTypesRecord extends org.jooq.impl.UpdatableRecordImpl { - private static final long serialVersionUID = 1736776678; + private static final long serialVersionUID = 242486662; /** * An uncommented item @@ -61,28 +61,28 @@ public class TMappedTypesRecord extends org.jooq.impl.UpdatableRecordImpl extends Serializable { */ DataType asEnumDataType(Class enumDataType); + /** + * Retrieve the data type for a given converter + */ + DataType asConvertedDataType(Converter converter); + /** * Retrieve the dialect-specific type name associated with this data type */ diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java b/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java index b4e463cfe7..88e4e0b086 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java @@ -49,6 +49,7 @@ import java.util.Map; import org.jooq.ArrayRecord; import org.jooq.Configuration; +import org.jooq.Converter; import org.jooq.DataType; import org.jooq.EnumType; import org.jooq.MasterDataType; @@ -192,7 +193,7 @@ public abstract class AbstractDataType implements DataType { } @Override - public final int getSQLType() { + public /* final */ int getSQLType() { if (type == Blob.class) { return Types.BLOB; } @@ -310,7 +311,7 @@ public abstract class AbstractDataType implements DataType { } @Override - public final String getCastTypeName(Configuration configuration, int precision, int scale) { + public /* final */ String getCastTypeName(Configuration configuration, int precision, int scale) { String result = getCastTypeName(configuration); if (precision != 0) { @@ -354,13 +355,18 @@ public abstract class AbstractDataType implements DataType { return new DefaultDataType(dialect, enumDataType, typeName, castTypeName); } + @Override + public final DataType asConvertedDataType(Converter converter) { + return new ConvertedDataType(this, converter); + } + @Override public final SQLDialect getDialect() { return dialect; } @Override - public final T convert(Object object) { + public /* final */ T convert(Object object) { return Convert.convert(object, type); } diff --git a/jOOQ/src/main/java/org/jooq/impl/ConvertedDataType.java b/jOOQ/src/main/java/org/jooq/impl/ConvertedDataType.java new file mode 100644 index 0000000000..f52391c093 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ConvertedDataType.java @@ -0,0 +1,93 @@ +/** + * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.impl; + +import org.jooq.Configuration; +import org.jooq.Converter; +import org.jooq.DataType; + +/** + * A DataType used for converted types using {@link Converter} + * + * @author Lukas Eder + */ +class ConvertedDataType extends AbstractDataType { + + /** + * Generated UID + */ + private static final long serialVersionUID = -2321926692580974126L; + + private final DataType delegate; + private final Converter converter; + + ConvertedDataType(DataType delegate, Converter converter) { + super(null, null, + converter.toType(), + delegate.getTypeName(), + delegate.getCastTypeName()); + + this.delegate = delegate; + this.converter = converter; + + DataTypes.registerConverter(converter.toType(), converter); + } + + @Override + public int getSQLType() { + return delegate.getSQLType(); + } + + @Override + public String getTypeName(Configuration configuration) { + return delegate.getTypeName(configuration); + } + + @Override + public String getCastTypeName(Configuration configuration) { + return delegate.getCastTypeName(configuration); + } + + @Override + public String getCastTypeName(Configuration configuration, int precision, int scale) { + return delegate.getCastTypeName(configuration, precision, scale); + } + + @Override + public U convert(Object object) { + return converter.from(delegate.convert(converter.to((U) object))); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DataTypes.java b/jOOQ/src/main/java/org/jooq/impl/DataTypes.java index 7257f6e071..38a442737f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DataTypes.java +++ b/jOOQ/src/main/java/org/jooq/impl/DataTypes.java @@ -40,9 +40,6 @@ import java.util.Map; import org.jooq.Converter; import org.jooq.DataType; -import org.jooq.Field; -import org.jooq.Record; -import org.jooq.Result; import org.jooq.exception.DataTypeException; /** @@ -52,7 +49,6 @@ import org.jooq.exception.DataTypeException; */ public final class DataTypes { - private static final Object INIT_LOCK = new Object(); private static final Map, Converter> CONVERTERS = new HashMap, Converter>(); // ------------------------------------------------------------------------ @@ -105,17 +101,6 @@ public final class DataTypes { } } - /** - * Register a Converter for a field - *

- * This registers a converter for a specific field in your generated schema. - * This converter will be used by jOOQ to convert database types to your own - * custom types and deliver those types in {@link Record} and {@link Result} - */ - public static final synchronized void registerConverter(Field field, Converter converter) { - - } - // ------------------------------------------------------------------------ // XXX: Internal API // ------------------------------------------------------------------------ diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java index faae49b227..2cad4f1f4c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java @@ -54,6 +54,7 @@ import java.util.Arrays; import org.jooq.ArrayRecord; import org.jooq.BindContext; import org.jooq.Configuration; +import org.jooq.Converter; import org.jooq.EnumType; import org.jooq.MasterDataType; import org.jooq.SQLDialect; @@ -97,6 +98,13 @@ class DefaultBindContext extends AbstractBindContext { protected final BindContext bindValue0(Object value, Class type) throws SQLException { SQLDialect dialect = configuration.getDialect(); + // [#650] Check first, if we have a converter for the supplied type + Converter converter = DataTypes.converter(type); + if (converter != null) { + value = ((Converter) converter).to(value); + type = converter.fromType(); + } + if (log.isTraceEnabled()) { if (value != null && value.getClass().isArray() && value.getClass() != byte[].class) { log.trace("Binding variable " + peekIndex(), Arrays.asList((Object[]) value) + " (" + type + ")"); diff --git a/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java b/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java index d1ff0e3aab..019946462f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java +++ b/jOOQ/src/main/java/org/jooq/impl/FieldTypeHelper.java @@ -60,6 +60,7 @@ import java.util.Map; import org.jooq.ArrayRecord; import org.jooq.Configuration; +import org.jooq.Converter; import org.jooq.DataType; import org.jooq.EnumType; import org.jooq.ExecuteContext; @@ -284,7 +285,20 @@ public final class FieldTypeHelper { throws SQLException { Class type = field.getType(); - return getFromResultSet(ctx, type, index); + Class actual = type; + + Converter converter = DataTypes.converter(type); + if (converter != null) { + actual = converter.fromType(); + } + + Object result = getFromResultSet(ctx, actual, index); + + if (converter != null) { + result = ((Converter) converter).from(result); + } + + return (T) result; } @SuppressWarnings("unchecked")