Skip to content

Commit

Permalink
implement DBI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wush978 committed Aug 28, 2015
1 parent c3ad11f commit e611d4a
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ Suggests:
xml2,
readxl,
maptools
Depends:
DBI
234 changes: 234 additions & 0 deletions R/dbi.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
## DBI Interface

setClass("CKANDriver", representation("DBIDriver"))

setMethod("dbUnloadDriver", "CKANDriver",
def = function(drv, ...) invisible(NULL),
valueClass = "logical"
)

setMethod("dbGetInfo", "CKANDriver",
def = function(dbObj, ...) {
cat(sprintf("DBI Interface for CKAN\n"))
}
)

setMethod("summary", "CKANDriver",
def = function(object, ...) dbGetInfo(object, ...)
)

##
## Class: DBIConnection
##
setClass("CKANConnection", representation("DBIConnection", "url" = "character"))

setMethod("initialize", "CKANConnection", function(.Object, url, ...) {
.Object@url <- url
.Object
})

setMethod("dbConnect", "CKANDriver",
def = function(drv, url, ...) new("CKANConnection", url),
valueClass = "CKANConnection"
)

## clone a connection
setMethod("dbConnect", "CKANConnection",
def = function(drv, ...) drv,
valueClass = "CKANConnection"
)

setMethod("dbDisconnect", "CKANConnection",
def = function(conn, ...) invisible(TRUE),
valueClass = "logical"
)

setClass("CKANResult", representation("DBIResult", value = "list"))

setMethod("initialize", "CKANResult", function(.Object, value, ...) {
.Object@value <- value
.Object
})

setMethod("dbSendQuery",
signature(conn = "CKANConnection", statement = "character"),
def = function(conn, statement,...) {
retval <- ds_search_sql(statement, url = conn@url, as = "table")
new("CKANResult", value = retval)
},
valueClass = "CKANResult"
)


setMethod("dbGetQuery",
signature(conn = "CKANConnection", statement = "character"),
def = function(conn, statement, ...) {
retval <- dbSendQuery(conn, statement, ...)
retval@value$records
}
)

setMethod("dbGetException", "CKANConnection",
def = function(conn, ...) {
list()
},
valueClass = "list"
)

setMethod("dbGetInfo", "CKANConnection",
def = function(dbObj, ...) {
cat(sprintf("url: %s\n", dbObj@url))
}
)

setMethod("dbListResults", "CKANConnection",
def = function(conn, ...) dbGetInfo(conn, "rsId")[[1]]
)

setMethod("summary", "CKANConnection",
def = function(object, ...) dbGetInfo(object)
)

## convenience methods
setMethod("dbListTables", "CKANConnection",
def = function(conn, ...){
stop("TODO: dbListTables")
},
valueClass = "character"
)

setMethod("dbReadTable", signature(conn="CKANConnection", name="character"),
def = function(conn, name, ...) {
sql <- sprintf('SELECT * FROM "%s"', name)
dbGetQuery(conn, sql)
},
valueClass = "data.frame"
)

setMethod("dbWriteTable",
signature(conn="CKANConnection", name="character", value="data.frame"),
def = function(conn, name, value, ...){
stop("(dbWriteTable) This interface is read-only currently")
},
valueClass = "logical"
)

setMethod("dbExistsTable",
signature(conn="CKANConnection", name="character"),
def = function(conn, name, ...){
stop("TODO: dbExistsTable")
},
valueClass = "logical"
)

setMethod("dbRemoveTable",
signature(conn="CKANConnection", name="character"),
def = function(conn, name, ...){
stop("(dbRemoveTable) This interface is read-only currently")
},
valueClass = "logical"
)

## return field names (no metadata)
setMethod("dbListFields",
signature(conn="CKANConnection", name="character"),
def = function(conn, name, ...){
stop("TODO: dbListFields")
},
valueClass = "character"
)


setMethod("dbCallProc", "CKANConnection",
def = function(conn, ...) {
stop("TODO: dbCallProc")
}
)

setMethod("dbCommit", "CKANConnection",
def = function(conn, ...) {
stop("TODO: dbCommit")
}
)

setMethod("dbRollback", "CKANConnection",
def = function(conn, ...) {
stop("TODO: dbRollback")
}
)

setMethod("dbClearResult", "CKANResult",
def = function(res, ...) {
TRUE
},
valueClass = "logical"
)

setMethod("fetch", signature(res="CKANResult", n="numeric"),
def = function(res, n, ...){
stop("TODO: fetch")
},
valueClass = "data.frame"
)

setMethod("fetch",
signature(res="CKANResult", n="missing"),
def = function(res, n, ...){
res@value$records
},
valueClass = "data.frame"
)

setMethod("dbGetInfo", "CKANResult",
def = function(dbObj, ...) {
dbObj@value[-1]
},
valueClass = "list"
)

setMethod("dbGetStatement", "CKANResult",
def = function(res, ...){
dbGetInfo(res)$sql
},
valueClass = "character"
)

setMethod("dbListFields",
signature(conn="CKANResult", name="missing"),
def = function(conn, name, ...){
dbGetInfo(res)$fields
},
valueClass = "character"
)

setMethod("dbColumnInfo", "CKANResult",
def = function(res, ...) dbListFields(res, ...),
valueClass = "data.frame"
)

setMethod("dbGetRowsAffected", "CKANResult",
def = function(res, ...) stop("TODO: dbGetRowsAffected"),
valueClass = "numeric"
)

setMethod("dbGetRowCount", "CKANResult",
def = function(res, ...) nrow(res@value$records),
valueClass = "numeric"
)

setMethod("dbHasCompleted", "CKANResult",
def = function(res, ...) TRUE,
valueClass = "logical"
)

setMethod("dbGetException", "CKANResult",
def = function(conn, ...){
list()
},
valueClass = "list" ## TODO: should be a DBIException?
)

setMethod("summary", "CKANResult",
def = function(object, ...) stop("TODO: summary.CKANResult")
)

0 comments on commit e611d4a

Please sign in to comment.