Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example with native db parameters support #79

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions 115-engine-sql.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ CREATE TABLE packages (id INTEGER, name TEXT)
```

```{sql, connection=db}
INSERT INTO packages VALUES (1, 'readr'), (2, 'readxl'), (3, 'haven')
INSERT INTO
packages
VALUES
(1, 'readr'),
(2, 'readxl'),
(3, 'haven')
```

```{sql connection=db}
Expand All @@ -27,15 +32,42 @@ SELECT * FROM packages
packageReadR <- "readr"
```

Engine can substitute named placeholders in sql that start with `?`.

Note that if you don't provide `params` as option values will be evaluated from environment

```{sql, connection=db, output.var="readrPackage"}
SELECT * FROM packages
WHERE name = ?packageReadR
SELECT
*
FROM
packages
WHERE
name = ?packageReadR
```

```{r}
readrPackage
```

To use parameterised queries with native database support provide `params` in the options of the chunk.

```{r setup-parameters}
parameters <- list(package = packageReadR)
```

```{sql, connection=db, output.var="readrPackage", params=parameters}
SELECT
*
FROM
packages
WHERE
name = @package
```

```{r}
readrPackage
```

```{sql, connection=db}
DROP TABLE IF EXISTS packages
```
Expand Down
62 changes: 47 additions & 15 deletions 115-engine-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ DROP TABLE IF EXISTS packages
CREATE TABLE packages (id INTEGER, name TEXT)
```


```sql
INSERT INTO packages VALUES (1, 'readr'), (2, 'readxl'), (3, 'haven')
INSERT INTO
packages
VALUES
(1, 'readr'),
(2, 'readxl'),
(3, 'haven')
```


```sql
SELECT * FROM packages
```
Expand All @@ -33,21 +36,28 @@ SELECT * FROM packages

Table: 3 records

|id |name |
|:--|:------|
|1 |readr |
|2 |readxl |
|3 |haven |

| id | name |
|:----|:-------|
| 1 | readr |
| 2 | readxl |
| 3 | haven |

```r
packageReadR <- "readr"
```

Engine can substitute named placeholders in sql that start with `?`.

Note that if you don’t provide `params` as option values will be
evaluated from environment

```sql
SELECT * FROM packages
WHERE name = ?packageReadR
SELECT
*
FROM
packages
WHERE
name = ?packageReadR
```


Expand All @@ -60,17 +70,39 @@ readrPackage
## 1 1 readr
```

To use parameterised queries with native database support provide
`params` in the options of the chunk.

```r
parameters <- list(package = packageReadR)
```

```sql
DROP TABLE IF EXISTS packages
SELECT
*
FROM
packages
WHERE
name = @package
```

```r
readrPackage
```


```sql
SELECT * FROM packages
```
##id name
## 1 1 readr
```


```sql
DROP TABLE IF EXISTS packages
```
## Error: no such table: packages

```sql
SELECT * FROM packages
```

## Error: no such table: packages