Skip to content

Commit 2d2ef61

Browse files
committed
Update documentation with new PL/SQL Associative Array bind support
1 parent bab895d commit 2d2ef61

File tree

1 file changed

+146
-6
lines changed

1 file changed

+146
-6
lines changed

doc/api.md

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ limitations under the License.
124124
- 12.3 [DML RETURNING Bind Parameters](#dmlreturn)
125125
- 12.4 [REF CURSOR Bind Parameters](#refcursors)
126126
- 12.5 [LOB Bind Parameters](#lobbinds)
127+
- 12.6 [PL/SQL Collection Associative Array (Index-by) Bind Parameters](#plsqlindexbybinds)
127128
13. [Transaction Management](#transactionmgt)
128129
14. [Statement Caching](#stmtcache)
129130
15. [External Configuration](#oraaccess)
@@ -1167,10 +1168,11 @@ If a bind value is an object it may have the following properties:
11671168

11681169
Bind Property | Description
11691170
---------------|------------
1170-
`val` | The input value or variable to be used for an IN or IN OUT bind variable.
11711171
`dir` | The direction of the bind. One of the [Oracledb Constants](#oracledbconstants) `BIND_IN`, `BIND_INOUT`, or `BIND_OUT`.
1172-
`type` | The datatype to be bound. One of the [Oracledb Constants](#oracledbconstants) `STRING`, `NUMBER`, `DATE`, `CURSOR` or `BUFFER`.
1172+
`maxArraySize` | The number of array elements to be allocated for a PL/SQL Collection `INDEX OF` associative array OUT or IN OUT array bind variable.
11731173
`maxSize` | The maximum number of bytes that an OUT or IN OUT bind variable of type STRING or BUFFER can use. The default value is 200. The maximum limit is 32767.
1174+
`type` | The datatype to be bound. One of the [Oracledb Constants](#oracledbconstants) `STRING`, `NUMBER`, `DATE`, `CURSOR` or `BUFFER`.
1175+
`val` | The input value or variable to be used for an IN or IN OUT bind variable.
11741176

11751177
The maximum size of a `BUFFER` type is 2000 bytes, unless you are
11761178
using Oracle Database 12c and the database initialization parameter
@@ -2970,7 +2972,9 @@ bind a Node.js Buffer to an Oracle Database `RAW` type. The type
29702972
For each OUT and IN OUT bind parameter, a bind value object containing
29712973
[`val`](#executebindParams), [`dir`](#executebindParams),
29722974
[`type`](#executebindParams) and [`maxSize`](#executebindParams)
2973-
properties is used.
2975+
properties is used. For
2976+
[PL/SQL Associative Array binds](#plsqlindexbybinds) a
2977+
[`maxArraySize`](#executebindParams) property is required.
29742978
29752979
The `dir` attribute should be `BIND_OUT` or `BIND_INOUT`.
29762980
@@ -3005,9 +3009,6 @@ bind-by-position is done by passing an array of bind values, then the
30053009
OUT and IN OUT binds are in an array with the bind positions in the
30063010
same order.
30073011
3008-
With PL/SQL statements, only scalar parameters can be bound. An array
3009-
of values cannot be passed to a PL/SQL indexed table bind parameter.
3010-
30113012
Here is an example program showing the use of binds:
30123013
30133014
```javascript
@@ -3252,6 +3253,145 @@ connection.execute(
32523253
See [Working with CLOB and BLOB Data](#lobhandling) for more information
32533254
on working with Lob streams.
32543255
3256+
### <a name="plsqlindexbybinds"></a> 12.6 PL/SQL Collection Associative Array (Index-by) Bind Parameters
3257+
3258+
Arrays of strings and numbers can be bound to PL/SQL IN, IN OUT, and
3259+
OUT parameters of PL/SQL `INDEX BY` associative array type. This type
3260+
was formerly called PL/SQL tables or index-by tables. This method of
3261+
binding can be a very efficient way of transferring small data sets.
3262+
Note PL/SQL's `VARRAY` and nested table collection types cannot be
3263+
bound.
3264+
3265+
To bind arrays use the named bind syntax:
3266+
3267+
```javascript
3268+
connection.execute(
3269+
"BEGIN mypkg.myinproc(:bv); END;",
3270+
{
3271+
bv: { type: oracledb.NUMBER,
3272+
dir: oracledb.BIND_IN,
3273+
val: [1, 2, 23, 4, 10]
3274+
}
3275+
}, . . .
3276+
```
3277+
3278+
Positional bind syntax is not supported in this release.
3279+
3280+
For OUT and IN OUT binds, the [`maxArraySize`](#executebindParams)
3281+
bind property must be set. Its value is the maximum number of
3282+
elements that can be returned in an array. An error will occur if the
3283+
PL/SQL block attempts to insert data beyond this limit. If the PL/SQL
3284+
code returns fewer items, the JavaScript array will have the actual
3285+
number of data elements and will not contain null entries. Setting
3286+
`maxArraySize` larger than needed will cause unnecessary memory
3287+
allocation.
3288+
3289+
For IN OUT binds, `maxArraySize` can be greater than the number of
3290+
elements in the input array. This allows more values to be returned
3291+
than are passed in.
3292+
3293+
For IN binds, `maxArraySize` is ignored, as also is `maxSize`.
3294+
3295+
For `STRING` IN OUT or OUT binds, the string length
3296+
[`maxSize`](#executebindParams) property may be set. If it is not set
3297+
the memory allocated per string will default to 200 bytes. If the
3298+
value is not large enough to hold the longest string data item in the
3299+
collection a runtime error occurs. To avoid unnecessary memory
3300+
allocation, do not let the size be larger than needed.
3301+
3302+
See
3303+
[plsqlarray.js](https://github.com/oracle/node-oracledb/tree/master/examples/plsqlarray.js)
3304+
for a full example.
3305+
3306+
The following example passes an array of values to a PL/SQL procedure
3307+
which inserts them into a table. The procedure is defined as:
3308+
3309+
```sql
3310+
CREATE TABLE mytab (numcol NUMBER);
3311+
3312+
CREATE OR REPLACE PACKAGE mypkg IS
3313+
TYPE numtype IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3314+
PROCEDURE myinproc(p IN numtype);
3315+
END;
3316+
/
3317+
3318+
CREATE OR REPLACE PACKAGE BODY mypkg IS
3319+
PROCEDURE myinproc(p IN numtype) IS
3320+
BEGIN
3321+
FORALL i IN INDICES OF p
3322+
INSERT INTO mytab (numcol) VALUES (p(i));
3323+
END;
3324+
END;
3325+
/
3326+
```
3327+
3328+
With this, the following JavaScript will result in `mytab` containing
3329+
one row per value:
3330+
3331+
```javascript
3332+
connection.execute(
3333+
"BEGIN mypkg.myinproc(:bv); END;",
3334+
{
3335+
bv: { type: oracledb.NUMBER,
3336+
dir: oracledb.BIND_IN,
3337+
val: [1, 2, 23, 4, 10]
3338+
}
3339+
},
3340+
function (err) { . . . });
3341+
```
3342+
3343+
The next example fetches an array of values from a table:
3344+
3345+
```sql
3346+
CREATE TABLE mytab (numcol NUMBER);
3347+
INSERT INTO mytable (numcol) VALUES (10);
3348+
INSERT INTO mytable (numcol) VALUES (25);
3349+
INSERT INTO mytable (numcol) VALUES (50);
3350+
COMMIT;
3351+
3352+
CREATE OR REPLACE PACKAGE mypkg IS
3353+
TYPE numtype IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3354+
PROCEDURE myoutproc(p OUT numtype);
3355+
END;
3356+
/
3357+
3358+
CREATE OR REPLACE PACKAGE BODY mypkg IS
3359+
PROCEDURE myoutproc(p OUT numtype) IS
3360+
BEGIN
3361+
SELECT numcol BULK COLLECT INTO p FROM mytab ORDER BY 1;
3362+
END;
3363+
END;
3364+
/
3365+
```
3366+
3367+
With this table and package, the following JavaScript will print
3368+
`[ 10, 25, 50 ]`.
3369+
3370+
```javascript
3371+
connection.execute(
3372+
"BEGIN mypkg.myoutproc(:bv); END;",
3373+
{
3374+
bv: { type: oracledb.NUMBER,
3375+
dir: oracledb.BIND_OUT,
3376+
maxArraySize: 10 // allocate memory to hold 10 numbers
3377+
}
3378+
},
3379+
function (err, result) {
3380+
if (err) { console.error(err.message); return; }
3381+
console.log(result.outBinds.bv);
3382+
});
3383+
```
3384+
3385+
If `maxArraySize` was reduced to `2`, the script would fail with:
3386+
3387+
```
3388+
ORA-06513: PL/SQL: index for PL/SQL table out of range for host language array
3389+
```
3390+
3391+
See [Oracledb Constants](#oracledbconstants) and
3392+
[execute(): Bind Parameters](#executebindParams) for more information
3393+
about binding.
3394+
32553395
## <a name="transactionmgt"></a> 13. Transaction Management
32563396
32573397
By default,

0 commit comments

Comments
 (0)