Skip to content

Commit 7c69b22

Browse files
Filmbostock
andauthored
DuckDBClient.sql (#1097)
* implement sql fenced code from DuckDBClient.sql(…) following @mbostock’s suggestion #1072 (comment) * fix typo --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 777b635 commit 7c69b22

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

docs/lib/duckdb.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,18 @@ And `db.queryRow`:
9090
db.queryRow("SELECT count() AS count FROM gaia")
9191
```
9292

93-
See the [DatabaseClient Specification](https://observablehq.com/@observablehq/database-client-specification) for more details.
93+
See the [DatabaseClient Specification](https://observablehq.com/@observablehq/database-client-specification) for more details on these methods.
94+
95+
Finally, the `DuckDBClient.sql` method takes the same arguments as `DuckDBClient.of` and returns the corresponding `db.sql` tagged template literal. The returned function can be used to redefine the built-in [`sql` tagged template literal](../sql#sql-literals) and thereby change the database used by [SQL code blocks](../sql), allowing you to query dynamically-registered tables (unlike the **sql** front matter option).
96+
97+
```js
98+
const feed = view(Inputs.select(new Map([["M4.5+", "4.5"], ["M2.5+", "2.5"], ["All", "all"]]), {label: "Earthquake feed"}));
99+
```
100+
101+
```js echo
102+
const sql = DuckDBClient.sql({quakes: `https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/${feed}_day.csv`});
103+
```
104+
105+
```sql echo
106+
SELECT * FROM quakes ORDER BY updated DESC;
107+
```

docs/sql.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ sql:
2727

2828
<div class="tip">For performance and reliability, we recommend using local files rather than loading data from external servers at runtime. If needed, you can use a <a href="./loaders">data loader</a> to take a snapshot of a remote data during build.</div>
2929

30+
You can also register tables via code (say to have sources that are defined dynamically via user input) by defining the `sql` symbol with [DuckDBClient.sql](./lib/duckdb).
31+
3032
## SQL code blocks
3133

3234
To run SQL queries, create a SQL fenced code block (<code>```sql</code>). For example, to query the first 10 rows from the `gaia` table:
@@ -182,6 +184,6 @@ Plot.plot({
182184

183185
The `sql` tag is available by default in Markdown. You can also import it explicitly as:
184186

185-
```js echo
187+
```js run=false
186188
import {sql} from "npm:@observablehq/duckdb";
187189
```

src/client/stdlib/duckdb.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ export class DuckDBClient {
173173
await Promise.all(Object.entries(sources).map(([name, source]) => insertSource(db, name, source)));
174174
return new DuckDBClient(db);
175175
}
176+
177+
static sql() {
178+
return this.of.apply(this, arguments).then((db) => db.sql.bind(db));
179+
}
176180
}
177181

178182
Object.defineProperty(DuckDBClient.prototype, "dialect", {

0 commit comments

Comments
 (0)