Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter support for DBI dbGetQuery #443

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Improve int64 support when reading BigQuery tables with dplyr syntax. `collect()` now utilizes `bigint` parameter in `DBI::dbConnect()` object. Set `bigint` in connection object to `"integer64"` or `"character"` to avoid integer coercion and overflow issues (@zoews, #439, #437).

* `DBI:dbGetQuery()` - now can take params attribute which will work with parameterised queries (@byapparov, #443)

# bigrquery 1.3.2

* BigQuery `BYTES` and `GEOGRAPHY` column types are now supported via
Expand Down
2 changes: 1 addition & 1 deletion R/bq-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ bq_perform_query <- function(query, billing,
priority = unbox(priority)
)

if (!is.null(parameters)) {
if (length(parameters) > 0) {
parameters <- as_bq_params(parameters)
query$queryParameters <- as_json(parameters)
}
Expand Down
5 changes: 4 additions & 1 deletion R/dbi-result.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#' @include dbi-connection.R
NULL

BigQueryResult <- function(conn, sql, ...) {
BigQueryResult <- function(conn, sql, params = NULL, ...) {

if (is.null(conn@dataset)) {
job <- bq_perform_query(sql,
billing = conn@billing,
quiet = conn@quiet,
parameters = params,
...
)
} else {
Expand All @@ -14,6 +16,7 @@ BigQueryResult <- function(conn, sql, ...) {
billing = conn@billing,
default_dataset = ds,
quiet = conn@quiet,
parameters = params,
...
)
}
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-dbi-result.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ test_that("can retrieve full query results", {
expect_equal(df, tibble(count = 32L))
})

test_that("can retrieve parameterized query results", {
con <- DBI::dbConnect(
bigquery(),
project = bq_test_project(),
dataset = "basedata",
bigint = "integer"
)

df <- DBI::dbGetQuery(con, "SELECT @value AS value", params = (value = 100))
expect_equal(df, tibble(value = 100))
})


test_that("can retrieve without dataset", {
con <- DBI::dbConnect(
bigquery(),
Expand Down