Skip to content

Commit 91c285e

Browse files
committed
Emphasize bind-by-position per comment by @bjouhier
1 parent b35151d commit 91c285e

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

doc/api.md

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ Oracledb.ARRAY // Fetch each row as array of column values
255255
Oracledb.OBJECT // Fetch each row as an object
256256
```
257257

258-
#### Type constants for `execute()` [bind parameter](#executebindParams) and [Lob](#proplobpiecesize) `type` properties, for [`fetchAsString`](#propdbfetchasstring), and for [`fetchInfo`](#propfetchinfo). Not all constants can be used in all places:
258+
#### Type constants for `execute()` [bind parameter](#executebindParams) and [Lob](#proplobpiecesize) `type` properties, for [`fetchAsString`](#propdbfetchasstring), and for [`fetchInfo`](#propfetchinfo)
259+
260+
Not all constants can be used in all places:
259261

260262
```
261263
Oracledb.BLOB // Bind a BLOB to a Node.js Stream
@@ -2842,38 +2844,51 @@ variable, is used during execution of the SQL statement.
28422844
28432845
In this example, the SQL bind parameters *:country\_id* and
28442846
*:country\_name* can be bound to values in node-oracledb using an
2845-
array:
2847+
array. This is often called "bind by position":
28462848
28472849
```javascript
2848-
connection.execute("INSERT INTO countries VALUES (:country_id, :country_name)",
2849-
[90, "Tonga"],
2850-
function(err, result)
2851-
{
2852-
if (err)
2853-
console.error(err.message);
2854-
else
2855-
console.log("Rows inserted " + result.rowsAffected);
2856-
});
2850+
connection.execute(
2851+
"INSERT INTO countries VALUES (:country_id, :country_name)",
2852+
[90, "Tonga"],
2853+
function(err, result)
2854+
{
2855+
if (err)
2856+
console.error(err.message);
2857+
else
2858+
console.log("Rows inserted " + result.rowsAffected);
2859+
});
28572860
```
28582861
28592862
The position of the array values corresponds to the position of the
2860-
SQL bind variables. This is often called "bind by position".
2863+
SQL bind variables as they occur in the statement, regardless of their
2864+
names. This is still true even if the bind variables are named like
2865+
`:0`, `:1` etc. The following snippet will fail because the country
2866+
name needs to be the second entry of the array so it becomes the
2867+
second value in the `INSERT` statement
2868+
2869+
```javascript
2870+
connection.execute(
2871+
"INSERT INTO countries VALUES (:1, :0)",
2872+
["Tonga", 90], // fail
2873+
. . .
2874+
```
28612875
28622876
Instead of binding by array, an object that names each bind value can
28632877
be used. The attributes can in be any order but their names must
28642878
match the SQL bind parameter names. This is often called "bind by
28652879
name":
28662880
28672881
```javascript
2868-
connection.execute("INSERT INTO countries VALUES (:country_id, :country_name)",
2869-
{country_id: 90, country_name: "Tonga"},
2870-
function(err, result)
2871-
{
2872-
if (err)
2873-
console.error(err.message);
2874-
else
2875-
console.log("Rows inserted " + result.rowsAffected);
2876-
});
2882+
connection.execute(
2883+
"INSERT INTO countries VALUES (:country_id, :country_name)",
2884+
{country_id: 90, country_name: "Tonga"},
2885+
function(err, result)
2886+
{
2887+
if (err)
2888+
console.error(err.message);
2889+
else
2890+
console.log("Rows inserted " + result.rowsAffected);
2891+
});
28772892
```
28782893
28792894
The default direction for binding is `BIND_IN`. The datatype used for
@@ -2886,18 +2901,19 @@ explicit attributes for the bind direction (`dir`), the datatype
28862901
```javascript
28872902
var oracledb = require('oracledb');
28882903
. . .
2889-
connection.execute("INSERT INTO countries VALUES (:country_id, :country_name)",
2890-
{
2891-
country_id: { val: 90, dir: oracledb.BIND_IN, type: oracledb.NUMBER },
2892-
country_name: { val: "Tonga", dir: oracledb.BIND_IN, type:oracledb.STRING }
2893-
},
2894-
function(err, result)
2895-
{
2896-
if (err)
2897-
console.error(err.message);
2898-
else
2899-
console.log("Rows inserted " + result.rowsAffected);
2900-
});
2904+
connection.execute(
2905+
"INSERT INTO countries VALUES (:country_id, :country_name)",
2906+
{
2907+
country_id: { val: 90, dir: oracledb.BIND_IN, type: oracledb.NUMBER },
2908+
country_name: { val: "Tonga", dir: oracledb.BIND_IN, type:oracledb.STRING }
2909+
},
2910+
function(err, result)
2911+
{
2912+
if (err)
2913+
console.error(err.message);
2914+
else
2915+
console.log("Rows inserted " + result.rowsAffected);
2916+
});
29012917
```
29022918
29032919
For IN binds the direction must be `BIND_IN`. The type can be

0 commit comments

Comments
 (0)