honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
2020-06-10T00:35:47.065800Z

Ah well. If you're set on using honeysql for ddl, you could probably adapt the definitions from postgres.

avi 2020-06-10T00:36:15.066Z

I’ll take a look. Thank you!

seancorfield 2020-06-10T00:46:07.066600Z

@aviflax Outside of the tests in clojure.java.jdbc, I've never used the DDL helpers -- they just don't add enough.

2020-06-10T01:21:23.067300Z

I don't use them, either. I tend to put ddl in raw sql migration files.

seancorfield 2020-06-10T01:26:27.068100Z

@codonnell Yeah, that's the route we've gone at work, so it's easier to roll a database up for testing to a specific "version".

avi 2020-06-10T01:43:13.071400Z

That makes sense. Thanks! FWIW, I was just hoping to find some kind of “standardized” way to represent a create table statement as a data structure rather than as a string, because I am working on a function that will eventually return something that has generated create table statement in its metadata, and I want to be able to write equality checks in my tests without worrying about string formatting. I whipped this up on the fly, thinking this might work:

[:create-table
 "technologies"
 "name varchar(255) not null primary key"
 "links-main text"]
But my SQL is so rusty I don’t have much confidence in something so simple being actually viable. 😅

seancorfield 2020-06-10T01:45:36.072300Z

I would probably use a hash map: {:create-table :technologies :columns ["name varchar(255) not null primary key", "links_main text"]}

seancorfield 2020-06-10T01:46:37.073400Z

then you can (clojure.string/join ", " ...) on the columns and then it's just (str "CREATE TABLE" (:create-table data) "(" column-string ")")

avi 2020-06-10T01:47:29.073700Z

I like that. More forwards-compatible. Thanks!

kwrooijen 2020-06-10T14:14:25.076400Z

Hello. I'm trying to create a new row, and return the generated primary key (or complete row).

(-> (insert-into :person)
    (values [{:name "foo"}])
    (sql/format)
    (->> (jdbc/execute! *database*)))
This inserts the row as expected, but returns (1) instead of the new row. Is there any way I can get the primary key? I also saw that there was a jdbc/insert! , but this takes a map instead of an SQL string. Meaning I can't use honeysql with this. Any tips?

gon 2020-06-10T15:50:23.077500Z

you can use 'returning id' clause at the end of the insert if your db implements it

kwrooijen 2020-06-10T16:27:45.078700Z

Ah ok, I also need to use jdbc/query instead of jdbc/execute! if I want it to return data

seancorfield 2020-06-10T17:18:15.079600Z

Use :return-keys true

seancorfield 2020-06-10T17:18:38.080300Z

(As an option passed to execute!