Skip to content

Commit

Permalink
Standardise sql method names
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 24, 2020
1 parent e082f3c commit b4edde5
Show file tree
Hide file tree
Showing 29 changed files with 118 additions and 116 deletions.
48 changes: 24 additions & 24 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,6 @@ S3method(same_src,tbl_sql)
S3method(select,tbl_lazy)
S3method(semi_join,tbl_lazy)
S3method(show_query,tbl_lazy)
S3method(sql_analyze,"Microsoft SQL Server")
S3method(sql_analyze,ACCESS)
S3method(sql_analyze,DBIConnection)
S3method(sql_analyze,Hive)
S3method(sql_analyze,Impala)
S3method(sql_analyze,MariaDBConnection)
S3method(sql_analyze,MySQL)
S3method(sql_analyze,MySQLConnection)
S3method(sql_analyze,OraConnection)
S3method(sql_analyze,Oracle)
S3method(sql_analyze,Teradata)
S3method(sql_build,ident)
S3method(sql_build,op_arrange)
S3method(sql_build,op_base_local)
Expand All @@ -159,7 +148,6 @@ S3method(sql_build,op_set_op)
S3method(sql_build,op_summarise)
S3method(sql_build,op_ungroup)
S3method(sql_build,tbl_lazy)
S3method(sql_create_index,DBIConnection)
S3method(sql_escape_date,AthenaConnection)
S3method(sql_escape_date,DBIConnection)
S3method(sql_escape_date,PrestoConnection)
Expand All @@ -176,12 +164,7 @@ S3method(sql_escape_raw,"Microsoft SQL Server")
S3method(sql_escape_raw,DBIConnection)
S3method(sql_escape_string,DBIConnection)
S3method(sql_escape_string,TestConnection)
S3method(sql_explain,DBIConnection)
S3method(sql_explain,Oracle)
S3method(sql_explain,PostgreSQL)
S3method(sql_explain,PostgreSQLConnection)
S3method(sql_explain,PqConnection)
S3method(sql_explain,SQLiteConnection)
S3method(sql_index_create,DBIConnection)
S3method(sql_join,DBIConnection)
S3method(sql_join,MariaDBConnection)
S3method(sql_join,MySQL)
Expand All @@ -191,8 +174,16 @@ S3method(sql_optimise,ident)
S3method(sql_optimise,query)
S3method(sql_optimise,select_query)
S3method(sql_optimise,sql)
S3method(sql_query_explain,DBIConnection)
S3method(sql_query_explain,Oracle)
S3method(sql_query_explain,PostgreSQL)
S3method(sql_query_explain,PostgreSQLConnection)
S3method(sql_query_explain,PqConnection)
S3method(sql_query_explain,SQLiteConnection)
S3method(sql_query_fields,DBIConnection)
S3method(sql_query_rows,DBIConnection)
S3method(sql_query_save,"Microsoft SQL Server")
S3method(sql_query_save,DBIConnection)
S3method(sql_render,ident)
S3method(sql_render,join_query)
S3method(sql_render,op)
Expand All @@ -201,8 +192,6 @@ S3method(sql_render,semi_join_query)
S3method(sql_render,set_op_query)
S3method(sql_render,sql)
S3method(sql_render,tbl_lazy)
S3method(sql_save_query,"Microsoft SQL Server")
S3method(sql_save_query,DBIConnection)
S3method(sql_select,"Microsoft SQL Server")
S3method(sql_select,ACCESS)
S3method(sql_select,DBIConnection)
Expand All @@ -216,6 +205,17 @@ S3method(sql_subquery,DBIConnection)
S3method(sql_subquery,OraConnection)
S3method(sql_subquery,Oracle)
S3method(sql_subquery,SQLiteConnection)
S3method(sql_table_analyze,"Microsoft SQL Server")
S3method(sql_table_analyze,ACCESS)
S3method(sql_table_analyze,DBIConnection)
S3method(sql_table_analyze,Hive)
S3method(sql_table_analyze,Impala)
S3method(sql_table_analyze,MariaDBConnection)
S3method(sql_table_analyze,MySQL)
S3method(sql_table_analyze,MySQLConnection)
S3method(sql_table_analyze,OraConnection)
S3method(sql_table_analyze,Oracle)
S3method(sql_table_analyze,Teradata)
S3method(sql_translate_env,"Microsoft SQL Server")
S3method(sql_translate_env,ACCESS)
S3method(sql_translate_env,DBIConnection)
Expand Down Expand Up @@ -312,18 +312,16 @@ export(sql)
export(sql_aggregate)
export(sql_aggregate_2)
export(sql_aggregate_n)
export(sql_analyze)
export(sql_build)
export(sql_call2)
export(sql_cast)
export(sql_cot)
export(sql_create_index)
export(sql_escape_date)
export(sql_escape_datetime)
export(sql_escape_logical)
export(sql_escape_raw)
export(sql_explain)
export(sql_expr)
export(sql_index_create)
export(sql_infix)
export(sql_join_suffix)
export(sql_log)
Expand All @@ -332,13 +330,15 @@ export(sql_optimise)
export(sql_paste)
export(sql_paste_infix)
export(sql_prefix)
export(sql_query_explain)
export(sql_query_fields)
export(sql_query_rows)
export(sql_query_save)
export(sql_quote)
export(sql_render)
export(sql_save_query)
export(sql_str_sub)
export(sql_substr)
export(sql_table_analyze)
export(sql_translator)
export(sql_try_cast)
export(sql_variant)
Expand Down
10 changes: 7 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
* Can now `copy_to()` can now `overwrite` when table is specified with schema
(#489).

* A number of `db_*` generics have been replaced with SQL generation generics:
`sql_analyze()`, `sql_create_index()`, `sql_explain()`,
`sql_save_query()`, `sql_query_fields()`, `sql_query_rows()`.
* A number of `db_*` generics have been replaced with SQL generation generics:

* `db_analyze()` -> `sql_table_analyze()`
* `db_create_index()` -> `sql_index_create()`
* `db_explain()` -> `sql_queriy_explain()`
* `db_query_fields()` -> `sql_query_fields()`
* `db_query_rows()` -> `sql_query_rows()`

This makes them easier to test and is an important part of the process of
moving all database generics in dbplyr (#284).
Expand Down
36 changes: 18 additions & 18 deletions R/backend-.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,12 @@ base_no_win <- sql_translator(

#' SQL generation methods for database methods
#'
#' * `explain()` -> `db_explain` -> `sql_explain()`
#' * `db_copy_to(analyze = TRUE)` -> `sql_analyze()` -> `sql_analyze()`
#' * `db_copy_to(indexes = ...)` -> `db_create_index()` -> `sql_create_index()`
#' * `compute()` -> `db_compute()` -> `db_save_query()` -> `sql_save_query()`
#' * `do()` -> `db_query_rows()` -> `sql_query_rows()`
#' * `tbl()` -> `db_query_fields()` -> `sql_query_fields()`
#' * `sql_table_analyze()` <- `db_analyze()` <- `db_copy_to(analyze = TRUE)`
#' * `sql_index_create()` <- `db_create_index()` <- `db_copy_to(indexes = ...)`
#' * `sql_query_explain()` <- `db_explain` <- `explain()`
#' * `sql_query_fields()` <- `db_query_fields()` <- `tbl()`
#' * `sql_query_rows()` <- `db_query_rows()` <- `do()`
#' * `sql_query_save()` <- `db_save_query()` <- `db_compute()` <- `compute()`
#'
#' @keywords internal
#' @name db_sql
Expand All @@ -462,31 +462,31 @@ sql_subquery.DBIConnection <- function(con, from, name = unique_subquery_name(),

#' @rdname db_sql
#' @export
sql_explain <- function(con, sql, ...) {
UseMethod("sql_explain")
sql_query_explain <- function(con, sql, ...) {
UseMethod("sql_query_explain")
}
#' @export
sql_explain.DBIConnection <- function(con, sql, ...) {
sql_query_explain.DBIConnection <- function(con, sql, ...) {
build_sql("EXPLAIN ", sql, con = con)
}

#' @rdname db_sql
#' @export
sql_analyze <- function(con, table, ...) {
UseMethod("sql_analyze")
sql_table_analyze <- function(con, table, ...) {
UseMethod("sql_table_analyze")
}
#' @export
sql_analyze.DBIConnection <- function(con, table, ...) {
sql_table_analyze.DBIConnection <- function(con, table, ...) {
build_sql("ANALYZE ", as.sql(table), con = con)
}

#' @rdname db_sql
#' @export
sql_create_index <- function(con, table, columns, name = NULL, unique = FALSE, ...) {
UseMethod("sql_create_index")
sql_index_create <- function(con, table, columns, name = NULL, unique = FALSE, ...) {
UseMethod("sql_index_create")
}
#' @export
sql_create_index.DBIConnection <- function(con, table, columns, name = NULL,
sql_index_create.DBIConnection <- function(con, table, columns, name = NULL,
unique = FALSE, ...) {
assert_that(is_string(table), is.character(columns))

Expand All @@ -501,11 +501,11 @@ sql_create_index.DBIConnection <- function(con, table, columns, name = NULL,

#' @rdname db_sql
#' @export
sql_save_query <- function(con, sql, name, temporary = TRUE, ...) {
UseMethod("sql_save_query")
sql_query_save <- function(con, sql, name, temporary = TRUE, ...) {
UseMethod("sql_query_save")
}
#' @export
sql_save_query.DBIConnection <- function(con, sql, name, temporary = TRUE, ...) {
sql_query_save.DBIConnection <- function(con, sql, name, temporary = TRUE, ...) {
build_sql(
"CREATE ", if (temporary) sql("TEMPORARY "), "TABLE \n",
as.sql(name), " AS ", sql,
Expand Down
2 changes: 1 addition & 1 deletion R/backend-access.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ sql_translate_env.ACCESS <- function(con) {
# db_ generics -----------------------------------

#' @export
sql_analyze.ACCESS <- function(con, table, ...) {
sql_table_analyze.ACCESS <- function(con, table, ...) {
# Do nothing. Access doesn't support an analyze / update statistics function
NULL
}
Expand Down
2 changes: 1 addition & 1 deletion R/backend-hive.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sql_translate_env.Hive <- function(con) {
}

#' @export
sql_analyze.Hive <- function(con, table, ...) {
sql_table_analyze.Hive <- function(con, table, ...) {
# https://cwiki.apache.org/confluence/display/Hive/StatsDev
build_sql(
"ANALYZE TABLE ", as.sql(table), " COMPUTE STATISTICS",
Expand Down
2 changes: 1 addition & 1 deletion R/backend-impala.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sql_translate_env.Impala <- function(con) {
}

#' @export
sql_analyze.Impala <- function(con, table, ...) {
sql_table_analyze.Impala <- function(con, table, ...) {
# Using COMPUTE STATS instead of ANALYZE as recommended in this article
# https://www.cloudera.com/documentation/enterprise/5-9-x/topics/impala_compute_stats.html
build_sql("COMPUTE STATS ", as.sql(table), con = con)
Expand Down
4 changes: 2 additions & 2 deletions R/backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mssql_version <- function(con) {
}

#' @export
`sql_analyze.Microsoft SQL Server` <- function(con, table, ...) {
`sql_table_analyze.Microsoft SQL Server` <- function(con, table, ...) {
# https://docs.microsoft.com/en-us/sql/t-sql/statements/update-statistics-transact-sql
build_sql("UPDATE STATISTICS ", as.sql(table), con = con)
}
Expand Down Expand Up @@ -275,7 +275,7 @@ mssql_table_rename <- function(name, temporary) {


#' @export
`sql_save_query.Microsoft SQL Server` <- function(con, sql, name,
`sql_query_save.Microsoft SQL Server` <- function(con, sql, name,
temporary = TRUE, ...){

name <- mssql_table_rename(name, temporary)
Expand Down
6 changes: 3 additions & 3 deletions R/backend-mysql.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ sql_translate_env.MySQL <- sql_translate_env.MariaDBConnection
sql_translate_env.MySQLConnection <- sql_translate_env.MariaDBConnection

#' @export
sql_analyze.MariaDBConnection <- function(con, table, ...) {
sql_table_analyze.MariaDBConnection <- function(con, table, ...) {
build_sql("ANALYZE TABLE ", as.sql(table), con = con)
}
#' @export
sql_analyze.MySQL <- sql_analyze.MariaDBConnection
sql_table_analyze.MySQL <- sql_table_analyze.MariaDBConnection
#' @export
sql_analyze.MySQLConnection <- sql_analyze.MariaDBConnection
sql_table_analyze.MySQLConnection <- sql_table_analyze.MariaDBConnection

#' @export
sql_join.MariaDBConnection <- function(con, x, y, vars, type = "inner", by = NULL, ...) {
Expand Down
6 changes: 3 additions & 3 deletions R/backend-oracle.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ sql_translate_env.Oracle <- function(con) {
}

#' @export
sql_explain.Oracle <- function(con, sql, ...) {
sql_query_explain.Oracle <- function(con, sql, ...) {
build_sql(
"EXPLAIN PLAN FOR ", sql, ";\n",
"SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY()));",
Expand All @@ -63,7 +63,7 @@ sql_explain.Oracle <- function(con, sql, ...) {
}

#' @export
sql_analyze.Oracle <- function(con, table, ...) {
sql_table_analyze.Oracle <- function(con, table, ...) {
# https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4005.htm
build_sql("ANALYZE TABLE ", as.sql(table), " COMPUTE STATISTICS", con = con)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ sql_translate_env.OraConnection <- sql_translate_env.Oracle
sql_select.OraConnection <- sql_select.Oracle

#' @export
sql_analyze.OraConnection <- sql_analyze.Oracle
sql_table_analyze.OraConnection <- sql_table_analyze.Oracle

#' @export
sql_subquery.OraConnection <- sql_subquery.Oracle
Expand Down
6 changes: 3 additions & 3 deletions R/backend-postgres.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ sql_translate_env.PqConnection <- sql_translate_env.PostgreSQLConnection

# http://www.postgresql.org/docs/9.3/static/sql-explain.html
#' @export
sql_explain.PostgreSQLConnection <- function(con, sql, format = "text", ...) {
sql_query_explain.PostgreSQLConnection <- function(con, sql, format = "text", ...) {
format <- match.arg(format, c("text", "json", "yaml", "xml"))

build_sql(
Expand All @@ -158,8 +158,8 @@ sql_explain.PostgreSQLConnection <- function(con, sql, format = "text", ...) {
)
}
#' @export
sql_explain.PostgreSQL <- sql_explain.PostgreSQLConnection
sql_query_explain.PostgreSQL <- sql_query_explain.PostgreSQLConnection
#' @export
sql_explain.PqConnection <- sql_explain.PostgreSQLConnection
sql_query_explain.PqConnection <- sql_query_explain.PostgreSQLConnection

globalVariables(c("strpos", "%::%", "%FROM%", "DATE", "EXTRACT", "TO_CHAR", "string_agg", "%~*%", "%~%", "MONTH", "DOY"))
2 changes: 1 addition & 1 deletion R/backend-sqlite.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ db_desc.SQLiteConnection <- function(x) {
}

#' @export
sql_explain.SQLiteConnection <- function(con, sql, ...) {
sql_query_explain.SQLiteConnection <- function(con, sql, ...) {
build_sql("EXPLAIN QUERY PLAN ", sql, con = con)
}

Expand Down
2 changes: 1 addition & 1 deletion R/backend-teradata.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ sql_translate_env.Teradata <- function(con) {
)}

#' @export
sql_analyze.Teradata <- function(con, table, ...) {
sql_table_analyze.Teradata <- function(con, table, ...) {
# https://www.tutorialspoint.com/teradata/teradata_statistics.htm
build_sql("COLLECT STATISTICS ", as.sql(table) , con = con)
}
Expand Down
8 changes: 4 additions & 4 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ db_desc.DBIConnection <- function(x) {
#' @export
db_save_query.DBIConnection <- function(con, sql, name, temporary = TRUE,
...) {
sql <- sql_save_query(con, sql, name, temporary = temporary, ...)
sql <- sql_query_save(con, sql, name, temporary = temporary, ...)
dbExecute(con, sql, immediate = TRUE)
name
}
Expand All @@ -32,13 +32,13 @@ db_write_table.DBIConnection <- function(con, table, types, values, temporary =
#' @export
db_create_index.DBIConnection <- function(con, table, columns, name = NULL,
unique = FALSE, ...) {
sql <- sql_create_index(con, table, columns, name = name, unique = unique, ...)
sql <- sql_index_create(con, table, columns, name = name, unique = unique, ...)
dbExecute(con, sql)
}

#' @export
db_analyze.DBIConnection <- function(con, table, ...) {
sql <- sql_analyze(con, table, ...)
sql <- sql_table_analyze(con, table, ...)
if (is.null(sql)) {
return()
}
Expand All @@ -47,7 +47,7 @@ db_analyze.DBIConnection <- function(con, table, ...) {

#' @export
db_explain.DBIConnection <- function(con, sql, ...) {
sql <- sql_explain(con, sql, ...)
sql <- sql_query_explain(con, sql, ...)
expl <- dbGetQuery(con, sql)
out <- utils::capture.output(print(expl))
paste(out, collapse = "\n")
Expand Down
Loading

0 comments on commit b4edde5

Please sign in to comment.