odbc icon indicating copy to clipboard operation
odbc copied to clipboard

dbQuoteString() needs a method for Hive

Open yutannihilation opened this issue 6 years ago • 3 comments

Hive uses C-style escape for string literals, so the result of this pseudo reprex should be 'it\'s ok'. (Sorry for not providing the actual reprex...)

# `con` is a Hive connection
DBI::dbQuoteString(con, "it's ok")
#> <SQL> 'it''s ok'

c.f.) https://cwiki.apache.org/confluence/display/Hive/Literals

To do this, is it necessary to define something like odbcQuoteString() generic function?

yutannihilation avatar May 24 '18 03:05 yutannihilation

You would define a dbQuoteString for Hive connections, in https://github.com/r-dbi/odbc/blob/master/R/db.R e.g.

setMethod(
  "dbQuoteString", "Hive",
  function(conn, x, ...) {
    if (is(x, "SQL"))
        return(x)
    if (!is.character(x))
        stop("x must be character or SQL", call. = FALSE)
    x <- gsub("'", "\\'", enc2utf8(x))
    if (length(x) == 0L) {
      DBI::SQL(character())
    }
    else {
      str <- paste("'", x, "'", sep = "")
      str[is.na(x)] <- "NULL"
      DBI::SQL(str)
    }
  })

If you are willing to do a PR for this and test that it is working for hive connections as expected I am happy to review it.

jimhester avatar Jun 08 '18 14:06 jimhester

You would also likely need to define a dbQuoteIdentifier method as well

jimhester avatar Jun 08 '18 14:06 jimhester

Thanks! I will try.

yutannihilation avatar Jun 09 '18 06:06 yutannihilation

Closed in #424.☃️

simonpcouch avatar Jan 17 '24 18:01 simonpcouch