Skip to content

Commit 7268ea5

Browse files
clemzarchgplanchat
andauthored
explain how to use the option "from" in SQL to bind an unknown number of parameters (#46)
Co-authored-by: Grégory PLANCHAT <gplanchat@users.noreply.github.com>
1 parent 3b1baf7 commit 7268ea5

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

content/connectivity/sql/_index.en.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ weight: 9
1919
- [Building a ConditionalLoader](#building-a-conditionalloader)
2020
- [Advanced usage](#advanced-usage)
2121
- [Using params in your queries](#using-params-in-your-queries)
22+
- [Using an unknown number of parameters](#using-an-unknown-number-of-parameters)
2223
- [Creating before and after queries](#creating-before-and-after-queries)
2324

2425
---
@@ -237,6 +238,43 @@ sql:
237238
# ...
238239
```
239240

241+
### Using an unknown number of parameters
242+
243+
In some cases, you may not know in advance how many parameters you will need to enter,
244+
for example if you are searching using an `IN` with many values.
245+
246+
Using `from` instead of `value` will bind as many parameters as there are values in the path.
247+
248+
And use [the expression `inSql(path, parameter_name)`](../../feature/expression-language/satellite-expression-functions/#list-of-available-functions) to prepare the values in the query.
249+
250+
```yaml
251+
sql:
252+
loader:
253+
query: '@="SELECT * FROM category WHERE id " ~ inSql(input["codes_list"], "identifier") ~ "'
254+
parameters:
255+
identifier:
256+
from: '@=input["codes_list"]'
257+
# ...
258+
```
259+
260+
If at runtime there are 4 values under `[codes_list]`, this would be equivalent to writing:
261+
262+
```yaml
263+
sql:
264+
loader:
265+
query: 'SELECT * FROM category WHERE id IN (:identifier_0, :identifier_1, :identifier_2, :identifier_3)'
266+
parameters:
267+
identifier_0:
268+
value: '@=input["codes_list"][0]'
269+
identifier_1:
270+
value: '@=input["codes_list"][1]'
271+
identifier_2:
272+
value: '@=input["codes_list"][2]'
273+
identifier_3:
274+
value: '@=input["codes_list"][3]'
275+
# ...
276+
```
277+
240278
### Creating before and after queries
241279

242280
In some cases, you may need to run queries in order to best prepare for the execution of your pipeline.

content/feature/expression-language/satellite-expression-functions/_index.en.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ foo: '@=env("MY_ENVIRONMENT_VARIABLE")'
3838

3939
## List of available functions
4040

41-
| Name | Description |
42-
|----------------------------------------------------|-----------------------------------------------------|
43-
| env(`string` name): `string`&vert;`false` | Gets the value of an environment variable |
44-
| envAsFile(`string` name): `string` | Create a file whose name is an environment variable |
45-
| file(`string` name): `string` | Create a file |
46-
| base64Decode(`string` name): `string`&vert;`false` | Decodes data encoded with Base64 |
47-
| temporaryFile(`string` name): `resource` | Create a temporary file |
41+
| Name | Description |
42+
|-------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
43+
| env(`string` name): `string|false` | Gets the value of an environment variable |
44+
| envAsFile(`string` name): `string` | Create a file whose name is an environment variable |
45+
| file(`string` name): `string` | Create a file |
46+
| base64Decode(`string` name): `string|false` | Decodes data encoded with Base64 |
47+
| temporaryFile(`string` name): `resource` | Create a temporary file |
48+
| inSql(`array` path, `string` parameterName): `string` | Writes "IN (...)" with as many parameters as there are values under `path`, in the format: `:parameterName_0`, `:parameterName_1`... To be used in a SQL query [when searching among an unknown number of values](../../../connectivity/sql/#using-an-unknown-number-of-parameters). |

0 commit comments

Comments
 (0)