diff --git a/DESCRIPTION b/DESCRIPTION index e069cf5..ac0f4a3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: dfeR Title: Common DfE R tasks -Version: 0.3.0 +Version: 0.3.1 Authors@R: c( person("Cam", "Race", , "cameron.race@education.gov.uk", role = c("aut", "cre")), person("Laura", "Selby", , "laura.selby@education.gov.uk", role = "aut"), @@ -30,4 +30,4 @@ Config/testthat/edition: 3 Encoding: UTF-8 Language: en-GB Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/NEWS.md b/NEWS.md index a2042a2..34c27bb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# dfeR 0.3.1 + +Fix bug in get_clean_sql() where using the additional settings would lose the original SQL statement + # dfeR 0.3.0 Add pretty_* functions for presenting pretty numbers: diff --git a/R/get_clean_sql.R b/R/get_clean_sql.R index 4512258..6ea4027 100644 --- a/R/get_clean_sql.R +++ b/R/get_clean_sql.R @@ -58,7 +58,8 @@ get_clean_sql <- function(filepath, additional_settings = FALSE) { # Prefix with settings that sometimes help sql_string <- paste( "SET ANSI_PADDING OFF", - "SET NOCOUNT ON;" + "SET NOCOUNT ON;", + sql_string ) } diff --git a/README.Rmd b/README.Rmd index e68b6ab..bf598b6 100644 --- a/README.Rmd +++ b/README.Rmd @@ -111,7 +111,6 @@ format_ay(202425) format_fy(202425) format_ay_reverse("2024/25") format_fy_reverse("2024-25") - ``` For more details on all the functions available in this package, and examples of how to use them, please see our [dfeR package reference documentation](https://dfe-analytical-services.github.io/dfeR/reference/index.html). diff --git a/inst/WORDLIST b/inst/WORDLIST index 6598236..497acf7 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -6,13 +6,15 @@ ORCID POSIXct SSMS Sys -alt ay +center dbplyr dfeshiny fy lauraselby +num odbc pkgdown renv +sep sql diff --git a/tests/testthat/test-get_clean_sql.R b/tests/testthat/test-get_clean_sql.R index 7aa4ea2..9a83c05 100644 --- a/tests/testthat/test-get_clean_sql.R +++ b/tests/testthat/test-get_clean_sql.R @@ -18,6 +18,16 @@ test_that("Adds additional settings", { ) }) +test_that("Keeps the query when adding additional settings", { + # Check that the output ends with the desired lines + expect_true( + grepl( + "\\[my_database_table\\]$", + get_clean_sql("../sql_scripts/simple.sql", additional_settings = TRUE) + ) + ) +}) + test_that("Doesn't add additional settings", { # Check that the output doesn't start with the additional lines expect_false( diff --git a/vignettes/connecting_to_sql.Rmd b/vignettes/connecting_to_sql.Rmd index 6605b48..855f5d6 100644 --- a/vignettes/connecting_to_sql.Rmd +++ b/vignettes/connecting_to_sql.Rmd @@ -24,6 +24,10 @@ Which you use will depend on how comfortable you are with SQL and R and if you a For more information on the other two methods, or on troubleshooting the connection between R and SQL in the Department for Education (DfE), please see the [Interacting with a SQL database section of our Analysts' Guide](https://dfe-analytical-services.github.io/analysts-guide/learning-development/r.html#interacting-with-a-sql-database-from-within-r-scripts). +## Pre-requisites + +To connect to SQL you will need SQL drivers installed. These come as standard currently with the Microsoft SQL Server Management Studio downloads from Department for Education's software center. Make sure to have downloaded the latest version before starting. + ## Connecting to a database Usually in the DfE we use a combination of [odbc](https://odbc.r-dbi.org/) and [DBI](https://dbi.r-dbi.org/) to connect to our SQL databases. @@ -50,6 +54,10 @@ con <- DBI::dbConnect(odbc::odbc(), For advice on finding your database details, or connecting to a SQL database from an R Shiny app that is deployed on a server, please contact the [Statistics Development Team](mailto:statistics.development@education.gov.uk) who will be able to advise on the setup and steps required. +Note that your server name may include backslashes, if it does you'll need to make sure to have two backslashes in your R code due to the way that special characters are escaped. + +For example, in SQL Server Management Studio (SSMS) or Azure Data Studio you might call your server `T1PRANMSQL\SQLPROD,60125`, but in R when adding the server name you'd need to call it `T1PRANMSQL\\SQLPROD,60125`. + ## Reading a SQL script into R 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. @@ -79,7 +87,7 @@ sql_query_result <- DBI::dbGetQuery( ### Troubleshooting -Our first advice if you hit an error, would be to check that your SQL script runs in SQL Server Management Studio (SSMS) and is a valid SQL 'SELECT' query that returns a single output. +Our first advice if you hit an error, would be to check that your SQL script runs in SQL Server Management Studio (SSMS) or Azure Data Studio and is a valid SQL 'SELECT' query that returns a single output. Assuming that it runs fine in SSMS, the next thing to try is to set additional settings while cleaning the SQL script. You can do this with the `additional_settings` argument in the `get_clean_sql()` function.