Skip to content

Commit

Permalink
Generate SQL 92 subqueries by default
Browse files Browse the repository at this point in the history
These are always named. SQLite can use unnamed subqueries, which is simpler for testing SQL generation.
  • Loading branch information
hadley committed Mar 18, 2016
1 parent 7b63add commit 164443b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ S3method(sql_render,tbl_sql)
S3method(sql_select,default)
S3method(sql_semi_join,default)
S3method(sql_set_op,default)
S3method(sql_subquery,PostgreSQLConnection)
S3method(sql_subquery,SQLiteConnection)
S3method(sql_subquery,default)
S3method(sql_translate_env,"NULL")
S3method(sql_translate_env,MySQLConnection)
Expand Down
12 changes: 4 additions & 8 deletions R/sql-generic.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,11 @@ sql_subquery <- function(con, from, name = random_table_name(), ...) {
UseMethod("sql_subquery")
}
#' @export
sql_subquery.default <- function(con, from, name = unique_name(), ...) {
if (is.ident(from)) {
setNames(from, name)
sql_subquery.default <- function(con, sql, name = unique_name(), ...) {
if (is.ident(sql)) {
setNames(sql, name)
} else {
if (is.null(name)) {
build_sql("(", from, ")", con = con)
} else {
build_sql("(", from, ") AS ", ident(name), con = con)
}
build_sql("(", sql, ") ", ident(name %||% random_table_name()), con = con)
}
}

Expand Down
11 changes: 0 additions & 11 deletions R/src-postgres.r
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,6 @@ sql_translate_env.PostgreSQLConnection <- function(con) {
)
}

#' @export
sql_subquery.PostgreSQLConnection <- function(con, sql, name = unique_name(), ...) {

if (is.ident(sql)) {
setNames(sql, name)
} else {
build_sql("(", sql, ") ", ident(name %||% random_table_name()), con = con)
}
}


# DBI methods ------------------------------------------------------------------

# Doesn't return TRUE for temporary tables
Expand Down
13 changes: 13 additions & 0 deletions R/src-sqlite.r
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ sql_escape_ident.SQLiteConnection <- function(con, x) {
sql_quote(x, '`')
}

#' @export
sql_subquery.SQLiteConnection <- function(con, from, name = unique_name(), ...) {
if (is.ident(from)) {
setNames(from, name)
} else {
if (is.null(name)) {
build_sql("(", from, ")", con = con)
} else {
build_sql("(", from, ") AS ", ident(name), con = con)
}
}
}


# DBI methods ------------------------------------------------------------------

Expand Down

0 comments on commit 164443b

Please sign in to comment.