Skip to content

Commit

Permalink
Fix bug in get_clean_sql and add pre-requisite and additional notes o…
Browse files Browse the repository at this point in the history
…n server naming (#74)

* Add pre-requisite and additional notes on server naming

* Fix bug in get_clean_sql additional settings that didn't include the SQL query

* Increment version number to 0.3.1
  • Loading branch information
cjrace authored Jul 24, 2024
1 parent 1572161 commit f082378
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre")),
person("Laura", "Selby", , "[email protected]", role = "aut"),
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 2 additions & 1 deletion R/get_clean_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
1 change: 0 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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).
4 changes: 3 additions & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ ORCID
POSIXct
SSMS
Sys
alt
ay
center
dbplyr
dfeshiny
fy
lauraselby
num
odbc
pkgdown
renv
sep
sql
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 @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion vignettes/connecting_to_sql.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:[email protected]) 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.
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit f082378

Please sign in to comment.