未找到与 JDBI 和 Clojure 匹配的方法

No matching method found with JDBI and Clojure

我有一个简单的 DAO ns:

(ns   alavita.dao
  (:require
    [clojure.tools.logging    :as     log     ]
    [clojure.java.io          :as     io      ]
  )
  (:import
    [org.jdbi.v3.core Jdbi Handle]
  ))

(defn create
  (^Jdbi [^String url]
    (Jdbi/create url))
  (^Jdbi [^String url ^String username ^String password]
    (Jdbi/create url username password)))

(defn open
  ^Handle [^Jdbi jdbi]
  (.open jdbi))

尝试使用库时:

alavita.core=> (def c (dao/create "jdbc:sqlite:/tmp/data.db"))
#'alavita.core/c
alavita.core=> (def h (dao/open c))
#'alavita.core/h
alavita.core=> (.execute h "show tables")

IllegalArgumentException No matching method found: execute for class org.jdbi.v3.core.Handle  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

这有点奇怪,因为 h 肯定有 .execute:

alavita.core=> (cli/all-methods h)
(.attach .begin .close .commit .createBatch .createCall .createQuery .createScript .createUpdate .execute .getConfig .getConnection .getExtensionMethod .getStatementBuilder .getTransactionIsolationLevel .inTransaction .isClosed .isInTransaction .isReadOnly .lambda$attach .lambda$new[=12=] .lambda$useTransaction .lambda$useTransaction .prepareBatch .release .rollback .rollbackToSavepoint .savepoint .select .setConfig .setConfigThreadLocal .setExtensionMethod .setExtensionMethodThreadLocal .setReadOnly .setStatementBuilder .setTransactionIsolation .useTransaction)

不确定它会横向移动到哪里。

打开和创建的类型:

alavita.core=> (type (dao/create "jdbc:sqlite:/tmp/data.db"))
org.jdbi.v3.core.Jdbi
alavita.core=> (type (dao/open c))
org.jdbi.v3.core.Handle

添加反射:

alavita.core=> (set! *warn-on-reflection* true)
true
alavita.core=> (.execute h "show tables")
Reflection warning, /private/var/folders/nr/g50ld9t91c555dzv91n43bg40000gn/T/form-init767780595230125901.clj:1:1 - call to method execute can't be resolved (target class is unknown).

IllegalArgumentException No matching method found: execute for class org.jdbi.v3.core.Handle  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

this port 中所述,这是 Java 如何处理可变参数的问题。您需要先将 var-args 包装在数组中,而不是依赖于 var-arg 行为。

我建议编写一个 Clojure 函数来处理这个问题:

(defn execute [^Handle h, ^String sql, & args]
  (.execute h sql (into-array Object args)))

并改用它。