[#1152] Add <E extends java.lang.Enum<E> & org.jooq.EnumType> E MySQLFactory.enumType(Class<E>, int) for enum reverse lookups of MySQL-specific enums
This commit is contained in:
parent
f095d9aaf1
commit
d429f5a417
@ -36,6 +36,7 @@
|
||||
|
||||
package org.jooq.test;
|
||||
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static org.jooq.impl.Factory.val;
|
||||
import static org.jooq.test.mysql.generatedclasses.Tables.T_BOOK_TO_BOOK_STORE;
|
||||
import static org.jooq.test.mysql.generatedclasses.Tables.T_IDENTITY_PK;
|
||||
@ -75,6 +76,7 @@ import org.jooq.UpdatableTable;
|
||||
import org.jooq.conf.Settings;
|
||||
import org.jooq.test.mysql.generatedclasses.Routines;
|
||||
import org.jooq.test.mysql.generatedclasses.TestFactory;
|
||||
import org.jooq.test.mysql.generatedclasses.enums.TBookStatus;
|
||||
import org.jooq.test.mysql.generatedclasses.enums.T_959JavaKeywords;
|
||||
import org.jooq.test.mysql.generatedclasses.tables.TAuthor;
|
||||
import org.jooq.test.mysql.generatedclasses.tables.TBook;
|
||||
@ -728,4 +730,14 @@ public class jOOQMySQLTest extends jOOQAbstractTest<
|
||||
assertEquals(T_959JavaKeywords.class_, result.get(1));
|
||||
assertEquals(T_959JavaKeywords.public_, result.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMySQLEnumIndex() throws Exception {
|
||||
assertNull(MySQLFactory.enumType(TBookStatus.class, -1));
|
||||
assertNull(MySQLFactory.enumType(TBookStatus.class, 0));
|
||||
assertEquals(TBookStatus.SOLD_OUT, MySQLFactory.enumType(TBookStatus.class, 1));
|
||||
assertEquals(TBookStatus.ORDERED, MySQLFactory.enumType(TBookStatus.class, 2));
|
||||
assertEquals(TBookStatus.ON_STOCK, MySQLFactory.enumType(TBookStatus.class, 3));
|
||||
assertNull(MySQLFactory.enumType(TBookStatus.class, 4));
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,6 +37,7 @@ package org.jooq.util.mysql;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import org.jooq.EnumType;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.SchemaMapping;
|
||||
@ -310,4 +311,58 @@ public class MySQLFactory extends Factory {
|
||||
public static Field<String> password(Field<String> string) {
|
||||
return function("password", String.class, string);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Other utilities
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get a field based {@link EnumType} by its MySQL-specific index
|
||||
* <p>
|
||||
* If your MySQL enum type contains these three values: <code>A, B, C</code>
|
||||
* , then this will be the mapping of indexes to values:
|
||||
* <table border="1">
|
||||
* <tr>
|
||||
* <th>Enum literal as in {@link Enum#name()}</th>
|
||||
* <th>Enum ordinal as in {@link Enum#ordinal()}</th>
|
||||
* <th>MySQL index</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><code>null</code></td>
|
||||
* <td><code>-</code></td>
|
||||
* <td><code>0</code></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><code>A</code></td>
|
||||
* <td><code>0</code></td>
|
||||
* <td><code>1</code></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><code>B</code></td>
|
||||
* <td><code>1</code></td>
|
||||
* <td><code>2</code></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><code>C</code></td>
|
||||
* <td><code>2</code></td>
|
||||
* <td><code>3</code></td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* <p>
|
||||
* See <a
|
||||
* href="dev.mysql.com/doc/refman/5.5/en/enum.html">dev.mysql.com/doc/
|
||||
* refman/5.5/en/enum.html</a> for more details about MySQL enum types
|
||||
*/
|
||||
public static <E extends java.lang.Enum<E> & org.jooq.EnumType> E enumType(Class<E> type, int index) {
|
||||
if (index <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
E[] values = type.getEnumConstants();
|
||||
if (index > values.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return values[index - 1];
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user