Skip to content

Commit

Permalink
DuckDBClient.sql (#1097)
Browse files Browse the repository at this point in the history
* implement sql fenced code from DuckDBClient.sql(…)

following @mbostock’s suggestion #1072 (comment)

* fix typo

---------

Co-authored-by: Mike Bostock <mbostock@gmail.com>
  • Loading branch information
Fil and mbostock committed Mar 18, 2024
1 parent 777b635 commit 7c69b22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
16 changes: 15 additions & 1 deletion docs/lib/duckdb.md
Expand Up @@ -90,4 +90,18 @@ And `db.queryRow`:
db.queryRow("SELECT count() AS count FROM gaia")
```

See the [DatabaseClient Specification](https://observablehq.com/@observablehq/database-client-specification) for more details.
See the [DatabaseClient Specification](https://observablehq.com/@observablehq/database-client-specification) for more details on these methods.

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).

```js
const feed = view(Inputs.select(new Map([["M4.5+", "4.5"], ["M2.5+", "2.5"], ["All", "all"]]), {label: "Earthquake feed"}));
```

```js echo
const sql = DuckDBClient.sql({quakes: `https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/${feed}_day.csv`});
```

```sql echo
SELECT * FROM quakes ORDER BY updated DESC;
```
4 changes: 3 additions & 1 deletion docs/sql.md
Expand Up @@ -27,6 +27,8 @@ sql:

<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>

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).

## SQL code blocks

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:
Expand Down Expand Up @@ -182,6 +184,6 @@ Plot.plot({

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

```js echo
```js run=false
import {sql} from "npm:@observablehq/duckdb";
```
4 changes: 4 additions & 0 deletions src/client/stdlib/duckdb.js
Expand Up @@ -173,6 +173,10 @@ export class DuckDBClient {
await Promise.all(Object.entries(sources).map(([name, source]) => insertSource(db, name, source)));
return new DuckDBClient(db);
}

static sql() {
return this.of.apply(this, arguments).then((db) => db.sql.bind(db));
}
}

Object.defineProperty(DuckDBClient.prototype, "dialect", {
Expand Down

0 comments on commit 7c69b22

Please sign in to comment.