-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
130 lines (92 loc) · 3.91 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
---
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%"
)
pkgload::load_all()
```
# dblog
<!-- 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/dblog)](https://cran.r-project.org/package=dblog)
<!-- badges: end -->
The goal of dblog is to implement logging for arbitrary DBI backends, similarly to Perl's [DBI::Log](https://metacpan.org/pod/DBI::Log).
This is useful for troubleshooting and auditing codes that access a database.
The initial use case for this package is to help debugging DBItest tests.
## Installation
You can install the released version of dblog from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("dblog")
```
Install the development version from GitHub using
``` r
# install.packages("devtools")
devtools::install_github("r-dbi/dblog")
```
## Basic example
The `dblog` driver wraps arbitrary drivers:
```{r init}
library(dblog)
drv <- dblog(RSQLite::SQLite())
```
All calls to DBI methods are logged, by default to the console.
```{r console}
conn <- dbConnect(drv, file = ":memory:")
dbWriteTable(conn, "iris", iris[1:3, ])
data <- dbGetQuery(conn, "SELECT * FROM iris")
dbDisconnect(conn)
data
```
The log is runnable R code!
Run it in a fresh session to repeat the operations, step by step or in an otherwise controlled fashion.
dblog is smart about DBI objects created or returned, and will assign a new variable name to each new object.
Cleared results or closed connections are not removed automatically.
## Logging options
Logging can be redirected to a file, optionally all outputs may be logged as well.
For example, use a collecting logger to output all calls and results after the fact.
```{r collect}
collecting_logger <- make_collect_logger()
drv <- dblog(RSQLite::SQLite(), logger = collecting_logger)
conn <- dbConnect(drv, file = ":memory:")
dbWriteTable(conn, "iris", iris[1:3, ])
data <- dbGetQuery(conn, "SELECT * FROM iris")
dbDisconnect(conn)
collecting_logger$retrieve()
ev <- evaluate::evaluate(collecting_logger$retrieve())
cat(unlist(ev, use.names = FALSE), sep = "\n")
```
## Logging complex operations
The full power is demonstrated when running with code where the underlying _DBI_ operations are not obvious:
```{r dplyr}
library(dplyr)
drv <- dblog(RSQLite::SQLite())
conn <- dbConnect(drv, file = ":memory:")
dbWriteTable(conn, "iris", iris[1:3, ])
src <- dbplyr::src_dbi(conn)
iris_tbl <- tbl(src, "iris")
iris_tbl %>%
summarize_if(is.numeric, mean)
```
## Inheritance hierarchy
Despite the common suggestion to [prefer composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance), the new logging classes are implemented as subclasses of the actual DBI classes.
Moreover, the class definitions are created on demand: for each different database backend, different subclasses are defined, to make sure dispatch is routed to the right methods.
The reason for this is that other methods, unknown to this package, might dispatch on the DBI class.
One such example is _dbplyr_ that introduces specialized behaviors for many classes.
The `explain()` method calls the internal `db_explain()` method which uses `EXPLAIN QUERY PLAN` for SQLite connections but `EXPLAIN` for unspecified database connections.
Without inheritance, _dbplyr_ would use the default method.
This might lead to errors for other databases that do not understand `EXPLAIN`.
```{r dplyr-explain}
iris_tbl %>%
summarize_if(is.numeric, mean) %>%
explain()
```
---
Please note that the 'dblog' project is released with a
[Contributor Code of Conduct](CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.