Skip to content

Commit 47b9697

Browse files
committed
Add connection.getStatementInfo()
1 parent 14001f7 commit 47b9697

File tree

6 files changed

+332
-10
lines changed

6 files changed

+332
-10
lines changed

doc/api.md

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ limitations under the License.
3636
- [`BIND_IN`](#oracledbconstantsbinddir), [`BIND_INOUT`](#oracledbconstantsbinddir), [`BIND_OUT`](#oracledbconstantsbinddir)
3737
- 3.1.5 [Privileged Connection Constants](#oracledbconstantsprivilege)
3838
- [`SYSDBA`](#oracledbconstantsprivilege), [`SYSOPER`](#oracledbconstantsprivilege), [`SYSASM`](#oracledbconstantsprivilege), [`SYSBACKUP`](#oracledbconstantsprivilege), [`SYSDG`](#oracledbconstantsprivilege), [`SYSKM`](#oracledbconstantsprivilege), [`SYSRAC`](#oracledbconstantsprivilege)
39+
- 3.1.6 [SQL Statement Type Constants](#oracledbconstantsstmttype)
40+
- [`STMT_TYPE_UNKNOWN`](#oracledbconstantsstmttype), [`STMT_TYPE_SELECT`](#oracledbconstantsstmttype), [`STMT_TYPE_UPDATE`](#oracledbconstantsstmttype), [`STMT_TYPE_DELETE`](#oracledbconstantsstmttype), [`STMT_TYPE_INSERT`](#oracledbconstantsstmttype), [`STMT_TYPE_CREATE`](#oracledbconstantsstmttype), [`STMT_TYPE_DROP`](#oracledbconstantsstmttype), [`STMT_TYPE_ALTER`](#oracledbconstantsstmttype), [`STMT_TYPE_BEGIN`](#oracledbconstantsstmttype), [`STMT_TYPE_DECLARE`](#oracledbconstantsstmttype), [`STMT_TYPE_CALL`](#oracledbconstantsstmttype), [`STMT_TYPE_EXPLAIN_PLAN`](#oracledbconstantsstmttype), [`STMT_TYPE_MERGE`](#oracledbconstantsstmttype), [`STMT_TYPE_ROLLBACK`](#oracledbconstantsstmttype), [`STMT_TYPE_COMMIT`](#oracledbconstantsstmttype)
41+
3942
- 3.2 [Oracledb Properties](#oracledbproperties)
4043
- 3.2.1 [`autoCommit`](#propdbisautocommit)
4144
- 3.2.2 [`connectionClass`](#propdbconclass)
@@ -134,10 +137,11 @@ limitations under the License.
134137
- 4.2.6.4.3 [`resultSet`](#execresultset)
135138
- 4.2.6.4.4 [`rows`](#execrows)
136139
- 4.2.6.4.5 [`rowsAffected`](#execrowsaffected)
137-
- 4.2.7 [`ping()`](#connectionping)
138-
- 4.2.8 [`queryStream()`](#querystream)
139-
- 4.2.9 [`release()`](#release)
140-
- 4.2.10 [`rollback()`](#rollback)
140+
- 4.2.7 [`getStatementInfo()`](#getstmtinfo)
141+
- 4.2.8 [`ping()`](#connectionping)
142+
- 4.2.9 [`queryStream()`](#querystream)
143+
- 4.2.10 [`release()`](#release)
144+
- 4.2.11 [`rollback()`](#rollback)
141145
5. [Lob Class](#lobclass)
142146
- 5.1 [Lob Properties](#lobproperties)
143147
- 5.1.1 [`chunkSize`](#proplobchunksize)
@@ -558,6 +562,44 @@ oracledb.SYSKM // (524288) SYSKM privileges
558562
oracledb.SYSRAC // (1048576) SYSRAC privileges
559563
```
560564

565+
#### <a name="oracledbconstantsstmttype"></a> 3.1.6 SQL Statement Type Constants
566+
567+
Constants for [`connection.getStatementInfo()`](#getstmtinfo)
568+
properties.
569+
570+
571+
```
572+
oracledb.STMT_TYPE_UNKNOWN // (0) Unknown statement type
573+
574+
oracledb.STMT_TYPE_SELECT // (1) SELECT
575+
576+
oracledb.STMT_TYPE_UPDATE // (2) UPDATE
577+
578+
oracledb.STMT_TYPE_DELETE // (3) DELETE
579+
580+
oracledb.STMT_TYPE_INSERT // (4) INSERT
581+
582+
oracledb.STMT_TYPE_CREATE // (5) CREATE
583+
584+
oracledb.STMT_TYPE_DROP // (6) DROP
585+
586+
oracledb.STMT_TYPE_ALTER // (7) ALTER
587+
588+
oracledb.STMT_TYPE_BEGIN // (8) BEGIN
589+
590+
oracledb.STMT_TYPE_DECLARE // (9) DECLARE
591+
592+
oracledb.STMT_TYPE_CALL // (10) CALL
593+
594+
oracledb.STMT_TYPE_EXPLAIN_PLAN // (15) EXPLAIN PLAN
595+
596+
oracledb.STMT_TYPE_MERGE // (16) MERGE
597+
598+
oracledb.STMT_TYPE_ROLLBACK // (17) ROLLBACK
599+
600+
oracledb.STMT_TYPE_COMMIT // (21) COMMIT
601+
```
602+
561603
### <a name="oracledbproperties"></a> 3.2 Oracledb Properties
562604

563605
The properties of the *Oracledb* object are used for setting up
@@ -2164,7 +2206,7 @@ variables, and the number of rows affected by the execution of
21642206

21652207
Parameter | Description
21662208
----------|------------
2167-
[`String sql`](#executesqlparam) | The SQL string that is executed. The SQL string may contain bind parameters.
2209+
[`String sql`](#executesqlparam) | The SQL statement that is executed. The statement may contain bind parameters.
21682210
[`Object bindParams`](#executebindParams) | This function parameter is needed if there are bind parameters in the SQL statement.
21692211
[`Object options`](#executeoptions) | This is an optional parameter to `execute()` that may be used to control statement execution.
21702212
[`function(Error error, [Object result])`](#executecallback) | Callback function with the execution results.
@@ -2469,7 +2511,69 @@ the number of rows affected, for example the number of rows
24692511
inserted. For non-DML statements such as queries, or if no rows are
24702512
affected, then `rowsAffected` will appear as undefined.
24712513

2472-
#### <a name="connectionping"></a> 4.2.7 `connection.ping()`
2514+
#### <a name="getstmtinfo"></a> 4.2.7 `connection.getStatementInfo()`
2515+
2516+
##### Prototype
2517+
2518+
Callback:
2519+
```
2520+
getStatementInfo(String sql, function(Error error, [Object information]){});
2521+
```
2522+
Promise:
2523+
```
2524+
promise = getStatementInfo(String sql);
2525+
```
2526+
2527+
##### Description
2528+
2529+
Parses a SQL statement and returns information about it. This is most
2530+
useful for finding column names of queries, and for finding the names
2531+
of bind variables used.
2532+
2533+
This method performs a round-trip to the database, so unnecessary
2534+
calls should be avoided.
2535+
2536+
The information is provided by lower level APIs that have some
2537+
limitations. Some uncommon statements will return the statement type
2538+
as `oracledb.STMT_TYPE_UNKNOWN`. DDL statements are not parsed, so
2539+
syntax errors in them will not be reported. The direction and types
2540+
of bind variables cannot be determined.
2541+
2542+
This method was added in node-oracledb 2.2.
2543+
2544+
##### Parameters
2545+
2546+
```
2547+
String sql
2548+
```
2549+
2550+
The SQL statement to parse.
2551+
2552+
```
2553+
function(Error error, [Object information])
2554+
```
2555+
2556+
The parameters of the callback function are:
2557+
2558+
Callback function parameter | Description
2559+
----------------------------|-------------
2560+
*Error error* | If `getStatementInfo()` succeeds, `error` is NULL. If an error occurs, then `error` contains the [error message](#errorobj).
2561+
*Object information* | The `information` object, described below.
2562+
2563+
Depending on the statement type, the `information` object may contain:
2564+
2565+
- `bindNames`: an array of strings corresponding to the unique names
2566+
of the bind variables used in the SQL statement.
2567+
2568+
- `metaData`: containing properties equivalent to those given by
2569+
`execute()` [extendedMetaData](#execmetadata). This property exists
2570+
only for queries.
2571+
2572+
- `statementType`: an integer corresponding to one of the [SQL
2573+
Statement Type Constants](#oracledbconstantsstmttype).
2574+
2575+
2576+
#### <a name="connectionping"></a> 4.2.8 `connection.ping()`
24732577

24742578
##### Prototype
24752579

@@ -2490,7 +2594,7 @@ health checks. A ping only confirms that a single connection is
24902594
usable at the time of the ping.
24912595

24922596
Pinging doesn't replace error checking during statement execution,
2493-
since network or database failure may occur in the time between
2597+
since network or database failure may occur in the interval between
24942598
`ping()` and `execute()` calls.
24952599

24962600
Pinging requires a round-trip to the database so unnecessary ping
@@ -2513,7 +2617,7 @@ Callback function parameter | Description
25132617
----------------------------|-------------
25142618
*Error error* | If `ping()` succeeds, `error` is NULL. If an error occurs, then `error` contains the [error message](#errorobj).
25152619

2516-
#### <a name="querystream"></a> 4.2.8 `connection.queryStream()`
2620+
#### <a name="querystream"></a> 4.2.9 `connection.queryStream()`
25172621

25182622
##### Prototype
25192623

@@ -2552,11 +2656,11 @@ This method was added in node-oracledb 1.8.
25522656

25532657
See [execute()](#execute).
25542658

2555-
#### <a name="release"></a> 4.2.9 `connection.release()`
2659+
#### <a name="release"></a> 4.2.10 `connection.release()`
25562660

25572661
An alias for [connection.close()](#connectionclose).
25582662

2559-
#### <a name="rollback"></a> 4.2.10 `connection.rollback()`
2663+
#### <a name="rollback"></a> 4.2.11 `connection.rollback()`
25602664

25612665
##### Prototype
25622666

lib/connection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var resultset = require('./resultset.js');
2323
var QueryStream = require('./querystream.js');
2424
var nodbUtil = require('./util.js');
2525
var executePromisified;
26+
var getStatementInfoPromisified;
2627
var commitPromisified;
2728
var createLobPromisified;
2829
var rollbackPromisified;
@@ -183,6 +184,18 @@ function execute(sql, a2, a3, a4) {
183184

184185
executePromisified = nodbUtil.promisify(execute);
185186

187+
// This getStatementInfo function is just a place holder to allow for easier extension later.
188+
function getStatementInfo(sql, getStatementInfoCb) {
189+
var self = this;
190+
191+
nodbUtil.assert(arguments.length === 2, 'NJS-009');
192+
nodbUtil.assert(typeof getStatementInfoCb === 'function', 'NJS-006', 1);
193+
194+
self._getStatementInfo.apply(self, arguments);
195+
}
196+
197+
getStatementInfoPromisified = nodbUtil.promisify(getStatementInfo);
198+
186199
// This commit function is just a place holder to allow for easier extension later.
187200
function commit(commitCb) {
188201
var self = this;
@@ -298,6 +311,14 @@ function extend(conn, oracledb, pool) {
298311
_execute: {
299312
value: conn.execute
300313
},
314+
_getStatementInfo: {
315+
value: conn.getStatementInfo
316+
},
317+
getStatementInfo: {
318+
value: getStatementInfoPromisified,
319+
enumerable: true,
320+
writable: true
321+
},
301322
queryStream: {
302323
value: queryStream,
303324
enumerable: true,

lib/oracledb.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,66 @@ function extend(oracledb) {
396396
value : 1112,
397397
enumerable : true
398398
},
399+
STMT_TYPE_UNKNOWN: {
400+
value : 0,
401+
enumerable : true
402+
},
403+
STMT_TYPE_SELECT: {
404+
value : 1,
405+
enumerable : true
406+
},
407+
STMT_TYPE_UPDATE: {
408+
value : 2,
409+
enumerable : true
410+
},
411+
STMT_TYPE_DELETE: {
412+
value : 3,
413+
enumerable : true
414+
},
415+
STMT_TYPE_INSERT: {
416+
value : 4,
417+
enumerable : true
418+
},
419+
STMT_TYPE_CREATE: {
420+
value : 5,
421+
enumerable : true
422+
},
423+
STMT_TYPE_DROP: {
424+
value : 6,
425+
enumerable : true
426+
},
427+
STMT_TYPE_ALTER: {
428+
value : 7,
429+
enumerable : true
430+
},
431+
STMT_TYPE_BEGIN: {
432+
value : 8,
433+
enumerable : true
434+
},
435+
STMT_TYPE_DECLARE: {
436+
value : 9,
437+
enumerable : true
438+
},
439+
STMT_TYPE_CALL: {
440+
value : 10,
441+
enumerable : true
442+
},
443+
STMT_TYPE_EXPLAIN_PLAN: {
444+
value : 15,
445+
enumerable : true
446+
},
447+
STMT_TYPE_MERGE: {
448+
value : 16,
449+
enumerable : true
450+
},
451+
STMT_TYPE_ROLLBACK: {
452+
value : 17,
453+
enumerable : true
454+
},
455+
STMT_TYPE_COMMIT: {
456+
value : 21,
457+
enumerable : true
458+
},
399459
STRING: {
400460
value: 2001,
401461
enumerable: true

src/njsCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ class njsBaton {
261261
dpiConn *dpiConnHandle;
262262
dpiStmt *dpiStmtHandle;
263263
dpiLob *dpiLobHandle;
264+
dpiStmtInfo stmtInfo;
264265
uint32_t stmtCacheSize;
265266
uint32_t lobPrefetchSize;
266267
uint32_t maxRows;

0 commit comments

Comments
 (0)