forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-query.R
125 lines (108 loc) · 3.14 KB
/
sql-query.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# select_query ------------------------------------------------------------
#' @export
#' @rdname sql_build
select_query <- function(from,
select = sql("*"),
where = character(),
group_by = character(),
having = character(),
order_by = character(),
limit = NULL,
distinct = FALSE) {
stopifnot(is.character(select))
stopifnot(is.character(where))
stopifnot(is.character(group_by))
stopifnot(is.character(having))
stopifnot(is.character(order_by))
stopifnot(is.null(limit) || (is.numeric(limit) && length(limit) == 1L))
stopifnot(is.logical(distinct), length(distinct) == 1L)
structure(
list(
from = from,
select = select,
where = where,
group_by = group_by,
having = having,
order_by = order_by,
distinct = distinct,
limit = limit
),
class = c("select_query", "query")
)
}
#' @export
print.select_query <- function(x, ...) {
cat("<SQL SELECT", if (x$distinct) " DISTINCT", ">\n", sep = "")
cat("From: ", x$from, "\n", sep = "")
if (length(x$select)) cat("Select: ", named_commas(x$select), "\n", sep = "")
if (length(x$where)) cat("Where: ", named_commas(x$where), "\n", sep = "")
if (length(x$group_by)) cat("Group by: ", named_commas(x$group_by), "\n", sep = "")
if (length(x$order_by)) cat("Order by: ", named_commas(x$order_by), "\n", sep = "")
if (length(x$having)) cat("Having: ", named_commas(x$having), "\n", sep = "")
if (length(x$limit)) cat("Limit: ", x$limit, "\n", sep = "")
}
#' @export
#' @rdname sql_build
join_query <- function(x, y, type = "inner", by = NULL, suffix = c(".x", ".y")) {
structure(
list(
x = x,
y = y,
type = type,
by = by,
suffix = suffix
),
class = c("join_query", "query")
)
}
#' @export
print.join_query <- function(x, ...) {
cat("<SQL JOIN (", toupper(x$type), ")>\n", sep = "")
cat("By: ", paste0(x$by$x, "-", x$by$y, collapse = ", "), "\n", sep = "")
cat(named_rule("X"), "\n", sep = "")
print(x$x$ops)
cat(named_rule("Y"), "\n", sep = "")
print(x$y$ops)
}
#' @export
#' @rdname sql_build
semi_join_query <- function(x, y, anti = FALSE, by = NULL) {
structure(
list(
x = x,
y = y,
anti = anti,
by = by
),
class = c("semi_join_query", "query")
)
}
#' @export
print.semi_join_query <- function(x, ...) {
cat("<SQL ", if (x$anti) "ANTI" else "SEMI", " JOIN>\n", sep = "")
cat("By: ", paste0(x$by$x, "-", x$by$y, collapse = ", "), "\n", sep = "")
cat(named_rule("X"), "\n", sep = "")
print(x$x$ops)
cat(named_rule("Y"), "\n", sep = "")
print(x$y$ops)
}
#' @export
#' @rdname sql_build
set_op_query <- function(x, y, type = type) {
structure(
list(
x = x,
y = y,
type = type
),
class = c("set_op_query", "query")
)
}
#' @export
print.set_op_query <- function(x, ...) {
cat("<SQL ", x$type, ">\n", sep = "")
cat(named_rule("X"), "\n", sep = "")
print(x$x$ops)
cat(named_rule("Y"), "\n", sep = "")
print(x$y$ops)
}