Skip to content

Commit

Permalink
update get_clean_sql to ignore 'USE' lines
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrace committed Sep 26, 2024
1 parent 45a6834 commit 111ee4e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
^codecov\.yml$
^README\.Rmd$
^data-raw$
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
.DS_Store
docs
inst/doc
/doc/
/Meta/
5 changes: 5 additions & 0 deletions R/get_clean_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ get_clean_sql <- function(filepath, additional_settings = FALSE) {
line <- gsub("\\t", " ", line)
line <- gsub("\\n", " ", line)

# Skip over any lines that start with 'Use'
if (grepl("^Use", line, ignore.case = TRUE)) {
next
}

if (grepl("--", line) == TRUE) {
line <- paste(sub("--", "/*", line), "*/")
}
Expand Down
2 changes: 1 addition & 1 deletion man/create_time_series_lookup.Rd

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

5 changes: 5 additions & 0 deletions tests/sql_scripts/use_example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Example SQL script use a Use call

USE my_super_cool_database;

SELECT * FROM [my_database_table];
10 changes: 10 additions & 0 deletions tests/testthat/test-get_clean_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ test_that("Can retrieve basic script", {
)
})

test_that("Ignores USE lines", {
expect_equal(
get_clean_sql("../sql_scripts/use_example.sql"),
paste0(
" /* Example SQL script use a Use call",
" */ SELECT * FROM [my_database_table];"
)
)
})

test_that("Adds additional settings", {
# Check that the output starts with the desired lines
expect_true(
Expand Down
4 changes: 4 additions & 0 deletions vignettes/connecting_to_sql.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ For example, in SQL Server Management Studio (SSMS) or Azure Data Studio you mig

There are a number of standard characters found in SQL scripts that can cause issues when reading in a SQL script within R and we have created the `get_clean_sql()` function to assist with this. Assume you have connected to the database and assigned that connection to a `con` variable, you would then use the following line to read a cleaned version of your SQL script into R.

`get_clean_sql()` will ignore any lines of code that start with `USE` to specify a database, as your database should already be specified in your connection setup.

```{r reading clean sql, eval=FALSE}
sql_query <- dfeR::get_clean_sql("path_to_sql_file.sql")
```
Expand All @@ -72,6 +74,8 @@ Now that the SQL query is saved as a variable in the R environment you can pass

It's important to note that `dbGetQuery()` is intended to work with 'SELECT' style queries only. If you're doing something that isn't a 'SELECT' query, such as writing back into SQL, consider using the `dbExecute()` or `dbSendQuery()` functions from the [DBI package](https://dbi.r-dbi.org/) instead.



```{r executing sql query, eval=FALSE}
sql_query_result <- DBI::dbGetQuery(con, statement = sql_query)
```
Expand Down

0 comments on commit 111ee4e

Please sign in to comment.