Fork me on GitHub

What's the best approach for Clojurric parametrized ddl? Say, if I wanted to build CREATE TABLE or CREATE INDEX statements on the fly. Wondering if there's a cleaner route than just going to


DDL isn’t well standardized so I’m not aware of any general purpose solutions but honeysql has a postgresql extension that covers some of the bases, e.g. :


In general Honeysql is easy to extend for custom statements. honeysql-postgres is a good start for postgres (covers the standard stuff (indexes, columns, tables)). It's a little tricky to know which DDL is and isn't transaction-safe in postgres since it sometimes varies between versions.


@bostonaholic I use HugSQL pretty much everywhere for querying, but don't see on the docs how to parametrize DDL - say, sending a table name as a parameter.


have you tried create table :table-name?


I haven’t, so I don’t know if that works or not


About to, needed to start a test project.


Doesn't work for either column or table names.


@ricardo sadly I think that your best bet is (str "create table " (your-escape-fn table-name))


note that this may induce php deja vu


So I may just go that route.


sweet. does it require support on the db driver side?


@pesterhazy Haven't tested it yet, but I'd be doing it with Postgres, so chances of support are high.


Basic CREATE TABLE / DROP TABLE works on everything java.jdbc is tested against (listed in the README). Conditional DDL is not widely supported tho' (e.g., CREATE TABLE IF NOT EXISTS ...)


java.jdbc has helpers for CREATE / DROP table, but not CREATE INDEX so it's down to db-do-commands and strings 😐


Dynamic DDL is a bit of a sore spot in my opinion. I haven't seen a ton of libraries do it well, and my company's solution (and that of migratus) is to stick some .sql on the resource path and load that up when necessary. The best approach I've seen is from honeysql-postgres, but that is postgres-specific given that you can easily compose it. A more generic honeysql-ddl would be do-able, but I doubt the demand is there. I would recommend trying to adapt that over string-banging if you have anything moderately complicated to do.


In particular, dynamic create tables populated by selects or similar.


Any c3p0 connection pool users around? I have an odd situation where it's creating its automatic test table in one DB but not in another.