From da0771e2b783b54be44cfc1241544ccad2e405fa Mon Sep 17 00:00:00 2001 From: Kjell Konis Date: Tue, 5 Mar 2024 05:52:45 -0800 Subject: [PATCH] Added Id method for dbExistsTable to address issue #498 --- DESCRIPTION | 1 + R/dbExistsTable_SQLiteConnection_Id.R | 27 +++++++++++++++++++++++++++ man/SQLiteConnection-class.Rd | 5 +++++ 3 files changed, 33 insertions(+) create mode 100644 R/dbExistsTable_SQLiteConnection_Id.R diff --git a/DESCRIPTION b/DESCRIPTION index 63884b05..c5106b03 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -103,6 +103,7 @@ Collate: 'dbDataType_SQLiteConnection.R' 'dbDataType_SQLiteDriver.R' 'dbDisconnect_SQLiteConnection.R' + 'dbExistsTable_SQLiteConnection_Id.R' 'dbExistsTable_SQLiteConnection_character.R' 'dbFetch_SQLiteResult.R' 'dbGetException_SQLiteConnection.R' diff --git a/R/dbExistsTable_SQLiteConnection_Id.R b/R/dbExistsTable_SQLiteConnection_Id.R new file mode 100644 index 00000000..056b181e --- /dev/null +++ b/R/dbExistsTable_SQLiteConnection_Id.R @@ -0,0 +1,27 @@ +#' @rdname SQLiteConnection-class +#' @usage NULL +dbExistsTable_SQLiteConnection_Id <- function(conn, name, ...) { + stopifnot(is(name, "Id")) + + id <- as.list(dbUnquoteIdentifier(conn, dbQuoteIdentifier(conn, name))[[1]]@name) + schema <- id$schema + table <- id$table + + if (!is.null(schema)) { + schemas <- dbGetQuery(conn, "SELECT name FROM pragma_database_list;")$name + + if (!(schema %in% schemas)) { + return(FALSE) + } + } + + sql <- sqliteListTablesQuery(conn, schema, SQL("$name")) + rs <- dbSendQuery(conn, sql) + dbBind(rs, list(name = tolower(table))) + on.exit(dbClearResult(rs), add = TRUE) + + nrow(dbFetch(rs, 1L)) > 0 +} +#' @rdname SQLiteConnection-class +#' @export +setMethod("dbExistsTable", c("SQLiteConnection", "Id"), dbExistsTable_SQLiteConnection_Id) diff --git a/man/SQLiteConnection-class.Rd b/man/SQLiteConnection-class.Rd index b4e00869..fc61b0e7 100644 --- a/man/SQLiteConnection-class.Rd +++ b/man/SQLiteConnection-class.Rd @@ -1,6 +1,7 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/SQLiteConnection.R, % R/dbAppendTable_SQLiteConnection.R, R/dbDataType_SQLiteConnection.R, +% R/dbExistsTable_SQLiteConnection_Id.R, % R/dbExistsTable_SQLiteConnection_character.R, % R/dbGetException_SQLiteConnection.R, R/dbGetInfo_SQLiteConnection.R, % R/dbIsValid_SQLiteConnection.R, R/dbListTables_SQLiteConnection.R, @@ -18,6 +19,8 @@ \alias{dbAppendTable,SQLiteConnection-method} \alias{dbDataType_SQLiteConnection} \alias{dbDataType,SQLiteConnection-method} +\alias{dbExistsTable_SQLiteConnection_Id} +\alias{dbExistsTable,SQLiteConnection,Id-method} \alias{dbExistsTable_SQLiteConnection_character} \alias{dbExistsTable,SQLiteConnection,character-method} \alias{dbGetException_SQLiteConnection} @@ -50,6 +53,8 @@ \S4method{dbDataType}{SQLiteConnection}(dbObj, obj, ...) +\S4method{dbExistsTable}{SQLiteConnection,Id}(conn, name, ...) + \S4method{dbExistsTable}{SQLiteConnection,character}(conn, name, ...) \S4method{dbGetException}{SQLiteConnection}(conn, ...)