简体   繁体   English

使用jOOQ,如何通过名称获取列的数据类型

[英]Using jOOQ, how to get data type of a column by name

I am trying to get the data type of a field by name. 我正在尝试通过名称获取字段的数据类型。 I found the method getDataType which works great if I know the field (eg MY_TABLE.MY_COLUMN_NAME.getDataType()). 我发现方法getDataType可以很好地工作,如果我知道该字段(例如MY_TABLE.MY_COLUMN_NAME.getDataType())。

But I want to find it by field name (eg aDSL.fieldByName("my_column_name").getDataType() ) where aDSL is of type DSLContext . 但是我想通过字段名称(例如aDSL.fieldByName("my_column_name").getDataType() )找到它,其中aDSL是DSLContext类型。

I want this because I am building a generic sort function on a collection used from the result set and I'd prefer to not rely on a developer to pass in the right data type for my comparator. 我之所以这样,是因为我正在基于结果集使用的集合上构建通用的排序函数,并且我不希望不依赖开发人员为我的比较器传递正确的数据类型。

Any suggestions? 有什么建议么?

The types are available from Result or Record via: 这些类型可通过以下方式从“ Result或“ Record

Class<?> type1 = result.field("MY_COLUMN").getType()
Class<?> type2 = record.field("MY_COLUMN").getType()

See: 看到:

But most standard data types are Comparable anyway ( I'm looking at the use-case in your answer ), so why not just use Java 8's new Comparator.naturalOrder() and perhaps Comparator.reverseOrder() 但是大多数标准数据类型仍然是Comparable的( 我正在看你的答案的用例 ),所以为什么不只使用Java 8的新Comparator.naturalOrder()甚至可能使用Comparator.reverseOrder()

I was building a custom Comparator (I took my result set and converted it to a List, storing the Record - among other things - so that I could do all manner of manipulation on it, sorting, filtering, etc). 我正在构建一个自定义的Comparator (我将结果集转换为列表,存储了Record以及其他内容,以便可以对其进行各种操作,排序,过滤等)。 In order to build the Comparator, I needed the type: 为了构建比较器,我需要输入以下类型:

switch (R1.dataRecord.getValue( element ).getClass().toString())
{
   case "class java.lang.Long":
      compareResult = ( (Long) R1.dataRecord.getValue( element ) ).compareTo( (Long) R2.dataRecord.getValue( element ) );
      if (compareResult != 0)
           return compareResult * compareOrder;
           break;
   case "class java.lang.String":
      compareResult = ( (String) R1.dataRecord.getValue( element ) ).compareTo( (String) R2.dataRecord.getValue( element ) );
      if (compareResult != 0)
           return compareResult * compareOrder;
      break;
    ... etc ...
}

I was able to get the type from the record. 我能够从记录中获取类型。

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

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