Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/colin-fraser/queries

Store parameterized queries
https://github.com/colin-fraser/queries

Last synced: 20 days ago
JSON representation

Store parameterized queries

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# queries

I have a number of parameterized SQL queries that I run regularly. The purpose of this package is to keep canonical versions of those queries with standardized parameterization. It uses the `glue` package to replace parameters.

## Usage

The package reads modified sql files that have metadata stored in yaml format in a comment block at the top. Here's a simple example: start with a file like `[tests/testthat/example_with_defaults.sql](tests/testthat/example_with_defaults.sql)`.

```{r}
cat(readr::read_file("tests/testthat/example_with_defaults.sql"))
```

The header is written in `yaml` format. I can import this into R with

```{r}
library(queries)
query <- query_load("tests/testthat/example_with_defaults.sql")
```

and then plug in parameters with

```{r}
query_substitute(query, metrics = c(total_sales = "SUM(Sales)", avg_sales = "AVG(Sales)")) %>%
cat
```

Notice that vector names are converted to column identifiers.

Defaults are applied as specified in the yaml header. You can also create a function that completes the query:

```{r}
qf <- query_as_function(query)
qf(dimensions = c("Country", "Segment", "Product"), metrics = c(Sales = "SUM(Sales)"))
```

The created function has arguments corresponding to the params as specified in the yaml header.

```{r}
print(args(qf))
```

RStudio will see these and autocomplete them, which is convenient. When I forget a parameter, it tells me.

```{r error=TRUE}
qf(dimensions = "Segment")
```

## Big picture usage

### Query Library

I use this in two ways. First, I have a library of queries stored in a repository—say `~/projects/path/to/queries`. These are canonical versions of parameterized queries that my team uses. In my .Rprofile, I have a line that sets the default query location to that path

`options(default_queries_location='~/projects/path/to/queries')`

Which contains files

- `sales_by_group.sql`
- `customer_list.sql`
- etc...

Whenever I need to run one of these queries, I build the query with

`q <- load_query('sales_by_group')`

If I don't remember all the parameters, I can view the header with `head`:
```{r}
head(query)
```

### R Projects
The other way that I use this is to store bespoke queries in a `/sql` directory in an R project. `load_query` will look for a `/sql` directory first, then check whether the `default_query_location` option is set up.

## Installation

Install with devtools:

``` r
devtools::install_github("colin-fraser/queries")
```