generated from r-dbi/RKazam
/
AdbiResult.R
130 lines (105 loc) · 3.33 KB
/
AdbiResult.R
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#' @include AdbiConnection.R
NULL
AdbiResult <- function(connection, statement, immediate = NULL,
type = c("query", "statement"), bigint = NULL,
rows_affected_callback = identity) {
init_result(
connection = connection,
statement = statement,
class = "AdbiResult",
immediate = immediate,
type = type,
bigint = bigint,
rows_affected_callback = rows_affected_callback
)
}
init_result <- function(connection, statement, class, immediate = NULL,
type = c("query", "statement"), bigint = NULL,
rows_affected_callback = identity) {
if (!(is.null(immediate) || identical(immediate, TRUE) ||
identical(immediate, FALSE))) {
stop("Expecting `immediate` to be either `TRUE` or `FALSE` (or `NULL` ",
"in which case the statement is inspected for placeholders)",
call. = FALSE)
}
if (!dbIsValid(connection)) {
stop("Invalid connection", call. = FALSE)
}
if (!length(statement) == 1L || is.na(statement)) {
stop("Expecting a non-NA string as `statement`.", call. = FALSE)
}
con <- connection@connection
stmt <- adbcdrivermanager::adbc_statement_init(con)
adbcdrivermanager::adbc_statement_set_sql_query(
stmt,
as.character(statement)
)
if (is.null(immediate)) {
adbcdrivermanager::adbc_statement_prepare(stmt)
prepared <- TRUE
schema <- nanoarrow::nanoarrow_schema_parse(
adbcdrivermanager::adbc_statement_get_parameter_schema(stmt),
recursive = TRUE
)
if ("children" %in% names(schema) && length(schema[["children"]]) > 0L) {
immediate <- FALSE
} else {
immediate <- TRUE
}
} else {
prepared <- FALSE
schema <- NULL
}
if (is.null(bigint)) {
bigint <- connection@bigint
}
res <- new_result(stmt, immediate, prepared, match.arg(type), statement,
class, bigint, rows_affected_callback)
register_result(connection, res)
meta(res, "params") <- schema
res
}
new_result <- function(statement, immediate, prepared, type, sql,
class = "AdbiResult", bigint = NULL,
rows_affected_callback = identity) {
meta <- list(
immediate = immediate,
prepared = prepared,
type = type,
sql = sql,
has_completed = switch(type, statement = TRUE, query = FALSE)
)
new(
class,
statement = statement,
metadata = list2env(meta, envir = new.env(parent = emptyenv())),
bigint = resolve_bigint(bigint),
rows_affected_callback = rows_affected_callback
)
}
#' Class AdbiResult (and methods)
#'
#' AdbiResult objects are created by [DBI::dbSendQuery()] or
#' [DBI::dbSendStatement()], and encapsulate the result of an SQL statement
#' (either `SELECT` or not). They are a superclass of the [DBIResult-class]
#' class. The "Usage" section lists the class methods overridden by
#' \pkg{adbi}.
#'
#' @seealso
#' The corresponding generic functions
#' [DBI::dbFetch()], [DBI::dbClearResult()], [DBI::dbBind()],
#' [DBI::dbColumnInfo()], [DBI::dbGetRowsAffected()], [DBI::dbGetRowCount()],
#' [DBI::dbHasCompleted()], and [DBI::dbGetStatement()].
#'
#' @export
#' @keywords internal
setClass(
"AdbiResult",
contains = "DBIResult",
slots = list(
statement = "ANY",
metadata = "environment",
bigint = "character",
rows_affected_callback = "function"
)
)