问题
When generating JOOQ classes via JOOQ code gen, for each field, there will be a SQLDataType associated with it like below.
public final TableField<EventsRecord, LocalDateTime> CREATED_AT = createField(DSL.name("CREATED_AT"), SQLDataType.LOCALDATETIME(6).nullable(false), this, "");
What's the usage or purpose to have SQLDataType with each generated field? Since we already have a return type and client code is likely to use the this type to do the compile check.
Why we still need to know the actual SQLDataType in generated class/fields?
回答1:
By client type, you probably mean the LocalDateTime
type, i.e. the <T>
type that you will find throughout the jOOQ API. Sure, that's the type you care about, but jOOQ, internally, will care about the org.jooq.DataType
instead. Your example already gives away two ideas why this may be useful:
- There's a precision of 6 fractional digits on
LOCALDATETIME(6)
, which is used (among other things):- In CAST expressions. Try
DSL.cast(inline("2000-01-01 00:00:00"), EVENTS.CREATED_AT)
, - In DDL statements. Try DSLContext.meta(EVENTS). You should see a
CREATE TABLE
statement with the appropriate data type - In the optimistic locking feature, to create modification timestamps with the right precision.
- In CAST expressions. Try
- There's an indication whether the column is nullable, which is used (again among other things):
- In DDL statements, see above
- In the implicit join feature, to decide whether to produce an
INNER JOIN
or aLEFT JOIN
- There are many other properties a
DataType
can have, which would be interesting for jOOQ at runtime including:- Custom data type bindings
- Character set
- Collation
- Converters
- Default value
- Whether it is an identity
- Besides, a
String
is not aString
. For example, it could meanCHAR(2)
,CHAR(5)
,VARCHAR(100)
,CLOB
, which are all quite different things in some dialects.
It would be a shame if your runtime meta model didn't have this information available.
来源:https://stackoverflow.com/questions/64788481/whats-the-usage-of-fields-sqldatatype-in-jooqs-auto-generated-classes