Skip to content

Commit 32b44b1

Browse files
authored
docs: expand docs of sparqlc:param
1 parent 1e44cd1 commit 32b44b1

2 files changed

Lines changed: 96 additions & 28 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ const rows = await execute(params, { env, client })
7979

8080
### Reference parameters with `sparqlc:param`
8181

82-
You can declare and consume query parameters inside `.rq` files using the RDF property/function identified by `https://sparqlc.described.at/param` (IRI). Use a prefix for convenience:
82+
You can declare and consume query parameters inside `.rq` files using the SPARQL function identified by `https://sparqlc.described.at/param` (IRI). Use a prefix for convenience:
8383

8484
```sparql
8585
PREFIX sparqlc: <https://sparqlc.described.at/>
8686
```
8787

88-
- (Recommended) Function style: `sparqlc:param("name")`
88+
- Parameter function: `sparqlc:param("name")` or `sparqlc:param(prefix:name)`
8989
- Can be easily inlined in `BIND`, `FILTER`, or anywhere an expression is allowed.
90+
- String literal keys: `BIND(sparqlc:param("minAge") AS ?minAge)`
91+
- NamedNode / IRI keys: `BIND(sparqlc:param(schema:mainEntity) AS ?entity)`
9092
- Example:
9193
```sparql
9294
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
@@ -97,37 +99,22 @@ PREFIX sparqlc: <https://sparqlc.described.at/>
9799
FILTER (?age >= sparqlc:param("minAge"))
98100
}
99101
```
100-
101-
- Pattern style: `?variable sparqlc:param "name" .`
102-
- This binds the value of the parameter `"name"` to the SPARQL variable `?variable`.
103-
- Example:
104-
```sparql
105-
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
106-
PREFIX sparqlc: <https://sparqlc.described.at/>
107-
108-
SELECT ?person WHERE {
109-
?name sparqlc:param "person" .
110-
111-
?person a foaf:Person ;
112-
foaf:name ?name .
113-
}
114-
```
115102
Runtime call:
116103
```ts
117104
import env from '@zazuko/env'
118-
const rows = await execute({ person: env.namedNode('http://example.com/alice') }, { env, client })
105+
const rows = await execute({ minAge: env.literal('18', env.ns.xsd.integer) }, { env, client })
119106
```
120107
121108
Passing parameters at runtime
122109
- You may pass parameters as:
123110
- `Record<string, Term>`: `{ person: env.namedNode('...'), minAge: env.literal('18', xsdInteger) }`
124111
- `URLSearchParams`: `new URLSearchParams([["person", "http://example.com/alice"], ["minAge", "18"]])` (simple string values)
125-
- `Map<Term, Term | Term[]>` for full control
112+
- `Map<Term, Term | Term[]>` to use NamedNode parameter names
126113
- Values are RDFJS `Term`s. For typed literals, construct them via your RDF environment (e.g., `@zazuko/env`).
127114
128115
Notes
129116
- The parameter IRI is exactly `https://sparqlc.described.at/param`.
130-
- Use `PREFIX sparqlc: <https://sparqlc.described.at/>` so `sparqlc:param` expands to that IRI in both triple and function forms.
117+
- Use `PREFIX sparqlc: <https://sparqlc.described.at/>` so `sparqlc:param` expands to that IRI.
131118
- Arrays (`Term[]`) are supported by the runtime API for positions that accept multiple terms (e.g., via custom processors). Your usage may vary depending on processors you apply.
132119
133120
### CLI usage

‎packages/sparqlc/README.md‎

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,100 @@ const { default: insertData } = await import('./queries/insert-data.ru')
6262
await insertData({ env, client })
6363
```
6464

65-
#### Binding parameters
65+
#### Binding parameters with `sparqlc:param`
6666

67-
Queries can declare variables to be bound at execution time. Provide a map of RDF terms as the first argument. Keys may be full variable names or well‑known IRIs (helpful with `@zazuko/env`).
67+
Queries can declare variables and placeholders to be bound at execution time using the `sparqlc:param` SPARQL function.
6868

69-
```ts
70-
const params = env.termMap([
71-
[env.ns.schema.mainEntity, env.namedNode('http://example.org/fruits/Banana')],
72-
])
69+
##### Declaring parameters in SPARQL queries
70+
71+
Declare the `sparqlc:` prefix:
7372

