Skip to content

Commit 74f8ff6

Browse files
committed
Improvements and corrections to USING statement and its documentation
1 parent 9f303e4 commit 74f8ff6

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

doc/sql.extensions/README.using_statement.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ When adapting a standard DSQL command to use `EXECUTE BLOCK` (for instance, to u
99
input parameter in multiple places), the developer is currently forced to explicitly declare all input parameters and,
1010
more tediously, all output fields.
1111

12-
The `USING` statement simplifies this workflow. It provides the ability to declare parameters and sub-routines while
13-
allowing the engine to infer outputs automatically from the contained SQL command.
12+
The `USING` statement simplifies this workflow. It provides the ability to declare parameters, sub-routines and
13+
variables while allowing the engine to infer outputs automatically from the contained SQL command.
1414

1515
## Syntax
1616

1717
```sql
1818
USING [ ( <input_parameter_list> ) ]
19-
[ <subroutines> ]
19+
[ <local_declarations> ]
2020
DO <sql_command>
2121
```
2222

23-
**Note:** At least one of `<input_parameter_list>` or `<subroutines>` must be present. A `USING ... DO` statement
24-
without parameters and without subroutines is invalid.
23+
**Note:** At least one of `<input_parameter_list>` or `<local_declarations>` must be present. A `USING DO ...` statement
24+
without parameters and without local declarations is invalid.
2525

2626
### Components
2727

2828
* **`<input_parameter_list>`**: A strictly typed list of parameters. These can be bound to values using the `?`
2929
placeholder.
30-
* **`<subroutines>`**: Standard PSQL function or procedure declarations.
30+
* **`<local_declarations>`**: Standard PSQL local declarations (variables, sub-functions and sub-procedures).
3131
* **`<sql_command>`**: The DSQL statement to execute. Supported statements include:
3232
* `SELECT`
3333
* `INSERT` (with or without `RETURNING`)
@@ -51,9 +51,9 @@ without parameters and without subroutines is invalid.
5151

5252
## Examples
5353

54-
### 1. Basic Parameter Reuse and Subroutines
54+
### 1. Basic Parameter Reuse and Local Declarations
5555

56-
This example demonstrates declaring typed parameters, defining local functions/procedures, and using them in a query.
56+
This example demonstrates declaring typed parameters, defining sub-routines, and using them in a query.
5757

5858
```sql
5959
using (p1 integer = ?, p2 integer = ?)
@@ -123,10 +123,10 @@ end
123123

124124
## Comparison
125125

126-
| Feature | Standard DSQL | `EXECUTE BLOCK` | `USING` |
127-
| :---------------------- | :------------------------------ | :-------------------------------------------- | :-------------------------------------------- |
128-
| **Subroutines** | No | Yes | Yes |
129-
| **Input Declarations** | Implicit (Positional) | Explicit | Hybrid (implicit and explicit) |
130-
| **Output Declarations** | Inferred | Explicit (`RETURNS`) | Inferred |
131-
| **Verbosity** | Low | High | Medium |
132-
| **Use Case** | Simple queries | Complex logic, loops, no result set inference | Reusing params, subroutines, standard queries |
126+
| Feature | Standard DSQL | `EXECUTE BLOCK` | `USING` |
127+
| :---------------------- | :------------------------------ | :-------------------------------------------- | :-------------------------------------------------------- |
128+
| **Local Declarations** | No | Yes | Yes |
129+
| **Input Declarations** | Implicit (Positional) | Explicit | Hybrid (implicit and explicit) |
130+
| **Output Declarations** | Inferred | Explicit (`RETURNS`) | Inferred |
131+
| **Verbosity** | Low | High | Medium |
132+
| **Use Case** | Simple queries | Complex logic, loops, no result set inference | Reusing params, variables, sub-routines, standard queries |

src/dsql/StmtNodes.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11071,7 +11071,18 @@ void UsingNode::genBlr(DsqlCompilerScratch* dsqlScratch)
1107111071
if (genSelectWrapper)
1107211072
GEN_port(dsqlScratch, sendMsg);
1107311073

11074-
if (dsqlScratch->variables.hasData())
11074+
bool hasNonLocalVariables = false;
11075+
11076+
for (const auto var : dsqlScratch->variables)
11077+
{
11078+
if (var->type != dsql_var::TYPE_LOCAL)
11079+
{
11080+
hasNonLocalVariables = true;
11081+
break;
11082+
}
11083+
}
11084+
11085+
if (hasNonLocalVariables)
1107511086
{
1107611087
// Generate receive statement to get parameter values - only for SELECT statements
1107711088
if (genSelectWrapper)
@@ -11083,7 +11094,10 @@ void UsingNode::genBlr(DsqlCompilerScratch* dsqlScratch)
1108311094
dsqlScratch->appendUChar(blr_begin);
1108411095

1108511096
for (const auto var : dsqlScratch->variables)
11086-
dsqlScratch->putLocalVariable(var);
11097+
{
11098+
if (var->type != dsql_var::TYPE_LOCAL)
11099+
dsqlScratch->putLocalVariable(var);
11100+
}
1108711101
}
1108811102

1108911103
dsqlScratch->appendUChar(blr_begin);
@@ -11112,7 +11126,7 @@ void UsingNode::genBlr(DsqlCompilerScratch* dsqlScratch)
1111211126

1111311127
dsqlScratch->appendUChar(blr_end);
1111411128

11115-
if (dsqlScratch->variables.hasData())
11129+
if (hasNonLocalVariables)
1111611130
dsqlScratch->appendUChar(blr_end);
1111711131

1111811132
if (genSelectWrapper)

0 commit comments

Comments
 (0)