This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
242 lines (191 loc) · 5.59 KB
/
README.Rmd
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# Overview
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/dbtools)](https://CRAN.R-project.org/package=dbtools)
<!-- badges: end -->
`dbtools` provides helpers to insert, update, or delete in a database table with
the rows of a data frame. The main functions are:
* `db_insert_data()` to insert rows,
* `db_update_data()` to update rows
* `db_insert_missing_data()` to insert new rows,
* `db_upsert_data()` to insert new and update existing rows,
* `db_delete_data()` to delete rows.
While they are simple to use they also provide these features:
* _batch operations_ to increase the speed,
* _custom SQL code_ to have even better control,
* _nesting transactions_ with the help of `with_transaction()`,
* _returning_ the inserted/updated rows,
* _upserts/insert missing_ also possible without a unique index.
And of course they can easily be used with the pipe `%>%`.
## Installation
<!-- You can install the released version of dbtools from [CRAN](https://CRAN.R-project.org) with: -->
<!-- ``` r -->
<!-- install.packages("dbtools") -->
<!-- ``` -->
<!-- ### Development version -->
<!-- To get a bug fix, or use a feature from the development version, you can install dbtools from GitHub. -->
`dbtools` is not yet on CRAN and can only be installed from GitHub.
```{r eval=FALSE}
# install.packages("devtools")
devtools::install_github("mgirlich/dbtools")
```
## Insert (Missing) Data
Insert records
```{r}
library(dbtools)
if (DBI::dbExistsTable(con_memdb(), "dbtools")) {
DBI::dbRemoveTable(con_memdb(), "dbtools")
}
dbplyr::db_copy_to(
con = con_memdb(),
table = "dbtools",
values = data.frame(
id = integer(),
value = character(),
update_counter = integer(),
updated_at = character()
)[0, ],
overwrite = TRUE,
unique_indexes = "id"
)
db_insert_data(
data.frame(
id = 1,
value = c("a"),
update_counter = 0,
updated_at = Sys.time()
),
table = "dbtools",
con = con_memdb()
)
DBI::dbReadTable(con_memdb(), "dbtools")
```
If you want to insert data where the `id` is already in the table you get an
error because of the unique constraint on `id`:
```{r error=TRUE}
db_insert_data(
data.frame(
id = 1,
value = c("a"),
update_counter = 0,
updated_at = Sys.time()
),
table = "dbtools",
con = con_memdb()
)
```
To only insert data where the `id` is not yet found in the table use
`db_insert_missing_data()`
```{r}
db_insert_missing_data(
data = data.frame(
id = 2:3,
value = c("b", "c"),
updated_at = Sys.time(),
update_counter = c(2, 3)
),
table = "dbtools",
con = con_memdb(),
conflict_target = "id"
)
DBI::dbReadTable(con_memdb(), "dbtools")
```
For more information on how to handle conflicts see the
[Conflicts and Unique Columns section](#conflicts).
## Update Data
```{r}
db_update_data(
data = data.frame(
id = 1:2,
value = c("x", "y"),
updated_at = Sys.time()
),
table = "dbtools",
con = con_memdb(),
update = c("value", "updated_at"),
where = "id"
)
DBI::dbReadTable(con_memdb(), "dbtools")
```
## Custom SQL
Let's say you only want to update rows with an `update_counter` of at most 2
and then you also want to increase the update counter by 1 for the updated rows.
This can easily be done by passing SQL code generated with `sql()`:
```{r}
db_update_data(
# sql_update(
data = data.frame(
id = 1:3,
value = "z",
updated_at = Sys.time()
),
table = "dbtools",
con = con_memdb(),
update = list(
"value",
update_counter = sql("update_counter + 1"),
"updated_at"
),
where = list(
"id",
sql("update_counter <= 2")
)
)
DBI::dbReadTable(con_memdb(), "dbtools")
```
## Data From Another Database Table
Instead of using local data to update/insert/upsert/delete you can also another
database table by providing its name to the `data` argument:
```{r}
if (DBI::dbExistsTable(con_memdb(), "dbtools2")) {
DBI::dbRemoveTable(con_memdb(), "dbtools2")
}
dbplyr::db_copy_to(
con = con_memdb(),
table = "dbtools2",
values = data.frame(
dbtools_id = 1
),
overwrite = TRUE
)
DBI::dbReadTable(con_memdb(), "dbtools")
DBI::dbReadTable(con_memdb(), "dbtools2")
db_delete_data(
"dbtools2",
"dbtools",
con_memdb(),
where = c(id = "dbtools_id")
)
DBI::dbReadTable(con_memdb(), "dbtools")
```
## Conflicts and Unique Columns {#conflicts}
When a table has a unique constraint on some columns (or it should have one but
for some reason it doesn't) and you try to insert data some rows might violate
this unique constraint. This is referred to as conflict target. There are two
types of conflict targets in `dbtools`:
* `sql_unique_cols()` to specify a set of unique columns,
* `sql_constraint()` to specify the name of an existing unique constraint.
Mind that `sql_constraint()` only works in newer versions of databases. If your
database version does not yet support the `ON CONFLICT` clause you have to use
`sql_unique_cols()` and use the `mode = "old"`.
## Generate SQL
You can also easily get the underlying SQL code with the corresponding
`sql_*()` function:
* `sql_insert()`
* `sql_insert_missing()`
* `sql_update()`
* `sql_delete()`
There are also some helper functions you might find useful:
* `sql_values()` to generate a `VALUES` clause from the data frame