File: verb-do-query.R

package info (click to toggle)
r-cran-dbplyr 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,644 kB
  • sloc: sh: 13; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,022 bytes parent folder | download
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
#' @importFrom R6 R6Class
NULL

Query <- R6::R6Class("Query",
  private = list(
    .vars = NULL
  ),
  public = list(
    con = NULL,
    sql = NULL,

    initialize = function(con, sql, vars) {
      self$con <- con
      self$sql <- sql
      private$.vars <- vars
    },

    # nocov start
    print = function(...) {
      cat_line("<Query> ", self$sql)
      print(self$con)
    },

    fetch = function(n = -1L) {
      res <- dbSendQuery(self$con, self$sql)
      on.exit(dbClearResult(res))

      out <- dbFetch(res, n)
      res_warn_incomplete(res)
      out
    },
    # nocov end

    fetch_paged = function(chunk_size = 1e4, callback) {
      qry <- dbSendQuery(self$con, self$sql)
      on.exit(dbClearResult(qry))

      while (!dbHasCompleted(qry)) {
        chunk <- dbFetch(qry, chunk_size)
        callback(chunk)
      }

      invisible(TRUE)
    },

    # nocov start
    vars = function() {
      private$.vars
    },

    ncol = function() {
      length(self$vars())
    }
    # nocov end
  )
)