1
+ /* Copyright (c) 2019, 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
+ * 228. lastRowid.js
20
+ *
21
+ * DESCRIPTION
22
+ * Test getting rowid of last updated row for DML statements.
23
+ *
24
+ *****************************************************************************/
25
+ 'use strict' ;
26
+
27
+ const oracledb = require ( 'oracledb' ) ;
28
+ const should = require ( 'should' ) ;
29
+ const assert = require ( 'assert' ) ;
30
+ const dbconfig = require ( './dbconfig.js' ) ;
31
+ const testsUtil = require ( './testsUtil.js' ) ;
32
+
33
+ describe ( '228. lastRowid.js' , function ( ) {
34
+
35
+ let conn ;
36
+ const TABLE = 'nodb_lastrowid' ;
37
+
38
+ before ( 'get connection and create table' , async ( ) => {
39
+ try {
40
+ conn = await oracledb . getConnection ( dbconfig ) ;
41
+ let sql =
42
+ `create table ${ TABLE } (
43
+ id number(9) not null,
44
+ value varchar2(100) not null
45
+ )` ;
46
+ let plsql = testsUtil . sqlCreateTable ( TABLE , sql ) ;
47
+ await conn . execute ( plsql ) ;
48
+ } catch ( err ) {
49
+ should . not . exist ( err ) ;
50
+ }
51
+ } ) ;
52
+
53
+ after ( async ( ) => {
54
+ try {
55
+ let sql = `drop table ${ TABLE } purge` ;
56
+ await conn . execute ( sql ) ;
57
+ await conn . close ( ) ;
58
+ } catch ( err ) {
59
+ should . not . exist ( err ) ;
60
+ }
61
+ } ) ;
62
+
63
+ it ( '228.1 examples' , async ( ) => {
64
+ const row1 = [ 1 , "First" ] ;
65
+ const row2 = [ 2 , "Second" ] ;
66
+
67
+ try {
68
+ // insert some rows and retain the rowid of each
69
+ let sql = `insert into ${ TABLE } values (:1, :2)` ;
70
+ const result1 = await conn . execute ( sql , row1 ) ;
71
+ should . exist ( result1 . lastRowid ) ;
72
+ should . strictEqual ( result1 . rowsAffected , 1 ) ;
73
+ const result2 = await conn . execute ( sql , row2 ) ;
74
+ should . exist ( result2 . lastRowid ) ;
75
+ should . strictEqual ( result2 . rowsAffected , 1 ) ;
76
+ const rowid2 = result2 . lastRowid ;
77
+
78
+ // the row can be fetched with the rowid that was retained
79
+ sql = `select * from ${ TABLE } where rowid = :1` ;
80
+ let result = await conn . execute ( sql , [ result1 . lastRowid ] ) ;
81
+ should . deepEqual ( result . rows [ 0 ] , row1 ) ;
82
+ result = await conn . execute ( sql , [ result2 . lastRowid ] ) ;
83
+ should . deepEqual ( result . rows [ 0 ] , row2 ) ;
84
+
85
+ // updating multiple rows only returns the rowid of the last updated row
86
+ sql = `update ${ TABLE } set value = value || ' (Modified)'` ;
87
+ result = await conn . execute ( sql ) ;
88
+ should . strictEqual ( result . lastRowid , rowid2 ) ;
89
+
90
+ // deleting multiple rows only returns the rowid of the last deleted row
91
+ sql = `delete from ${ TABLE } ` ;
92
+ result = await conn . execute ( sql ) ;
93
+ should . strictEqual ( result . lastRowid , rowid2 ) ;
94
+
95
+ // deleting no rows results in an undefined value
96
+ result = await conn . execute ( sql ) ;
97
+ should . not . exist ( result . lastRowid ) ;
98
+ } catch ( err ) {
99
+ should . not . exist ( err ) ;
100
+ }
101
+ } ) ; // 228.1
102
+
103
+ it ( '228.2 MERGE statement' , async ( ) => {
104
+ const row1 = [ 11 , "Eleventh" ] ;
105
+ const sqlMerge = `
106
+ merge into ${ TABLE } x
107
+ using (select :id as tempId, :value as tempValue from dual) y
108
+ on (x.id = y.tempId)
109
+ when matched then
110
+ update set x.value = y.tempValue
111
+ when not matched then
112
+ insert (x.id, x.value)
113
+ values (y.tempId, y.tempValue)
114
+ ` ;
115
+ try {
116
+ const result1 = await conn . execute ( sqlMerge , row1 , { autoCommit : true } ) ;
117
+ should . exist ( result1 . lastRowid ) ;
118
+ should . strictEqual ( result1 . rowsAffected , 1 ) ;
119
+ const rowID = result1 . lastRowid ;
120
+
121
+ // check it out
122
+ let sql = `select * from ${ TABLE } where rowid = :1` ;
123
+ let result2 = await conn . execute ( sql , [ rowID ] ) ;
124
+ should . deepEqual ( result2 . rows [ 0 ] , row1 ) ;
125
+
126
+ } catch ( err ) {
127
+ should . not . exist ( err ) ;
128
+ }
129
+ } ) ; // 228.2
130
+
131
+ it ( '228.3 Negative - not applicable to executeMany()' , async ( ) => {
132
+ const rows = [
133
+ { id : 21 , value : "Twenty-first" } ,
134
+ { id : 22 , value : "Twenty-second" }
135
+ ] ;
136
+ const sqlMerge = `
137
+ merge into ${ TABLE } x
138
+ using (select :id as tempId, :value as tempValue from dual) y
139
+ on (x.id = y.tempId)
140
+ when matched then
141
+ update set x.value = y.tempValue
142
+ when not matched then
143
+ insert (x.id, x.value)
144
+ values (y.tempId, y.tempValue)
145
+ ` ;
146
+ const options = {
147
+ autoCommit : true ,
148
+ bindDefs : {
149
+ id : { type : oracledb . NUMBER } ,
150
+ value : { type : oracledb . STRING , maxSize : 2000 }
151
+ }
152
+ } ;
153
+
154
+ try {
155
+ const result1 = await conn . executeMany ( sqlMerge , rows , options ) ;
156
+ should . not . exist ( result1 . lastRowid ) ;
157
+ should . strictEqual ( result1 . rowsAffected , 2 ) ;
158
+
159
+ let sql = `select * from ${ TABLE } where id >= :1` ;
160
+ let result2 = await conn . execute (
161
+ sql ,
162
+ [ rows [ 0 ] . id ] ,
163
+ { outFormat : oracledb . OUT_FORMAT_OBJECT }
164
+ ) ;
165
+ should . strictEqual ( result2 . rows [ 0 ] . ID , rows [ 0 ] . id ) ;
166
+ should . strictEqual ( result2 . rows [ 0 ] . VALUE , rows [ 0 ] . value ) ;
167
+ should . strictEqual ( result2 . rows [ 1 ] . ID , rows [ 1 ] . id ) ;
168
+ should . strictEqual ( result2 . rows [ 1 ] . VALUE , rows [ 1 ] . value ) ;
169
+ } catch ( err ) {
170
+ should . not . exist ( err ) ;
171
+ }
172
+ } ) ;
173
+ } ) ;
0 commit comments