|
| 1 | +# The `USING` Statement |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `USING` statement is a new DSQL extension designed to bridge the gap between standard DSQL statements and the |
| 6 | +powerful but verbose `EXECUTE BLOCK`. |
| 7 | + |
| 8 | +When adapting a standard DSQL command to use `EXECUTE BLOCK` (for instance, to utilize sub-routines or reuse a single |
| 9 | +input parameter in multiple places), the developer is currently forced to explicitly declare all input parameters and, |
| 10 | +more tediously, all output fields. |
| 11 | + |
| 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. |
| 14 | + |
| 15 | +## Syntax |
| 16 | + |
| 17 | +```sql |
| 18 | +USING [ ( <input_parameter_list> ) ] |
| 19 | + [ <subroutines> ] |
| 20 | +DO <sql_command> |
| 21 | +``` |
| 22 | + |
| 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. |
| 25 | + |
| 26 | +### Components |
| 27 | + |
| 28 | +* **`<input_parameter_list>`**: A strictly typed list of parameters. These can be bound to values using the `?` |
| 29 | + placeholder. |
| 30 | +* **`<subroutines>`**: Standard PSQL function or procedure declarations. |
| 31 | +* **`<sql_command>`**: The DSQL statement to execute. Supported statements include: |
| 32 | + * `SELECT` |
| 33 | + * `INSERT` (with or without `RETURNING`) |
| 34 | + * `UPDATE` (with or without `RETURNING`) |
| 35 | + * `UPDATE OR INSERT` (with or without `RETURNING`) |
| 36 | + * `DELETE` (with or without `RETURNING`) |
| 37 | + * `MERGE` (with or without `RETURNING`) |
| 38 | + * `CALL` |
| 39 | + * `EXECUTE PROCEDURE` |
| 40 | + |
| 41 | +## Key Features |
| 42 | + |
| 43 | +1. **Inferred Outputs**: Unlike `EXECUTE BLOCK`, you do not need to explicitly declare a `RETURNS (...)` clause. The |
| 44 | + output columns are automatically inferred from the `<sql_command>` in the `DO` clause. |
| 45 | +2. **Statement Type Transparency**: The API returns the statement type of the inner `<sql_command>` (e.g., if the |
| 46 | + inner command is a `SELECT`, the client sees a `SELECT` statement). |
| 47 | +3. **Parameter Reuse**: Input parameters declared in the `USING` clause can be used multiple times within the script |
| 48 | + using named references (e.g., `:p1`), while only requiring a single bind from the client application. |
| 49 | +4. **Mixed Parameter Binding**: You can mix declared parameters (bound via `?` in the declaration) and direct |
| 50 | + positional parameters (using `?` inside the `DO` command). |
| 51 | + |
| 52 | +## Examples |
| 53 | + |
| 54 | +### 1. Basic Parameter Reuse and Subroutines |
| 55 | + |
| 56 | +This example demonstrates declaring typed parameters, defining local functions/procedures, and using them in a query. |
| 57 | + |
| 58 | +```sql |
| 59 | +using (p1 integer = ?, p2 integer = ?) |
| 60 | + -- Declare a local function |
| 61 | + declare function subfunc (i1 integer) returns integer |
| 62 | + as |
| 63 | + begin |
| 64 | + return i1; |
| 65 | + end |
| 66 | + |
| 67 | + -- Declare a local procedure |
| 68 | + declare procedure subproc (i1 integer) returns (o1 integer) |
| 69 | + as |
| 70 | + begin |
| 71 | + o1 = i1; |
| 72 | + suspend; |
| 73 | + end |
| 74 | +do |
| 75 | +-- The main query |
| 76 | +select subfunc(:p1) + o1 |
| 77 | + from subproc(:p2 + ?) |
| 78 | +``` |
| 79 | + |
| 80 | +In this scenario: |
| 81 | +1. The client binds values to `p1` and `p2`. |
| 82 | +2. The client binds a third value to the `?` inside the `DO` clause. |
| 83 | +3. The result set structure is inferred from the `SELECT` statement. |
| 84 | + |
| 85 | +### 2. Simplifying Parameter Reuse |
| 86 | + |
| 87 | +Without `USING`, inserting the same bind value into multiple columns requires sending the data twice. |
| 88 | + |
| 89 | +**Standard DSQL:** |
| 90 | +```sql |
| 91 | +insert into generic_table (col_a, col_b) values (?, ?); |
| 92 | +-- Client must bind: [100, 100] |
| 93 | +``` |
| 94 | + |
| 95 | +**With `USING`:** |
| 96 | +```sql |
| 97 | +using (val integer = ?) |
| 98 | +do insert into generic_table (col_a, col_b) values (:val, :val); |
| 99 | +-- Client binds: [100] |
| 100 | +``` |
| 101 | + |
| 102 | +### 3. EXECUTE STATEMENT with Named Parameters |
| 103 | + |
| 104 | +```sql |
| 105 | +execute block returns (ri integer, rs varchar(20)) |
| 106 | +as |
| 107 | +begin |
| 108 | + execute statement ('using(i int = ?, s varchar(20) = ?) do select :i, :s from rdb$database') (s := '2', i := 1) into ri, rs; |
| 109 | + suspend; |
| 110 | +end |
| 111 | +``` |
| 112 | + |
| 113 | +### 4. EXECUTE STATEMENT with Unnamed Parameters |
| 114 | + |
| 115 | +```sql |
| 116 | +execute block returns (ri integer, rs varchar(20)) |
| 117 | +as |
| 118 | +begin |
| 119 | + execute statement ('using(i int = ?, s varchar(20) = ?) do select :i, :s from rdb$database') (1, '2') into ri, rs; |
| 120 | + suspend; |
| 121 | +end |
| 122 | +``` |
| 123 | + |
| 124 | +## Comparison |
| 125 | + |
| 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 | |
0 commit comments