jOOQ - 如何将列和表传递给函数?
jOOQ - How to pass columns and tables to functions?
在我的数据库中我有一个 table:
CREATE TABLE calibration_factor_pm1
(
sensor_id Integer REFERENCES sensor(id),
factor_a double precision NOT NULL,
factor_b double precision NOT NULL,
time_from_which_factor_is_used timestamp NOT NULL
)
并且由于还有其他一些 table 像这个我想创建一个函数,我可以将有关我想获取的因素(a 或 b)以及从中获取的信息传递给
=29=].
我尝试这样做:
private Double getCalibrationFactor(String sensorId, TableField<Record, TableImpl> factor, TableImpl PM, TableField<Record, TableImpl> sensorIdTableField, TableField<Record, TableImpl> timeFromWhichFactorIsUsed) {
Double calibrationFactorAPM1 = 1.0;
try {
calibrationFactorAPM1 = Double.valueOf(Database.create
.select(factor) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1.FACTOR_A
.from(PM) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1
.where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
.orderBy(timeFromWhichFactorIsUsed.desc())
.fetchOne()
.getValue(0)
.toString());
} catch (NullPointerException e) {
System.out.println("DEBUG: Can't find calibration factor 'a' for this sensor");
e.printStackTrace();
}
}
但是,我在 .where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
行得到 Cannot resolve method 'equal(java.lang.Integer)'
。
如何正确处理这个问题?
@edit
这就是 equal() 所期望的:
我建议在您的方法签名中使用更多抽象类型,特别是因为您似乎知道传递给您的方法的参数的类型。这样您就可以从 jooq 的类型安全功能中受益。
private Double getCalibrationFactor(String sensorId,
Field<Double> factor,
Table<?> PM,
Field<Integer> sensorIdTableField,
Field<Timestamp> timeFromWhichFactorIsUsed) {
Record1<Double> calibrationFactorAPM1 = Database.create
.select(factor) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1.FACTOR_A
.from(PM) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1
.where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
.orderBy(timeFromWhichFactorIsUsed.desc())
.fetchOne();
return calibrationFactorAPM1 == null ? calibrationFactorAPM1.value1() : 1d;
}
您可能还想传递 Integer sensorId
而不是 String sensorId
。
在我的数据库中我有一个 table:
CREATE TABLE calibration_factor_pm1
(
sensor_id Integer REFERENCES sensor(id),
factor_a double precision NOT NULL,
factor_b double precision NOT NULL,
time_from_which_factor_is_used timestamp NOT NULL
)
并且由于还有其他一些 table 像这个我想创建一个函数,我可以将有关我想获取的因素(a 或 b)以及从中获取的信息传递给
=29=].
我尝试这样做:
private Double getCalibrationFactor(String sensorId, TableField<Record, TableImpl> factor, TableImpl PM, TableField<Record, TableImpl> sensorIdTableField, TableField<Record, TableImpl> timeFromWhichFactorIsUsed) {
Double calibrationFactorAPM1 = 1.0;
try {
calibrationFactorAPM1 = Double.valueOf(Database.create
.select(factor) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1.FACTOR_A
.from(PM) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1
.where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
.orderBy(timeFromWhichFactorIsUsed.desc())
.fetchOne()
.getValue(0)
.toString());
} catch (NullPointerException e) {
System.out.println("DEBUG: Can't find calibration factor 'a' for this sensor");
e.printStackTrace();
}
}
但是,我在 .where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
行得到 Cannot resolve method 'equal(java.lang.Integer)'
。
如何正确处理这个问题?
@edit
这就是 equal() 所期望的:
我建议在您的方法签名中使用更多抽象类型,特别是因为您似乎知道传递给您的方法的参数的类型。这样您就可以从 jooq 的类型安全功能中受益。
private Double getCalibrationFactor(String sensorId,
Field<Double> factor,
Table<?> PM,
Field<Integer> sensorIdTableField,
Field<Timestamp> timeFromWhichFactorIsUsed) {
Record1<Double> calibrationFactorAPM1 = Database.create
.select(factor) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1.FACTOR_A
.from(PM) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1
.where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
.orderBy(timeFromWhichFactorIsUsed.desc())
.fetchOne();
return calibrationFactorAPM1 == null ? calibrationFactorAPM1.value1() : 1d;
}
您可能还想传递 Integer sensorId
而不是 String sensorId
。