diff --git a/.Rbuildignore b/.Rbuildignore index a1d5fe5..b9c98a3 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,3 +9,5 @@ ^codecov\.yml$ ^README\.Rmd$ ^data-raw$ +^doc$ +^Meta$ diff --git a/.gitignore b/.gitignore index 16add95..a3c506a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ .DS_Store docs inst/doc +/doc/ +/Meta/ diff --git a/R/get_clean_sql.R b/R/get_clean_sql.R index 6ea4027..42ab87c 100644 --- a/R/get_clean_sql.R +++ b/R/get_clean_sql.R @@ -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), "*/") } diff --git a/man/create_time_series_lookup.Rd b/man/create_time_series_lookup.Rd index e37c776..9547671 100644 --- a/man/create_time_series_lookup.Rd +++ b/man/create_time_series_lookup.Rd @@ -14,7 +14,7 @@ usually the output of tidy_raw_lookup} single data.frame of all lookup files combined } \description{ -Take a list of tidied files, likely produced by the tidy_downloaded_lookup +Take a list of tidied files, likely produced by the tidy_raw_lookup function append together } \details{ diff --git a/tests/sql_scripts/use_example.sql b/tests/sql_scripts/use_example.sql new file mode 100644 index 0000000..fde7816 --- /dev/null +++ b/tests/sql_scripts/use_example.sql @@ -0,0 +1,5 @@ +-- Example SQL script use a Use call + +USE my_super_cool_database; + +SELECT * FROM [my_database_table]; diff --git a/tests/testthat/test-get_clean_sql.R b/tests/testthat/test-get_clean_sql.R index 9a83c05..21a7b8c 100644 --- a/tests/testthat/test-get_clean_sql.R +++ b/tests/testthat/test-get_clean_sql.R @@ -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( diff --git a/vignettes/connecting_to_sql.Rmd b/vignettes/connecting_to_sql.Rmd index 855f5d6..581c8b0 100644 --- a/vignettes/connecting_to_sql.Rmd +++ b/vignettes/connecting_to_sql.Rmd @@ -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") ``` @@ -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) ```