Skip to content

Commit 7e15f21

Browse files
committed
Add connection.executeMany() for batch statement execution
1 parent b263da9 commit 7e15f21

25 files changed

+2789
-434
lines changed

doc/api.md

Lines changed: 532 additions & 43 deletions
Large diffs are not rendered by default.

examples/demo.sql

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -130,7 +130,6 @@ BEGIN EXECUTE IMMEDIATE 'DROP TABLE dmlrupdtab'; EXCEPTION WHEN OTHERS THEN IF S
130130
CREATE TABLE dmlrupdtab (id NUMBER, name VARCHAR2(40));
131131
INSERT INTO dmlrupdtab VALUES (1001, 'Venkat');
132132
INSERT INTO dmlrupdtab VALUES (1002, 'Neeharika');
133-
COMMIT;
134133

135134
-- For LOB examples
136135
BEGIN EXECUTE IMMEDIATE 'DROP TABLE mylobs'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END;
@@ -186,3 +185,68 @@ SHOW ERRORS
186185
BEGIN EXECUTE IMMEDIATE 'DROP TABLE myraw'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END;
187186
/
188187
CREATE TABLE myraw (r RAW(64));
188+
189+
-- For the executemany*.js examples
190+
191+
BEGIN EXECUTE IMMEDIATE 'DROP TABLE em_tab'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END;
192+
/
193+
BEGIN EXECUTE IMMEDIATE 'DROP TABLE em_childtab'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END;
194+
/
195+
BEGIN EXECUTE IMMEDIATE 'DROP TABLE em_parenttab'; EXCEPTION WHEN OTHERS THEN IF SQLCODE <> -942 THEN RAISE; END IF; END;
196+
/
197+
198+
CREATE TABLE em_tab (
199+
id NUMBER NOT NULL,
200+
val VARCHAR2(20)
201+
);
202+
203+
CREATE TABLE em_parenttab (
204+
parentid NUMBER NOT NULL,
205+
description VARCHAR2(60) NOT NULL,
206+
CONSTRAINT parenttab_pk PRIMARY KEY (parentid)
207+
);
208+
209+
CREATE TABLE em_childtab (
210+
childid NUMBER NOT NULL,
211+
parentid NUMBER NOT NULL,
212+
description VARCHAR2(30) NOT NULL,
213+
CONSTRAINT em_childtab_pk PRIMARY KEY (childid),
214+
CONSTRAINT em_childtab_fk FOREIGN KEY (parentid) REFERENCES em_parenttab
215+
);
216+
217+
INSERT INTO em_parenttab VALUES (10, 'Parent 10');
218+
INSERT INTO em_parenttab VALUES (20, 'Parent 20');
219+
INSERT INTO em_parenttab VALUES (30, 'Parent 30');
220+
INSERT INTO em_parenttab VALUES (40, 'Parent 40');
221+
INSERT INTO em_parenttab VALUES (50, 'Parent 50');
222+
223+
INSERT INTO em_childtab VALUES (1001, 10, 'Child 1001 of Parent 10');
224+
INSERT INTO em_childtab VALUES (1002, 20, 'Child 1002 of Parent 20');
225+
INSERT INTO em_childtab VALUES (1003, 20, 'Child 1003 of Parent 20');
226+
INSERT INTO em_childtab VALUES (1004, 20, 'Child 1004 of Parent 20');
227+
INSERT INTO em_childtab VALUES (1005, 30, 'Child 1005 of Parent 30');
228+
INSERT INTO em_childtab VALUES (1006, 30, 'Child 1006 of Parent 30');
229+
INSERT INTO em_childtab VALUES (1007, 40, 'Child 1007 of Parent 40');
230+
INSERT INTO em_childtab VALUES (1008, 40, 'Child 1008 of Parent 40');
231+
INSERT INTO em_childtab VALUES (1009, 40, 'Child 1009 of Parent 40');
232+
INSERT INTO em_childtab VALUES (1010, 40, 'Child 1010 of Parent 40');
233+
INSERT INTO em_childtab VALUES (1011, 40, 'Child 1011 of Parent 40');
234+
INSERT INTO em_childtab VALUES (1012, 50, 'Child 1012 of Parent 50');
235+
INSERT INTO em_childtab VALUES (1013, 50, 'Child 1013 of Parent 50');
236+
INSERT INTO em_childtab VALUES (1014, 50, 'Child 1014 of Parent 50');
237+
INSERT INTO em_childtab VALUES (1015, 50, 'Child 1015 of Parent 50');
238+
239+
CREATE OR REPLACE PROCEDURE em_testproc (
240+
a_num IN NUMBER,
241+
a_outnum OUT NUMBER,
242+
a_outstr OUT VARCHAR2)
243+
AS
244+
BEGIN
245+
a_outnum := a_num * 2;
246+
FOR i IN 1..a_num LOOP
247+
a_outstr := a_outstr || 'X';
248+
END LOOP;
249+
END;
250+
/
251+
252+
COMMIT;

