~hrbrmstr/sergeant-caffeinated

ref: 135af6c1c0872d07fbd6df70bf876743976281ae sergeant-caffeinated/R/utils.r -rw-r--r-- 1.2 KiB
135af6c1hrbrmstr 4j 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
.verify.JDBC.result <- function(result, ..., conn=NULL) {

  if (rJava::is.jnull(result)) {

    x <- rJava::.jgetEx(TRUE)

    if (rJava::is.jnull(x)) {
      stop(...)
    } else if (is.null(conn)) {
      stop(...)
    } else {

      jr <- unlist(rJava::.jcall(x, "S", "getMessage"))

      resp <- unlist(list(...))

      jdbc_err <- resp[1]
      oq <- resp[2]

      resp <- unlist(strsplit(jr, "\n"))

      err <- resp[grepl("Error Id", resp)]

      resp <- resp[resp != ""]
      resp <- resp[!grepl("Error Id", resp)]

      err <- sub("^.*: ", "", err)
      err <- unlist(strsplit(err, "[[:space:]]+"))[1]

      oq <- unlist(strsplit(oq, "\n"))

      c(
        sprintf("%s:\n", jdbc_err),
        sprintf("%3d: %s", 1:length(oq), oq),
        " ",
        resp,
        sprintf(
          "\nQuery Profile Error Id: %s", err
        )
      ) -> resp

      resp <- paste0(resp, collapse="\n")

      stop(resp, call.=FALSE)

    }

  }

}


try_require <- function(package, fun) {
  if (requireNamespace(package, quietly = TRUE)) {
    library(package, character.only = TRUE)
    return(invisible())
  }

  stop("Package `", package, "` required for `", fun , "`.\n", # nocov start
       "Please install and try again.", call. = FALSE) # nocov end
}