[#456] Add runtime support for PRECISION, SCALE, and LENGTH attributes -

Added convenience method DataType.precision(int precision, int scale)
This commit is contained in:
Lukas Eder 2012-12-22 08:39:06 +01:00
parent d7587ee4d7
commit fa7edefc65
16 changed files with 60 additions and 35 deletions

View File

@ -2497,11 +2497,15 @@ public class JavaGenerator extends AbstractGenerator {
sb.append(DefaultDataType.normalise(sqlDataType.getTypeName()));
if (dataType.hasPrecision() && p > 0) {
sb.append(".precision(").append(p).append(")");
}
if (dataType.hasScale() && s > 0) {
sb.append(".scale(").append(s).append(")");
sb.append(".precision(").append(p);
if (dataType.hasScale() && s > 0) {
sb.append(", ").append(s);
}
sb.append(")");
}
if (dataType.hasLength() && l > 0) {
sb.append(".length(").append(l).append(")");
}

View File

@ -11,7 +11,7 @@ package org.jooq.test.h2.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class TBook extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.h2.generatedclasses.tables.records.TBookRecord> {
private static final long serialVersionUID = 328169272;
private static final long serialVersionUID = 729291910;
/**
* The singleton instance of <code>PUBLIC.T_BOOK</code>
@ -64,12 +64,12 @@ public class TBook extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.h2.gen
/**
* The column <code>PUBLIC.T_BOOK.CONTENT_TEXT</code>. Some textual content of the book
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.TBookRecord, java.lang.String> CONTENT_TEXT = createField("CONTENT_TEXT", org.jooq.impl.SQLDataType.CLOB, T_BOOK);
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.TBookRecord, java.lang.String> CONTENT_TEXT = createField("CONTENT_TEXT", org.jooq.impl.SQLDataType.CLOB.length(2147483647), T_BOOK);
/**
* The column <code>PUBLIC.T_BOOK.CONTENT_PDF</code>. Some binary content of the book
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.TBookRecord, byte[]> CONTENT_PDF = createField("CONTENT_PDF", org.jooq.impl.SQLDataType.BLOB, T_BOOK);
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.TBookRecord, byte[]> CONTENT_PDF = createField("CONTENT_PDF", org.jooq.impl.SQLDataType.BLOB.length(2147483647), T_BOOK);
/**
* The column <code>PUBLIC.T_BOOK.REC_VERSION</code>.

View File

@ -9,7 +9,7 @@ package org.jooq.test.h2.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.h2.generatedclasses.tables.records.T_639NumbersTableRecord> {
private static final long serialVersionUID = 1565640409;
private static final long serialVersionUID = 1016283208;
/**
* The singleton instance of <code>PUBLIC.T_639_NUMBERS_TABLE</code>
@ -77,7 +77,7 @@ public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq
/**
* The column <code>PUBLIC.T_639_NUMBERS_TABLE.BIG_DECIMAL</code>.
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("BIG_DECIMAL", org.jooq.impl.SQLDataType.DECIMAL.precision(22).scale(5), T_639_NUMBERS_TABLE);
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("BIG_DECIMAL", org.jooq.impl.SQLDataType.DECIMAL.precision(22, 5), T_639_NUMBERS_TABLE);
/**
* The column <code>PUBLIC.T_639_NUMBERS_TABLE.FLOAT</code>.

View File

@ -9,7 +9,7 @@ package org.jooq.test.h2.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class T_725LobTest extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.h2.generatedclasses.tables.records.T_725LobTestRecord> {
private static final long serialVersionUID = 171668914;
private static final long serialVersionUID = -962962607;
/**
* The singleton instance of <code>PUBLIC.T_725_LOB_TEST</code>
@ -32,7 +32,7 @@ public class T_725LobTest extends org.jooq.impl.UpdatableTableImpl<org.jooq.test
/**
* The column <code>PUBLIC.T_725_LOB_TEST.LOB</code>.
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.T_725LobTestRecord, byte[]> LOB = createField("LOB", org.jooq.impl.SQLDataType.BLOB, T_725_LOB_TEST);
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.T_725LobTestRecord, byte[]> LOB = createField("LOB", org.jooq.impl.SQLDataType.BLOB.length(2147483647), T_725_LOB_TEST);
/**
* No further instances allowed

View File

@ -9,7 +9,7 @@ package org.jooq.test.h2.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class VBook extends org.jooq.impl.TableImpl<org.jooq.test.h2.generatedclasses.tables.records.VBookRecord> {
private static final long serialVersionUID = -1346732880;
private static final long serialVersionUID = 128508086;
/**
* The singleton instance of <code>PUBLIC.V_BOOK</code>
@ -62,12 +62,12 @@ public class VBook extends org.jooq.impl.TableImpl<org.jooq.test.h2.generatedcla
/**
* The column <code>PUBLIC.V_BOOK.CONTENT_TEXT</code>.
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.VBookRecord, java.lang.String> CONTENT_TEXT = createField("CONTENT_TEXT", org.jooq.impl.SQLDataType.CLOB, V_BOOK);
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.VBookRecord, java.lang.String> CONTENT_TEXT = createField("CONTENT_TEXT", org.jooq.impl.SQLDataType.CLOB.length(2147483647), V_BOOK);
/**
* The column <code>PUBLIC.V_BOOK.CONTENT_PDF</code>.
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.VBookRecord, byte[]> CONTENT_PDF = createField("CONTENT_PDF", org.jooq.impl.SQLDataType.BLOB, V_BOOK);
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.VBookRecord, byte[]> CONTENT_PDF = createField("CONTENT_PDF", org.jooq.impl.SQLDataType.BLOB.length(2147483647), V_BOOK);
/**
* The column <code>PUBLIC.V_BOOK.REC_VERSION</code>.

View File

@ -9,7 +9,7 @@ package org.jooq.test.h2.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.h2.generatedclasses.tables.records.XUnusedRecord> {
private static final long serialVersionUID = -1778630516;
private static final long serialVersionUID = 425236155;
/**
* The singleton instance of <code>PUBLIC.X_UNUSED</code>
@ -102,7 +102,7 @@ public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.h2.g
/**
* The column <code>PUBLIC.X_UNUSED.FIELD 737</code>.
*/
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.DECIMAL.precision(25).scale(2), X_UNUSED);
public static final org.jooq.TableField<org.jooq.test.h2.generatedclasses.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.DECIMAL.precision(25, 2), X_UNUSED);
/**
* No further instances allowed

View File

@ -9,7 +9,7 @@ package org.jooq.test.hsqldb.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.hsqldb.generatedclasses.tables.records.T_639NumbersTableRecord> {
private static final long serialVersionUID = -818421797;
private static final long serialVersionUID = -618860148;
/**
* The singleton instance of <code>PUBLIC.T_639_NUMBERS_TABLE</code>
@ -77,7 +77,7 @@ public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq
/**
* The column <code>PUBLIC.T_639_NUMBERS_TABLE.BIG_DECIMAL</code>.
*/
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("BIG_DECIMAL", org.jooq.impl.SQLDataType.DECIMAL.precision(22).scale(5), this);
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("BIG_DECIMAL", org.jooq.impl.SQLDataType.DECIMAL.precision(22, 5), this);
/**
* The column <code>PUBLIC.T_639_NUMBERS_TABLE.FLOAT</code>.

View File

@ -9,7 +9,7 @@ package org.jooq.test.hsqldb.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.hsqldb.generatedclasses.tables.records.XUnusedRecord> {
private static final long serialVersionUID = 868916292;
private static final long serialVersionUID = -575442189;
/**
* The singleton instance of <code>PUBLIC.X_UNUSED</code>
@ -92,7 +92,7 @@ public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.hsql
/**
* The column <code>PUBLIC.X_UNUSED.FIELD 737</code>.
*/
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.DECIMAL.precision(25).scale(2), this);
public final org.jooq.TableField<org.jooq.test.hsqldb.generatedclasses.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.DECIMAL.precision(25, 2), this);
/**
* The column <code>PUBLIC.X_UNUSED.CONNECTION</code>.

View File

@ -9,7 +9,7 @@ package org.jooq.test.oracle.generatedclasses.multi_schema.tables;
@java.lang.SuppressWarnings("all")
public class TBookSale extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.oracle.generatedclasses.multi_schema.tables.records.TBookSaleRecord> {
private static final long serialVersionUID = -2127720052;
private static final long serialVersionUID = -2069651779;
/**
* The singleton instance of <code>MULTI_SCHEMA.T_BOOK_SALE</code>
@ -47,7 +47,7 @@ public class TBookSale extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.or
/**
* The column <code>MULTI_SCHEMA.T_BOOK_SALE.SOLD_FOR</code>.
*/
public final org.jooq.TableField<org.jooq.test.oracle.generatedclasses.multi_schema.tables.records.TBookSaleRecord, java.math.BigDecimal> SOLD_FOR = createField("SOLD_FOR", org.jooq.impl.SQLDataType.NUMERIC.precision(10).scale(2), this);
public final org.jooq.TableField<org.jooq.test.oracle.generatedclasses.multi_schema.tables.records.TBookSaleRecord, java.math.BigDecimal> SOLD_FOR = createField("SOLD_FOR", org.jooq.impl.SQLDataType.NUMERIC.precision(10, 2), this);
/**
* Create a <code>MULTI_SCHEMA.T_BOOK_SALE</code> table reference

View File

@ -9,7 +9,7 @@ package org.jooq.test.oracle.generatedclasses.test.tables;
@java.lang.SuppressWarnings("all")
public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.oracle.generatedclasses.test.tables.records.T_639NumbersTableRecord> {
private static final long serialVersionUID = -1297171678;
private static final long serialVersionUID = -382160559;
/**
* The singleton instance of <code>TEST.T_639_NUMBERS_TABLE</code>
@ -77,7 +77,7 @@ public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq
/**
* The column <code>TEST.T_639_NUMBERS_TABLE.BIG_DECIMAL</code>.
*/
public final org.jooq.TableField<org.jooq.test.oracle.generatedclasses.test.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("BIG_DECIMAL", org.jooq.impl.SQLDataType.NUMERIC.precision(22).scale(5), this);
public final org.jooq.TableField<org.jooq.test.oracle.generatedclasses.test.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("BIG_DECIMAL", org.jooq.impl.SQLDataType.NUMERIC.precision(22, 5), this);
/**
* Create a <code>TEST.T_639_NUMBERS_TABLE</code> table reference

View File

@ -11,7 +11,7 @@ package org.jooq.test.oracle.generatedclasses.test.tables;
@java.lang.SuppressWarnings("all")
public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.oracle.generatedclasses.test.tables.records.XUnusedRecord> {
private static final long serialVersionUID = 1669317032;
private static final long serialVersionUID = -678837737;
/**
* The singleton instance of <code>TEST.X_UNUSED</code>
@ -94,7 +94,7 @@ public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.orac
/**
* The column <code>TEST.X_UNUSED.FIELD 737</code>.
*/
public final org.jooq.TableField<org.jooq.test.oracle.generatedclasses.test.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.NUMERIC.precision(25).scale(2), this);
public final org.jooq.TableField<org.jooq.test.oracle.generatedclasses.test.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.NUMERIC.precision(25, 2), this);
/**
* The column <code>TEST.X_UNUSED.MS_UNUSED_ID_REF</code>.

View File

@ -9,7 +9,7 @@ package org.jooq.test.postgres.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.postgres.generatedclasses.tables.records.T_639NumbersTableRecord> {
private static final long serialVersionUID = -1496351383;
private static final long serialVersionUID = 1869943768;
/**
* The singleton instance of <code>public.t_639_numbers_table</code>
@ -72,7 +72,7 @@ public class T_639NumbersTable extends org.jooq.impl.UpdatableTableImpl<org.jooq
/**
* The column <code>public.t_639_numbers_table.big_decimal</code>.
*/
public final org.jooq.TableField<org.jooq.test.postgres.generatedclasses.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("big_decimal", org.jooq.impl.SQLDataType.NUMERIC.precision(22).scale(5), this);
public final org.jooq.TableField<org.jooq.test.postgres.generatedclasses.tables.records.T_639NumbersTableRecord, java.math.BigDecimal> BIG_DECIMAL = createField("big_decimal", org.jooq.impl.SQLDataType.NUMERIC.precision(22, 5), this);
/**
* The column <code>public.t_639_numbers_table.float</code>.

View File

@ -9,7 +9,7 @@ package org.jooq.test.postgres.generatedclasses.tables;
@java.lang.SuppressWarnings("all")
public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.postgres.generatedclasses.tables.records.XUnusedRecord> {
private static final long serialVersionUID = 1011107005;
private static final long serialVersionUID = 1798808172;
/**
* The singleton instance of <code>public.x_unused</code>
@ -97,7 +97,7 @@ public class XUnused extends org.jooq.impl.UpdatableTableImpl<org.jooq.test.post
/**
* The column <code>public.x_unused.FIELD 737</code>.
*/
public final org.jooq.TableField<org.jooq.test.postgres.generatedclasses.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.NUMERIC.precision(25).scale(2), this);
public final org.jooq.TableField<org.jooq.test.postgres.generatedclasses.tables.records.XUnusedRecord, java.math.BigDecimal> FIELD_737 = createField("FIELD 737", org.jooq.impl.SQLDataType.NUMERIC.precision(25, 2), this);
/**
* Create a <code>public.x_unused</code> table reference

View File

@ -165,12 +165,28 @@ public interface DataType<T> extends Serializable {
* Return a new data type like this, with a new precision value
* <p>
* This will have no effect if {@link #hasPrecision()} is <code>false</code>
* <p>
* This is the same as calling {@link #precision(int, int)} with
* <code>scale == 0</code>
*
* @param precision The new precision value
* @return The new data type
*/
DataType<T> precision(int precision);
/**
* Return a new data type like this, with a new precision and scale value
* <p>
* This will have no effect if {@link #hasPrecision()} is <code>false</code>
* , or if <code>scale > 0</code> and {@link #hasScale()} is
* <code>false</code>
*
* @param precision The new precision value
* @param scale The new scale value
* @return The new data type
*/
DataType<T> precision(int precision, int scale);
/**
* Get the precision of this data type
*

View File

@ -281,11 +281,16 @@ public class DefaultDataType<T> implements DataType<T> {
@Override
public final DataType<T> precision(int p) {
if (precision == p) {
return precision(p, scale);
}
@Override
public final DataType<T> precision(int p, int s) {
if (precision == p && scale == s) {
return this;
}
else if (hasPrecision()) {
return new DefaultDataType<T>(dialect, sqlDataType, type, typeName, castTypeName, p, scale, length);
else if (hasPrecision() && (s == 0 || hasScale())) {
return new DefaultDataType<T>(dialect, sqlDataType, type, typeName, castTypeName, p, s, length);
}
else {
return this;
@ -362,7 +367,7 @@ public class DefaultDataType<T> implements DataType<T> {
// Be sure to reset length, precision, and scale, as those values
// were not registered in the below cache
DataType<?> dataType = TYPES_BY_SQL_DATATYPE[configuration.getDialect().ordinal()].get(length(0).precision(0).scale(0));
DataType<?> dataType = TYPES_BY_SQL_DATATYPE[configuration.getDialect().ordinal()].get(length(0).precision(0, 0));
if (dataType != null) {
return (DataType<T>) dataType;

View File

@ -280,7 +280,7 @@ class Val<T> extends AbstractField<T> implements Param<T> {
context.keyword("cast(");
toSQL(context, getValue(), getType());
context.keyword(" as ")
.sql(type.length(length).precision(precision).scale(scale).getCastTypeName(context))
.sql(type.length(length).precision(precision, scale).getCastTypeName(context))
.sql(")");
}