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
+ * The node-oracledb test suite uses 'mocha', 'should' and 'async'.
19
+ * See LICENSE.md for relevant licenses.
20
+ *
21
+ * NAME
22
+ * 158. insertAll.js
23
+ *
24
+ * DESCRIPTION
25
+ * Test INSERT ALL statements. It originates from issue 780.
26
+ * https://github.com/oracle/node-oracledb/issues/780
27
+ *
28
+ *****************************************************************************/
29
+ 'use strict' ;
30
+
31
+ var oracledb = require ( 'oracledb' ) ;
32
+ var should = require ( 'should' ) ;
33
+ var async = require ( 'async' ) ;
34
+ var dbConfig = require ( './dbconfig.js' ) ;
35
+
36
+ describe ( '158. insertAll.js' , function ( ) {
37
+
38
+ var conn ;
39
+
40
+ before ( function ( done ) {
41
+ oracledb . getConnection (
42
+ dbConfig ,
43
+ function ( err , connection ) {
44
+ should . not . exist ( err ) ;
45
+ conn = connection ;
46
+ done ( ) ;
47
+ }
48
+ ) ;
49
+ } ) ;
50
+
51
+ after ( function ( done ) {
52
+ conn . close ( function ( err ) {
53
+ should . not . exist ( err ) ;
54
+ done ( ) ;
55
+ } ) ;
56
+ } ) ;
57
+
58
+ it ( '158.1 original case from the issue' , function ( done ) {
59
+
60
+ var dataLength = 35000 ;
61
+ var doCreate = function ( cb ) {
62
+ var proc = "BEGIN \n" +
63
+ " DECLARE \n" +
64
+ " e_table_missing EXCEPTION; \n" +
65
+ " PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" +
66
+ " BEGIN \n" +
67
+ " EXECUTE IMMEDIATE('DROP TABLE nodb_tab_insertall PURGE'); \n" +
68
+ " EXCEPTION \n" +
69
+ " WHEN e_table_missing \n" +
70
+ " THEN NULL; \n" +
71
+ " END; \n" +
72
+ " EXECUTE IMMEDIATE (' \n" +
73
+ " CREATE TABLE nodb_tab_insertall ( \n" +
74
+ " code NUMBER, \n" +
75
+ " val CLOB \n" +
76
+ " ) \n" +
77
+ " '); \n" +
78
+ "END; " ;
79
+ conn . execute (
80
+ proc ,
81
+ function ( err ) {
82
+ should . not . exist ( err ) ;
83
+ cb ( ) ;
84
+ }
85
+ ) ;
86
+ } ;
87
+
88
+ var doDrop = function ( cb ) {
89
+ var sql = "DROP TABLE nodb_tab_insertall PURGE" ;
90
+ conn . execute (
91
+ sql ,
92
+ function ( err ) {
93
+ should . not . exist ( err ) ;
94
+ cb ( ) ;
95
+ }
96
+ ) ;
97
+ } ;
98
+
99
+ var doInsert = function ( cb ) {
100
+ var myval = 'a' . repeat ( dataLength ) ;
101
+ var sql = "INSERT ALL INTO nodb_tab_insertall \n" +
102
+ " WITH nt AS (SELECT 1, :C FROM DUAL) \n" +
103
+ " SELECT * FROM nt" ;
104
+ conn . execute (
105
+ sql ,
106
+ { c : { val : myval , type : oracledb . CLOB } } ,
107
+ function ( err , result ) {
108
+ should . not . exist ( err ) ;
109
+ ( result . rowsAffected ) . should . be . exactly ( 1 ) ;
110
+ cb ( ) ;
111
+ }
112
+ ) ;
113
+ } ;
114
+
115
+ var doQuery = function ( cb ) {
116
+ var sql = "select dbms_lob.getlength(val) from nodb_tab_insertall" ;
117
+ conn . execute (
118
+ sql ,
119
+ function ( err , result ) {
120
+ should . not . exist ( err ) ;
121
+ var buf = result . rows [ 0 ] [ 0 ] ;
122
+ should . strictEqual ( buf , dataLength ) ;
123
+ cb ( ) ;
124
+ }
125
+ ) ;
126
+ } ;
127
+
128
+ async . series ( [
129
+ doCreate ,
130
+ doInsert ,
131
+ doQuery ,
132
+ doDrop
133
+ ] , done ) ;
134
+
135
+ } ) ; // 158.1
136
+
137
+ it ( '158.2 inserts into one table' , function ( done ) {
138
+ async . series ( [
139
+ makeTab1 ,
140
+ function dotest ( cb ) {
141
+ var sql = "INSERT ALL \n" +
142
+ " INTO nodb_tab_ia1 (id, content) VALUES (100, :a) \n" +
143
+ " INTO nodb_tab_ia1 (id, content) VALUES (200, :b) \n" +
144
+ " INTO nodb_tab_ia1 (id, content) VALUES (300, :c) \n" +
145
+ "SELECT * FROM DUAL" ;
146
+ conn . execute (
147
+ sql ,
148
+ [ 'Changjie' , 'Shelly' , 'Chris' ] ,
149
+ function ( err , result ) {
150
+ should . not . exist ( err ) ;
151
+ should . strictEqual ( result . rowsAffected , 3 ) ;
152
+ cb ( ) ;
153
+ }
154
+ ) ;
155
+ } ,
156
+ function doverify ( cb ) {
157
+ var sql = "select content from nodb_tab_ia1 order by id" ;
158
+ conn . execute (
159
+ sql ,
160
+ function ( err , result ) {
161
+ should . not . exist ( err ) ;
162
+ should . deepEqual (
163
+ result . rows ,
164
+ [ [ 'Changjie' ] , [ 'Shelly' ] , [ 'Chris' ] ]
165
+ ) ;
166
+ cb ( ) ;
167
+ }
168
+ ) ;
169
+ } ,
170
+ dropTab1
171
+ ] , done ) ;
172
+ } ) ; // 158.2
173
+
174
+ it ( '158.3 inserts into multiple tables' , function ( done ) {
175
+ async . series ( [
176
+ makeTab1 ,
177
+ makeTab2 ,
178
+ function dotest ( cb ) {
179
+ var sql = "INSERT ALL \n" +
180
+ " INTO nodb_tab_ia1 (id, content) VALUES (100, :a) \n" +
181
+ " INTO nodb_tab_ia1 (id, content) VALUES (200, :b) \n" +
182
+ " INTO nodb_tab_ia2 (id, content) VALUES (300, :c) \n" +
183
+ "SELECT * FROM DUAL" ;
184
+ conn . execute (
185
+ sql ,
186
+ [ 'Redwood city' , 'Sydney' , 'Shenzhen' ] ,
187
+ function ( err , result ) {
188
+ should . not . exist ( err ) ;
189
+ should . strictEqual ( result . rowsAffected , 3 ) ;
190
+ cb ( ) ;
191
+ }
192
+ ) ;
193
+ } ,
194
+ function doverify1 ( cb ) {
195
+ var sql = "select content from nodb_tab_ia1 order by id" ;
196
+ conn . execute (
197
+ sql ,
198
+ function ( err , result ) {
199
+ should . not . exist ( err ) ;
200
+ should . deepEqual (
201
+ result . rows ,
202
+ [ [ 'Redwood city' ] , [ 'Sydney' ] ]
203
+ ) ;
204
+ cb ( ) ;
205
+ }
206
+ ) ;
207
+ } ,
208
+ function doverify2 ( cb ) {
209
+ var sql = "select content from nodb_tab_ia2 order by id" ;
210
+ conn . execute (
211
+ sql ,
212
+ function ( err , result ) {
213
+ should . not . exist ( err ) ;
214
+ should . deepEqual (
215
+ result . rows ,
216
+ [ [ 'Shenzhen' ] ]
217
+ ) ;
218
+ cb ( ) ;
219
+ }
220
+ ) ;
221
+ } ,
222
+ dropTab1 ,
223
+ dropTab2
224
+ ] , done ) ;
225
+ } ) ; // 158.3
226
+
227
+ var makeTab1 = function ( cb ) {
228
+ var proc = "BEGIN \n" +
229
+ " DECLARE \n" +
230
+ " e_table_missing EXCEPTION; \n" +
231
+ " PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" +
232
+ " BEGIN \n" +
233
+ " EXECUTE IMMEDIATE('DROP TABLE nodb_tab_ia1 PURGE'); \n" +
234
+ " EXCEPTION \n" +
235
+ " WHEN e_table_missing \n" +
236
+ " THEN NULL; \n" +
237
+ " END; \n" +
238
+ " EXECUTE IMMEDIATE (' \n" +
239
+ " CREATE TABLE nodb_tab_ia1 ( \n" +
240
+ " id NUMBER, \n" +
241
+ " content VARCHAR2(100) \n" +
242
+ " ) \n" +
243
+ " '); \n" +
244
+ "END; " ;
245
+ conn . execute (
246
+ proc ,
247
+ function ( err ) {
248
+ should . not . exist ( err ) ;
249
+ cb ( ) ;
250
+ }
251
+ ) ;
252
+ } ; // makeTab1
253
+
254
+ var dropTab1 = function ( cb ) {
255
+ var sql = "DROP TABLE nodb_tab_ia1 PURGE" ;
256
+ conn . execute (
257
+ sql ,
258
+ function ( err ) {
259
+ should . not . exist ( err ) ;
260
+ cb ( ) ;
261
+ }
262
+ ) ;
263
+ } ;
264
+
265
+ var makeTab2 = function ( cb ) {
266
+ var proc = "BEGIN \n" +
267
+ " DECLARE \n" +
268
+ " e_table_missing EXCEPTION; \n" +
269
+ " PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" +
270
+ " BEGIN \n" +
271
+ " EXECUTE IMMEDIATE('DROP TABLE nodb_tab_ia2 PURGE'); \n" +
272
+ " EXCEPTION \n" +
273
+ " WHEN e_table_missing \n" +
274
+ " THEN NULL; \n" +
275
+ " END; \n" +
276
+ " EXECUTE IMMEDIATE (' \n" +
277
+ " CREATE TABLE nodb_tab_ia2 ( \n" +
278
+ " id NUMBER, \n" +
279
+ " content VARCHAR2(50) " +
280
+ " ) \n" +
281
+ " '); \n" +
282
+ "END; " ;
283
+ conn . execute (
284
+ proc ,
285
+ function ( err ) {
286
+ should . not . exist ( err ) ;
287
+ cb ( ) ;
288
+ }
289
+ ) ;
290
+ } ;
291
+
292
+ var dropTab2 = function ( cb ) {
293
+ var sql = "DROP TABLE nodb_tab_ia2 PURGE" ;
294
+ conn . execute (
295
+ sql ,
296
+ function ( err ) {
297
+ should . not . exist ( err ) ;
298
+ cb ( ) ;
299
+ }
300
+ ) ;
301
+ } ;
302
+
303
+ } ) ;
0 commit comments