1+ /* Copyright (c) 2020, 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+ * 224. booleanBind.js
20+ *
21+ * DESCRIPTION
22+ * Test PL/SQL boolean parameters to be bound and to be included in PL/SQL
23+ * records/collections. Based on
24+ * https://github.com/oracle/node-oracledb/pull/1190
25+ *
26+ *****************************************************************************/
27+ 'use strict' ;
28+
29+ const oracledb = require ( 'oracledb' ) ;
30+ const should = require ( 'should' ) ;
31+ const assert = require ( 'assert' ) ;
32+ const dbconfig = require ( './dbconfig.js' ) ;
33+
34+ describe ( '224. booleanBind.js' , ( ) => {
35+
36+ let conn ;
37+ const pkgName = 'NODB_PKG_TEST_BOOLEANS' ;
38+ before ( async ( ) => {
39+ let plsqlPkg = `
40+ create or replace package ${ pkgName } as
41+
42+ type udt_BooleanList is table of boolean index by binary_integer;
43+
44+ type udt_DemoRecord is record (
45+ NumberValue number,
46+ StringValue varchar2(30),
47+ DateValue date,
48+ BooleanValue boolean
49+ );
50+
51+ function GetStringRep (
52+ a_Value boolean
53+ ) return varchar2;
54+
55+ function IsLessThan10 (
56+ a_Value number
57+ ) return boolean;
58+
59+ function TestInArrays (
60+ a_Value udt_BooleanList
61+ ) return number;
62+
63+ procedure TestOutArrays (
64+ a_NumElements number,
65+ a_Value out nocopy udt_BooleanList
66+ );
67+
68+ procedure DemoRecordsInOut (
69+ a_Value in out nocopy udt_DemoRecord
70+ );
71+
72+ end;
73+ ` ;
74+
75+ let plsqlPkgBody = `
76+ create or replace package body ${ pkgName } as
77+
78+ function GetStringRep (
79+ a_Value boolean
80+ ) return varchar2 is
81+ begin
82+ if a_Value is null then
83+ return 'NULL';
84+ elsif a_Value then
85+ return 'TRUE';
86+ end if;
87+ return 'FALSE';
88+ end;
89+
90+ function IsLessThan10 (
91+ a_Value number
92+ ) return boolean is
93+ begin
94+ return a_Value < 10;
95+ end;
96+
97+ function TestInArrays (
98+ a_Value udt_BooleanList
99+ ) return number is
100+ t_Result pls_integer;
101+ begin
102+ t_Result := 0;
103+ for i in 0..a_Value.count - 1 loop
104+ if a_Value(i) then
105+ t_Result := t_Result + 1;
106+ end if;
107+ end loop;
108+ return t_Result;
109+ end;
110+
111+ procedure TestOutArrays (
112+ a_NumElements number,
113+ a_Value out nocopy udt_BooleanList
114+ ) is
115+ begin
116+ for i in 1..a_NumElements loop
117+ a_Value(i) := (mod(i, 2) = 1);
118+ end loop;
119+ end;
120+
121+ procedure DemoRecordsInOut (
122+ a_Value in out nocopy udt_DemoRecord
123+ ) is
124+ begin
125+ a_Value.NumberValue := a_Value.NumberValue * 2;
126+ a_Value.StringValue := a_Value.StringValue || ' (Modified)';
127+ a_Value.DateValue := a_Value.DateValue + 5;
128+ a_Value.BooleanValue := not a_Value.BooleanValue;
129+ end;
130+
131+ end;
132+ ` ;
133+
134+ try {
135+ conn = await oracledb . getConnection ( dbconfig ) ;
136+ await conn . execute ( plsqlPkg ) ;
137+ await conn . execute ( plsqlPkgBody ) ;
138+ } catch ( err ) {
139+ should . not . exist ( err ) ;
140+ }
141+ } ) ; // before()
142+
143+ after ( async ( ) => {
144+ try {
145+ let plsql = `drop package ${ pkgName } ` ;
146+ await conn . execute ( plsql ) ;
147+ await conn . close ( ) ;
148+ } catch ( err ) {
149+ should . not . exist ( err ) ;
150+ }
151+ } ) ; // after()
152+
153+ it ( '224.1 IN bind boolean value' , async ( ) => {
154+
155+ let binds = {
156+ inval : true ,
157+ outval : { dir : oracledb . BIND_OUT , type : oracledb . STRING , maxSize : 10 }
158+ } ;
159+ let sql = `begin :outval := ${ pkgName } .GetStringRep(:inval); end;` ;
160+
161+ try {
162+ let result = await conn . execute ( sql , binds ) ;
163+ should . strictEqual ( 'TRUE' , result . outBinds . outval ) ;
164+ } catch ( error ) {
165+ should . not . exist ( error ) ;
166+ }
167+ } ) ; // 224.1
168+
169+ it ( '224.2 IN bind value "false"' , async ( ) => {
170+ let binds = {
171+ inval : false ,
172+ outval : { dir : oracledb . BIND_OUT , type : oracledb . STRING , maxSize : 10 }
173+ } ;
174+ let sql = `begin :outval := ${ pkgName } .GetStringRep(:inval); end;` ;
175+
176+ try {
177+ let result = await conn . execute ( sql , binds ) ;
178+ should . strictEqual ( 'FALSE' , result . outBinds . outval ) ;
179+ } catch ( error ) {
180+ should . not . exist ( error ) ;
181+ }
182+ } ) ; // 224.2
183+
184+ it ( '224.3 IN bind value "null"' , async ( ) => {
185+ let binds = {
186+ inval : { type : oracledb . DB_TYPE_BOOLEAN , val : null } ,
187+ outval : { dir : oracledb . BIND_OUT , type : oracledb . STRING , maxSize : 10 }
188+ } ;
189+ let sql = `begin :outval := ${ pkgName } .GetStringRep(:inval); end;` ;
190+
191+ try {
192+ let result = await conn . execute ( sql , binds ) ;
193+ should . strictEqual ( 'NULL' , result . outBinds . outval ) ;
194+ } catch ( error ) {
195+ should . not . exist ( error ) ;
196+ }
197+ } ) ; // 224.3
198+
199+ it ( '224.4 Negative - IN bind value type mismatch' , async ( ) => {
200+ let binds = {
201+ inval : { type : oracledb . DB_TYPE_BOOLEAN , val : 123 } ,
202+ outval : { dir : oracledb . BIND_OUT , type : oracledb . STRING , maxSize : 10 }
203+ } ;
204+ let sql = `begin :outval := ${ pkgName } .GetStringRep(:inval); end;` ;
205+
206+ try {
207+ await assert . rejects (
208+ async ( ) => {
209+ await conn . execute ( sql , binds ) ;
210+ } ,
211+ / N J S - 0 1 1 /
212+ ) ;
213+ } catch ( error ) {
214+ should . not . exist ( error ) ;
215+ }
216+ } ) ;
217+
218+ it ( '224.5 OUT bind value "false"' , async ( ) => {
219+ let binds = {
220+ inval : 12 ,
221+ outval : { dir : oracledb . BIND_OUT , type : oracledb . DB_TYPE_BOOLEAN }
222+ } ;
223+ let sql = `begin :outval := ${ pkgName } .IsLessThan10(:inval); end;` ;
224+
225+ try {
226+ let result = await conn . execute ( sql , binds ) ;
227+ should . strictEqual ( false , result . outBinds . outval ) ;
228+ } catch ( error ) {
229+ should . not . exist ( error ) ;
230+ }
231+ } ) ; // 224.5
232+
233+ it ( '224.6 OUT bind value "true"' , async ( ) => {
234+ let binds = {
235+ inval : 9 ,
236+ outval : { dir : oracledb . BIND_OUT , type : oracledb . DB_TYPE_BOOLEAN }
237+ } ;
238+ let sql = `begin :outval := ${ pkgName } .IsLessThan10(:inval); end;` ;
239+
240+ try {
241+ let result = await conn . execute ( sql , binds ) ;
242+ should . strictEqual ( true , result . outBinds . outval ) ;
243+ } catch ( error ) {
244+ should . not . exist ( error ) ;
245+ }
246+ } ) ; // 224.6
247+
248+ it ( '224.7 IN bind array with boolean data' , async ( ) => {
249+ try {
250+ const cls = await conn . getDbObjectClass ( `${ pkgName } .UDT_BOOLEANLIST` ) ;
251+ const arr = new cls ( [ true , false , true , true , false , true , false , true ] ) ;
252+ const binds = {
253+ inval : arr ,
254+ outval : { dir : oracledb . BIND_OUT , type : oracledb . NUMBER }
255+ } ;
256+ const sql = `begin :outval := ${ pkgName } .TestInArrays(:inval); end;` ;
257+ const result = await conn . execute ( sql , binds ) ;
258+ should . strictEqual ( 5 , result . outBinds . outval ) ;
259+ } catch ( error ) {
260+ should . not . exist ( error ) ;
261+ }
262+ } ) ; // 224.7
263+
264+ it ( '224.8 OUT bind array with boolean data' , async ( ) => {
265+ try {
266+ const cls = await conn . getDbObjectClass ( `${ pkgName } .UDT_BOOLEANLIST` ) ;
267+ const arr = new cls ( [ true , false , true , true , false , true , false , true ] ) ;
268+ const binds = {
269+ inval : arr ,
270+ outval : { dir : oracledb . BIND_OUT , type : oracledb . NUMBER }
271+ } ;
272+ const sql = `begin :outval := ${ pkgName } .TestInArrays(:inval); end;` ;
273+ const result = await conn . execute ( sql , binds ) ;
274+ should . strictEqual ( 5 , result . outBinds . outval ) ;
275+ } catch ( error ) {
276+ should . not . exist ( error ) ;
277+ }
278+ } ) ; // 224.8
279+
280+ it ( '224.9 INOUT bind record with boolean data' , async ( ) => {
281+ try {
282+ const cls = await conn . getDbObjectClass ( `${ pkgName } .UDT_DEMORECORD` ) ;
283+ const obj = new cls ( ) ;
284+ obj . NUMBERVALUE = 6 ;
285+ obj . STRINGVALUE = "A string" ;
286+ obj . DATEVALUE = new Date ( ) ;
287+ obj . BOOLEANVALUE = false ;
288+ const binds = [
289+ { val : obj , type : cls , dir : oracledb . BIND_INOUT }
290+ ] ;
291+ let sql = `begin ${ pkgName } .DemoRecordsInOut(:1); end;` ;
292+ const result = await conn . execute ( sql , binds ) ;
293+ should . strictEqual ( 12 , result . outBinds [ 0 ] . NUMBERVALUE ) ;
294+ should . strictEqual ( 'A string (Modified)' , result . outBinds [ 0 ] . STRINGVALUE ) ;
295+ ( result . outBinds [ 0 ] . DATEVALUE ) . should . be . a . Date ( ) ;
296+ should . strictEqual ( true , result . outBinds [ 0 ] . BOOLEANVALUE ) ;
297+ } catch ( error ) {
298+ should . not . exist ( error ) ;
299+ }
300+ } ) ; // 224.9
301+ } ) ;
0 commit comments