-
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.
chore: move getTableSignature to separate files
- Loading branch information
1 parent
e2c4934
commit 5f043bd
Showing
4 changed files
with
50 additions
and
55 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#' @importFrom methods setGeneric | ||
methods::setGeneric("getTableSignature", | ||
function(.data, conn = NULL) standardGeneric("getTableSignature"), | ||
signature = "conn") | ||
|
||
methods::setMethod("getTableSignature", "DBIConnection", function(.data, conn) { | ||
# Define the column types to be updated based on backend class | ||
col_types <- DBI::dbDataType(conn, .data) | ||
|
||
backend_coltypes <- list( | ||
"PqConnection" = c( | ||
checksum = "TEXT", | ||
from_ts = "TIMESTAMP", | ||
until_ts = "TIMESTAMP" | ||
), | ||
"SQLiteConnection" = c( | ||
checksum = "TEXT", | ||
from_ts = "TEXT", | ||
until_ts = "TEXT" | ||
), | ||
"Microsoft SQL Server" = c( | ||
checksum = "varchar(32)", | ||
from_ts = "DATETIME2", | ||
until_ts = "DATETIME2" | ||
) | ||
) | ||
|
||
checkmate::assert_choice(class(conn), names(backend_coltypes)) | ||
|
||
# Update columns with indices instead of names to avoid conflicts | ||
special_cols <- backend_coltypes[[class(conn)]] | ||
special_indices <- (1 + length(.data) - length(special_cols)):length(.data) | ||
|
||
return(replace(col_types, special_indices, special_cols)) | ||
}) | ||
|
||
methods::setMethod("getTableSignature", "NULL", function(.data, conn) { | ||
# Emulate product of DBI::dbDataType | ||
signature <- dplyr::summarise(.data, dplyr::across(tidyselect::everything(), ~ class(.)[1])) | ||
|
||
stats::setNames(as.character(signature), names(signature)) | ||
|
||
return(signature) | ||
}) |
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,6 @@ | ||
test_that("getTableSignature() generates a signature for NULL connections", { | ||
expect_identical( | ||
lapply(cars, class), | ||
as.list(getTableSignature(cars, conn = NULL)) | ||
) | ||
}) |