74-
const { default: construct } = await import('./queries/construct-named-node-param.rq')
75-
const dataset = await construct(params, { env, client })
73+
```sparql
74+
PREFIX sparqlc: <https://sparqlc.described.at/>
7675
```
7776

77+
You can use `sparqlc:param(...)` in `BIND(...)` expressions, `FILTER(...)` expressions, or anywhere SPARQL expressions are valid:
78+
79+
- **String literal parameter keys**: Identify parameters by string name.
80+
```sparql
81+
# queries/select-by-type.rq
82+
PREFIX sparqlc: <https://sparqlc.described.at/>
83+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
84+
85+
SELECT ?res ?label WHERE {
86+
BIND(sparqlc:param("type") AS ?type)
87+
88+
?res a ?type ;
89+
rdfs:label ?label .
90+
}
91+
```
92+
93+
- **NamedNode / IRI parameter keys**: Identify parameters using full IRIs or prefixed names (such as schema.org terms).
94+
```sparql
95+
# queries/construct-named-node-param.rq
96+
PREFIX sparqlc: <https://sparqlc.described.at/>
97+
PREFIX schema: <http://schema.org/>
98+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
99+
100+
CONSTRUCT {
101+
?fruit rdfs:label ?label .
102+
} WHERE {
103+
BIND(IRI(sparqlc:param(schema:mainEntity)) AS ?fruit)
104+
105+
?fruit rdfs:label ?label .
106+
}
107+
```
108+
109+
- **In `FILTER` expressions**:
110+
```sparql
111+
PREFIX sparqlc: <https://sparqlc.described.at/>
112+
113+
SELECT ?s ?val WHERE {
114+
?s ?p ?val .
115+
FILTER(?val = sparqlc:param("expected"))
116+
}
117+
```
118+
119+
- **Scoped evaluation**: `sparqlc:param` can be used inside `GRAPH`, `OPTIONAL`, `SERVICE`, `MINUS`, unions, sub-selects, and update operations (`INSERT`, `DELETE`). Parameter values are injected into the query via appropriately scoped `VALUES` clauses according to SPARQL bottom-up evaluation semantics.
120+
121+
##### Passing parameters at execution time
122+
123+
Pass parameter values as the first argument(s) to the imported query function. `sparqlc` supports several formats:
124+
125+
- **Plain object dictionary (`Record<string, Term>`)**:
126+
```ts
127+
import env from '@zazuko/env'
128+
const { default: selectByType } = await import('./queries/select-by-type.rq')
129+
130+
const rows = await selectByType(
131+
{ type: env.namedNode('http://example.org/Fruit') },
132+
{ env, client }
133+
)
134+
```
135+
136+
- **RDF/JS `TermMap` / `Map<Term, Term | Term[]>`**: Useful for NamedNode parameter keys and full control over RDF terms.
137+
```ts
138+
import env from '@zazuko/env'
139+
const { default: construct } = await import('./queries/construct-named-node-param.rq')
140+
141+
const params = env.termMap([
142+
[env.ns.schema.mainEntity, env.namedNode('http://example.org/fruits/Banana')],
143+
])
144+
145+
const dataset = await construct(params, { env, client })
146+
```
147+
148+
- **`URLSearchParams`**: Useful for query strings.
149+
```ts
150+
const params = new URLSearchParams([['type', 'http://example.org/Fruit']])
151+
const rows = await selectByType(params, { env, client })
152+
```
153+
154+
- **Multiple parameter sources**: Multiple parameter maps or objects can be provided in sequence before the options argument.
155+
```ts
156+
const rows = await selectQuery(params1, params2, { env, client })
157+
```
158+
78159
#### Runtime query options
79160

80161
Queries can be modified at execution time using runtime options:

0 commit comments

Comments
 (0)