Comments (11)
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.
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.
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.
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.
@kanwei Did the solution provided above work for you?
from hugsql.
Closing this, but feel free to re-open if the above solution with casting doesn't work out.
from hugsql.
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.
Yes, the example above was a run on Postgresql. Did you pass in "10 days" as the parameter?
from hugsql.
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:
And here's a small test case:
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.
@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.
@csummers got it now, thanks
from hugsql.
Related Issues (20)
- Taking array as an input
- Not able to update multiple column values
- Optional null parameters HOT 1
- Way to get generated SQL without executing? HOT 1
- Add support for next.jdbc.sql/insert-multi! HOT 1
- Avoid calling apply in 'hugsql.adapter.clojure-java-jdbc' HOT 5
- Incorrect Arities Shown in Docs for def-fb-fns HOT 1
- HugSql fails during AOT compilation with depstar HOT 4
- Switch to newer groupId of next.jdbc dependency in hugsql-adapter-next-jdbc HOT 1
- How to use a function from the database? HOT 2
- Migration fails if a comment is present on line with semicolon HOT 2
- Babashka support HOT 2
- No comments in doc on mobile? HOT 1
- Using classpath resource sql functions breaks cider HOT 3
- Hugsql 0.5.3 still requires SQL files on uberjar compilation (with leiningen) HOT 1
- strange key with jsonb HOT 3
- Should hugsql switch to next.jdbc by default ? HOT 2
- Cannot look up by UUID HOT 2
- Bulk update? HOT 1
- Inline parameters
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hugsql.