queryWithoutGeom.R 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #' Get query string without geom column.
  2. #' @details Päringu stringi koostamine ilma geomeetria veergudeta, et oleks "kerge"
  3. #' importida df.
  4. #' @param con An PostgreSQLConnection object as produced by dbConnect.
  5. #' @param tbl The database table name.
  6. #' @param geom The array with postgresql geom column names.
  7. #' @param where Other conditions in request.
  8. #' @return query string.
  9. #' @seealso \code{\link{queryDropColumnsFromPostgresDbAccordingDF}},
  10. #' \code{\link{queryAddColumnsToPostgresDbAccordingDF}},
  11. #' \code{\link{queryWithoutGeom}},
  12. #' \code{\link{queryWithGeom}}
  13. #' @examples \dontrun{
  14. #' # Connect with database
  15. #' con <- myPostgresConnect(host = conf$host, port = conf$port, user = conf$user,
  16. #' password = conf$password, dbname = conf$dbname, type = "RPostgreSQL")
  17. #' tblName = "transport_tsoonid_tallinn"
  18. #' q <- queryWithoutGeom(con = con, tbl = tblName, geom = c('geom', 'lines'), where = "LIMIT 100")
  19. #' df <- dbGetQuery(con, q)
  20. #' df
  21. #' DBI::dbClearResult(res)
  22. #' DBI::dbDisconnect(con)
  23. #'
  24. #' }
  25. #'
  26. #' @export
  27. #'
  28. queryWithoutGeom <- function(con, tbl, geom = 'geom', where = NULL) {
  29. ## All columns without geom as shp@data
  30. geom = paste(paste0("'", geom, "'"), collapse = ", ")
  31. q <- paste0("SELECT 'SELECT ' ||
  32. ARRAY_TO_STRING(ARRAY(SELECT COLUMN_NAME::VARCHAR(50)
  33. FROM INFORMATION_SCHEMA.COLUMNS
  34. WHERE TABLE_NAME='", tbl, "' AND
  35. COLUMN_NAME NOT IN (", geom,")
  36. ORDER BY ORDINAL_POSITION
  37. ), ', ') || ' FROM ", tbl, "'")
  38. q <- DBI::dbGetQuery(con,q)[1,1]
  39. q <- paste(q, where)
  40. q
  41. }