Skip to content

Should glue_sql_collapse() exist? #103

@DavisVaughan

Description

@DavisVaughan

I'm working on a template for a SQL Server MERGE function and want to make the ON statement as dynamic as possible. I do that by building it up one piece at a time. This process involves a glue_collapse() followed by a DBI::SQL() because glue_collapse() coerces to a character(). Should there be a glue_sql_collapse() to support this?

library(glue)
library(odbc)
#> Warning: package 'odbc' was built under R version 3.4.4
library(dbplyr)
#> Warning: package 'dbplyr' was built under R version 3.4.4

x <- c("thing1", "thing2")
y <- c("other1", "other2")

# Mock my S4 sql server connection
setClass("Microsoft SQL Server", representation(quote = "character"), contains = "OdbcConnection")
conn <- new("Microsoft SQL Server", quote = "\"")

sql_conditions <- glue_sql(' "target".{`x`} = "source".{`y`} ', .con = conn)
sql_conditions
#> <SQL>  "target"."thing1" = "source"."other1" 
#> <SQL>  "target"."thing2" = "source"."other2"

# Can't just use glue_collapse(). Get back a character.
sql_combined_conditions <- DBI::SQL(glue::glue_collapse(sql_conditions, sep = " AND "))
sql_combined_conditions
#> <SQL>  "target"."thing1" = "source"."other1"  AND  "target"."thing2" = "source"."other2"

sql_ON <- glue::glue_sql("ON {sql_combined_conditions}", .con = conn)
sql_ON
#> <SQL> ON  "target"."thing1" = "source"."other1"  AND  "target"."thing2" = "source"."other2"

Created on 2018-07-31 by the reprex package (v0.2.0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions