-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix typo in contributing guide * start to add get_clean_sql and tests (WIP) * add code cov details to contributing file * Add link to reference documentation in the examples section * commit current progress adding unit tests (WIP) * fix typo in contributing guide * start to add get_clean_sql and tests (WIP) * add code cov details to contributing file * Add link to reference documentation in the examples section * commit current progress adding unit tests (WIP) * fix tests for sql function * add roxygen2 examples to contributing file * add connecting to sql vignette * update get clean sql, contributing and reference documentation * Increment version number to 0.2.0 * tidy package following lintr advice * remove capitalised extension test (causes test errors on linux) * add Jen as a contributor to the package * Initial response to PR comments (more to complete on troubleshooting) * fix lint issues * update connecting to sql vignette to specific select style queries only * update wordlist and rebuild package docs
- Loading branch information
Showing
16 changed files
with
355 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
.httr-oauth | ||
.DS_Store | ||
docs | ||
inst/doc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
Type: Package | ||
Package: dfeR | ||
Title: Common DfE R tasks | ||
Version: 0.1.1 | ||
Version: 0.2.0 | ||
Authors@R: c( | ||
person("Cam", "Race", , "[email protected]", role = c("aut", "cre")), | ||
person("Laura", "Selby", , "[email protected]", role = "aut"), | ||
person("Adam", "Robinson", role = "aut") | ||
person("Adam", "Robinson", role = "aut"), | ||
person("Jen", "Machin", , "[email protected]", role = "ctb"), | ||
person("Rich", "Bielby", , "[email protected]", role = "ctb", | ||
comment = c(ORCID = "0000-0001-9070-9969")) | ||
) | ||
Description: This package contains R functions to allow DfE analysts to | ||
re-use code for common analytical tasks that are undertaken across the | ||
|
@@ -17,8 +20,12 @@ BugReports: https://github.com/dfe-analytical-services/dfeR/issues | |
Imports: | ||
lifecycle | ||
Suggests: | ||
knitr, | ||
rmarkdown, | ||
spelling, | ||
testthat (>= 3.0.0) | ||
VignetteBuilder: | ||
knitr | ||
Config/testthat/edition: 3 | ||
Encoding: UTF-8 | ||
Language: en-GB | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#' Get a cleaned SQL script into R | ||
#' | ||
#' @description | ||
#' This function cleans a SQL script, ready for using within R in the DfE. | ||
#' | ||
#' @param filepath path to a SQL script | ||
#' @param additional_settings TRUE or FALSE boolean for the addition of | ||
#' settings at the start of the SQL script | ||
#' @return Cleaned string containing SQL query | ||
#' @export | ||
#' @examples | ||
#' # This assumes you have already set up a database connection | ||
#' # and that the filepath for the function exists | ||
#' # For more details see the vignette on connecting to SQL | ||
#' | ||
#' # Pull a cleaned version of the SQL file into R | ||
#' if (file.exists("your_script.sql")) { | ||
#' sql_query <- get_clean_sql("your_script.sql") | ||
#' } | ||
get_clean_sql <- function(filepath, additional_settings = FALSE) { | ||
if (!additional_settings %in% c(TRUE, FALSE)) { | ||
stop( | ||
"additional_settings must be either TRUE or FALSE" | ||
) | ||
} | ||
|
||
# check filepath leads to a SQL file | ||
if (tolower(tools::file_ext(filepath)) != "sql") { | ||
stop("filepath must point to a SQL script, with a .sql extension") | ||
} | ||
|
||
# The file() function will error if the file can't be found | ||
# Open a connection to the file | ||
con <- file(filepath, "r") | ||
sql_string <- "" | ||
|
||
while (TRUE) { | ||
line <- readLines(con, n = 1) | ||
|
||
if (length(line) == 0) { | ||
break | ||
} | ||
|
||
line <- gsub("\\t", " ", line) | ||
line <- gsub("\\n", " ", line) | ||
|
||
if (grepl("--", line) == TRUE) { | ||
line <- paste(sub("--", "/*", line), "*/") | ||
} | ||
|
||
sql_string <- paste(sql_string, line) | ||
} | ||
|
||
# Close connection to the file | ||
close(con) | ||
|
||
if (additional_settings == TRUE) { | ||
# Prefix with settings that sometimes help | ||
sql_string <- paste( | ||
"SET ANSI_PADDING OFF", | ||
"SET NOCOUNT ON;" | ||
) | ||
} | ||
|
||
return(sql_string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ CMD | |
Codecov | ||
DfE | ||
Lifecycle | ||
ORCID | ||
SSMS | ||
ay | ||
ch | ||
dbplyr | ||
dfeshiny | ||
ethz | ||
fy | ||
lauraselby | ||
odbc | ||
pkgdown | ||
renv | ||
stat | ||
sql |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- Simple SQL script to get all data from my database table | ||
|
||
SELECT * FROM [my_database_table] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
test_that("Can retrieve basic script", { | ||
expect_equal( | ||
get_clean_sql("../sql_scripts/simple.sql"), | ||
paste0( | ||
" /* Simple SQL script to get all data from my database table", | ||
" */ SELECT * FROM [my_database_table]" | ||
) | ||
) | ||
}) | ||
|
||
test_that("Adds additional settings", { | ||
# Check that the output starts with the desired lines | ||
expect_true( | ||
grepl( | ||
"^SET ANSI_PADDING OFF SET NOCOUNT ON;", | ||
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( | ||
grepl( | ||
"^SET ANSI_PADDING OFF SET NOCOUNT ON;", | ||
get_clean_sql("../sql_scripts/simple.sql", additional_settings = FALSE) | ||
) | ||
) | ||
# Check that the output doesn't start with the additional lines | ||
expect_false( | ||
grepl( | ||
"^SET ANSI_PADDING OFF SET NOCOUNT ON;", | ||
get_clean_sql("../sql_scripts/simple.sql") | ||
) | ||
) | ||
}) | ||
|
||
test_that("Rejects non-boolean values for additional_settings", { | ||
expect_error( | ||
get_clean_sql("../sql_scripts/simple.sql", additional_settings = "True"), | ||
"additional_settings must be either TRUE or FALSE" | ||
) | ||
expect_error( | ||
get_clean_sql("../sql_scripts/simple.sql", additional_settings = ""), | ||
"additional_settings must be either TRUE or FALSE" | ||
) | ||
}) | ||
|
||
test_that("Rejects file that don't have SQL extension", { | ||
expect_error( | ||
get_clean_sql("../spelling.R"), | ||
"" | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.html | ||
*.R |
Oops, something went wrong.