Fork me on GitHub
#sql
<
2023-03-06
>
sheluchin14:03:08

I'm trying to read from stdin in order to use DuckDB's https://duckdb.org/docs/data/csv/overview.html functionality:

(jdbc/execute! datasource
               (cond-> (-> (hh/select :*)
                           (hh/from [[:read_csv_auto "/dev/stdin"
                                      [:= :ALL_VARCHAR true]]]))
                                      ; [:= :dateformat "%m-%d-%Y"]]]))
                 true (hh/create-table-as "stdin_test")
                 true (fmt {:inline true}))
but I get an error when trying this:
SQLException IO Error: No files found that match the pattern "/dev/stdin"
    org.duckdb.DuckDBNative.duckdb_jdbc_prepare (DuckDBNative.java:-2)
    org.duckdb.DuckDBPreparedStatement.prepare (DuckDBPreparedStatement.java:106)
    org.duckdb.DuckDBPreparedStatement.<init> (DuckDBPreparedStatement.java:65)
    org.duckdb.DuckDBConnection.prepareStatement (DuckDBConnection.java:60)
    org.duckdb.DuckDBConnection.prepareStatement (DuckDBConnection.java:182)
    next.jdbc.prepare/create (prepare.clj:133)
    next.jdbc.prepare/create (prepare.clj:83)
    next.jdbc.result-set/eval35027/fn--35035 (result_set.clj:920)
    next.jdbc.protocols/eval34109/fn--34110/G--34100--34119 (protocols.clj:33)
    next.jdbc/execute! (jdbc.clj:250)
    next.jdbc/execute! (jdbc.clj:237)
    db/eval36533 (NO_SOURCE_FILE:56)
Not sure if this is a problem with something I'm doing or if it's a limitation/out of scope with next.jdbc. I've tried to execute the SQL string my code generates directly in the DB and in the shell and those options both work.

lucas15:03:27

Hello! We are facing a strange issue with next.jdbc.sql/insert! function when wrapping the spec with next.jdbc/with-logging and next.jdbc/with-options. Depending on the order of execution of wrappers, the next.jdbc.sql/insert! throws an exception. For example:

(defn foo
  []
  (let [db-spec (-> (jdbc/get-datasource {:dbtype "postgresql"
                                          :dbname "postgres"
                                          :user "postgres"
                                          :host "postgres"
                                          :password "sa"
                                          :port 5432})
                    (jdbc/with-options jdbc/unqualified-snake-kebab-opts)
                    (jdbc/with-logging prn prn))]
    (jdbc.sql/insert! db-spec :foo {:id 1 :foo-type "foo"})))
The above throws the following exception:
ERROR: syntax error at or near \"-\"\n  Position: 25
But if you change the order of the wrappers, that is, first jdbc/with-logging and then jdbc/with-options it works just fine. We think it's because the underlying for-insert function used by next.jdbc.sql/insert! , which doesn't take into account the nesting of connectables.

seancorfield16:03:10

Entirely possible it's a bug. Not many people use the wrappers. Can you create an issue on GitHub with this repro case?

lucas16:03:53

Sure thing!