Skip to content

Commit

Permalink
Document db_col_types()
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich committed Aug 8, 2023
1 parent adb195e commit ee78c43
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 36 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ S3method(cross_join,tbl_lazy)
S3method(db_analyze,DBIConnection)
S3method(db_col_types,DBIConnection)
S3method(db_col_types,MariaDBConnection)
S3method(db_col_types,MySQL)
S3method(db_col_types,MySQLConnection)
S3method(db_col_types,PostgreSQL)
S3method(db_col_types,PqConnection)
S3method(db_col_types,TestConnection)
S3method(db_collect,DBIConnection)
Expand Down
11 changes: 11 additions & 0 deletions R/backend-mysql.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ db_connection_describe.MySQL <- db_connection_describe.MariaDBConnection
#' @export
db_connection_describe.MySQLConnection <- db_connection_describe.MariaDBConnection

#' @export
db_col_types.MariaDBConnection <- function(con, table, call) {
table <- as_table_ident(table, error_call = call)
col_info_df <- DBI::dbGetQuery(con, glue_sql2(con, "SHOW COLUMNS FROM {.tbl table};"))
set_names(col_info_df[["Type"]], col_info_df[["Field"]])
}
#' @export
db_col_types.MySQL <- db_col_types.MariaDBConnection
#' @export
db_col_types.MySQLConnection <- db_col_types.MariaDBConnection

#' @export
sql_translation.MariaDBConnection <- function(con) {
sql_variant(
Expand Down
13 changes: 13 additions & 0 deletions R/backend-postgres.R
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,17 @@ db_supports_table_alias_with_as.PostgreSQL <- function(con) {
TRUE
}

#' @export
db_col_types.PqConnection <- function(con, table, call) {
table <- as_table_ident(table, error_call = call)
res <- DBI::dbSendQuery(con, glue_sql2(con, "SELECT * FROM {.tbl table} LIMIT 0"))
on.exit(DBI::dbClearResult(res))
DBI::dbFetch(res, n = 0)
col_info_df <- DBI::dbColumnInfo(res)
set_names(col_info_df[[".typname"]], col_info_df[["name"]])
}

#' @export
db_col_types.PostgreSQL <- db_col_types.PqConnection

utils::globalVariables(c("strpos", "%::%", "%FROM%", "%ILIKE%", "DATE", "EXTRACT", "TO_CHAR", "string_agg", "%~*%", "%~%", "MONTH", "DOY", "DATE_TRUNC", "INTERVAL", "FLOOR", "WEEK"))
22 changes: 22 additions & 0 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#' * `dbplyr_edition()` declares which version of the dbplyr API you want.
#' See below for more details.
#'
#' * `db_col_types()` returns the column types of a table.
#'
#' @section dbplyr 2.0.0:
#' dbplyr 2.0.0 renamed a number of generics so that they could be cleanly moved
#' from dplyr to dbplyr. If you have an existing backend, you'll need to rename
Expand Down Expand Up @@ -80,6 +82,26 @@ db_sql_render.DBIConnection <- function(con, sql, ..., cte = FALSE, sql_options
sql_render(sql, con = con, ..., sql_options = sql_options)
}

#' @rdname db-misc
#' @export
db_col_types <- function(con, table, call) {
if (is_null(table)) {
return(NULL)
}

UseMethod("db_col_types")
}

#' @export
db_col_types.TestConnection <- function(con, table, call) {
NULL
}

#' @export
db_col_types.DBIConnection <- function(con, table, call) {
NULL
}

#' Options for generating SQL
#'
#' @param cte If `FALSE`, the default, subqueries are used. If `TRUE` common
Expand Down
36 changes: 0 additions & 36 deletions R/rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -758,42 +758,6 @@ rows_auto_copy <- function(x, y, copy, call = caller_env()) {
auto_copy(x, y, copy = copy, types = x_types)
}

#' @export
db_col_types <- function(con, table, call) {
if (is_null(table)) {
return(NULL)
}

UseMethod("db_col_types")
}

#' @export
db_col_types.TestConnection <- function(con, table, call) {
NULL
}

#' @export
db_col_types.DBIConnection <- function(con, table, call) {
NULL
}

#' @export
db_col_types.PqConnection <- function(con, table, call) {
table <- as_table_ident(table, error_call = call)
res <- DBI::dbSendQuery(con, glue_sql2(con, "SELECT * FROM {.tbl table} LIMIT 0"))
on.exit(DBI::dbClearResult(res))
DBI::dbFetch(res, n = 0)
col_info_df <- DBI::dbColumnInfo(res)
set_names(col_info_df[[".typname"]], col_info_df[["name"]])
}

#' @export
db_col_types.MariaDBConnection <- function(con, table, call) {
table <- as_table_ident(table, error_call = call)
col_info_df <- DBI::dbGetQuery(con, glue_sql2(con, "SHOW COLUMNS FROM {.tbl table};"))
set_names(col_info_df[["Type"]], col_info_df[["Field"]])
}

rows_get_or_execute <- function(x, sql, returning_cols, call = caller_env()) {
con <- remote_con(x)
msg <- "Can't modify database table {.val {remote_name(x)}}."
Expand Down
4 changes: 4 additions & 0 deletions man/db-misc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee78c43

Please sign in to comment.