| 12345678910111213141516171819202122232425262728293031323334 |
- #' Query to drop columns from PostgreSQL database according df column names
- #' @details Veergude kustutamine Postgresql andmebaasist etteantud df veergude nimede alusel.
- #' Andmebaasist kustutatakse df veeru nimedega kokkulangevad andmebaasi veerud.
- #' @param tbl The database table name.
- #' @param df The DataFrame name.
- #' @return query string.
- #' @seealso \code{\link{queryDropColumnsFromPostgresDbAccordingDF}},
- #' \code{\link{queryAddColumnsToPostgresDbAccordingDF}},
- #' \code{\link{queryWithoutGeom}}
- #' @examples \dontrun{
- #' # Connect with database
- #' tblName = "transport_tsoonid_tallinn"
- #' q <- queryDropColumnsFromPostgresDbAccordingDF(df = tsoonAsumLosa, tbl = tblName)
- #' con <- myPostgresConnect(host = conf$host, port = conf$port, user = conf$user,
- #' password = conf$password, dbname = conf$dbname, type = "RPostgreSQL")
- #' res <- DBI::dbSendQuery(conn = con, statement = q)
- #' DBI::dbClearResult(res)
- #' DBI::dbDisconnect(con)
- #'
- #' }
- #'
- #' @export
- #'
- queryDropColumnsFromPostgresDbAccordingDF <- function(df, tbl) {
- colnames(df) <- dfNamesToLower(df = df)
- # Kustutame andmebaasist need veerud.
- res <- sapply(1:ncol(df), function(x) {
- paste0("DROP COLUMN IF EXISTS ", names(df)[x])
- })
- res <- paste(res, collapse = ", ")
- res <- paste0("ALTER TABLE ", tbl , " ", res, "")
- res
- }
|