Guten Tag Lukas Eder,
am Donnerstag, 21. August 2014 um 19:40 schrieben Sie:
> Thank you for your detailed explanations. I must admit that
> suggesting using a CustomField was really just a suggestion. I have
> not actually evaluated that option to see whether it works in all cases, e.g. in yours.
From what I've tried until now I think it should and I already get my
SQL rendered correctly, but there's still something wrong with it and
binding values to it. I don't seem to really understand how
CustomField.toSQL and .bind play together or such and would be very
grateful if your could have a look at my implementation.
What I currently see is that toSQL gets called once with a paramType of
null for some reason, therefore I fallback to INDEXED and generate my
SQL with "?". Afterwards toSQL is called again with paramType INLINED
and I generate my SQL with inlined values. The third call is to bind
itself and I bind according the documentation using statement of the
context, nextIndex etc.
Those calls fit to logging: First the statement gets logged as a
prepared statement with placeholders, afterwards inlined and I guess
the third call to bind is because it gets executed afterwards.
That's what I get logged:
> 21:32:12 DEBUG org.jooq.tools.LoggerListener.debug: Executing query : SELECT
> "meter_record"."id",
> "meter_record"."captured_at"
> FROM "meter_record"
> JOIN "oms_record"
> ON "meter_record"."id" = "oms_record"."meter_record"
> LEFT OUTER JOIN "meter_status_history"
> ON "oms_record"."id" = "meter_status_history"."oms_record"
> WHERE (
> CAST(("meter_record"."captured_at" AT TIME ZONE 'UTC') AS DATE) = CAST(((TIMESTAMP WITH TIME ZONE ?) AT TIME ZONE ?) AS DATE)
> AND "oms_record"."meter" = ?
> AND (
> "meter_status_history"."status" IS NULL
> OR "meter_status_history"."status" = ?
> )
> )
> ORDER BY "meter_record"."captured_at" DESC
> 21:32:15 DEBUG org.jooq.tools.LoggerListener.debug: -> with bind values : SELECT
> "meter_record"."id",
> "meter_record"."captured_at"
> FROM "meter_record"
> JOIN "oms_record"
> ON "meter_record"."id" = "oms_record"."meter_record"
> LEFT OUTER JOIN "meter_status_history"
> ON "oms_record"."id" = "meter_status_history"."oms_record"
> WHERE (
> CAST(("meter_record"."captured_at" AT TIME ZONE 'UTC') AS DATE) = CAST(((TIMESTAMP WITH TIME ZONE '2014-07-03 01:01:23.0') AT TIME ZONE 'UTC') AS DATE)
> AND "oms_record"."meter" = 1
> AND (
> "meter_status_history"."status" IS NULL
> OR "meter_status_history"."status" = 0
> )
> )
> ORDER BY "meter_record"."captured_at" DESC
The problem now is that besides the SQL looks syntactically OK to me,
I get an exception:
> Caused by: org.jooq.exception.DataAccessException: SQL [SELECT
> "meter_record"."id",
> "meter_record"."captured_at"
> FROM "meter_record"
> JOIN "oms_record"
> ON "meter_record"."id" = "oms_record"."meter_record"
> LEFT OUTER JOIN "meter_status_history"
> ON "oms_record"."id" = "meter_status_history"."oms_record"
> WHERE (
> CAST(("meter_record"."captured_at" AT TIME ZONE 'UTC') AS DATE) = CAST(((TIMESTAMP WITH TIME ZONE ?) AT TIME ZONE ?) AS DATE)
> AND "oms_record"."meter" = ?
> AND (
> "meter_status_history"."status" IS NULL
> OR "meter_status_history"."status" = ?
> )
> )
> ORDER BY "meter_record"."captured_at" DESC]; ERROR: syntax error at or near "$1"
> Position: 369
> at org.jooq.impl.Utils.translate(Utils.java:1428) ~[jooq-3.4.1.jar:na]
I guess there's something wrong with my binding, but I don't see the
problem. That's what I do:
> context.statement().setTimestamp( context.nextIndex(), this.ts);
> context.statement().setString( context.nextIndex(), this.tz.getID());
Do you have any idea of what could be the problem? I feel I'm this
close to get what I want... For your interest I attached my class.
Thanks again for any help you can provide!