Skip to content

[Experimental]

get_returned_rows() extracts the RETURNING rows produced by rows_insert(), rows_append(), rows_update(), rows_upsert(), or rows_delete() if these are called with the returning argument. An error is raised if this information is not available.

has_returned_rows() checks if x has stored RETURNING rows produced by rows_insert(), rows_append(), rows_update(), rows_upsert(), or rows_delete().

Usage

get_returned_rows(x)

has_returned_rows(x)

Arguments

x

A lazy tbl.

Value

For get_returned_rows(), a tibble.

For has_returned_rows(), a scalar logical.

Examples

library(dplyr)

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
DBI::dbExecute(con, "CREATE TABLE Info (
   id INTEGER PRIMARY KEY AUTOINCREMENT,
   number INTEGER
)")
#> [1] 0
info <- tbl(con, "Info")

rows1 <- copy_inline(con, data.frame(number = c(1, 5)))
rows_insert(info, rows1, conflict = "ignore", in_place = TRUE)
#> Matching, by = "number"
info
#> # Source:   table<`Info`> [2 x 2]
#> # Database: sqlite 3.45.2 [:memory:]
#>      id number
#>   <int>  <int>
#> 1     1      1
#> 2     2      5

# If the table has an auto incrementing primary key, you can use
# the returning argument + `get_returned_rows()` its value
rows2 <- copy_inline(con, data.frame(number = c(13, 27)))
info <- rows_insert(
  info,
  rows2,
  conflict = "ignore",
  in_place = TRUE,
  returning = id
)
#> Matching, by = "number"
info
#> # Source:   table<`Info`> [4 x 2]
#> # Database: sqlite 3.45.2 [:memory:]
#>      id number
#>   <int>  <int>
#> 1     1      1
#> 2     2      5
#> 3     3     13
#> 4     4     27
get_returned_rows(info)
#> # A tibble: 2 × 1
#>      id
#>   <int>
#> 1     3
#> 2     4