#344 Update low end numeric mappings

This commit is contained in:
Timo Westkämper 2013-02-15 12:09:15 +02:00
parent 686c43465c
commit dc7be3734e
4 changed files with 97 additions and 32 deletions

View File

@ -73,22 +73,6 @@ import com.mysema.query.sql.support.SizeImpl;
public class MetaDataExporter {
private static final Logger logger = LoggerFactory.getLogger(MetaDataExporter.class);
private static final int COLUMN_NAME = 4;
private static final int COLUMN_TYPE = 5;
private static final int COLUMN_SIZE = 7;
private static final int COLUMN_DIGITS = 9;
private static final int COLUMN_NULLABLE = 11;
private static final int CATALOG_NAME = 1;
private static final int SCHEMA_NAME = 2;
private static final int TABLE_NAME = 3;
private final SQLCodegenModule module = new SQLCodegenModule();
@ -235,10 +219,10 @@ public class MetaDataExporter {
}
private void handleColumn(EntityType classModel, String tableName, ResultSet columns) throws SQLException {
String columnName = normalize(columns.getString(COLUMN_NAME));
int columnType = columns.getInt(COLUMN_TYPE);
int columnSize = columns.getInt(COLUMN_SIZE);
int columnDigits = columns.getInt(COLUMN_DIGITS);
String columnName = normalize(columns.getString("COLUMN_NAME"));
int columnType = columns.getInt("DATA_TYPE");
int columnSize = columns.getInt("COLUMN_SIZE");
int columnDigits = columns.getInt("DECIMAL_DIGITS");
String propertyName = namingStrategy.getPropertyName(columnName, classModel);
Class<?> clazz = configuration.getJavaType(columnType, columnSize, columnDigits,
tableName, columnName);
@ -257,11 +241,11 @@ public class MetaDataExporter {
property.addAnnotation(new ColumnImpl(namingStrategy.normalizeColumnName(columnName)));
}
if (validationAnnotations) {
int nullable = columns.getInt(COLUMN_NULLABLE);
int nullable = columns.getInt("NULLABLE");
if (nullable == DatabaseMetaData.columnNoNulls) {
property.addAnnotation(new NotNullImpl());
}
int size = columns.getInt(COLUMN_SIZE);
int size = columns.getInt("COLUMN_SIZE");
if (size > 0 && clazz.equals(String.class)) {
property.addAnnotation(new SizeImpl(0, size));
}
@ -270,10 +254,10 @@ public class MetaDataExporter {
}
private void handleTable(DatabaseMetaData md, ResultSet tables) throws SQLException {
String catalog = tables.getString(CATALOG_NAME);
String schema = tables.getString(SCHEMA_NAME);
String schemaName = normalize(tables.getString(SCHEMA_NAME));
String tableName = normalize(tables.getString(TABLE_NAME));
String catalog = tables.getString("TABLE_CAT");
String schema = tables.getString("TABLE_SCHEM");
String schemaName = normalize(tables.getString("TABLE_SCHEM"));
String tableName = normalize(tables.getString("TABLE_NAME"));
String className = namingStrategy.getClassName(tableName);
EntityType classModel = createEntityType(schemaName,
namingStrategy.normalizeTableName(tableName), className);

View File

@ -114,9 +114,9 @@ public final class JDBCTypeMapping {
return Long.class;
} else if (size > 5) {
return Integer.class;
} else if (size > 3) {
} else if (size > 2) {
return Short.class;
} else if (size > 1) {
} else if (size > 0) {
return Byte.class;
} else {
return Boolean.class;

View File

@ -61,17 +61,20 @@ public class JDBCTypeMappingTest {
public void NumericTypes() {
// 19-...,0 -> Long
// 6-18,0 -> Integer
// 4-5,0 -> Short
// 2-3,0 -> Byte
// 1,0 -> Boolean
// 3-5,0 -> Short
// 1-2,0 -> Byte
// 0 -> Boolean
// 17-...,? -> BigDecimal
// 0-16,? -> Double
assertEquals(typeMapping.get(Types.NUMERIC, 20, 0), Long.class);
assertEquals(typeMapping.get(Types.NUMERIC, 19, 0), Long.class);
assertEquals(typeMapping.get(Types.NUMERIC, 6, 0), Integer.class);
assertEquals(typeMapping.get(Types.NUMERIC, 5, 0), Short.class);
assertEquals(typeMapping.get(Types.NUMERIC, 4, 0), Short.class);
assertEquals(typeMapping.get(Types.NUMERIC, 3, 0), Short.class);
assertEquals(typeMapping.get(Types.NUMERIC, 2, 0), Byte.class);
assertEquals(typeMapping.get(Types.NUMERIC, 1, 0), Boolean.class);
assertEquals(typeMapping.get(Types.NUMERIC, 1, 0), Byte.class);
assertEquals(typeMapping.get(Types.NUMERIC, 17, 2), BigDecimal.class);
assertEquals(typeMapping.get(Types.NUMERIC, 5, 2), Double.class);

View File

@ -0,0 +1,78 @@
package com.mysema.query.sql.postgres;
import java.lang.reflect.Field;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mysema.query.Connections;
import com.mysema.query.sql.JDBCTypeMapping;
import com.mysema.query.sql.PostgresTemplates;
import com.mysema.query.sql.SQLTemplates;
public class PostgresTypesTest {
@Before
public void setUp() throws SQLException, ClassNotFoundException {
Connections.initPostgres();
}
@After
public void tearDown() throws SQLException {
Connections.close();
}
@Test
public void test() throws SQLException, IllegalArgumentException, IllegalAccessException {
SQLTemplates templates = new PostgresTemplates();
Connections.dropTable(templates, "type_tests");
Statement stmt = Connections.getStatement();
stmt.execute("create table type_tests (" +
"_numeric_1 numeric(1), " +
"_numeric_2 numeric(2), " +
"_numeric_3 numeric(3), " +
"_numeric_4 numeric(3), " +
"_smallint smallint, " +
"_integer integer, " +
"_bigint bigint, " +
"_decimal decimal, " +
"_numeric numeric, " +
"_real real, " +
"_double_precision double precision, " +
"_serial serial, " +
"_bigserial bigserial)");
Map<Integer, String> types = new HashMap<Integer, String>();
for (Field field : java.sql.Types.class.getFields()) {
types.put((Integer)field.get(null), field.getName());
}
JDBCTypeMapping jdbcTypeMapping = new JDBCTypeMapping();
DatabaseMetaData metadata = Connections.getConnection().getMetaData();
ResultSet rs = metadata.getColumns(null, null, "type_tests", null);
try {
while (rs.next()) {
System.out.println(rs.getString("COLUMN_NAME"));
System.out.println(types.get(rs.getInt("DATA_TYPE")));
System.out.println(rs.getInt("COLUMN_SIZE"));
System.out.println(rs.getInt("DECIMAL_DIGITS"));
System.out.println(jdbcTypeMapping.get(
rs.getInt("DATA_TYPE"),
rs.getInt("COLUMN_SIZE"),
rs.getInt("DECIMAL_DIGITS")));
System.out.println();
}
} finally {
rs.close();
}
}
}