examples/demodrop.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -54,3 +54,11 @@ DROP PROCEDURE lob_in_out;
5454
DROP PROCEDURE lobs_in;
5555

5656
DROP PROCEDURE lobs_out;
57+
58+
DROP TABLE em_tab;
59+
60+
DROP TABLE em_childtab;
61+
62+
DROP TABLE em_parenttab;
63+
64+
DROP PROCEDURE em_testproc;

examples/em_batcherrors.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Copyright (c) 2018, 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+
* em_batcherrors.js
20+
*
21+
* DESCRIPTION
22+
* Array DML example showing batchErrors behavior. Note, despite the
23+
* autoCommit flag, no commit occurs because of data errors. However
24+
* valid rows are part of a transaction that can be committed if
25+
* desired.
26+
* Use demo.sql to create the required schema.
27+
*
28+
*****************************************************************************/
29+
30+
var async = require('async');
31+
var oracledb = require('oracledb');
32+
var dbConfig = require('./dbconfig.js');
33+
34+
var doconnect = function(cb) {
35+
oracledb.getConnection(dbConfig, cb);
36+
};
37+
38+
var dorelease = function(conn) {
39+
conn.close(function (err) {
40+
if (err)
41+
console.error(err.message);
42+
});
43+
};
44+
45+
var doinsert = function(conn, cb) {
46+
var sql = "INSERT INTO em_childtab VALUES (:1, :2, :3)";
47+
48+
var binds = [
49+
[1016, 10, "Child 2 of Parent A"],
50+
[1017, 10, "Child 3 of Parent A"],
51+
[1018, 20, "Child 4 of Parent B"],
52+
[1018, 20, "Child 4 of Parent B"], // duplicate key
53+
[1019, 30, "Child 3 of Parent C"],
54+
[1020, 40, "Child 4 of Parent D"],
55+
[1021, 75, "Child 1 of Parent F"], // parent does not exist
56+
[1022, 40, "Child 6 of Parent D"]
57+
];
58+
59+
var options = {
60+
autoCommit: true,
61+
batchErrors: true,
62+
dmlRowCounts: true,
63+
bindDefs: [
64+
{ type: oracledb.NUMBER },
65+
{ type: oracledb.NUMBER },
66+
{ type: oracledb.STRING, maxSize: 20 }
67+
]
68+
};
69+
70+
conn.executeMany(sql, binds, options, function (err, result) {
71+
if (err)
72+
return cb(err, conn);
73+
else {
74+
console.log("Result is:", result);
75+
return cb(null, conn);
76+
}
77+
});
78+
};
79+
80+
async.waterfall(
81+
[
82+
doconnect,
83+
doinsert
84+
],
85+
function (err, conn) {
86+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
87+
if (conn)
88+
dorelease(conn);
89+
});

