You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* illustrates how to make sql fenced code blocks from a DuckDBClient isntance
* syncs the sql fenced code blocks with the mosaic db (thanks, @tel!)
* gives better guidance for arbitrary query creation (with a safer approach for `xxx IN(${array})`)
supersedes #1849
Copy file name to clipboardExpand all lines: docs/lib/duckdb.md
+12-1
Original file line number
Diff line number
Diff line change
@@ -92,7 +92,11 @@ db.queryRow("SELECT count() AS count FROM gaia")
92
92
93
93
See the [DatabaseClient Specification](https://observablehq.com/@observablehq/database-client-specification) for more details on these methods.
94
94
95
-
Finally, the `DuckDBClient.sql` method <ahref="https://github.com/observablehq/framework/releases/tag/v1.4.0"class="observablehq-version-badge"data-version="^1.4.0"title="Added in 1.4.0"></a> 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).
95
+
## Custom setup
96
+
97
+
The `DuckDBClient.sql` method <ahref="https://github.com/observablehq/framework/releases/tag/v1.4.0"class="observablehq-version-badge"data-version="^1.4.0"title="Added in 1.4.0"></a> takes the same arguments as `DuckDBClient.of` and returns the corresponding `db.sql` tagged template literal.
98
+
99
+
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).
## Extensions <ahref="https://github.com/observablehq/framework/releases/tag/v1.13.0"class="observablehq-version-badge"data-version="^1.13.0"title="Added in 1.13.0"></a>
110
121
111
122
[DuckDB extensions](https://duckdb.org/docs/extensions/overview.html) extend DuckDB’s functionality, adding support for additional file formats, new types, and domain-specific functions. For example, the [`json` extension](https://duckdb.org/docs/data/json/overview.html) provides a `read_json` method for reading JSON files:
The code below creates three views, coordinated by Mosaic’s [crossfilter](https://uwdata.github.io/mosaic/api/core/selection.html#selection-crossfilter) helper.
Copy file name to clipboardExpand all lines: docs/sql.md
+23-5
Original file line number
Diff line number
Diff line change
@@ -191,18 +191,36 @@ The `sql` tag is available by default in Markdown. You can also import it explic
191
191
import {sql} from"npm:@observablehq/duckdb";
192
192
```
193
193
194
-
The `sql` tag is also useful for working around a current limitation of DuckDB-Wasm: prepared statements do not support array arguments. (Please upvote [#447](https://github.com/duckdb/duckdb-wasm/issues/447) if you run into this issue.) Instead of passing the array as a parameter, you can interpolate the array values directly into the SQL query.
194
+
For a more custom setup, see [DuckDBClient](./lib/duckdb#custom-setup).
195
+
196
+
<divclass="tip">
197
+
198
+
DuckDB only supports interpolation of strings and numbers with `${…}`. To interpolate an array of values, such as a list of ids, serialize to JSON:
WHERE source_id::string IN JSON(${JSON.stringify(ids)});
198
207
```
199
208
209
+
If you need to create a query on the fly, say with inputs that drive the name of a field or table, you can call the `sql` tagged template literal directly.
210
+
200
211
```js echo
201
-
Inputs.table(awaitsql([`SELECT * FROM gaia WHERE source_id IN (${[source_ids]})`]))
constquery=`SELECT MIN(${field}) AS min, MAX(${field}) AS max FROM gaia;`;
221
+
const [extent] =awaitsql([query]);
222
+
```
205
223
206
-
When interpolating values into SQL queries, be careful to avoid [SQL injection](https://en.wikipedia.org/wiki/SQL_injection) by properly escaping or sanitizing user input. The example above is safe only because `source_ids` are known to be numeric.
224
+
Be careful to avoid [SQL injection](https://en.wikipedia.org/wiki/SQL_injection) by properly escaping or sanitizing user input.
0 commit comments