diff --git a/DESCRIPTION b/DESCRIPTION index ae86914..fc088f8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,8 @@ BugReports: http://www.github.com/ropensci/ckanr/issues VignetteBuilder: knitr Imports: httr, - jsonlite + jsonlite, + RPostgreSQL Suggests: roxygen2, testthat, @@ -25,3 +26,5 @@ Suggests: xml2, readxl, maptools +Depends: + DBI diff --git a/R/dbi.R b/R/dbi.R new file mode 100644 index 0000000..b88dfda --- /dev/null +++ b/R/dbi.R @@ -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") + ) +