【问题标题】:Postgres / JDBC with pgjdbc-ng: Writing EAN type to databasePostgres / JDBC 与 pgjdbc-ng:将 EAN 类型写入数据库
【发布时间】:2020-03-25 10:53:08
【问题描述】:

我正在使用 postgres 12、PGJDBC-NG 0.8.3org.clojure/java.jdbc 0.7.10

我正在尝试将我的代码从使用标准 postgres JDBC 实现 (https://jdbc.postgresql.org/) 切换到 pgjdbc-ng (https://impossibl.github.io/pgjdbc-ng/),以便我可以使用现有 postgres JDBC 所缺乏的侦听/通知功能。

我已设法使基本功能正常工作,但在将 EAN13 类型插入数据库时​​遇到了一些问题(扩展类型:https://www.postgresql.org/docs/9.1/isn.html)。

这是一个示例表:

create extension isn;
create table p (barcode ean13 primary key);
insert into p values ('5023652298064');
select barcode from p;
+-----------------+
| barcode         |
|-----------------|
| 502-365229806-4 |
+-----------------+

现在在我的代码中,我有一个记录 EAN 用于插入 eans:

(defrecord ean [s])

(def ds (doto (PGDataSource.)
          (.setDatabaseUrl "jdbc:pgsql://localhost:5432/web?user=root")))

(jdbc/execute! {:datasource ds}
            ["update products set ean = ? where id = 5242" (db/->ean "5012583002819")])
Execution error (IllegalStateException) at com.impossibl.postgres.types.Type/getParameterFormat (Type.java:318).
type has no supported parameter format: ean13(3767891)

这是我在股票 postgres JDBC 中设置正确类型的代码:

(extend-protocol clojure.java.jdbc/ISQLParameter
  ean
  (set-parameter [val ^PreparedStatement stmt ^long i]
    ; The type of this parameter should be PGObject, which is a wrapper provided by
    ; the postgres JDBC driver for types which does not have a corresponding type in
    ; the JDBC interface.
    (.setObject stmt i (doto (PGobject.)
                         (.setType "ean13")
                         (.setValue (.s val))))))

但我一生都无法弄清楚这如何转化为适用于 PGJDBC-NG 的东西!什么是正确的 ISQLParameter 扩展以使其正常工作?

【问题讨论】:

  • 我正在解决基本相同的问题。你做了什么来解决这个问题?
  • 恐怕最后我意识到我真的不需要监听/通知,所以放弃了。

标签: postgresql jdbc clojure


【解决方案1】:

看起来您可以将 PGobject 复制到您的项目中并正常导入。好像没什么特别的。

【讨论】:

    猜你喜欢
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多