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

Overload +-Operator for Character vectors Marked as SQL? #260

Closed
petermeissner opened this issue Sep 21, 2018 · 1 comment
Closed

Overload +-Operator for Character vectors Marked as SQL? #260

petermeissner opened this issue Sep 21, 2018 · 1 comment

Comments

@petermeissner
Copy link

petermeissner commented Sep 21, 2018

The Problem

Writing SQL is a lot of pasting-together strings. The SQL() function provides a nice entry point to mark character vectors as SQL (which will result in nicer printing).

If we have a way to mark text as SQL would it not be nice to be able to do something 'simply' put together parts of an SQL-statement in an easy manner?


The Solution

SQL() in combination with overloading the plus operator might bring a lot of productivity gain here.

#' +-Operator for SQL
#' 
#' Pastes together two character vectors marked as SQL (via \code{SQL()}). 
#' 
#' @param a first part of SQL statement
#' @param b second part of SQL statement
#'
#' @export
#' @examples
#' 
#' ## SQL("SELECT * FROM mytable ") + "WHERE id ='"  + 1:10 + "'"
#' ## <SQL> SELECT * FROM mytable WHERE id ='1'
#' ## <SQL> SELECT * FROM mytable WHERE id ='2'
#' ## <SQL> SELECT * FROM mytable WHERE id ='3'
#' ## <SQL> SELECT * FROM mytable WHERE id ='4'
#' ## <SQL> SELECT * FROM mytable WHERE id ='5'
#' ## <SQL> SELECT * FROM mytable WHERE id ='6'
#' ## <SQL> SELECT * FROM mytable WHERE id ='7'
#' ## <SQL> SELECT * FROM mytable WHERE id ='8'
#' ## <SQL> SELECT * FROM mytable WHERE id ='9'
#' ## <SQL> SELECT * FROM mytable WHERE id ='10'
#' 
"+.SQL" <- function(a,b) {SQL(paste0(a,b))}
@krlmlr
Copy link
Member

krlmlr commented Oct 1, 2018

Thanks. If dbx adopts dbxSQL(), the + operator (perhaps implemented as S4 method?) could also live there. I'd rather not expand DBI in this direction at this time.

@krlmlr krlmlr closed this as completed Oct 1, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Oct 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants