Giter Club home page Giter Club logo

Comments (11)

csummers avatar csummers commented on July 24, 2024 1

Glad you got it working.

This ends up being an edge case because the syntax for an interval in SQL is strange, but the way to think about it is to realize that Postgresql treats an interval as a data type. So, the easy work-around in HugSQL is to pass in a string and then cast it. However, you could also create an instance of the PGInterval object from the JDBC driver and pass it in as a normal HugSQL value parameter. (See https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/util/PGInterval.html)

from hugsql.

csummers avatar csummers commented on July 24, 2024

I don't see anything immediately suspect here. Can you look at your postgresql log for hints? Are you sure that the error is from the interval? If so, what does the resulting query look like in your postgresql log?

from hugsql.

csummers avatar csummers commented on July 24, 2024

So, I'm not exactly sure why the above doesn't work, but I confirmed that it does not.

I think it has to do with what Postgresql considers an interval data type and what the JDBC driver is ok with. You can get it to work by casting a string like so:

(hugsql/db-run db "select now() - :recent::interval as test" {:recent "10 days"})
;=> ({:test #inst "2016-03-15T20:42:29.773498000-00:00"})

(You will need to be on HugSQL 0.4.5 to use this pg cast syntax as a suffix on the HugSQL parameter, since 0.4.5 added support for this case.)

from hugsql.

csummers avatar csummers commented on July 24, 2024

Yes, so this looks to be related to Postgresql's JDBC. See this StackOverflow post that has a few other suggestions. For instance, if you are only working in days, you can put a 1 day interval in the query and then pass in a day multiplier as an integer.

from hugsql.

csummers avatar csummers commented on July 24, 2024

@kanwei Did the solution provided above work for you?

from hugsql.

csummers avatar csummers commented on July 24, 2024

Closing this, but feel free to re-open if the above solution with casting doesn't work out.

from hugsql.

kanwei avatar kanwei commented on July 24, 2024

The '1 days' * n worked, but not the :recent::interval (still same error and I tried it before). Are you sure the latter works for you?

from hugsql.

csummers avatar csummers commented on July 24, 2024

Yes, the example above was a run on Postgresql. Did you pass in "10 days" as the parameter?

from hugsql.

lukaszkorecki avatar lukaszkorecki commented on July 24, 2024

I'm running into this issue as well - I've created a small test repo:

https://github.com/lukaszkorecki/hugsql-bug/

The relevant queries are here:

https://github.com/lukaszkorecki/hugsql-bug/blob/d91bcd95e1d1ceb4af680e9ae4d37744b32efad3/src/hugsql_interval_bug/test.sql#L19-L24

And here's a small test case:

https://github.com/lukaszkorecki/hugsql-bug/blob/d91bcd95e1d1ceb4af680e9ae4d37744b32efad3/test/hugsql_interval_bug/core_test.clj#L17-L28

The resulting error is:

ERROR in (a-test) (QueryExecutorImpl.java:2505)
expected: (= 3 (core/find-widgets-broken conn {:added-since "1 day"}))
  actual: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
  Position: 72
 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2505)
    org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:2241)
    org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java:310)
    org.postgresql.jdbc.PgStatement.executeInternal (PgStatement.java:447)
    org.postgresql.jdbc.PgStatement.execute (PgStatement.java:368)
    org.postgresql.jdbc.PgPreparedStatement.executeWithFlags (PgPreparedStatement.java:158)
    org.postgresql.jdbc.PgPreparedStatement.execute (PgPreparedStatement.java:147)
    com.zaxxer.hikari.pool.ProxyPreparedStatement.execute (ProxyPreparedStatement.java:44)
    com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute (HikariProxyPreparedStatement.java:-1)
    next.jdbc.result_set$stmt__GT_result_set.invokeStatic (result_set.clj:509)
    next.jdbc.result_set$stmt__GT_result_set.invoke (result_set.clj:504)
    next.jdbc.result_set$eval1575$fn__1583.invoke (result_set.clj:628)
    next.jdbc.protocols$eval1003$fn__1034$G__994__1043.invoke (protocols.clj:33)
    next.jdbc.result_set$eval1611$fn__1616.invoke (result_set.clj:688)
    next.jdbc.protocols$eval1003$fn__1034$G__994__1043.invoke (protocols.clj:33)
    next.jdbc$execute_BANG_.invokeStatic (jdbc.clj:202)
    next.jdbc$execute_BANG_.invoke (jdbc.clj:189)
    hugsql.adapter.next_jdbc.HugsqlAdapterNextJdbc.query (next_jdbc.clj:20)

from hugsql.

csummers avatar csummers commented on July 24, 2024

@lukaszkorecki Thanks for the detailed report. In this instance, you don't need the interval before the parameter. The idea is that you are casting a string to an interval with the ::interval, so the interval before the string is not needed:

diff --git a/src/hugsql_interval_bug/test.sql b/src/hugsql_interval_bug/test.sql
index 6ea0ee5..0ddd830 100644
--- a/src/hugsql_interval_bug/test.sql
+++ b/src/hugsql_interval_bug/test.sql
@@ -21,4 +21,4 @@ select * from widgets where created_at >= current_timestamp - interval '1 day';
 
 -- :name find-widgets-broken :?
 
-select * from widgets where created_at >= current_timestamp - interval :added-since::interval
+select * from widgets where created_at >= current_timestamp - :added-since::interval

from hugsql.

lukaszkorecki avatar lukaszkorecki commented on July 24, 2024

@csummers got it now, thanks 👍 if that makes sense I can make a PR in the documentation to clarify the behavior, it wasn't that obvious (to me) when I read the part about casting passed in parameters.

from hugsql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.