Skip to contents

set_pk() attaches keys to x, so that collect_and_sort_by_pk() can sort the result. DBTable_v9$tbl() calls it for every table. Call it for a lazy table that you build yourself, for example from SQL text with dplyr::sql(), when its rows have a primary key.

Usage

set_pk(x, keys)

Arguments

x

A lazy table.

keys

A character vector with the primary key columns, or NULL. NULL removes the key, and collect_and_sort_by_pk() then stops.

Value

x, with the key attached.

See also

Examples

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
DBI::dbWriteTable(con, "t", data.frame(a = c(2L, 1L), b = c("y", "x")))
q <- set_pk(dplyr::tbl(con, dplyr::sql("SELECT a, b FROM t")), "a")
collect_and_sort_by_pk(q)
#>        a      b
#>    <int> <char>
#> 1:     1      x
#> 2:     2      y
DBI::dbDisconnect(con)