[#1217] Add EnumConverter as a base type for custom enum converters
[#1218] Add code generation options to generate <customTypes/> referencing a Java type and a Converter
This commit is contained in:
parent
cfd934c9fe
commit
7cb6568873
@ -45,12 +45,28 @@
|
||||
</masterDataTables>
|
||||
<customTypes>
|
||||
<customType>
|
||||
<name>org.jooq.test._.StringEnum</name>
|
||||
<converter>org.jooq.test._.StringEnumMapper</converter>
|
||||
<name>org.jooq.test._.converters.StringEnum</name>
|
||||
<converter>org.jooq.test._.converters.StringEnumConverter</converter>
|
||||
</customType>
|
||||
<customType>
|
||||
<name>org.jooq.test._.OrdinalEnum</name>
|
||||
<converter>org.jooq.test._.OrdinalEnumMapper</converter>
|
||||
<name>org.jooq.test._.converters.StringEnum1</name>
|
||||
<converter>org.jooq.test._.converters.StringEnum1Converter</converter>
|
||||
</customType>
|
||||
<customType>
|
||||
<name>org.jooq.test._.converters.OrdinalEnum</name>
|
||||
<converter>org.jooq.test._.converters.OrdinalEnumConverter</converter>
|
||||
</customType>
|
||||
<customType>
|
||||
<name>org.jooq.test._.converters.OrdinalEnum1</name>
|
||||
<converter>org.jooq.test._.converters.OrdinalEnum1Converter</converter>
|
||||
</customType>
|
||||
<customType>
|
||||
<name>java.util.Date</name>
|
||||
<converter>org.jooq.test._.converters.DateConverter</converter>
|
||||
</customType>
|
||||
<customType>
|
||||
<name>java.util.GregorianCalendar</name>
|
||||
<converter>org.jooq.test._.converters.CalendarConverter</converter>
|
||||
</customType>
|
||||
</customTypes>
|
||||
<enumTypes>
|
||||
@ -85,13 +101,29 @@
|
||||
</enumTypes>
|
||||
<forcedTypes>
|
||||
<forcedType>
|
||||
<name>org.jooq.test._.StringEnum</name>
|
||||
<name>org.jooq.test._.converters.StringEnum</name>
|
||||
<expressions>(?i:(.*?\.)?T_MAPPED_TYPES.DEFAULT_ENUM_NAME)</expressions>
|
||||
</forcedType>
|
||||
<forcedType>
|
||||
<name>org.jooq.test._.OrdinalEnum</name>
|
||||
<name>org.jooq.test._.converters.StringEnum1</name>
|
||||
<expressions>(?i:(.*?\.)?T_MAPPED_TYPES.CUSTOM_ENUM_TEXT)</expressions>
|
||||
</forcedType>
|
||||
<forcedType>
|
||||
<name>org.jooq.test._.converters.OrdinalEnum</name>
|
||||
<expressions>(?i:(.*?\.)?T_MAPPED_TYPES.DEFAULT_ENUM_ORDINAL)</expressions>
|
||||
</forcedType>
|
||||
<forcedType>
|
||||
<name>org.jooq.test._.converters.OrdinalEnum1</name>
|
||||
<expressions>(?i:(.*?\.)?T_MAPPED_TYPES.CUSTOM_ENUM_NUMERIC)</expressions>
|
||||
</forcedType>
|
||||
<forcedType>
|
||||
<name>java.util.Date</name>
|
||||
<expressions>(?i:(.*?\.)?T_MAPPED_TYPES.JAVA_UTIL_DATE)</expressions>
|
||||
</forcedType>
|
||||
<forcedType>
|
||||
<name>java.util.GregorianCalendar</name>
|
||||
<expressions>(?i:(.*?\.)?T_MAPPED_TYPES.JAVA_UTIL_CALENDAR)</expressions>
|
||||
</forcedType>
|
||||
|
||||
<forcedType>
|
||||
<name>BOOLEAN_YES_NO_LC</name>
|
||||
|
||||
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* 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._.converters;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.jooq.Converter;
|
||||
|
||||
public class CalendarConverter implements Converter<Timestamp, GregorianCalendar> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -5060861060926377086L;
|
||||
|
||||
@Override
|
||||
public GregorianCalendar from(Timestamp databaseObject) {
|
||||
GregorianCalendar calendar = (GregorianCalendar) Calendar.getInstance();
|
||||
calendar.setTimeInMillis(databaseObject.getTime());
|
||||
return calendar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp to(GregorianCalendar userObject) {
|
||||
return new Timestamp(userObject.getTime().getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Timestamp> fromType() {
|
||||
return Timestamp.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<GregorianCalendar> toType() {
|
||||
return GregorianCalendar.class;
|
||||
}
|
||||
}
|
||||
@ -33,41 +33,37 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test._;
|
||||
package org.jooq.test._.converters;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
|
||||
import org.jooq.Converter;
|
||||
|
||||
public class StringEnumMapper implements Converter<String, StringEnum> {
|
||||
public class DateConverter implements Converter<Timestamp, Date> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -4252074829213730476L;
|
||||
|
||||
public static final StringEnumMapper INSTANCE = new StringEnumMapper();
|
||||
private static final long serialVersionUID = -5060861060926377086L;
|
||||
|
||||
@Override
|
||||
public StringEnum from(String t) {
|
||||
try {
|
||||
return StringEnum.valueOf(t);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
public Date from(Timestamp databaseObject) {
|
||||
return new Date(databaseObject.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String to(StringEnum u) {
|
||||
return u == null ? null : u.name();
|
||||
public Timestamp to(Date userObject) {
|
||||
return new Timestamp(userObject.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<String> fromType() {
|
||||
return String.class;
|
||||
public Class<Timestamp> fromType() {
|
||||
return Timestamp.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<StringEnum> toType() {
|
||||
return StringEnum.class;
|
||||
public Class<Date> toType() {
|
||||
return Date.class;
|
||||
}
|
||||
}
|
||||
@ -33,9 +33,8 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test._;
|
||||
package org.jooq.test._.converters;
|
||||
|
||||
public enum OrdinalEnum {
|
||||
|
||||
A, B, C
|
||||
}
|
||||
52
jOOQ-test/src/org/jooq/test/_/converters/OrdinalEnum1.java
Normal file
52
jOOQ-test/src/org/jooq/test/_/converters/OrdinalEnum1.java
Normal file
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 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._.converters;
|
||||
|
||||
public enum OrdinalEnum1 {
|
||||
A(2),
|
||||
B(4),
|
||||
C(6);
|
||||
|
||||
private final Integer value;
|
||||
|
||||
private OrdinalEnum1(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -33,42 +33,23 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test._;
|
||||
package org.jooq.test._.converters;
|
||||
|
||||
import org.jooq.Converter;
|
||||
import org.jooq.impl.EnumConverter;
|
||||
|
||||
|
||||
public class OrdinalEnumMapper implements Converter<Integer, OrdinalEnum> {
|
||||
public class OrdinalEnum1Converter extends EnumConverter<Integer, OrdinalEnum1> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -4252074829213730476L;
|
||||
|
||||
public static final OrdinalEnumMapper INSTANCE = new OrdinalEnumMapper();
|
||||
|
||||
@Override
|
||||
public OrdinalEnum from(Integer t) {
|
||||
try {
|
||||
return OrdinalEnum.values()[t];
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
public OrdinalEnum1Converter() {
|
||||
super(Integer.class, OrdinalEnum1.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer to(OrdinalEnum u) {
|
||||
return u == null ? null : u.ordinal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Integer> fromType() {
|
||||
return Integer.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<OrdinalEnum> toType() {
|
||||
return OrdinalEnum.class;
|
||||
public Integer to(OrdinalEnum1 userObject) {
|
||||
return userObject.getValue();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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._.converters;
|
||||
|
||||
import org.jooq.impl.EnumConverter;
|
||||
|
||||
public class OrdinalEnumConverter extends EnumConverter<Integer, OrdinalEnum> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -4252074829213730476L;
|
||||
|
||||
public OrdinalEnumConverter() {
|
||||
super(Integer.class, OrdinalEnum.class);
|
||||
}
|
||||
}
|
||||
@ -33,9 +33,8 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.test._;
|
||||
package org.jooq.test._.converters;
|
||||
|
||||
public enum StringEnum {
|
||||
|
||||
A, B, C
|
||||
}
|
||||
52
jOOQ-test/src/org/jooq/test/_/converters/StringEnum1.java
Normal file
52
jOOQ-test/src/org/jooq/test/_/converters/StringEnum1.java
Normal file
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 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._.converters;
|
||||
|
||||
public enum StringEnum1 {
|
||||
A("X"),
|
||||
B("Y"),
|
||||
C("Z");
|
||||
|
||||
private final String value;
|
||||
|
||||
private StringEnum1(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* 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._.converters;
|
||||
|
||||
import org.jooq.impl.EnumConverter;
|
||||
|
||||
public class StringEnum1Converter extends EnumConverter<String, StringEnum1> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -4252074829213730476L;
|
||||
|
||||
public StringEnum1Converter() {
|
||||
super(String.class, StringEnum1.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String to(StringEnum1 userObject) {
|
||||
return userObject.getValue();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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._.converters;
|
||||
|
||||
import org.jooq.impl.EnumConverter;
|
||||
|
||||
public class StringEnumConverter extends EnumConverter<String, StringEnum> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -4252074829213730476L;
|
||||
|
||||
public StringEnumConverter() {
|
||||
super(String.class, StringEnum.class);
|
||||
}
|
||||
}
|
||||
@ -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<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord> {
|
||||
|
||||
private static final long serialVersionUID = 1206625875;
|
||||
private static final long serialVersionUID = 197285916;
|
||||
|
||||
/**
|
||||
* The singleton instance of PUBLIC.T_MAPPED_TYPES
|
||||
@ -40,32 +40,32 @@ public class TMappedTypes extends org.jooq.impl.UpdatableTableImpl<org.jooq.test
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, java.sql.Timestamp> JAVA_UTIL_DATE = createField("JAVA_UTIL_DATE", org.jooq.impl.SQLDataType.TIMESTAMP, this);
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, java.util.Date> JAVA_UTIL_DATE = createField("JAVA_UTIL_DATE", org.jooq.impl.SQLDataType.TIMESTAMP.asConvertedDataType(new org.jooq.test._.converters.DateConverter()), this);
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, java.sql.Timestamp> JAVA_UTIL_CALENDAR = createField("JAVA_UTIL_CALENDAR", org.jooq.impl.SQLDataType.TIMESTAMP, this);
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, java.util.GregorianCalendar> JAVA_UTIL_CALENDAR = createField("JAVA_UTIL_CALENDAR", org.jooq.impl.SQLDataType.TIMESTAMP.asConvertedDataType(new org.jooq.test._.converters.CalendarConverter()), this);
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, org.jooq.test._.OrdinalEnum> DEFAULT_ENUM_ORDINAL = createField("DEFAULT_ENUM_ORDINAL", org.jooq.impl.SQLDataType.INTEGER.asConvertedDataType(new org.jooq.test._.OrdinalEnumMapper()), this);
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, org.jooq.test._.converters.OrdinalEnum> DEFAULT_ENUM_ORDINAL = createField("DEFAULT_ENUM_ORDINAL", org.jooq.impl.SQLDataType.INTEGER.asConvertedDataType(new org.jooq.test._.converters.OrdinalEnumConverter()), this);
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, org.jooq.test._.StringEnum> DEFAULT_ENUM_NAME = createField("DEFAULT_ENUM_NAME", org.jooq.impl.SQLDataType.VARCHAR.asConvertedDataType(new org.jooq.test._.StringEnumMapper()), this);
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, org.jooq.test._.converters.StringEnum> DEFAULT_ENUM_NAME = createField("DEFAULT_ENUM_NAME", org.jooq.impl.SQLDataType.VARCHAR.asConvertedDataType(new org.jooq.test._.converters.StringEnumConverter()), this);
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, java.lang.Integer> CUSTOM_ENUM_NUMERIC = createField("CUSTOM_ENUM_NUMERIC", org.jooq.impl.SQLDataType.INTEGER, this);
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, org.jooq.test._.converters.OrdinalEnum1> CUSTOM_ENUM_NUMERIC = createField("CUSTOM_ENUM_NUMERIC", org.jooq.impl.SQLDataType.INTEGER.asConvertedDataType(new org.jooq.test._.converters.OrdinalEnum1Converter()), this);
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, java.lang.String> CUSTOM_ENUM_TEXT = createField("CUSTOM_ENUM_TEXT", org.jooq.impl.SQLDataType.VARCHAR, this);
|
||||
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord, org.jooq.test._.converters.StringEnum1> CUSTOM_ENUM_TEXT = createField("CUSTOM_ENUM_TEXT", org.jooq.impl.SQLDataType.VARCHAR.asConvertedDataType(new org.jooq.test._.converters.StringEnum1Converter()), this);
|
||||
|
||||
/**
|
||||
* No further instances allowed
|
||||
|
||||
@ -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<org.jooq.test.hsqldb.generatedclasses.tables.records.TMappedTypesRecord> {
|
||||
|
||||
private static final long serialVersionUID = 242486662;
|
||||
private static final long serialVersionUID = 1171355220;
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
@ -33,84 +33,84 @@ public class TMappedTypesRecord extends org.jooq.impl.UpdatableRecordImpl<org.jo
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public void setJavaUtilDate(java.sql.Timestamp value) {
|
||||
public void setJavaUtilDate(java.util.Date value) {
|
||||
setValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.JAVA_UTIL_DATE, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public java.sql.Timestamp getJavaUtilDate() {
|
||||
public java.util.Date getJavaUtilDate() {
|
||||
return getValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.JAVA_UTIL_DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public void setJavaUtilCalendar(java.sql.Timestamp value) {
|
||||
public void setJavaUtilCalendar(java.util.GregorianCalendar value) {
|
||||
setValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.JAVA_UTIL_CALENDAR, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public java.sql.Timestamp getJavaUtilCalendar() {
|
||||
public java.util.GregorianCalendar getJavaUtilCalendar() {
|
||||
return getValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.JAVA_UTIL_CALENDAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public void setDefaultEnumOrdinal(org.jooq.test._.OrdinalEnum value) {
|
||||
public void setDefaultEnumOrdinal(org.jooq.test._.converters.OrdinalEnum value) {
|
||||
setValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.DEFAULT_ENUM_ORDINAL, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public org.jooq.test._.OrdinalEnum getDefaultEnumOrdinal() {
|
||||
public org.jooq.test._.converters.OrdinalEnum getDefaultEnumOrdinal() {
|
||||
return getValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.DEFAULT_ENUM_ORDINAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public void setDefaultEnumName(org.jooq.test._.StringEnum value) {
|
||||
public void setDefaultEnumName(org.jooq.test._.converters.StringEnum value) {
|
||||
setValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.DEFAULT_ENUM_NAME, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public org.jooq.test._.StringEnum getDefaultEnumName() {
|
||||
public org.jooq.test._.converters.StringEnum getDefaultEnumName() {
|
||||
return getValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.DEFAULT_ENUM_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public void setCustomEnumNumeric(java.lang.Integer value) {
|
||||
public void setCustomEnumNumeric(org.jooq.test._.converters.OrdinalEnum1 value) {
|
||||
setValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.CUSTOM_ENUM_NUMERIC, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public java.lang.Integer getCustomEnumNumeric() {
|
||||
public org.jooq.test._.converters.OrdinalEnum1 getCustomEnumNumeric() {
|
||||
return getValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.CUSTOM_ENUM_NUMERIC);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public void setCustomEnumText(java.lang.String value) {
|
||||
public void setCustomEnumText(org.jooq.test._.converters.StringEnum1 value) {
|
||||
setValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.CUSTOM_ENUM_TEXT, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* An uncommented item
|
||||
*/
|
||||
public java.lang.String getCustomEnumText() {
|
||||
public org.jooq.test._.converters.StringEnum1 getCustomEnumText() {
|
||||
return getValue(org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED_TYPES.CUSTOM_ENUM_TEXT);
|
||||
}
|
||||
|
||||
|
||||
@ -58,6 +58,8 @@ import static org.jooq.test.hsqldb.generatedclasses.tables.TMappedTypes.T_MAPPED
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.sql.Date;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.jooq.ArrayRecord;
|
||||
import org.jooq.DataType;
|
||||
@ -70,8 +72,10 @@ import org.jooq.UDTRecord;
|
||||
import org.jooq.UpdatableTable;
|
||||
import org.jooq.conf.Settings;
|
||||
import org.jooq.impl.Factory;
|
||||
import org.jooq.test._.OrdinalEnum;
|
||||
import org.jooq.test._.StringEnum;
|
||||
import org.jooq.test._.converters.OrdinalEnum;
|
||||
import org.jooq.test._.converters.OrdinalEnum1;
|
||||
import org.jooq.test._.converters.StringEnum;
|
||||
import org.jooq.test._.converters.StringEnum1;
|
||||
import org.jooq.test.hsqldb.generatedclasses.PublicFactory;
|
||||
import org.jooq.test.hsqldb.generatedclasses.Routines;
|
||||
import org.jooq.test.hsqldb.generatedclasses.Sequences;
|
||||
@ -680,13 +684,19 @@ public class jOOQHSQLDBTest extends jOOQAbstractTest<
|
||||
public void testMapper() {
|
||||
jOOQAbstractTest.reset = false;
|
||||
|
||||
// TODO: Run this test twice, once with stmt, once with pstmt
|
||||
TMappedTypesRecord record;
|
||||
GregorianCalendar now = (GregorianCalendar) Calendar.getInstance();
|
||||
|
||||
// Storing a record using fields from a mapper
|
||||
record = create().newRecord(T_MAPPED_TYPES);
|
||||
record.setId(1);
|
||||
record.setValue(T_MAPPED_TYPES.DEFAULT_ENUM_NAME, StringEnum.A);
|
||||
record.setValue(T_MAPPED_TYPES.DEFAULT_ENUM_ORDINAL, OrdinalEnum.B);
|
||||
record.setValue(T_MAPPED_TYPES.CUSTOM_ENUM_TEXT, StringEnum1.C);
|
||||
record.setValue(T_MAPPED_TYPES.CUSTOM_ENUM_NUMERIC, OrdinalEnum1.C);
|
||||
record.setValue(T_MAPPED_TYPES.JAVA_UTIL_DATE, now.getTime());
|
||||
record.setValue(T_MAPPED_TYPES.JAVA_UTIL_CALENDAR, now);
|
||||
|
||||
assertEquals(1, record.store());
|
||||
|
||||
@ -696,5 +706,20 @@ public class jOOQHSQLDBTest extends jOOQAbstractTest<
|
||||
record.refresh();
|
||||
assertEquals(StringEnum.A, record.getValue(T_MAPPED_TYPES.DEFAULT_ENUM_NAME));
|
||||
assertEquals(OrdinalEnum.B, record.getValue(T_MAPPED_TYPES.DEFAULT_ENUM_ORDINAL));
|
||||
assertEquals(StringEnum1.C, record.getValue(T_MAPPED_TYPES.CUSTOM_ENUM_TEXT));
|
||||
assertEquals(OrdinalEnum1.C, record.getValue(T_MAPPED_TYPES.CUSTOM_ENUM_NUMERIC));
|
||||
assertEquals(now.getTime(), record.getValue(T_MAPPED_TYPES.JAVA_UTIL_DATE));
|
||||
assertEquals(now, record.getValue(T_MAPPED_TYPES.JAVA_UTIL_CALENDAR));
|
||||
|
||||
// Check if using custom types in filters works
|
||||
assertEquals(1, create().selectCount()
|
||||
.from(T_MAPPED_TYPES)
|
||||
.where(T_MAPPED_TYPES.DEFAULT_ENUM_NAME.equal(StringEnum.A))
|
||||
.and(T_MAPPED_TYPES.DEFAULT_ENUM_ORDINAL.equal(OrdinalEnum.B))
|
||||
.and(T_MAPPED_TYPES.CUSTOM_ENUM_TEXT.equal(StringEnum1.C))
|
||||
.and(T_MAPPED_TYPES.CUSTOM_ENUM_NUMERIC.equal(OrdinalEnum1.C))
|
||||
.and(T_MAPPED_TYPES.JAVA_UTIL_DATE.equal(now.getTime()))
|
||||
.and(T_MAPPED_TYPES.JAVA_UTIL_CALENDAR.equal(now))
|
||||
.fetchOne(0));
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +51,24 @@ import org.jooq.impl.SQLDataType;
|
||||
* <li>to store user types converting them to database types "TO" the database.
|
||||
* Hence, {@link #toType()} is the user-defined type</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* Note: In order to avoid unwanted side-effects, it is highly recommended (yet
|
||||
* not required) for {@link #from(Object)} and {@link #to(Object)} to be
|
||||
* <strong>reciprocal</strong>. The two methods are reciprocal, if for all
|
||||
* <code>X and Y</code>, it can be said that
|
||||
* <ul>
|
||||
* <li>if <code>Y.equals(converter.from(X))</code>, then
|
||||
* <code>X.equals(converter.to(Y))</code>.</li>
|
||||
* <li>
|
||||
* <code>X.equals(converter.from(converter.to(X)))</code></li>
|
||||
* <li><code>X.equals(converter.to(converter.from(X)))</code></li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* Furthermore, it is recommended (yet not required) that
|
||||
* <ul>
|
||||
* <li><code>converter.from(null) == null</code></li>
|
||||
* <li><code>converter.to(null) == null</code></li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Lukas Eder
|
||||
* @param <T> The database type - i.e. any type available from
|
||||
|
||||
@ -94,7 +94,7 @@ class DefaultBindContext extends AbstractBindContext {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
protected final BindContext bindValue0(Object value, Class<?> type) throws SQLException {
|
||||
SQLDialect dialect = configuration.getDialect();
|
||||
|
||||
|
||||
@ -61,6 +61,7 @@ import java.util.List;
|
||||
import org.jooq.ArrayRecord;
|
||||
import org.jooq.Attachable;
|
||||
import org.jooq.BindContext;
|
||||
import org.jooq.Converter;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.EnumType;
|
||||
import org.jooq.MasterDataType;
|
||||
@ -254,9 +255,17 @@ class Val<T> extends AbstractField<T> implements Param<T>, BindingProvider {
|
||||
/**
|
||||
* Inlining abstraction
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void toSQL(RenderContext context, Object val, Class<?> type) {
|
||||
SQLDialect dialect = context.getDialect();
|
||||
|
||||
// [#650] Check first, if we have a converter for the supplied type
|
||||
Converter<?, ?> converter = DataTypes.converter(type);
|
||||
if (converter != null) {
|
||||
val = ((Converter) converter).to(val);
|
||||
type = converter.fromType();
|
||||
}
|
||||
|
||||
if (context.inline()) {
|
||||
if (val == null) {
|
||||
context.keyword("null");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user