Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support array literal spread param transform #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion docs-new/docs/sql-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ A query can also contain multiple expansions if needed:
SELECT FROM users WHERE age in :ages or name in :names;
```

At the moment, PgTyped supports three expansion types:
At the moment, PgTyped supports four expansion types:

### Array spread

Expand Down Expand Up @@ -91,6 +91,36 @@ selectSomeUsers.run(parameters, connection);
SELECT FROM users WHERE age in ($1, $2, $3);
```

### Array literal

The array literal expansion allows to pass an array of scalars as parameter.

#### Syntax:

```
@param paramName -> ARRAY[...]
```

#### Example:

```sql title="Query definition:"
/*
@name selectFunctionResult
@param values -> ARRAY[...]
*/
SELECT FROM my_function(:values);
```

```ts title="Execution:"
const parameters = { values: ['a', 'b', 'c'] };
selectSomeUsers.run(parameters, connection);
```

```sql title="Resulting query:"
-- Parameters: ['a', 'b', 'c']
SELECT FROM my_function(ARRAY[$1, $2, $3]);
```

### Object pick

The object pick expansion allows to pass an object as a parameter.
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/loader/sql/grammar/SQLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ NAME_TAG : '@name';
TYPE_TAG : '@param';
OB: '(';
CB: ')';
OPEN_ARRAY: 'ARRAY[';
CLOSE_ARRAY: ']';
COMMA: ',';
C_REQUIRED_MARK: '!';
ANY: .+?;
Expand Down
3 changes: 3 additions & 0 deletions packages/parser/src/loader/sql/grammar/SQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ paramTransform: TRANSFORM_ARROW transformRule;
transformRule
: spreadTransform
| pickTransform
| arrayLiteralTransform
| spreadPickTransform;

spreadTransform: OB SPREAD CB;
Expand All @@ -47,6 +48,8 @@ pickTransform: OB key (COMMA key)* COMMA? CB;

spreadPickTransform: OB pickTransform SPREAD CB;

arrayLiteralTransform: OPEN_ARRAY SPREAD CLOSE_ARRAY;

key: ID C_REQUIRED_MARK?;

queryName: ID;
Expand Down
10 changes: 10 additions & 0 deletions packages/parser/src/loader/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum TransformType {
Scalar = 'scalar',
PickTuple = 'pick_tuple',
ArraySpread = 'array_spread',
ArrayLiteralSpread = 'array_literal_spread',
PickArraySpread = 'pick_array_spread',
}

Expand All @@ -38,6 +39,9 @@ export type ParamTransform =
| {
type: TransformType.ArraySpread;
}
| {
type: TransformType.ArrayLiteralSpread;
}
| {
type: TransformType.PickTuple | TransformType.PickArraySpread;
keys: ParamKey[];
Expand Down Expand Up @@ -168,6 +172,12 @@ class ParseListener implements SQLParserListener {
};
}

enterArrayLiteralTransform() {
this.currentTransform = {
type: TransformType.ArrayLiteralSpread,
};
}

enterSpreadPickTransform() {
this.currentTransform = {
type: TransformType.PickArraySpread,
Expand Down
200 changes: 105 additions & 95 deletions packages/parser/src/loader/sql/parser/SQLLexer.ts

Large diffs are not rendered by default.

Loading
Loading