检查 R-DBI 连接对象的 class

checking class of R-DBI connection object

我是rewriting a function that optionally accepts a connection object. The parameter should be verified that it's a valid connection/channel. How do I do this robustly with the DBI package? (Specifically, I'm using odbc包裹。)

我想要能容纳所有 DBI 连接对象的东西,但我只满足于 odbc 连接对象

运行 这样的东西不会产生我知道如何用 inherits().

查询的值
library(DBI)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
class(con)
# [1] "SQLiteConnection"
# attr(,"package")
# [1] "RSQLite"
DBI::dbDisconnect(con)

相比之下,RODBC and poolreturn一个class比我能抢的好(ie,RODBCPool) .

如果有帮助,请参阅此特定 DBI 连接的更多内部信息。

> str(con)
Formal class 'SQLiteConnection' [package "RSQLite"] with 7 slots
  ..@ ptr                :<externalptr> 
  ..@ dbname             : chr ":memory:"
  ..@ loadable.extensions: logi TRUE
  ..@ flags              : int 70
  ..@ vfs                : chr ""
  ..@ ref                :<environment: 0x0000000000000000> 
  ..@ bigint             : chr "integer64"

DBI 规范要求¹所有连接都继承自 DBIConnection。 "new style" 后端都是这样实现的,IIRC:

library(RSQLite)
con <- dbConnect(SQLite())
inherits(con, "DBIConnection")
#> [1] TRUE

Created on 2018-05-18 by the reprex package (v0.2.0).


¹ scratch "requires": 最终会明确要求,https://github.com/r-dbi/DBItest/issues/170