-
Notifications
You must be signed in to change notification settings - Fork 15
/
paths.R
68 lines (60 loc) · 1.68 KB
/
paths.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#' make a mock path
#'
#' @param path the path to look in
#' @param type what type of query is it? (e.g. `SELECT`, `INSERT`)
#' @param hash the hash of the query
#'
#' @return a constructed path to a mock
#'
#' @keywords internal
#'
#' @export
make_path <- function(path, type, hash) {
path_out <- file.path(path, db_path_sanitize(paste0(type, "-", hash, ".R")))
return(path_out)
}
#' Make a (short) hash from a string
#'
#' @param string the string to hash
#' @param n how long should the hash be? (default: 6)
#'
#' @return a hash for the string
#'
#' @importFrom digest digest
#'
#' @keywords internal
#'
#' @export
hash <- function(string, n = 6) {
string <- clean_statement(string)
return(substr(digest(as.character(string)), 1, n))
}
#' Clean a statement string
#'
#' SQL statement strings sometimes have characters and specifications that don't
#' change the meaning or are determined at query time. To avoid this, before
#' hashing a statement we clean/strip these from the statement
#'
#' @param string an SQL statement to clean
#'
#' @return the SQL statement stripped of extraneous bits
#'
#' @keywords internal
#'
#' @export
clean_statement <- function(string) {
string <- ignore_quotes(string)
string <- ignore_dbplyr_unique_names(string)
return(string)
}
read_file <- function(file_path) source(file_path, keep.source = FALSE)$value
# search through db_mock_paths() to find a file, returning the first
find_file <- function(file_path) {
for (mock_path in db_mock_paths()) {
path_to_check <- file.path(mock_path, file_path)
if (file.exists(path_to_check)) {
return(path_to_check)
}
}
stop("Couldn't find the file ", file_path, " in any of the mock directories.")
}