简体   繁体   English

使用jOOQ生成不带代码生成的SQL时如何使用jOOQ转换器?

[英]How to use jOOQ converter when using jOOQ to generate SQL without Code-genreation?

I am using jOOQ to generate mySql queries without generating code and without using jooq to query database. 我正在使用jOOQ来生成mySql查询,而无需生成代码,也无需使用jooq来查询数据库。

While I create query like this : 当我创建这样的查询时:

String tableName = "sample_table";        
DSLContext create = DSL.using(SQLDialect.MYSQL);
Table<Record> table = DSL.tableByName(tableName);
String sql1 = create.insertInto(table, Arrays.asList(
            DSL.fieldByName("id"),
            DSL.fieldByName("roll_no"),
            DSL.fieldByName("name"),
            DSL.fieldByName("date_enrolled"),
            DSL.fieldByName("sex")
            ))
    .values(
            "1",
            12,
            "Raman",
            new DateTime(),
            Sex.Male
           )
    .getSQL(ParamType.INLINED);

The generated query looks like : 生成的查询如下所示:

insert into `sample_table` (`id`, `roll_no`, `name`, `date_enrolled`, `sex`) values ('1', 12, 'Raman', '2016-04-03T03:55:37.940+05:30', 'Male')

The datetime value generated here is not accepted by MySQL. MySQL不接受此处生成的datetime值。 I explored and found jOOQ converter can be used for custom conversions. 我探索并发现jOOQ转换器可用于自定义转换。 I could find examples on how to use conversion while fetching data , but unable to figure out on how to use convertor while querying. 我可以找到有关如何在获取数据时使用转换的示例,但无法弄清楚如何在查询时使用转换器。 How can I use jOOQ Convertor to generate SQL without Code-generation ? 如何在不使用代码生成的情况下使用jOOQ Convertor生成SQL? Or if there is any better way to generate this query right for sql. 或者是否有更好的方法针对sql生成此查询。

(I'm assuming in this answer that your DateTime type is really org.joda.time.DateTime ) (我在这个答案中假设您的DateTime类型实际上是org.joda.time.DateTime

Using JDBC Timestamp instead 改用JDBC Timestamp

The easiest way forward here would be to manually convert the DateTime data type to a JDBC Timestamp 此处最简单的方法是将DateTime数据类型手动转换为JDBC Timestamp

new Timestamp(new DateTime().getMillis());

Using Converters only occasionally. 仅偶尔使用转换器。

Using Converters and data type Bindings is much more tedious when you are not using the code generator. 当不使用代码生成器时,使用Converters和数据类型的Bindings会更加繁琐。 I would personally advise against it. 我个人不建议这样做。

Still, you can pass a converter indirectly to a bind variable by creating a new DataType that contains the converter: 不过,您可以通过创建一个包含转换器的新DataType将转换器间接传递给绑定变量:

// This will be your converter
class DateTimeConverter implements Converter<Timestamp, DateTime> { ... }

// This is how you create a new data type:
public static final DataType<DateTime> DATETIME = 
    SQLDataType.TIMESTAMP.asConvertedDataType(new DateTimeConverter);

You can now reuse the above DATETIME data type everywhere you construct bind variables, as such: 现在,您可以在构造绑定变量的任何地方重用上述DATETIME数据类型,如下所示:

Field<DateTime> value = DSL.val(new DateTime(), DATETIME);

And insert that instead. 然后插入。

Using Converters regularly. 定期使用转换器。

Much better than wrapping individual bind values all the time explicitly with a DSL.val() call, you could declare the individual fields of your table like this: 与始终通过DSL.val()调用显式地包装单个绑定值相比,您可以这样声明表的各个字段:

Field<Integer> id = field(name("id"), Integer.class);
Field<Integer> rollNo = field(name("roll_no"), Integer.class);
Field<String> name = field(name("name"), String.class);
Field<DateTime> dateEnrolled = field(name("date_enrolled"), DATETIME);
Field<Object> sex = field(name("sex")); // Don't know what this type is in your code

And now, you can use the above fields to insert data using your converter: 现在,您可以使用转换器使用上述字段来插入数据:

create.insertInto(table, 
          id, rollNo, name, dateEnrolled, sex)
      .values(
          "1",
          12,
          "Raman",
          new DateTime(),
          Sex.Male)
      .getSQL(ParamType.INLINED);

As you can see, after a while of doing this, you will save much time by using the code generator instead... 如您所见,经过一段时间后,通过使用代码生成器可以节省很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM