Skip to content

Commit

Permalink
Merge pull request #29 from poissonconsulting/styler
Browse files Browse the repository at this point in the history
Styler
  • Loading branch information
dunkenwg authored Nov 7, 2024
2 parents a1d5a1b + 3448db1 commit 3c3da21
Show file tree
Hide file tree
Showing 18 changed files with 610 additions and 381 deletions.
10 changes: 5 additions & 5 deletions R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ check_flob_query <- function(x, slob = FALSE) {
abort_chk("Can't find flob in that location.")
}

if(vld_false(slob)){
if (vld_false(slob)) {
class(x) <- c("flob", "blob")
flobr::chk_flob(x) # this is a problem when x is a blob and blob = FALSE
} else if (vld_true(slob)){
} else if (vld_true(slob)) {
class(x) <- "blob"
flobr::chk_slob(x)
class(x) <- "list"
x <- blob::as_blob(x)
names(x) <- NULL
} else {
class(x) <- c("flob", "blob")
if(!flobr::vld_flob(x)){
if (!flobr::vld_flob(x)) {
class(x) <- c("blob")
chkor_vld(vld_slob(x), flobr::vld_flob(x))
class(x) <- "list"
Expand All @@ -85,9 +85,9 @@ check_flob_query <- function(x, slob = FALSE) {
invisible(x)
}

check_pk <- function(table_name, conn){
check_pk <- function(table_name, conn) {
pk <- table_pk(table_name, conn)
if(!length(pk)){
if (!length(pk)) {
abort_chk("Table `", table_name, "` must have a primary key.")
}
return(pk)
Expand Down
14 changes: 7 additions & 7 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ table_info <- function(table_name, conn) {
table_info
}

table_pk <- function(table_name, conn){
table_pk <- function(table_name, conn) {
info <- table_info(table_name, conn)
info$name[info$pk > 0]
}

table_pk_df <- function(table_name, conn){
table_pk_df <- function(table_name, conn) {
info <- table_info(table_name, conn)
pk <- info$name[info$pk > 0]
key <- data.frame(matrix(ncol = length(pk), nrow = 1, dimnames = list(NULL, pk)))
for(i in pk){
for (i in pk) {
type <- info$type[info$name == i]
x <- switch (type,
x <- switch(type,
"TEXT" = character(),
"INTEGER" = integer(),
"BOOLEAN" = logical(),
Expand All @@ -54,10 +54,10 @@ table_pk_df <- function(table_name, conn){
)
key[i] <- x
}
key[0,,drop = FALSE]
key[0, , drop = FALSE]
}

sql_pk <- function(x){
sql_pk <- function(x) {
paste0("`", paste(x, collapse = "`, `"), "`")
}

Expand All @@ -70,7 +70,7 @@ is_column_blob <- function(column_name, table_name, conn) {
toupper(table_column_type(column_name, table_name, conn)) == "BLOB"
}

blob_columns <- function(table_name, conn){
blob_columns <- function(table_name, conn) {
table_info <- table_info(table_name, conn)
table_info$name[table_info$type == "BLOB"]
}
Expand Down
9 changes: 4 additions & 5 deletions R/dbflob.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Write flob
#'
#' Write a \code{\link[flobr]{flob}} to a SQLite database.
#' Write a [flobr::flob()] to a SQLite database.
#'
#' @param flob A flob.
#' @param column_name A string of the name of the BLOB column.
Expand Down Expand Up @@ -50,7 +50,7 @@ write_flob <- function(flob, column_name, table_name, key, conn, exists = NA) {

#' Read flob
#'
#' Read a \code{\link[flobr]{flob}} from a SQLite database.
#' Read a [flobr::flob()] from a SQLite database.
#'
#' @inheritParams write_flob
#' @param slob A logical scalar specifying whether to process as slobs (serialized blobs) instead of flobs.
Expand Down Expand Up @@ -137,11 +137,10 @@ add_blob_column <- function(column_name, table_name, conn) {

sql <- "ALTER TABLE ?table_name ADD ?column_name BLOB"
sql <- sql_interpolate(sql, conn,
table_name = table_name,
column_name = column_name
table_name = table_name,
column_name = column_name
)

execute(sql, conn)
invisible(TRUE)
}

77 changes: 43 additions & 34 deletions R/import.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Import flobs.
#'
#' Import \code{\link[flobr]{flob}}s to SQLite database column from directory.
#' Import [flobr::flob()]s to SQLite database column from directory.
#' Values in file name are matched to table primary key to determine where to write flob.
#'
#' @inheritParams write_flob
Expand All @@ -22,15 +22,15 @@
#' conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
#' DBI::dbGetQuery(conn, "CREATE TABLE Table1 (CharColumn TEXT PRIMARY KEY NOT NULL)")
#' DBI::dbWriteTable(conn, "Table1", data.frame(CharColumn = c("a", "b")), append = TRUE)
#' key <- data.frame(CharColumn = "a", stringsAsFactors = FALSE)[0,,drop = FALSE]
#' key <- data.frame(CharColumn = "a", stringsAsFactors = FALSE)[0, , drop = FALSE]
#' dir <- tempdir()
#' write.csv(key, file.path(dir, "a.csv"))
#' import_flobs("BlobColumn", "Table1", conn, dir)
#' DBI::dbDisconnect(conn)
import_flobs <- function(column_name, table_name, conn,
dir = ".", sep = "_-_", pattern = ".*", sub = FALSE,
exists = FALSE, recursive = FALSE,
replace = FALSE){
replace = FALSE) {
check_sqlite_connection(conn)
check_table_name(table_name, conn)
check_column_name(column_name, table_name, exists = exists, conn)
Expand All @@ -44,17 +44,19 @@ import_flobs <- function(column_name, table_name, conn,
chk_string(pattern)


if(vld_false(sub)) {
if (vld_false(sub)) {
files <- list_files(dir, recursive = recursive, pattern = pattern)
names(files) <- basename(files)
if(anyDuplicated(names(files)))
if (anyDuplicated(names(files))) {
stop("File names must be unique.", call. = FALSE)
}
} else {
files <- list_files(dir, recursive = NA, pattern = pattern)
names(files) <- basename(dirname(files))
if(anyDuplicated(names(files)))
if (anyDuplicated(names(files))) {
stop("Directory names must be unique.", call. = FALSE)
if(is.na(sub)) {
}
if (is.na(sub)) {
dirs <- list_dirs(dir, recursive = NA, pattern = pattern)
names(dirs) <- basename(dirs)
dirs[names(files)] <- files
Expand All @@ -66,41 +68,44 @@ import_flobs <- function(column_name, table_name, conn,
key <- table_pk_df(table_name, conn)

column_exists <- column_exists(column_name, table_name, conn = conn)
if(!exists && !column_exists)
if (!exists && !column_exists) {
add_blob_column(column_name, table_name, conn)
}

ui_line(glue("Writing files to database"))

success <- rep(FALSE, length = length(files))
names(success) <- names(files)

for(i in seq_along(files)){
for (i in seq_along(files)) {
values <- parse_filename(names(files)[i], sep)

if(is_length_unequal(values, key)){
if (is_length_unequal(values, key)) {
ui_oops(glue("File {i}: can't write {names(files)[i]} to database. The number of hyphen-separated values must be identical to the number of columns in `key`."))
next
}

for(j in seq_along(values)){
for (j in seq_along(values)) {
key[i, j] <- values[j]
}

y <- try(read_flob(column_name, table_name, key[i,, drop = FALSE], conn), silent = TRUE)
if(!replace && !is_try_error(y)){
y <- try(read_flob(column_name, table_name, key[i, , drop = FALSE], conn), silent = TRUE)
if (!replace && !is_try_error(y)) {
ui_oops(glue("File {i}: can't write {names(files)[i]} to database. Flob already exists in that location and replace = FALSE"))
next
}

if(!is.na(files[i])) {
if (!is.na(files[i])) {
flob <- flobr::flob(files[i])
x <- try(write_flob(flob, key = key[i,,drop = FALSE],
column_name = column_name,
table_name = table_name,
conn = conn,
exists = TRUE), silent = TRUE)

if(!is_try_error(x)){
x <- try(write_flob(flob,
key = key[i, , drop = FALSE],
column_name = column_name,
table_name = table_name,
conn = conn,
exists = TRUE
), silent = TRUE)

if (!is_try_error(x)) {
success[i] <- TRUE
ui_done(glue("File {i}: {names(files)[i]} written to database"))
} else {
Expand All @@ -109,16 +114,18 @@ import_flobs <- function(column_name, table_name, conn,
next
}

if(is_try_error(y)) {
if (is_try_error(y)) {
ui_done(glue("File {i}: {names(files)[i]} already absent from database."))
success[i] <- TRUE
next
}
x <- try(delete_flob(key = key[i,,drop = FALSE],
column_name = column_name,
table_name = table_name,
conn = conn))
if(!is_try_error(x)){
x <- try(delete_flob(
key = key[i, , drop = FALSE],
column_name = column_name,
table_name = table_name,
conn = conn
))
if (!is_try_error(x)) {
success[i] <- TRUE
ui_done(glue("File {i}: {names(files)[i]} deleted in database."))
} else {
Expand All @@ -130,7 +137,7 @@ import_flobs <- function(column_name, table_name, conn,

#' Import all flobs.
#'
#' Import \code{\link[flobr]{flob}}s to SQLite database from directory.
#' Import [flobr::flob()]s to SQLite database from directory.
#' Table and column names are matched to directory names within main directory.
#' Values in file names are matched to table primary key to determine where to write flob.
#'
Expand All @@ -154,7 +161,7 @@ import_flobs <- function(column_name, table_name, conn,
#' DBI::dbDisconnect(conn)
import_all_flobs <- function(conn, dir = ".", sep = "_-_", pattern = ".*",
sub = FALSE,
exists = FALSE, replace = FALSE){
exists = FALSE, replace = FALSE) {
check_sqlite_connection(conn)
chk_dir(dir)
chk_string(sep)
Expand All @@ -166,17 +173,19 @@ import_all_flobs <- function(conn, dir = ".", sep = "_-_", pattern = ".*",
dirs <- dir_tree(dir, sub)
success <- vector(mode = "list", length = length(dirs))

for(i in seq_along(dirs)){
for (i in seq_along(dirs)) {
x <- dirs[[i]]
table_name <- x[1]
column_name <- x[2]
inner_dir <- file.path(dir, table_name, column_name)
ui_line(glue("Table name: {ui_value(table_name)}"))
ui_line(glue("Column name: {ui_value(column_name)}"))
success[[i]] <- import_flobs(column_name = x[2], table_name = x[1],
conn = conn, dir = inner_dir, sep = sep,
pattern = pattern, sub = sub,
exists = exists, replace = replace)
success[[i]] <- import_flobs(
column_name = x[2], table_name = x[1],
conn = conn, dir = inner_dir, sep = sep,
pattern = pattern, sub = sub,
exists = exists, replace = replace
)
ui_line("")
}

Expand Down
Loading

0 comments on commit 3c3da21

Please sign in to comment.