Fork me on GitHub
#clojure
<
2023-12-23
>
hifumi12307:12:22

Is there a way to set default pragma directives for connections in clojure.java.jdbc? My use case involves using SQLite with foreign key constraints enabled, without having to wrap the body of several functions with

(j/with-open-connection [conn db-spec]
  (j/execute! conn ["PRAGMA foreign_keys = ON"])
  ...)

hifumi12307:12:11

I found a way to do this by using a raw JDBC URI such as jdbc:sqlite:example.db?foreign_keys=on But I would rather use the db-spec map, if possible. Reading the API documentation, I see

DriverManager (preferred):
    :dbtype      (required) a String, the type of the database (the jdbc subprotocol)
    :dbname      (required) a String, the name of the database
    ...
    (others)     (optional) passed to the driver as properties
so it seems that I can probably use
{:dbtype "sqlite"
 :dbname "example.db"
 :foreign_keys "on"}
but this didn’t work, alas.