-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathadbc.R
536 lines (472 loc) · 16.2 KB
/
adbc.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#' Databases
#'
#' @param driver An [adbc_driver()].
#' @param database An [adbc_database][adbc_database_init].
#' @param option A specific option name
#' @param ... Driver-specific options. For the default method, these are
#' named values that are converted to strings.
#' @param options A named `character()` or `list()` whose values are converted
#' to strings.
#' @param subclass An extended class for an object so that drivers can specify
#' finer-grained control over behaviour at the R level.
#'
#' @return An object of class adbc_database
#' @export
#'
#' @examples
#' adbc_database_init(adbc_driver_void())
#'
adbc_database_init <- function(driver, ...) {
UseMethod("adbc_database_init")
}
#' @export
adbc_database_init.default <- function(driver, ...) {
adbc_database_init_default(driver, list(...))
}
#' @rdname adbc_database_init
#' @export
adbc_database_init_default <- function(driver, options = NULL, subclass = character()) {
database <- .Call(RAdbcDatabaseNew, driver$driver_init_func)
if (!is.null(driver$name)) {
adbc_database_set_options(
database,
c("driver" = driver$name, "entrypoint" = driver$entrypoint)
)
}
database$driver <- driver
with_adbc(database, {
adbc_database_set_options(database, options)
error <- adbc_allocate_error()
status <- .Call(RAdbcDatabaseInit, database, error)
stop_for_error(status, error)
class(database) <- c(subclass, class(database))
adbc_xptr_move(database)
})
}
#' @rdname adbc_database_init
#' @export
adbc_database_release <- function(database) {
stop_for_nonzero_child_count(database)
error <- adbc_allocate_error()
status <- .Call(RAdbcDatabaseRelease, database, error)
stop_for_error(status, error)
invisible(database)
}
#' Connections
#'
#' @inheritParams adbc_database_init
#' @param connection An [adbc_connection][adbc_connection_init]
#'
#' @return An object of class 'adbc_connection'
#' @export
#'
#' @examples
#' db <- adbc_database_init(adbc_driver_void())
#' adbc_connection_init(db)
#'
adbc_connection_init <- function(database, ...) {
UseMethod("adbc_connection_init")
}
#' @export
adbc_connection_init.default <- function(database, ...) {
adbc_connection_init_default(database, list(...))
}
#' @rdname adbc_connection_init
#' @export
adbc_connection_init_default <- function(database, options = NULL, subclass = character()) {
connection <- .Call(RAdbcConnectionNew)
connection$database <- database
with_adbc(connection, {
adbc_connection_set_options(connection, options)
error <- adbc_allocate_error()
status <- .Call(RAdbcConnectionInit, connection, database, error)
stop_for_error(status, error)
class(connection) <- c(subclass, class(connection))
adbc_xptr_move(connection)
})
}
#' @rdname adbc_connection_init
#' @export
adbc_connection_release <- function(connection) {
stop_for_nonzero_child_count(connection)
if (isTRUE(connection$.release_database)) {
database <- connection$database
on.exit(adbc_database_release(database))
}
error <- adbc_allocate_error()
status <- .Call(RAdbcConnectionRelease, connection, error)
stop_for_error(status, error)
invisible(connection)
}
#' Connection methods
#'
#' @inheritParams adbc_connection_init
#' @param info_codes A list of metadata codes to fetch, or NULL to fetch all.
#' Valid values are documented in the adbc.h header.
#' @param depth The level of nesting to display. If 0, display all levels. If 1,
#' display only catalogs (i.e., catalog_schemas will be null). If 2, display
#' only catalogs and schemas (i.e., db_schema_tables will be null). If 3,
#' display only catalogs, schemas, and tables.
#' @param catalog Only show tables in the given catalog. If NULL, do not filter
#' by catalog. If an empty string, only show tables without a catalog. May be
#' a search pattern.
#' @param db_schema Only show tables in the given database schema. If NULL, do
#' not filter by database schema. If an empty string, only show tables without
#' a database schema. May be a search pattern.
#' @param table_name Constrain an object or statistics query for a specific table.
#' If NULL, do not filter by name. May be a search pattern.
#' @param table_type Only show tables matching one of the given table types. If
#' NULL, show tables of any type. Valid table types can be fetched from
#' GetTableTypes. Terminate the list with a NULL entry.
#' @param column_name Only show columns with the given name. If NULL, do not
#' filter by name. May be a search pattern.
#' @param serialized_partition The partition descriptor.
#' @param approximate If `FALSE`, request exact values of statistics,
#' else allow for best-effort, approximate, or cached values. The database
#' may return approximate values regardless, as indicated in the result.
#' Requesting exact values may be expensive or unsupported.
#' @param value A string or identifier.
#'
#' @return
#' - `adbc_connection_get_info()`, `adbc_connection_get_objects()`,
#' `adbc_connection_get_table_types()`, and `adbc_connection_read_partition()`
#' return a [nanoarrow_array_stream][nanoarrow::as_nanoarrow_array_stream()].
#' - `adbc_connection_get_table_schema()` returns a
#' [nanoarrow_schena][nanoarrow::as_nanoarrow_schema()]
#' - `adbc_connection_commit()` and `adbc_connection_rollback()` return
#' `connection`, invisibly.
#' @export
#'
#' @examples
#' db <- adbc_database_init(adbc_driver_void())
#' con <- adbc_connection_init(db)
#' # (not implemented by the void driver)
#' try(adbc_connection_get_info(con, 0))
#'
adbc_connection_get_info <- function(connection, info_codes = NULL) {
error <- adbc_allocate_error()
out_stream <- nanoarrow::nanoarrow_allocate_array_stream()
status <- .Call(
RAdbcConnectionGetInfo,
connection,
info_codes,
out_stream,
error
)
stop_for_error(status, error)
adbc_child_stream(connection, out_stream)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_get_objects <- function(connection, depth = 0L, catalog = NULL, db_schema = NULL,
table_name = NULL, table_type = NULL, column_name = NULL) {
error <- adbc_allocate_error()
out_stream <- nanoarrow::nanoarrow_allocate_array_stream()
status <- .Call(
RAdbcConnectionGetObjects,
connection,
depth,
catalog,
db_schema,
table_name,
table_type,
column_name,
out_stream,
error
)
stop_for_error(status, error)
adbc_child_stream(connection, out_stream)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_get_table_schema <- function(connection, catalog, db_schema, table_name) {
error <- adbc_allocate_error()
out_schema <- nanoarrow::nanoarrow_allocate_schema()
status <- .Call(
RAdbcConnectionGetTableSchema,
connection,
catalog,
db_schema,
table_name,
out_schema,
error
)
stop_for_error(status, error)
out_schema
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_get_table_types <- function(connection) {
error <- adbc_allocate_error()
out_stream <- nanoarrow::nanoarrow_allocate_array_stream()
status <- .Call(RAdbcConnectionGetTableTypes, connection, out_stream, error)
stop_for_error(status, error)
adbc_child_stream(connection, out_stream)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_read_partition <- function(connection, serialized_partition) {
error <- adbc_allocate_error()
out_stream <- nanoarrow::nanoarrow_allocate_array_stream()
status <- .Call(
RAdbcConnectionReadPartition,
connection,
serialized_partition,
out_stream,
error
)
stop_for_error(status, error)
adbc_child_stream(connection, out_stream)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_commit <- function(connection) {
error <- adbc_allocate_error()
status <- .Call(RAdbcConnectionCommit, connection, error)
stop_for_error(status, error)
invisible(connection)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_rollback <- function(connection) {
error <- adbc_allocate_error()
status <- .Call(RAdbcConnectionRollback, connection, error)
stop_for_error(status, error)
invisible(connection)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_cancel <- function(connection) {
error <- adbc_allocate_error()
status <- .Call(RAdbcConnectionCancel, connection, error)
stop_for_error(status, error)
invisible(connection)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_get_statistic_names <- function(connection) {
error <- adbc_allocate_error()
out_stream <- nanoarrow::nanoarrow_allocate_array_stream()
status <- .Call(RAdbcConnectionGetStatisticNames, connection, out_stream, error)
stop_for_error(status, error)
adbc_child_stream(connection, out_stream)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_get_statistics <- function(connection, catalog, db_schema,
table_name, approximate = FALSE) {
error <- adbc_allocate_error()
out_stream <- nanoarrow::nanoarrow_allocate_array_stream()
status <- .Call(
RAdbcConnectionGetStatistics,
connection,
catalog,
db_schema,
table_name,
approximate,
out_stream,
error
)
stop_for_error(status, error)
adbc_child_stream(connection, out_stream)
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_quote_identifier <- function(connection, value, ...) {
UseMethod("adbc_connection_quote_identifier")
}
#' @rdname adbc_connection_get_info
#' @export
adbc_connection_quote_string <- function(connection, value, ...) {
UseMethod("adbc_connection_quote_string")
}
#' @export
adbc_connection_quote_identifier.default <- function(connection, value, ...) {
out <- gsub('"', '""', enc2utf8(value))
paste0('"', out, '"')
}
#' @export
adbc_connection_quote_string.default <- function(connection, value, ...) {
out <- gsub("'", "''", enc2utf8(value))
paste0("'", out, "'")
}
#' Statements
#'
#' @inheritParams adbc_connection_init
#' @param statement An [adbc_statement][adbc_statement_init]
#'
#' @return An object of class 'adbc_statement'
#' @export
#'
#' @examples
#' db <- adbc_database_init(adbc_driver_void())
#' con <- adbc_connection_init(db)
#' adbc_statement_init(con)
#'
adbc_statement_init <- function(connection, ...) {
UseMethod("adbc_statement_init")
}
#' @export
adbc_statement_init.default <- function(connection, ...) {
adbc_statement_init_default(connection, list(...))
}
#' @rdname adbc_statement_init
#' @export
adbc_statement_init_default <- function(connection, options = NULL, subclass = character()) {
statement <- .Call(RAdbcStatementNew, connection)
statement$connection <- connection
with_adbc(statement, {
adbc_statement_set_options(statement, options)
class(statement) <- c(subclass, class(statement))
adbc_xptr_move(statement)
})
}
#' @rdname adbc_statement_init
#' @export
adbc_statement_release <- function(statement) {
stop_for_nonzero_child_count(statement)
if (isTRUE(statement$.release_connection)) {
connection <- statement$connection
on.exit(adbc_connection_release(connection))
}
error <- adbc_allocate_error()
status <- .Call(RAdbcStatementRelease, statement, error)
stop_for_error(status, error)
invisible(statement)
}
#' Statement methods
#'
#' @inheritParams adbc_statement_init
#' @param query An SQL query as a string
#' @param plan A raw vector representation of a serialized Substrait plan.
#' @param values A [nanoarrow_array][nanoarrow::as_nanoarrow_array] or object
#' that can be coerced to one.
#' @param stream A [nanoarrow_array_stream][nanoarrow::as_nanoarrow_array_stream]
#' or object that can be coerced to one.
#' @param schema A [nanoarrow_schema][nanoarrow::as_nanoarrow_schema] or object
#' that can be coerced to one.
#' @param stream_join_parent Use `TRUE` to invalidate `statement` and tie its
#' lifecycle to `stream`.
#'
#' @return
#' - `adbc_statement_set_sql_query()`, `adbc_statement_set_substrait_plan()`,
#' `adbc_statement_prepare()`, `adbc_statement_bind()`,
#' `adbc_statement_bind_stream()`, and `adbc_statement_execute_query()`
#' return `statement`, invisibly.
#' - `adbc_statement_get_parameter_schema()` returns a
#' [nanoarrow_schema][nanoarrow::as_nanoarrow_schema].
#'
#' @export
#'
#' @examples
#' db <- adbc_database_init(adbc_driver_void())
#' con <- adbc_connection_init(db)
#' stmt <- adbc_statement_init(con)
#' # (not implemented by the void driver)
#' try(adbc_statement_set_sql_query(stmt, "some query"))
#'
adbc_statement_set_sql_query <- function(statement, query) {
error <- adbc_allocate_error()
status <- .Call(RAdbcStatementSetSqlQuery, statement, query, error)
stop_for_error(status, error)
invisible(statement)
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_set_substrait_plan <- function(statement, plan) {
error <- adbc_allocate_error()
status <- .Call(RAdbcStatementSetSubstraitPlan, statement, plan, error)
stop_for_error(status, error)
invisible(statement)
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_prepare <- function(statement) {
error <- adbc_allocate_error()
status <- .Call(RAdbcStatementPrepare, statement, error)
stop_for_error(status, error)
invisible(statement)
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_get_parameter_schema <- function(statement) {
error <- adbc_allocate_error()
schema <- nanoarrow::nanoarrow_allocate_schema()
status <- .Call(RAdbcStatementGetParameterSchema, statement, schema, error)
stop_for_error(status, error)
schema
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_bind <- function(statement, values, schema = NULL) {
values <- nanoarrow::as_nanoarrow_array(values, schema = schema)
schema <- nanoarrow::infer_nanoarrow_schema(values)
error <- adbc_allocate_error()
status <- .Call(RAdbcStatementBind, statement, values, schema, error)
stop_for_error(status, error)
invisible(statement)
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_bind_stream <- function(statement, stream, schema = NULL) {
stream <- nanoarrow::as_nanoarrow_array_stream(stream, schema = schema)
error <- adbc_allocate_error()
status <- .Call(RAdbcStatementBindStream, statement, stream, error)
stop_for_error(status, error)
invisible(statement)
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_execute_query <- function(statement, stream = NULL,
stream_join_parent = FALSE) {
error <- adbc_allocate_error()
if (is.null(stream)) {
result <- .Call(RAdbcStatementExecuteQuery, statement, NULL, error)
} else {
stream_tmp <- nanoarrow::nanoarrow_allocate_array_stream()
result <- .Call(RAdbcStatementExecuteQuery, statement, stream_tmp, error)
if (identical(result$status, 0L)) {
stream_tmp <- adbc_child_stream(
statement,
stream_tmp,
release_parent = stream_join_parent
)
nanoarrow::nanoarrow_pointer_export(stream_tmp, stream)
}
}
stop_for_error(result$status, error)
result$rows_affected
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_execute_schema <- function(statement) {
error <- adbc_allocate_error()
out_schema <- nanoarrow::nanoarrow_allocate_schema()
status <- .Call(RAdbcStatementExecuteSchema, statement, out_schema, error)
stop_for_error(status, error)
out_schema
}
#' @rdname adbc_statement_set_sql_query
#' @export
adbc_statement_cancel <- function(statement) {
error <- adbc_allocate_error()
status <- .Call(RAdbcStatementCancel, statement, error)
stop_for_error(status, error)
invisible(statement)
}