What's the usage of field's SQLDataType in JOOQ's auto generated classes

﹥>﹥吖頭↗ 提交于 2020-12-13 04:53:07

问题


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.
  • 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 a LEFT 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 a String. For example, it could mean CHAR(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!