Fork me on GitHub
#sql
<
2018-01-29
>
seancorfield06:01:22

Can I just go on the record and say I hate ORMs? 🙂

rymndhng07:01:38

Clojure definitely helps make the choice to avoid ORMs, because of how well the language emphasizes programming with values

souenzzo16:01:05

How to create a new "database"(`:dbname`)?

souenzzo17:01:05

(let [db-uri "jdbc:"
      _ (Class/forName "org.postgresql.Driver")
      conn (java.sql.DriverManager/getConnection db-uri "foo" "bar")
      stmt (.createStatement conn)
      ]
  (.executeUpdate stmt "CREATE DATABASE foobar"))

seancorfield20:01:58

With MySQL you'd specify :dbname "mysql" which always exists and provide a user/password that has rights to create databases.

seancorfield20:01:34

For the code you have above, I'd do that whole thing via java.jdbc (which is how we do our DB creation).

seancorfield20:01:13

(jdbc/db-do-commands {:dbtype "postgres" :dbname "" :user "foo" :password "bar"} "CREATE DATABASE foobar")
would probably work (empty :dbname)...

seancorfield20:01:49

Yup, confirmed that works as expected (with MySQL, at least).

seancorfield20:01:56

Interesting, PostgreSQL doesn't let you run CREATE DATABASE in a transaction so you have to use this:

(jdbc/db-do-commands {:dbtype "postgres" :dbname "" :user "clojure_test" :password "******"} false "CREATE DATABASE foobar")
-- note the false argument to disable transaction-wrapping!

bja19:01:43

@souenzzo I maintain a lot of SQL applications with nothing more than a runner for sql-based migrations and HoneySQL

bja19:01:06

I like HoneySQL since a lot of the SQL I need is dynamic in nature (which tables to join against and such)

bja19:01:36

but HoneySQL is not an ORM, it's a SQL clause builder using clojure data (maps mostly)

bja19:01:02

the Clojure community is possibly the most hostile towards ORMs that I've encountered, with the three most common solutions being "sql strings with a jdbc wrapper", honeysql (queries represented as data with a query compiler), and yesql/hugsql (sql is stored in sql files on the resource path)

bja19:01:51

although there is no reason you couldn't use a Hibernate or write your own ORM. It's probably a useful exercise to write your own ORM for the insights gained (in particular, why you probably don't need/want one)

seancorfield20:01:56

Interesting, PostgreSQL doesn't let you run CREATE DATABASE in a transaction so you have to use this:

(jdbc/db-do-commands {:dbtype "postgres" :dbname "" :user "clojure_test" :password "******"} false "CREATE DATABASE foobar")
-- note the false argument to disable transaction-wrapping!