[R-sig-DB] character to factor transform in package RpgSQL

Xiaobo Gu gux|@obo1982 @end|ng |rom gm@||@com
Sun Nov 14 05:28:33 CET 2010


Hi,
I have looked at the source code of package RpgSQL, it seems the fetch
function does not transform character column into factors, but when I
use the class function to test the result data frame of function
fetch, it all tells factor, can you help identify which part of the
function doing the transformation, in our usage we want to keep some
character column just as characters, bellowing is the source code of
function fetch in package RpgSQL.

Regards,

Xiaobo Gu

setMethod("fetch", signature(res="pgSQLResult", n="numeric"),
def=function(res, n, ...) {
  cols <- .jcall(res using md, "I", "getColumnCount")
  if (cols < 1) return(NULL)
  l <- list()
  for (i in 1:cols) {
    ct <- .jcall(res using md, "I", "getColumnType", i)
    l[[i]] <- if (ct == -5 | ct ==-6 | (ct >= 2 & ct <= 8)) {
           numeric()
       } else if (ct == 91) {
           structure(numeric(), class = "Date")
       } else if (ct == 93) {
           structure(numeric(), class = class(Sys.time()))
       } else character()
    names(l)[i] <- .jcall(res using md, "S", "getColumnName", i)
  }

  j <- 0
  while (.jcall(res using jr, "Z", "next")) {
    j <- j + 1
    for (i in 1:cols) {
      l[[i]][j] <- if (is.numeric(l[[i]])) {
          l[[i]][j] <- .jcall(res using jr, "D", "getDouble", i)
      } else if (inherits(l[[i]], "Date")) {
        tentativeDate <- .jcall(res using jr, "S", "getString", i)
	    if (length(tentativeDate) == 0 || tentativeDate == "0001-01-01
BC") tentativeDate <- NA
        l[[i]][j] <- as.Date(tentativeDate)
      } else {
        a <- .jcall(res using jr, "S", "getString", i)
        l[[i]][j] <- if (is.null(a)) NA else a
      }
    }
    if (n > 0 && j >= n) break
  }
  if (j)
    as.data.frame(l, row.names=1:j)
  else
    as.data.frame(l)
})




More information about the R-sig-DB mailing list