examples/em_batcherrors_aa.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Copyright (c) 2018, 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+
* em_batcherrors_aa.js
20+
*
21+
* DESCRIPTION
22+
* Array DML example showing batchErrors behavior. Note, despite the
23+
* autoCommit flag, no commit occurs because of data errors. However
24+
* valid rows are part of a transaction that can be committed if
25+
* desired.
26+
* This example also uses Async/Await of Node 8.
27+
* Use demo.sql to create the required schema.
28+
*
29+
*****************************************************************************/
30+
31+
var oracledb = require('oracledb');
32+
var dbConfig = require('./dbconfig.js');
33+
34+
const sql = "INSERT INTO em_childtab VALUES (:1, :2, :3)";
35+
36+
const binds = [
37+
[1016, 10, "Child 2 of Parent A"],
38+
[1017, 10, "Child 3 of Parent A"],
39+
[1018, 20, "Child 4 of Parent B"],
40+
[1018, 20, "Child 4 of Parent B"], // duplicate key
41+
[1019, 30, "Child 3 of Parent C"],
42+
[1020, 40, "Child 4 of Parent D"],
43+
[1021, 75, "Child 1 of Parent F"], // parent does not exist
44+
[1022, 40, "Child 6 of Parent D"]
45+
];
46+
47+
const options = {
48+
autoCommit: true,
49+
batchErrors: true,
50+
dmlRowCounts: true,
51+
bindDefs: [
52+
{ type: oracledb.NUMBER },
53+
{ type: oracledb.NUMBER },
54+
{ type: oracledb.STRING, maxSize: 20 }
55+
]
56+
};
57+
58+
async function run() {
59+
let conn;
60+
let result;
61+
62+
try {
63+
conn = await oracledb.getConnection(
64+
{
65+
user : dbConfig.user,
66+
password : dbConfig.password,
67+
connectString : dbConfig.connectString
68+
});
69+
70+
result = await conn.executeMany(sql, binds, options);
71+
72+
console.log("Result is:", result);
73+
74+
} catch (err) {
75+
console.error(err);
76+
} finally {
77+
if (conn) {
78+
try {
79+
await conn.close();
80+
} catch (err) {
81+
console.error(err);
82+
}
83+
}
84+
}
85+
}
86+
87+
run();

examples/em_dmlreturn1.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* Copyright (c) 2018, 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+
* em_dmlreturn1.js
20+
*
21+
* DESCRIPTION
22+
* executeMany() example of DML RETURNING that returns single values
23+
* Use demo.sql to create the required schema.
24+
*
25+
*****************************************************************************/
26+
27+
var async = require('async');
28+
var oracledb = require('oracledb');
29+
var dbConfig = require('./dbconfig.js');
30+
31+
var doconnect = function(cb) {
32+
oracledb.getConnection(dbConfig, cb);
33+
};
34+
35+
var dorelease = function(conn) {
36+
conn.close(function (err) {
37+
if (err)
38+
console.error(err.message);
39+
});
40+
};
41+
42+
var dotruncate = function(conn, cb) {
43+
conn.execute("TRUNCATE TABLE em_tab", function (err) {
44+
return cb(err, conn);
45+
});
46+
};
47+
48+
var doinsert = function(conn, cb) {
49+
var sql = "INSERT INTO em_tab VALUES (:1, :2) RETURNING ROWID, id, val INTO :3, :4, :5";
50+
51+
var binds = [
52+
[1, "Test 1 (One)"],
53+
[2, "Test 2 (Two)"],
54+
[3, "Test 3 (Three)"],
55+
[4, null],
56+
[5, "Test 5 (Five)"]
57+
];
58+
59+
var options = {
60+
bindDefs: [
61+
{ type: oracledb.NUMBER },
62+
{ type: oracledb.STRING, maxSize: 20 },
63+
{ type: oracledb.STRING, maxSize: 18, dir: oracledb.BIND_OUT },
64+
{ type: oracledb.NUMBER, dir: oracledb.BIND_OUT },
65+
{ type: oracledb.STRING, maxSize: 25, dir: oracledb.BIND_OUT }
66+
]
67+
};
68+
69+
conn.executeMany(sql, binds, options, function (err, result) {
70+
if (err)
71+
return cb(err, conn);
72+
else {
73+
console.log("rowsAffected is:", result.rowsAffected);
74+
console.log("Out binds:");
75+
for (let i = 0; i < result.outBinds.length; i++) {
76+
console.log("-->", result.outBinds[i]);
77+
}
78+
return cb(null, conn);
79+
}
80+
});
81+
};
82+
83+
async.waterfall(
84+
[
85+
doconnect,
86+
dotruncate,
87+
doinsert
88+
],
89+
function (err, conn) {
90+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
91+
if (conn)
92+
dorelease(conn);
93+
});

0 commit comments

Comments
 (0)