Changeset: 19529fe14374 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=19529fe14374
Modified Files:
        clients/R/MonetDB.R/DESCRIPTION
        clients/R/MonetDB.R/NEWS
        clients/R/MonetDB.R/R/monetdb.R
        clients/R/MonetDB.R/man/mc.Rd
        clients/R/db.tests/monetdb.test.R
Branch: default
Log Message:

R Connector: Preparation for 0.9.2 and code cleanup


diffs (truncated from 1681 to 300 lines):

diff --git a/clients/R/MonetDB.R/DESCRIPTION b/clients/R/MonetDB.R/DESCRIPTION
--- a/clients/R/MonetDB.R/DESCRIPTION
+++ b/clients/R/MonetDB.R/DESCRIPTION
@@ -1,6 +1,6 @@
 Package: MonetDB.R
 Version: 0.9.2
-Date: 2013-03-18
+Date: 2013-04-16
 Title: Connect MonetDB to R
 Authors@R: c(person("Hannes Muehleisen", role = c("aut", "cre"),email = 
"[email protected]"),
        person("Thomas Lumley", role = "ctb"),
diff --git a/clients/R/MonetDB.R/NEWS b/clients/R/MonetDB.R/NEWS
--- a/clients/R/MonetDB.R/NEWS
+++ b/clients/R/MonetDB.R/NEWS
@@ -3,6 +3,11 @@ 0.9.2
 - new options monetdb.sequential, monetdb.debug.query and monetdb.debug.mapi
 - debug output now uses the message() function
 - reinstated warning about monetdb:// db urls, we do not like them
+- source code cleanup
+- dbListTables() now hides system tables per default, also got options to 
include the
+  schema name and quoting in the result (sys_tables, schema_names and quote)
+- fixed various TODO's in the code to get closer to 1.0 (exciting)
+- fixed a bug when the error identifier sent by MonetDB was non-numeric
 
 0.9.1
 - dbGetInfo() now supported on MonetDBConnection (dplyr compatibility)
diff --git a/clients/R/MonetDB.R/R/monetdb.R b/clients/R/MonetDB.R/R/monetdb.R
--- a/clients/R/MonetDB.R/R/monetdb.R
+++ b/clients/R/MonetDB.R/R/monetdb.R
@@ -4,337 +4,377 @@ require(digest)
 C_LIBRARY <- "MonetDB.R"
 
 .onLoad <- function(lib, pkg) {
-       library.dynam( C_LIBRARY, pkg, lib )
-       .Call("mapiInit",PACKAGE=C_LIBRARY)
+  library.dynam( C_LIBRARY, pkg, lib )
+  .Call("mapiInit", PACKAGE=C_LIBRARY)
 }
 
 # Make S4 aware of S3 classes
-setOldClass(c("sockconn","connection","monetdb_mapi_conn"))
+setOldClass(c("sockconn", "connection", "monetdb_mapi_conn"))
 
 ### MonetDBDriver
 setClass("MonetDBDriver", representation("DBIDriver"))
 
 # allow instantiation of this driver with MonetDB to allow existing programs 
to work
 MonetR <- MonetDB <- MonetDBR <- MonetDB.R <- function() {
-       new("MonetDBDriver")
+  new("MonetDBDriver")
 }
 
 setMethod("dbGetInfo", "MonetDBDriver", def=function(dbObj, ...)
-                       list(name="MonetDBDriver", 
-                                       driver.version="0.9.2",
-                                       DBI.version="0.2-7",
-                                       client.version=NA,
-                                       max.connections=NA)
+  list(name="MonetDBDriver", 
+       driver.version="0.9.2", 
+       DBI.version="0.2-7", 
+       client.version=NA, 
+       max.connections=NA)
 )
 
 # shorthand for connecting to the DB, very handy, e.g. dbListTables(mc("acs"))
-mc <- function(dbname="demo", user="monetdb", password="monetdb", 
host="localhost",port=50000L, timeout=86400L, wait=FALSE,language="sql",...) {
-       
dbConnect(MonetDB.R(),dbname,user,password,host,port,timeout,wait,language,...)
+mc <- function(dbname="demo", user="monetdb", password="monetdb", 
host="localhost", port=50000L, 
+  timeout=86400L, wait=FALSE, language="sql", ...) {
+  
+  dbConnect(MonetDB.R(), dbname, user, password, host, port, timeout, wait, 
language, ...)
 }
 
-# TODO: document
-mq <- function(db,query,...) {
-  conn <- mc(db,...)
-  res <- dbGetQuery(conn,query)
+mq <- function(dbname, query, ...) {
+  conn <- mc(dbname, ...)
+  res <- dbGetQuery(conn, query)
   dbDisconnect(conn)
   return(res)
 }
 
-setMethod("dbConnect", "MonetDBDriver", def=function(drv,dbname="demo", 
user="monetdb", password="monetdb", host="localhost",port=50000L, 
timeout=86400L, wait=FALSE,language="sql",...,url="") {
-                       if (substring(url,1,10) == "monetdb://") {
-                               dbname <- url
-                       }
-                       port <- as.integer(port)
-                       timeout <- as.integer(timeout)
-                       
-                       if (substring(dbname,1,10) == "monetdb://") {
-                               message("MonetDB.R: Using 'monetdb://...' URIs 
in dbConnect() is deprecated. Please switch to dbname, host, port named 
arguments.")
-                               rest <- substring(dbname,11,nchar(dbname))
-                               # split at /, so we get the dbname
-                               slashsplit <- strsplit(rest,"/",fixed=TRUE)
-                               hostport <- slashsplit[[1]][1]
-                               dbname <- slashsplit[[1]][2]
-                               
-                               # TODO: handle IPv6 IPs, they contain : chars, 
later
-                               if (length(grep(":",hostport,fixed=TRUE)) == 1) 
{
-                                       hostportsplit <- 
strsplit(hostport,":",fixed=TRUE)
-                                       host <- hostportsplit[[1]][1]
-                                       port <- hostportsplit[[1]][2]
-                               } else {
-                                       host <- hostport
-                               }
-                       }
-                       
-                       if (getOption("monetdb.debug.mapi",F)) message("II: 
Connecting to MonetDB on host ",host," at port ",port, " to DB ", dbname, " 
with user ", user," and a non-printed password, timeout is ",timeout," 
seconds.")
-                       socket <- FALSE
-                       if (wait) {
-                               repeat {
-                                       continue <- FALSE
-                                       tryCatch ({
-                                                               # open socket 
with 5-sec timeout so we can check whether everything works
-                                                               socket <- 
socket <<- .Call("mapiConnect",host,port,5,PACKAGE=C_LIBRARY)
-                                                               # authenticate
-                                                               
.monetAuthenticate(socket,dbname,user,password,language=language)
-                                                               # test the 
connection to make sure it works before
-                                                               
.Call("mapiDisconnect",socket,PACKAGE=C_LIBRARY)
-                                                               break
-                                                       }, error = function(e) {
-                                                               if 
("connection" %in% class(socket)) {
-                                                                       
.Call("mapiDisconnect",socket,PACKAGE=C_LIBRARY)
-                                                               }
-                                                               message("Server 
not ready(",e$message,"), retrying (ESC or CTRL+C to abort)")
-                                                               Sys.sleep(1)
-                                                               continue <<- 
TRUE
-                                                       })
-                               }
-                       }
-                       
-                       # make new socket with user-specified timeout
-                       socket <- 
.Call("mapiConnect",host,port,timeout,PACKAGE=C_LIBRARY)
-                       
.monetAuthenticate(socket,dbname,user,password,language=language)
-                       connenv <- new.env(parent=emptyenv())
-                       connenv$lock <- 0
-                       connenv$deferred <- list()
-                       connenv$exception <- list()
-      
-      conn <- new("MonetDBConnection",socket=socket,connenv=connenv,Id=-1L)
-      if (getOption("monetdb.sequential",F)) {
-        message("MonetDB: Switching to single-threaded query execution.")
-        dbSendQuery(conn,"set optimizer='sequential_pipe'")
-      }
+setMethod("dbConnect", "MonetDBDriver", def=function(drv, dbname="demo", 
user="monetdb", 
+  password="monetdb", host="localhost", port=50000L, timeout=86400L, 
wait=FALSE, language="sql", 
+  ..., url="") {
+  
+  if (substring(url, 1, 10) == "monetdb://") {
+    dbname <- url
+  }
+  port <- as.integer(port)
+  timeout <- as.integer(timeout)
+  
+  if (substring(dbname, 1, 10) == "monetdb://") {
+    message("MonetDB.R: Using 'monetdb://...' URIs in dbConnect() is 
deprecated. Please switch to ",
+    "dbname, host, port named arguments.")
+    rest <- substring(dbname, 11, nchar(dbname))
+    # split at /, so we get the dbname
+    slashsplit <- strsplit(rest, "/", fixed=TRUE)
+    hostport <- slashsplit[[1]][1]
+    dbname <- slashsplit[[1]][2]
+    
+    # TODO: handle IPv6 IPs, they contain : chars, later
+    if (length(grep(":", hostport, fixed=TRUE)) == 1) {
+      hostportsplit <- strsplit(hostport, ":", fixed=TRUE)
+      host <- hostportsplit[[1]][1]
+      port <- hostportsplit[[1]][2]
+    } else {
+      host <- hostport
+    }
+  }
+  
+  if (getOption("monetdb.debug.mapi", F)) message("II: Connecting to MonetDB 
on host ", host, " at "
+    ,"port ", port, " to DB ", dbname, " with user ", user, " and a 
non-printed password, timeout is "
+    , timeout, " seconds.")
+  socket <- FALSE
+  if (wait) {
+    repeat {
+      continue <- FALSE
+      tryCatch ({
+        # open socket with 5-sec timeout so we can check whether everything 
works
+        socket <- socket <<- .Call("mapiConnect", host, port, 5, 
PACKAGE=C_LIBRARY)
+        # authenticate
+        .monetAuthenticate(socket, dbname, user, password, language=language)
+        # test the connection to make sure it works before
+        .Call("mapiDisconnect", socket, PACKAGE=C_LIBRARY)
+        break
+      }, error = function(e) {
+        if ("connection" %in% class(socket)) {
+          .Call("mapiDisconnect", socket, PACKAGE=C_LIBRARY)
+        }
+        message("Server not ready(", e$message, "), retrying (ESC or CTRL+C to 
abort)")
+        Sys.sleep(1)
+        continue <<- TRUE
+      })
+    }
+  }
+  
+  # make new socket with user-specified timeout
+  socket <- .Call("mapiConnect", host, port, timeout, PACKAGE=C_LIBRARY)
+  .monetAuthenticate(socket, dbname, user, password, language=language)
+  connenv <- new.env(parent=emptyenv())
+  connenv$lock <- 0
+  connenv$deferred <- list()
+  connenv$exception <- list()
+  
+  conn <- new("MonetDBConnection", socket=socket, connenv=connenv, Id=-1L)
+  if (getOption("monetdb.sequential", F)) {
+    message("MonetDB: Switching to single-threaded query execution.")
+    dbSendQuery(conn, "set optimizer='sequential_pipe'")
+  }
+  
+  return(conn)
+}, 
+valueClass="MonetDBConnection")
 
-                       return(conn)
-               },
-               valueClass="MonetDBConnection")
 
-
-### MonetDBConnection, #monetdb_mapi_conn
-setClass("MonetDBConnection", 
representation("DBIConnection",socket="externalptr",connenv="environment",fetchSize="integer",Id="integer"))
+### MonetDBConnection
+setClass("MonetDBConnection", representation("DBIConnection", 
socket="externalptr", 
+  connenv="environment", fetchSize="integer", Id="integer"))
 
 setMethod("dbGetInfo", "MonetDBConnection", def=function(dbObj, ...) {
-                       envdata <- dbGetQuery(dbObj,"SELECT name, value from 
env()")
-                       ll <- as.list(envdata$value)
-                       names(ll) <- envdata$name
-                       ll$name <- "MonetDBConnection"
-                       return(ll)
-               })
+  envdata <- dbGetQuery(dbObj, "SELECT name, value from env()")
+  ll <- as.list(envdata$value)
+  names(ll) <- envdata$name
+  ll$name <- "MonetDBConnection"
+  return(ll)
+})
 
 setMethod("dbDisconnect", "MonetDBConnection", def=function(conn, ...) {
-                       .Call("mapiDisconnect",conn@socket,PACKAGE=C_LIBRARY)
-                       return(invisible(TRUE))
-               })
+  .Call("mapiDisconnect", conn@socket, PACKAGE=C_LIBRARY)
+  return(invisible(TRUE))
+})
 
-setMethod("dbListTables", "MonetDBConnection", def=function(conn, ...) {
-                       df <- dbGetQuery(conn,"select name from sys.tables")    
-                       df$name
-               })
+setMethod("dbListTables", "MonetDBConnection", def=function(conn, ..., 
sys_tables=F, schema_names=F, quote=F) {
+  q <- "select schemas.name as sn, tables.name as tn from tables join schemas 
on tables.schema_id=schemas.id"
+  if (!sys_tables) q <- paste0(q, " where system=false")
+  df <- dbGetQuery(conn, q)
+  if (quote) {
+    df$tn <- paste0("\"", df$tn, "\"")
+  }
+  res <- df$tn
+  if (schema_names) {
+    if (quote) {
+      df$sn <- paste0("\"", df$sn, "\"")
+    }
+    res <- paste0(df$sn, ".", df$tn)
+  }
+  return(as.character(res))
+})
 
-if (is.null(getGeneric("dbTransaction"))) setGeneric("dbTransaction", 
function(conn,...) standardGeneric("dbTransaction"))
+if (is.null(getGeneric("dbTransaction"))) setGeneric("dbTransaction", 
function(conn, ...) 
+  standardGeneric("dbTransaction"))
+
 setMethod("dbTransaction", signature(conn="MonetDBConnection"),  
def=function(conn, ...) {
-      dbSendQuery(conn,"start transaction")
-      invisible(TRUE)
-    })
+  dbSendQuery(conn, "start transaction")
+  invisible(TRUE)
+})
 
 setMethod("dbCommit", "MonetDBConnection", def=function(conn, ...) {
-                       dbSendQuery(conn,"commit")
-                       invisible(TRUE)
-               })
+  dbSendQuery(conn, "commit")
+  invisible(TRUE)
+})
 
 setMethod("dbRollback", "MonetDBConnection", def=function(conn, ...) {
-                       dbSendQuery(conn,"rollback")
-                       invisible(TRUE)
-               })
+  dbSendQuery(conn, "rollback")
+  invisible(TRUE)
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to