Skip to content

Commit 0c726ef

Browse files
committed
Move SQL Execution section to a major heading level
1 parent 3408789 commit 0c726ef

File tree

1 file changed

+128
-127
lines changed

1 file changed

+128
-127
lines changed

doc/api.md

Lines changed: 128 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,24 @@
5454
- 5.2.3 [execute()](#execute)
5555
- 5.2.4 [release()](#release)
5656
- 5.2.5 [rollback()](#rollback)
57-
- 5.3 [SQL Execution](#sqlexecution)
58-
- 5.3.1 [IN Bind Parameters](#inbind)
59-
- 5.3.2 [OUT and IN OUT Bind Parameters](#outbind)
60-
- 5.3.3 [SELECT Statements](#select)
61-
- [Result Type Mapping](#typemap)
62-
- [Statement Caching](#stmtcache)
63-
6. [Transaction Management](#transactionmgt)
64-
7. [Database Resident Connection Pooling](#drcp)
65-
8. [External Configuration](#oraaccess)
57+
6. [SQL Execution](#sqlexecution)
58+
- 6.1 [SELECT Statements](#select)
59+
- 6.1.1 [Result Type Mapping](#typemap)
60+
- 6.1.2 [Statement Caching](#stmtcache)
61+
- 6.2 [Bind Parameters for Prepared Statements](#bind)
62+
- 6.2.1 [IN Bind Parameters](#inbind)
63+
- 6.2.2 [OUT and IN OUT Bind Parameters](#outbind)
64+
7. [Transaction Management](#transactionmgt)
65+
8. [Database Resident Connection Pooling](#drcp)
66+
9. [External Configuration](#oraaccess)
6667

6768
## <a name="intro"></a> 1. Introduction
6869

6970
The Oracle Database Node.js driver *node-oracledb* powers high
7071
performance Node.js applications.
7172

7273
This document shows how to use node-oracledb. For how to install
73-
node-oracledb, see [INSTALL](#../INSTALL.md).
74+
node-oracledb, see [INSTALL](../INSTALL.md).
7475

7576
### Example: Simple SELECT statement implementation in Node.js
7677

@@ -1093,26 +1094,135 @@ Callback function parameter | Description
10931094
----------------------------|-------------
10941095
*Error error* | If `rollback()` succeeds `error` is NULL. If an error occurs, then `error` contains the [error message](#errorobj).
10951096

1096-
### <a name="sqlexecution"></a> 5.3 SQL Execution
1097+
## <a name="sqlexecution"></a> 6. SQL Execution
10971098

10981099
A SQL or PL/SQL statement may be executed using the *Connection*
10991100
[`execute()`](#execute) method.
11001101

1102+
After all database calls on the connection complete, the application
1103+
should use the [`release()`](#release) call to release the connection.
1104+
11011105

11021106
[DML](https://docs.oracle.com/database/121/CNCPT/glossary.htm#CNCPT2042)
11031107
statements are not committed unless the `commit()` call is issued or
11041108
the `isAutoCommit` property is *true* at the time of execution. Any
11051109
ongoing transaction will be rolled back when [`release()`](#release)
11061110
is called, or when the application ends.
11071111

1112+
### <a name="select"></a> 6.1 SELECT Statements
1113+
1114+
If the `execute()` method contains a SQL `SELECT` statement, it returns
1115+
an array of rows. Each row, by default, is an array of column values.
1116+
The rows array holds up to `maxRows` number of rows.
1117+
1118+
The following example shows how to obtain rows returned a `SELECT`
1119+
statement.
1120+
1121+
```javascript
1122+
connection.execute("SELECT first_name, salary, hire_date "
1123+
+ "FROM employees, departments "
1124+
+ "WHERE employees.department_id = departments.department_id "
1125+
+ "AND departments.department_name = 'Accounting'",
1126+
function(err, result) {
1127+
if (err) { console.error(err.message); return; }
1128+
var rows = result.rows;
1129+
for (var i = 0; i < rows.length; i++)
1130+
console.log("Row " + i + " : " + rows[i]);
1131+
});
1132+
```
1133+
1134+
If run with Oracle's sample HR schema, the output is:
1135+
1136+
```
1137+
Row 0 : Shelley,12000,Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1138+
Row 1 : William,8300,Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1139+
```
1140+
1141+
Using this format is recommended for efficiency.
1142+
1143+
Alternatively, rows may be fetched as JavaScript objects. To do so,
1144+
specify the `outFormat` option to be `OBJECT`:
1145+
1146+
```javascript
1147+
connection.execute("SELECT first_name, salary, hire_date "
1148+
+ "FROM employees, departments "
1149+
+ "WHERE employees.department_id = departments.department_id "
1150+
+ "AND departments.department_name = 'Accounting'",
1151+
[], // No bind variables
1152+
{outFormat: oracledb.OBJECT},
1153+
function(err, result) {
1154+
if (err) { console.error(err.message); return; }
1155+
var rows = result.rows;
1156+
for (var i = 0; i < rows.length; i++)
1157+
console.log("Row " + i + " : " +
1158+
rows[i].FIRST_NAME + ", ",
1159+
rows[i].SALARY + ", ", rows[i].HIRE_DATE);
1160+
});
1161+
```
1162+
1163+
If run with Oracle's sample HR schema, the output is:
1164+
1165+
```
1166+
Row 0 : Shelley, 12000, Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1167+
Row 1 : William, 8300, Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1168+
```
1169+
1170+
In the preceding example, each row is a JavaScript object that
1171+
specifies column names and their respective values. Note that the
1172+
property names are uppercase. This is the default casing behavior for
1173+
Oracle client programs when a database table is created with
1174+
case-insensitive column names.
1175+
1176+
### <a name="typemap"></a> 6.1.1 Result Type Mapping
1177+
1178+
Oracle character, number and date columns can be selected. Data types
1179+
that are currently unsupported give a "datatype is not supported"
1180+
error.
1181+
1182+
Query result type mappings for Oracle Database types to JavaScript types are:
1183+
1184+
- Variable and fixed length character columns are mapped to JavaScript strings.
1185+
1186+
- All numeric columns are mapped to JavaScript numbers.
1187+
1188+
- Date and Timestamp columns are mapped to JavaScript dates.
1189+
Note that JavaScript Date has millisecond precision.
1190+
Therefore, timestamps having greater
1191+
precision lose their sub-millisecond fractional part
1192+
when fetched. Internally, `TIMESTAMP` and `DATE`
1193+
columns are fetched as `TIMESTAMP WITH LOCAL TIMEZONE` using
1194+
[OCIDateTime](https://docs.oracle.com/database/121/LNOCI/oci12oty.htm#LNOCI16840).
1195+
When binding a JavaScript Date value in an `INSERT` statement, the date is also inserted as `TIMESTAMP WITH
1196+
LOCAL TIMEZONE` using
1197+
[OCIDateTime](https://docs.oracle.com/database/121/LNOCI/oci12oty.htm#LNOCI16840).
1198+
1199+
### <a name="stmtcache"></a> 6.1.2 Statement Caching
1200+
1201+
Node-oracledb uses the
1202+
[Oracle OCI statement cache](https://docs.oracle.com/database/121/LNOCI/oci09adv.htm#i471377)
1203+
which manages a cache of statements for each session. In the database
1204+
server, statement caching lets cursors be used without reparsing the
1205+
statement. This eliminates repetitive statement parsing and reduces
1206+
meta data transfer costs between the driver and the database. This
1207+
improve performance and scalability.
1208+
1209+
In general, set the statement cache to the size of the working set of
1210+
statements being executed by the application.
1211+
1212+
The statement cache can be automatically tuned with the
1213+
[oraaccess.xml file](#oraaccess).
1214+
1215+
1216+
### <a name="bind"></a> 6.2 Bind Parameters for Prepared Statements
1217+
11081218
Using bind variables in SQL statements is recommended in preference to
11091219
constructing SQL statements by string concatenation. This is for
11101220
performance and security. IN binds are values passed into the
11111221
database. OUT binds are used to retrieve data. IN OUT binds are
11121222
passed in, and may return a different value after the statement
11131223
executes.
11141224

1115-
#### <a name="inbind"></a> 5.3.1 IN Bind Parameters
1225+
#### <a name="inbind"></a> 6.2.1 IN Bind Parameters
11161226

11171227
SQL and PL/SQL statements may contain bind parameters, indicated by
11181228
colon-prefixed identifiers or numerals. For example, this SQL
@@ -1158,7 +1268,7 @@ variables are used for the SQL execution.
11581268
With PL/SQL statements, only scalar parameters can be passed. An
11591269
array of values cannot be passed to a PL/SQL bind parameter.
11601270

1161-
#### <a name="outbind"></a> 5.3.2 OUT and IN OUT Bind Parameters
1271+
#### <a name="outbind"></a> 6.2.2 OUT and IN OUT Bind Parameters
11621272

11631273
For OUT and IN OUT binds, the bind value is an object containing
11641274
`val`, `dir`, `type` and `maxSize` properties. The `results`
@@ -1236,117 +1346,11 @@ The output would be:
12361346
{ io: 'ChrisJones', o: 101 }
12371347
```
12381348

1239-
#### <a name="select"></a> 5.3.3 SELECT Statements
1240-
1241-
If the the `execute()` method contains a SQL `SELECT` statement, it returns
1242-
an array of rows. Each row, by default, is an array of column values.
1243-
The rows array holds up to `maxRows` number of rows.
1244-
1245-
The following example shows how to obtain rows returned a `SELECT`
1246-
statement.
1247-
1248-
```javascript
1249-
connection.execute("SELECT first_name, salary, hire_date "
1250-
+ "FROM employees, departments "
1251-
+ "WHERE employees.department_id = departments.department_id "
1252-
+ "AND departments.department_name = :1",
1253-
["Accounting"],
1254-
function(err, result) {
1255-
if (err) { console.error(err.message); return; }
1256-
var rows = result.rows;
1257-
for (var i = 0; i < rows.length; i++)
1258-
console.log("Row " + i + " : " + rows[i]);
1259-
});
1260-
1261-
```
1262-
1263-
If run with Oracle's sample HR schema, the output is:
1264-
1265-
```
1266-
Row 0 : Shelley,12000,Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1267-
Row 1 : William,8300,Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1268-
```
1269-
1270-
Using this format is recommended for efficiency.
1271-
1272-
Alternatively, rows may be fetched as JavaScript objects. To do so,
1273-
specify the `outFormat` option to be `OBJECT`:
1274-
1275-
```javascript
1276-
connection.execute("SELECT first_name, salary, hire_date "
1277-
+ "FROM employees, departments "
1278-
+ "WHERE employees.department_id = departments.department_id "
1279-
+ "AND departments.department_name = :1",
1280-
["Accounting"],
1281-
{outFormat: oracledb.OBJECT},
1282-
function(err, result) {
1283-
if (err) { console.error(err.message); return; }
1284-
var rows = result.rows;
1285-
for (var i = 0; i < rows.length; i++)
1286-
console.log("Row " + i + " : " +
1287-
rows[i].FIRST_NAME + ", ",
1288-
rows[i].SALARY + ", ", rows[i].HIRE_DATE);
1289-
});
1290-
1291-
```
1292-
1293-
If run with Oracle's sample HR schema, the output is:
1294-
1295-
```
1296-
Row 0 : Shelley, 12000, Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1297-
Row 1 : William, 8300, Tue Jun 07 1994 01:00:00 GMT-0700 (PDT)
1298-
```
1299-
1300-
In the preceding example, each row is a JavaScript object that
1301-
specifies column names and their respective values. Note that the
1302-
property names are uppercase. This is the default casing behavior for
1303-
Oracle client programs when a database table is created with
1304-
case-insensitive column names.
1305-
1306-
#### <a name="typemap"></a> Result Type Mapping
1307-
1308-
Oracle character, number and date columns can be selected. Data types
1309-
that are currently unsupported give a "datatype is not supported"
1310-
error.
1311-
1312-
Query result type mappings for Oracle Database types to JavaScript types are:
1313-
1314-
- Variable and fixed length character columns are mapped to JavaScript strings.
1315-
1316-
- All numeric columns are mapped to JavaScript numbers.
1317-
1318-
- Date and Timestamp columns are mapped to JavaScript dates.
1319-
Note that JavaScript Date has millisecond precision.
1320-
Therefore, timestamps having greater
1321-
precision lose their sub-millisecond fractional part
1322-
when fetched. Internally, `TIMESTAMP` and `DATE`
1323-
columns are fetched as `TIMESTAMP WITH LOCAL TIMEZONE` using
1324-
[OCIDateTime](https://docs.oracle.com/database/121/LNOCI/oci12oty.htm#LNOCI16840).
1325-
When binding a JavaScript Date value in an `INSERT` statement, the date is also inserted as `TIMESTAMP WITH
1326-
LOCAL TIMEZONE` using
1327-
[OCIDateTime](https://docs.oracle.com/database/121/LNOCI/oci12oty.htm#LNOCI16840).
1328-
1329-
#### <a name="stmtcache"></a> Statement Caching
1330-
1331-
Node-oracledb uses the
1332-
[Oracle OCI statement cache](https://docs.oracle.com/database/121/LNOCI/oci09adv.htm#i471377)
1333-
which manages a cache of statements for each session. In the database
1334-
server, statement caching lets cursors be used without reparsing the
1335-
statement. This eliminates repetitive statement parsing and reduces
1336-
meta data transfer costs between the driver and the database. This
1337-
improve performance and scalability.
1338-
1339-
In general, set the statement cache to the size of the working set of
1340-
statements being executed by the application.
1341-
1342-
The statement cache can be automatically tuned with the
1343-
[oraaccess.xml file](#oraaccess).
1344-
1345-
## <a name="transactionmgt"></a> 6. Transaction Management
1349+
## <a name="transactionmgt"></a> 7. Transaction Management
13461350

13471351
Node-oraclebd implements [`commit()`](#commit) and
1348-
[`rollback()`](#rollback) methods that can be used to control
1349-
transactions.
1352+
[`rollback()`](#rollback) methods that can be used to explicitly
1353+
control transactions.
13501354

13511355
If the [`isAutoCommit`](#propdbisautocommit) flag is set, then a
13521356
commit occurs at the end of each `execute()` call. This does not
@@ -1355,9 +1359,6 @@ For maximum efficiency, if a transaction consists of a number of
13551359
`execute()` calls, then set `isAutoCommit` to true for the last call
13561360
in preference to using an additional, explicit `commit()` call.
13571361

1358-
After all database calls on the connection complete, the application
1359-
should use the [`release()`](#release) call to release the connection.
1360-
13611362
When a connection is released, it rolls back any ongoing
13621363
transaction. Therefore if a released, pooled connection is used by
13631364
a subsequent [`pool.getConnection()`](#getconnection2) call, then any
@@ -1368,7 +1369,7 @@ transaction.
13681369
When an application ends, any uncommitted transaction on a connection
13691370
will be rolled back if there is no explicit commit.
13701371

1371-
## <a name="drcp"></a> 7. Database Resident Connection Pooling
1372+
## <a name="drcp"></a> 8. Database Resident Connection Pooling
13721373

13731374
[Database Resident Connection Pooling](http://docs.oracle.com/database/121/ADFNS/adfns_perf_scale.htm#ADFNS228)
13741375
enables database resource sharing for applications that run in
@@ -1411,7 +1412,7 @@ Oracle white paper
14111412
[PHP Scalability and High Availability](http://www.oracle.com/technetwork/topics/php/php-scalability-ha-twp-128842.pdf).
14121413
This paper also gives more detail on configuring DRCP.
14131414

1414-
## <a name="oraaccess"></a> 8. External Configuration
1415+
## <a name="oraaccess"></a> 9. External Configuration
14151416

14161417
When node-oracledb is linked with Oracle 12c client libraries, the Oracle
14171418
client-side configuration file

0 commit comments

Comments
 (0)