|
49 | 49 | #' and a function with the necessary selection_parameters are given, |
50 | 50 | #' the select_statement constructed by the function will be used. |
51 | 51 | #' |
| 52 | +#' Be aware that there is a known problem for the R-package "odbc" when selecting |
| 53 | +#' long text variables like "anamnese". Such variables need to be put last in |
| 54 | +#' the select statement. The variable "anamnese" is included in the main views |
| 55 | +#' used for retrieving data from PJS. \code{retrieve_PJSdata} will rewrite |
| 56 | +#' select statements generated by the \code{build_query}-functions and sql |
| 57 | +#' statements starting with "SELECT * FROM 'tablename'", but will not rewrite |
| 58 | +#' the sql statement in other situations. If this happens, you must either |
| 59 | +#' rewrite the sql statement and put "anamnese" last in the select statement |
| 60 | +#' or use the R-package "RODBC" instead. If using "RODBC", you cannot use |
| 61 | +#' \code{retrieve_PJSdata}. |
| 62 | +#' |
52 | 63 | #' The output is a named list where each entry is a data frame with PJS data. If |
53 | 64 | #' the select statement is named, the returned data frame will have that name. |
54 | 65 | #' If the select statement is unnamed, it will try to identify the first |
@@ -237,6 +248,25 @@ retrieve_PJSdata <- function(year = NULL, |
237 | 248 | # PERFORM SELECTION AND STANDARDISATION FOR EACH SELECT STATEMENT ---- |
238 | 249 | for (i in c(1:length(select_statement))) { |
239 | 250 |
|
| 251 | + # MOVE anamnese LAST IN SELECTION STATEMENT ---- |
| 252 | + # Identify table in the first select clause in sql statement |
| 253 | + db_table <- sub("SELECT[[:space:]]*\\*[[:space:]]*FROM[[:space:]]*([^[:space:]]*).*", |
| 254 | + "\\1", |
| 255 | + select_statement[[i]], ignore.case = TRUE) |
| 256 | + # List fields in the db_table |
| 257 | + if (nchar(db_table) > 0 && regexpr("[[:space:],\\*]", db_table) < 0) { |
| 258 | + fields <- DBI::dbListFields(conn = journal_rapp, name = db_table) |
| 259 | + # Put anamnese last in select clause if exist in the db_table |
| 260 | + if ("anamnese" %in% tolower(fields)) { |
| 261 | + fields <- paste0(fields, collapse = ", ") |
| 262 | + fields <- paste0(sub("anamnese, ", "", fields, ignore.case = TRUE), ", anamnese") |
| 263 | + select_statement[[i]] <- sub(paste0("SELECT[[:space:]]*\\*[[:space:]]*FROM[[:space:]]*", db_table), |
| 264 | + paste("SELECT", fields, "FROM", db_table), |
| 265 | + select_statement[[i]], |
| 266 | + ignore.case = TRUE) |
| 267 | + } |
| 268 | + } |
| 269 | + |
240 | 270 | # READ DATA FROM PJS ---- |
241 | 271 | PJSdata[[i]] <- DBI::dbGetQuery(con = journal_rapp, |
242 | 272 | statement = select_statement[[i]]) |
|
0 commit comments