Skip to content

Commit be4fe26

Browse files
committed
Simplify code with get_geom_name
1 parent 062b1f8 commit be4fe26

38 files changed

Lines changed: 832 additions & 1007 deletions

R/db_utils_not_exported.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dbConnCheck <- function(conn) { # nocov start
2626
#'
2727
#' @keywords internal
2828
#' @returns name of the geometry column of a table
29-
get_geom_name <- function(conn, x, rest = FALSE, collapse = FALSE) { # nocov start
29+
get_geom_name <- function(conn, x, rest = FALSE, collapse = FALSE, table_id = NULL) { # nocov start
3030

3131
# check if the table exists
3232
if (isFALSE(DBI::dbExistsTable(conn, x))) {
@@ -43,7 +43,12 @@ get_geom_name <- function(conn, x, rest = FALSE, collapse = FALSE) { # nocov st
4343

4444
# collapse columns with quoted names
4545
if (isTRUE(collapse)) {
46-
other_cols <- glue::glue_collapse(glue::glue('"{other_cols}"'), sep = ", ")
46+
if (is.null(table_id)) {
47+
other_cols <- if (length(other_cols) > 0) paste0('"', other_cols, '",', collapse = ' ') else ""
48+
} else {
49+
other_cols <- if (length(other_cols) > 0) paste0(table_id, '."', other_cols, '",', collapse = ' ') else ""
50+
}
51+
4752
}
4853

4954
return(other_cols)

R/ddbs_affine.R

Lines changed: 72 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -161,33 +161,26 @@ ddbs_rotate <- function(
161161
## handle overwrite
162162
overwrite_table(name_list$query_name, conn, quiet, overwrite)
163163

164-
## create query (no st_as_text)
165-
if (length(x_rest) == 0) {
166-
tmp.query <- glue::glue("
167-
SELECT {rotation_expr} as {x_geom} FROM {x_list$query_name};
168-
")
169-
} else {
170-
tmp.query <- glue::glue("
171-
SELECT {x_rest}, {rotation_expr} as {x_geom} FROM {x_list$query_name};
164+
## create query
165+
tmp.query <- glue::glue("
166+
CREATE TABLE {name_list$query_name} AS
167+
SELECT {x_rest}
168+
{rotation_expr} as {x_geom}
169+
FROM {x_list$query_name};
172170
")
173-
}
174171
## execute rotation query
175-
DBI::dbExecute(conn, glue::glue("CREATE TABLE {name_list$query_name} AS {tmp.query}"))
172+
DBI::dbExecute(conn, tmp.query)
176173
feedback_query(quiet)
177174
return(invisible(TRUE))
178175
}
179176

180177
# 4. Get data frame
181178
## 4.1. create query
182-
if (length(x_rest) == 0) {
183-
tmp.query <- glue::glue("
184-
SELECT ST_AsWKB({rotation_expr}) as {x_geom} FROM {x_list$query_name};
185-
")
186-
} else {
187-
tmp.query <- glue::glue("
188-
SELECT {x_rest}, ST_AsWKB({rotation_expr}) as {x_geom} FROM {x_list$query_name};
189-
")
190-
}
179+
tmp.query <- glue::glue("
180+
SELECT {x_rest}
181+
ST_AsWKB({rotation_expr}) as {x_geom}
182+
FROM {x_list$query_name};
183+
")
191184
## 4.2. retrieve results from the query
192185
data_tbl <- DBI::dbGetQuery(conn, tmp.query)
193186

@@ -310,33 +303,26 @@ ddbs_rotate_3d <- function(
310303
## handle overwrite
311304
overwrite_table(name_list$query_name, conn, quiet, overwrite)
312305

313-
## create query (no st_as_text)
314-
if (length(x_rest) == 0) {
315-
tmp.query <- glue::glue("
316-
SELECT {rotation_expr} as {x_geom} FROM {x_list$query_name};
317-
")
318-
} else {
319-
tmp.query <- glue::glue("
320-
SELECT {x_rest}, {rotation_expr} as {x_geom} FROM {x_list$query_name};
306+
## create query
307+
tmp.query <- glue::glue("
308+
CREATE TABLE {name_list$query_name} AS
309+
SELECT {x_rest}
310+
{rotation_expr} as {x_geom}
311+
FROM {x_list$query_name};
321312
")
322-
}
323313
## execute rotation query
324-
DBI::dbExecute(conn, glue::glue("CREATE TABLE {name_list$query_name} AS {tmp.query}"))
314+
DBI::dbExecute(conn, tmp.query)
325315
feedback_query(quiet)
326316
return(invisible(TRUE))
327317
}
328318

329319
# 4. Get data frame
330320
## 4.1. create query
331-
if (length(x_rest) == 0) {
332-
tmp.query <- glue::glue("
333-
SELECT ST_AsWKB({rotation_expr}) as {x_geom} FROM {x_list$query_name};
334-
")
335-
} else {
336-
tmp.query <- glue::glue("
337-
SELECT {x_rest}, ST_AsWKB({rotation_expr}) as {x_geom} FROM {x_list$query_name};
338-
")
339-
}
321+
tmp.query <- glue::glue("
322+
SELECT {x_rest}
323+
ST_AsWKB({rotation_expr}) as {x_geom}
324+
FROM {x_list$query_name};
325+
")
340326
## 4.2. retrieve results from the query
341327
data_tbl <- DBI::dbGetQuery(conn, tmp.query)
342328

@@ -444,33 +430,26 @@ ddbs_shift <- function(
444430
## handle overwrite
445431
overwrite_table(name_list$query_name, conn, quiet, overwrite)
446432

447-
## create query (no st_as_text)
448-
if (length(x_rest) == 0) {
449-
tmp.query <- glue::glue("
450-
SELECT {shift_expr} as {x_geom} FROM {x_list$query_name};
451-
")
452-
} else {
453-
tmp.query <- glue::glue("
454-
SELECT {x_rest}, {shift_expr} as {x_geom} FROM {x_list$query_name};
433+
## create query
434+
tmp.query <- glue::glue("
435+
CREATE TABLE {name_list$query_name} AS
436+
SELECT {x_rest}
437+
{shift_expr} as {x_geom}
438+
FROM {x_list$query_name};
455439
")
456-
}
457440
## execute shift query
458-
DBI::dbExecute(conn, glue::glue("CREATE TABLE {name_list$query_name} AS {tmp.query}"))
441+
DBI::dbExecute(conn, tmp.query)
459442
feedback_query(quiet)
460443
return(invisible(TRUE))
461444
}
462445

463446
# 4. Get data frame
464447
## 4.1. create query
465-
if (length(x_rest) == 0) {
466-
tmp.query <- glue::glue("
467-
SELECT ST_AsWKB({shift_expr}) as {x_geom} FROM {x_list$query_name};
468-
")
469-
} else {
470-
tmp.query <- glue::glue("
471-
SELECT {x_rest}, ST_AsWKB({shift_expr}) as {x_geom} FROM {x_list$query_name};
472-
")
473-
}
448+
tmp.query <- glue::glue("
449+
SELECT {x_rest}
450+
ST_AsWKB({shift_expr}) as {x_geom}
451+
FROM {x_list$query_name};
452+
")
474453
## 4.2. retrieve results from the query
475454
data_tbl <- DBI::dbGetQuery(conn, tmp.query)
476455

@@ -630,33 +609,26 @@ ddbs_flip <- function(
630609
## handle overwrite
631610
overwrite_table(name_list$query_name, conn, quiet, overwrite)
632611

633-
## create query (no st_as_text)
634-
if (length(x_rest) == 0) {
635-
tmp.query <- glue::glue("
636-
SELECT {flip_expr} as {x_geom} FROM {x_list$query_name};
637-
")
638-
} else {
639-
tmp.query <- glue::glue("
640-
SELECT {x_rest}, {flip_expr} as {x_geom} FROM {x_list$query_name};
612+
## create query
613+
tmp.query <- glue::glue("
614+
CREATE TABLE {name_list$query_name} AS
615+
SELECT {x_rest}
616+
{flip_expr} as {x_geom}
617+
FROM {x_list$query_name};
641618
")
642-
}
643619
## execute flip query
644-
DBI::dbExecute(conn, glue::glue("CREATE TABLE {name_list$query_name} AS {tmp.query}"))
620+
DBI::dbExecute(conn, tmp.query)
645621
feedback_query(quiet)
646622
return(invisible(TRUE))
647623
}
648624

649625
# 4. Get data frame
650626
## 4.1. create query
651-
if (length(x_rest) == 0) {
652-
tmp.query <- glue::glue("
653-
SELECT ST_AsWKB({flip_expr}) as {x_geom} FROM {x_list$query_name};
654-
")
655-
} else {
656-
tmp.query <- glue::glue("
657-
SELECT {x_rest}, ST_AsWKB({flip_expr}) as {x_geom} FROM {x_list$query_name};
658-
")
659-
}
627+
tmp.query <- glue::glue("
628+
SELECT {x_rest}
629+
ST_AsWKB({flip_expr}) as {x_geom}
630+
FROM {x_list$query_name};
631+
")
660632
## 4.2. retrieve results from the query
661633
data_tbl <- DBI::dbGetQuery(conn, tmp.query)
662634

@@ -798,33 +770,26 @@ ddbs_scale <- function(
798770
## handle overwrite
799771
overwrite_table(name_list$query_name, conn, quiet, overwrite)
800772

801-
## create query (no st_as_text)
802-
if (length(x_rest) == 0) {
803-
tmp.query <- glue::glue("
804-
SELECT {scale_expr} as {x_geom} FROM {x_list$query_name};
805-
")
806-
} else {
807-
tmp.query <- glue::glue("
808-
SELECT {x_rest}, {scale_expr} as {x_geom} FROM {x_list$query_name};
773+
## create query
774+
tmp.query <- glue::glue("
775+
CREATE TABLE {name_list$query_name} AS
776+
SELECT {x_rest}
777+
{scale_expr} as {x_geom}
778+
FROM {x_list$query_name};
809779
")
810-
}
811780
## execute scale query
812-
DBI::dbExecute(conn, glue::glue("CREATE TABLE {name_list$query_name} AS {tmp.query}"))
781+
DBI::dbExecute(conn, tmp.query)
813782
feedback_query(quiet)
814783
return(invisible(TRUE))
815784
}
816785

817786
# 4. Get data frame
818787
## 4.1. create query
819-
if (length(x_rest) == 0) {
820-
tmp.query <- glue::glue("
821-
SELECT ST_AsWKB({scale_expr}) as {x_geom} FROM {x_list$query_name};
822-
")
823-
} else {
824-
tmp.query <- glue::glue("
825-
SELECT {x_rest}, ST_AsWKB({scale_expr}) as {x_geom} FROM {x_list$query_name};
826-
")
827-
}
788+
tmp.query <- glue::glue("
789+
SELECT {x_rest}
790+
ST_AsWKB({scale_expr}) as {x_geom}
791+
FROM {x_list$query_name};
792+
")
828793
## 4.2. retrieve results from the query
829794
data_tbl <- DBI::dbGetQuery(conn, tmp.query)
830795

@@ -964,33 +929,26 @@ ddbs_shear <- function(
964929
## handle overwrite
965930
overwrite_table(name_list$query_name, conn, quiet, overwrite)
966931

967-
## create query (no st_as_text)
968-
if (length(x_rest) == 0) {
969-
tmp.query <- glue::glue("
970-
SELECT {shear_expr} as {x_geom} FROM {x_list$query_name};
971-
")
972-
} else {
973-
tmp.query <- glue::glue("
974-
SELECT {x_rest}, {shear_expr} as {x_geom} FROM {x_list$query_name};
932+
## create query
933+
tmp.query <- glue::glue("
934+
CREATE TABLE {name_list$query_name} AS
935+
SELECT {x_rest}
936+
{shear_expr} as {x_geom}
937+
FROM {x_list$query_name};
975938
")
976-
}
977939
## execute shear query
978-
DBI::dbExecute(conn, glue::glue("CREATE TABLE {name_list$query_name} AS {tmp.query}"))
940+
DBI::dbExecute(conn, tmp.query)
979941
feedback_query(quiet)
980942
return(invisible(TRUE))
981943
}
982944

983945
# 4. Get data frame
984946
## 4.1. create query
985-
if (length(x_rest) == 0) {
986-
tmp.query <- glue::glue("
987-
SELECT ST_AsWKB({shear_expr}) as {x_geom} FROM {x_list$query_name};
988-
")
989-
} else {
990-
tmp.query <- glue::glue("
991-
SELECT {x_rest}, ST_AsWKB({shear_expr}) as {x_geom} FROM {x_list$query_name};
992-
")
993-
}
947+
tmp.query <- glue::glue("
948+
SELECT {x_rest}
949+
ST_AsWKB({shear_expr}) as {x_geom}
950+
FROM {x_list$query_name};
951+
")
994952
## 4.2. retrieve results from the query
995953
data_tbl <- DBI::dbGetQuery(conn, tmp.query)
996954

R/ddbs_combine.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ ddbs_combine <- function(
409409

410410
## create the query
411411
tmp.query <- glue::glue("
412+
CREATE TABLE {name_list$query_name} AS
412413
SELECT
413414
ST_Collect(LIST({x_geom})) as {x_geom},
414415
FIRST({crs_column}) as {crs_column}
@@ -417,9 +418,10 @@ ddbs_combine <- function(
417418
")
418419

419420
## execute the query
420-
DBI::dbExecute(conn, glue::glue("CREATE TABLE {name_list$query_name} AS {tmp.query}"))
421+
DBI::dbExecute(conn, tmp.query)
421422
feedback_query(quiet)
422423
return(invisible(TRUE))
424+
423425
}
424426

425427
## 4. if name is NULL (sf returned)

0 commit comments

Comments
 (0)