Skip to content

Commit c884855

Browse files
committed
Add DML RETURNING support
1 parent 3844f84 commit c884855

File tree

11 files changed

+837
-88
lines changed

11 files changed

+837
-88
lines changed

doc/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ Callback function parameter | Description
936936
##### Prototype
937937

938938
```
939-
void execute(String sql, [Object bindParams, [Object options,]] function(Error error, [Object result]){});
939+
void execute(String sql [, Object bindParams [, Object options]], function(Error error [, Object result]){});
940940
```
941941

942942
##### Return Value
@@ -1013,7 +1013,7 @@ Options Property | Description
10131013
*String outFormat* | Overrides *Oracledb* [`outFormat`](#propdboutformat)
10141014

10151015
```
1016-
function(Error error, [Object result])
1016+
function(Error error [, Object result])
10171017
```
10181018

10191019
The parameters of the callback function are:

examples/demo.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ SHOW ERRORS
4141
DROP TABLE j_purchaseorder;
4242
CREATE TABLE j_purchaseorder
4343
(po_document VARCHAR2(4000) CONSTRAINT ensure_json CHECK (po_document IS JSON));
44+
45+
-- DML RETURNING
46+
DROP TABLE dmlrupdtab;
47+
CREATE TABLE dmlrupdtab (id NUMBER, name VARCHAR2(40));
48+
INSERT INTO dmlrupdtab VALUES (1001, 'Venkat');
49+
INSERT INTO dmlrupdtab VALUES (1002, 'Neeharika');
50+
COMMIT;

examples/dmlrupd1.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */
2+
3+
/******************************************************************************
4+
*
5+
* You may not use the identified files except in compliance with the Apache
6+
* License, Version 2.0 (the "License.")
7+
*
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* NAME
19+
* dmlrupd1.js
20+
*
21+
* DESCRIPTION
22+
* Example of 'DML Returning' with a single row match
23+
* Use demo.sql to create the required table or do:
24+
* DROP TABLE dmlrupdtab;
25+
* CREATE TABLE dmlrupdtab (id NUMBER, name VARCHAR2(40));
26+
* INSERT INTO dmlrupdtab VALUES (1001, 'Venkat');
27+
* INSERT INTO dmlrupdtab VALUES (1002, 'Neeharika');
28+
* COMMIT;
29+
*
30+
*****************************************************************************/
31+
32+
var oracledb = require( 'oracledb' );
33+
var dbConfig = require('./dbconfig.js');
34+
35+
oracledb.getConnection(
36+
{
37+
user : dbConfig.user,
38+
password : dbConfig.password,
39+
connectString : dbConfig.connectString
40+
},
41+
function(err, connection)
42+
{
43+
if (err)
44+
{
45+
console.error(err);
46+
return;
47+
}
48+
49+
connection.execute(
50+
"UPDATE DMLRUPDTAB SET NAME = :name WHERE ID = :id RETURNING ID, NAME INTO :RID, :RNAME",
51+
{
52+
id: 1001,
53+
name: "Krishna",
54+
rid: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT },
55+
rname: { type: oracledb.STRING, dir: oracledb.BIND_OUT }
56+
},
57+
{ isAutoCommit: true },
58+
function(err, result)
59+
{
60+
if (err)
61+
{
62+
console.error(err);
63+
return;
64+
}
65+
console.log(result.outBinds);
66+
});
67+
});

examples/dmlrupd2.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */
2+
3+
/******************************************************************************
4+
*
5+
* You may not use the identified files except in compliance with the Apache
6+
* License, Version 2.0 (the "License.")
7+
*
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* NAME
19+
* dmlrupd2.js
20+
*
21+
* DESCRIPTION
22+
* Example of 'DML Returning' with multiple rows matched
23+
* Use demo.sql to create the required table or do:
24+
* DROP TABLE dmlrupdtab;
25+
* CREATE TABLE dmlrupdtab (id NUMBER, name VARCHAR2(40));
26+
* INSERT INTO dmlrupdtab VALUES (1001, 'Venkat');
27+
* INSERT INTO dmlrupdtab VALUES (1002, 'Neeharika');
28+
* COMMIT;
29+
*
30+
*****************************************************************************/
31+
32+
var oracledb = require( 'oracledb' );
33+
var dbConfig = require('./dbconfig.js');
34+
35+
oracledb.getConnection(
36+
{
37+
user : dbConfig.user,
38+
password : dbConfig.password,
39+
connectString : dbConfig.connectString
40+
},
41+
function(err, connection)
42+
{
43+
if (err)
44+
{
45+
console.error(err);
46+
return;
47+
}
48+
49+
connection.execute(
50+
"UPDATE DMLRUPDTAB SET NAME = :name RETURNING ID, NAME INTO :RID, :RNAME",
51+
{
52+
name: "Chris",
53+
rid: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT },
54+
rname: { type: oracledb.STRING, dir: oracledb.BIND_OUT }
55+
},
56+
{ isAutoCommit: true },
57+
function(err, result)
58+
{
59+
if (err)
60+
{
61+
console.error(err);
62+
return;
63+
}
64+
console.log(result.outBinds);
65+
});
66+
});

src/dpi/include/dpiStmt.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ typedef struct
131131
} MetaData;
132132

133133

134+
// Application (Driver) level callback function prototype
135+
typedef int (*cbtype) (void *ctx, DPI_SZ_TYPE nRows, unsigned long iter,
136+
unsigned long index, dvoid **bufpp, void **alenp,
137+
dvoid **indpp, unsigned short **rcodepp,
138+
unsigned char *piecep );
134139

135140
class Stmt
136141
{
@@ -141,17 +146,23 @@ class Stmt
141146
// properties
142147
virtual DpiStmtType stmtType() const = 0;
143148

149+
virtual bool isDML() const = 0 ;
150+
151+
virtual bool isReturning() = 0 ;
152+
144153
virtual DPI_SZ_TYPE rowsAffected() const = 0;
145154

146155
virtual unsigned int numCols() = 0;
147156

148157
// methods
149158
virtual void bind(unsigned int pos, unsigned short type, void *buf,
150-
DPI_SZ_TYPE bufSize, short *ind, DPI_BUFLEN_TYPE *bufLen) = 0;
159+
DPI_SZ_TYPE bufSize, short *ind, DPI_BUFLEN_TYPE *bufLen,
160+
void *data, cbtype cb = NULL ) = 0;
151161

152162
virtual void bind(const unsigned char *name, int nameLen,
153163
unsigned short type, void *buf, DPI_SZ_TYPE bufSize,
154-
short *ind, DPI_BUFLEN_TYPE *bufLen) = 0;
164+
short *ind, DPI_BUFLEN_TYPE *bufLen,
165+
void *data, cbtype cb = NULL ) = 0;
155166

156167
virtual void execute ( int numIterations, bool isAutoCommit = false) = 0;
157168

0 commit comments

Comments
 (0)