sql

All things SQL and JDBC...
Timofey Sitnikov 2021-06-05T13:23:23.086700Z

Good morning, I am trying to figure out how to insert data into DB, with this code:

(jdbc/insert! db "auth_token" {:id  "35cf7140-b2d4-4b5d-9067-70fdae337a99"
                                 :account-id  "ffffffff-ffff-ffff-ffff-000000000100"
                                 :token  "7e58bcb3-198c-4884-998a-d3df08f1cb73"
                                 :email "<mailto:tony@example.com|tony@example.com>"
                                 :expires-at  "2021-06-06T08:57:14.544792"}))
And I get the error:
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2553).
ERROR: syntax error at or near "-"
  Position: 36
Any way to print out the query string to see where there is an issue?

seancorfield 2021-06-05T13:30:34.087300Z

@timofey.sitnikov Because you have kebab-case key names.

seancorfield 2021-06-05T13:30:47.087600Z

Those are not valid in SQL.

seancorfield 2021-06-05T13:30:55.088Z

Are you using next.jdbc?

seancorfield 2021-06-05T13:31:20.088500Z

If so, you can leverage the snake-kebab-opts (if you’re using the latest version of next.jdbc).

Timofey Sitnikov 2021-06-05T14:02:14.090600Z

@seancorfield, ahhh... thank you, that worked. I do use next.jdbc. . How do you use snake-kebab-opts ? I did find it https://github.com/seancorfield/next-jdbc/blob/8d02bd68f06a25300011532fed6dedab731f5990/src/next/jdbc.clj#L401 , but cannot figure out how to use it.

seancorfield 2021-06-05T14:04:05.091100Z

It’s just options. You pass it into next.jdbc calls.

seancorfield 2021-06-05T14:04:40.091800Z

It causes kebab-case keys to be converted to snake_case columns (and tables) and vice versa when result sets come back.

Timofey Sitnikov 2021-06-05T14:09:31.093Z

OK, like so?

(sql/insert! ds :address {:name "A. Person" :email "<mailto:albert@person.org|albert@person.org>"} {:snake-kebab-opts true})

seancorfield 2021-06-05T14:39:59.093300Z

No, it’s an options map.

seancorfield 2021-06-05T14:40:53.094400Z

(sql/insert! ds :address {:name "A. Person" :email "<mailto:albert@person.org|albert@person.org>"} jdbc/snake-kebab-opts)
Although in this case there’s no point: you don’t have any kebab-case keys.

1
seancorfield 2021-06-05T14:42:59.095Z

I guess the docs are not clear enough. Suggestions on how to improve them?

In addition, two pre-built option hash maps are available in next.jdbc, that leverage the camel-snake-kebab library:

snake-kebab-opts -- provides :column-fn, :table-fn, :label-fn, :qualifier-fn, and :builder-fn that will convert Clojure identifiers in :kebab-case to SQL entities in snake_case and will produce result sets with qualified :kebab-case names from SQL entities that use snake_case,
unqualified-snake-kebab-opts -- provides :column-fn, :table-fn, :label-fn, :qualifier-fn, and :builder-fn that will convert Clojure identifiers in :kebab-case to SQL entities in snake_case and will produce result sets with unqualified :kebab-case names from SQL entities that use snake_case.

seancorfield 2021-06-05T14:43:36.095500Z

(this is in Getting Started, just after an example showing an options map with :builder-fn)

seancorfield 2021-06-05T14:49:13.096100Z

Has anyone used next.jdbc/with-logging on the develop branch yet? Any feedback on it?

2021-06-07T11:05:58.123Z

Yeah we've (team of 3) used in in development since the announcement to tap> queries and results. So far it has been a blessing! We tap queries before execution + a promise for the results, which are delivered in the second fn. (That way broken statements which result in exceptions still tap their value). Needed to play around with Datafy to remove those promises from the view, but over all 👍:skin-tone-2:

2021-06-07T16:44:56.125100Z

That would certainly help! I think the exception in the result slot should be fine for my usecase at least

2021-06-07T20:28:42.131600Z

My team uses lein git down just for this feature right now, happy to bump early and let you know how it works out :)

1