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+ * 215. dbObject16.js
20+ *
21+ * DESCRIPTION
22+ * Test DB Object collection methods.
23+ *
24+ *****************************************************************************/
25+ 'use strict' ;
26+
27+ const oracledb = require ( 'oracledb' ) ;
28+ const should = require ( 'should' ) ;
29+ const dbconfig = require ( './dbconfig.js' ) ;
30+ const testsUtil = require ( './testsUtil.js' ) ;
31+
32+ describe ( '215. dbObject16.js' , ( ) => {
33+
34+ let conn ;
35+
36+ const TABLE = 'NODB_TAB_SPORTS' ;
37+ const PLAYER_T = 'NODB_TYP_PLAYERTYPE' ;
38+ const TEAM_T = 'NODB_TYP_TEAMTYPE' ;
39+
40+ before ( async ( ) => {
41+ try {
42+ conn = await oracledb . getConnection ( dbconfig ) ;
43+
44+ let plsql = `
45+ CREATE OR REPLACE TYPE ${ PLAYER_T } AS OBJECT (
46+ shirtnumber NUMBER,
47+ name VARCHAR2(20),
48+ draft DATE
49+ );
50+ ` ;
51+ await conn . execute ( plsql ) ;
52+
53+ plsql = `
54+ CREATE OR REPLACE TYPE ${ TEAM_T } AS VARRAY(10) OF ${ PLAYER_T } ;
55+ ` ;
56+ await conn . execute ( plsql ) ;
57+
58+ let sql = `
59+ CREATE TABLE ${ TABLE } (sportname VARCHAR2(20), team ${ TEAM_T } )
60+ ` ;
61+ plsql = testsUtil . sqlCreateTable ( TABLE , sql ) ;
62+ await conn . execute ( plsql ) ;
63+
64+ } catch ( err ) {
65+ should . not . exist ( err ) ;
66+ }
67+ } ) ; // before()
68+
69+ after ( async ( ) => {
70+ try {
71+
72+ let sql = `DROP TABLE ${ TABLE } PURGE` ;
73+ await conn . execute ( sql ) ;
74+
75+ sql = `DROP TYPE ${ TEAM_T } FORCE` ;
76+ await conn . execute ( sql ) ;
77+
78+ sql = `DROP TYPE ${ PLAYER_T } FORCE` ;
79+ await conn . execute ( sql ) ;
80+
81+ await conn . close ( ) ;
82+ } catch ( err ) {
83+ should . not . exist ( err ) ;
84+ }
85+ } ) ; // after()
86+
87+ it ( '215.1 Collection of DATE, named Oracle type binds' , async ( ) => {
88+ try {
89+ const TeamTypeClass = await conn . getDbObjectClass ( TEAM_T ) ;
90+
91+ // Insert with explicit constructor
92+ const FrisbeePlayers = [
93+ { SHIRTNUMBER : 11 , NAME : 'Elizabeth' , DRAFT : new Date ( 2008 , 11 , 17 ) } ,
94+ { SHIRTNUMBER : 22 , NAME : 'Frank' , DRAFT : new Date ( 2011 , 2 , 4 ) } ,
95+ { SHIRTNUMBER : 30 , NAME : 'Charlie' , DRAFT : new Date ( 2010 , 9 , 1 ) }
96+ ] ;
97+ const FrisbeeTeam = new TeamTypeClass ( FrisbeePlayers ) ;
98+
99+ let sql = `INSERT INTO ${ TABLE } VALUES (:sn, :t)` ;
100+ let binds = { sn : "Frisbee" , t : FrisbeeTeam } ;
101+ const result1 = await conn . execute ( sql , binds ) ;
102+ should . strictEqual ( result1 . rowsAffected , 1 ) ;
103+
104+ // Insert with direct binding
105+ const hockeyPlayers = [
106+ { SHIRTNUMBER : 11 , NAME : 'Elizabeth' , DRAFT : new Date ( 1997 , 3 , 25 ) } ,
107+ { SHIRTNUMBER : 22 , NAME : 'Frank' , DRAFT : new Date ( 1999 , 7 , 8 ) } ,
108+ ] ;
109+ binds = { sn : "Hockey" , t : { type : TeamTypeClass , val : hockeyPlayers } } ;
110+ const result2 = await conn . execute ( sql , binds ) ;
111+ should . strictEqual ( result2 . rowsAffected , 1 ) ;
112+
113+ // Use the Oracle type name
114+ const TennisPlayers = [
115+ { SHIRTNUMBER : 21 , NAME : 'John' , DRAFT : new Date ( 1995 , 2 , 5 ) } ,
116+ { SHIRTNUMBER : 23 , NAME : 'Michael' , DRAFT : new Date ( 2000 , 6 , 6 ) }
117+ ] ;
118+ binds = { sn : "Tennis" , t : { type : TEAM_T , val : TennisPlayers } } ;
119+ const result3 = await conn . execute ( sql , binds ) ;
120+ should . strictEqual ( result3 . rowsAffected , 1 ) ;
121+
122+ sql = `SELECT * FROM ${ TABLE } ` ;
123+ const result4 = await conn . execute ( sql , [ ] , { outFormat :oracledb . OUT_FORMAT_OBJECT } ) ;
124+ should . strictEqual ( result4 . rows [ 0 ] . SPORTNAME , 'Frisbee' ) ;
125+ should . strictEqual ( result4 . rows [ 1 ] . SPORTNAME , 'Hockey' ) ;
126+
127+ for ( let i = 0 ; i < result4 . rows [ 0 ] . TEAM . length ; i ++ ) {
128+ should . strictEqual ( result4 . rows [ 0 ] . TEAM [ i ] . SHIRTNUMBER , FrisbeePlayers [ i ] . SHIRTNUMBER ) ;
129+ should . strictEqual ( result4 . rows [ 0 ] . TEAM [ i ] . NAME , FrisbeePlayers [ i ] . NAME ) ;
130+ should . strictEqual ( result4 . rows [ 0 ] . TEAM [ i ] . DRAFT . getTime ( ) , FrisbeePlayers [ i ] . DRAFT . getTime ( ) ) ;
131+ }
132+
133+ for ( let i = 0 ; i < result4 . rows [ 1 ] . TEAM . length ; i ++ ) {
134+ should . strictEqual ( result4 . rows [ 1 ] . TEAM [ i ] . SHIRTNUMBER , hockeyPlayers [ i ] . SHIRTNUMBER ) ;
135+ should . strictEqual ( result4 . rows [ 1 ] . TEAM [ i ] . NAME , hockeyPlayers [ i ] . NAME ) ;
136+ should . strictEqual ( result4 . rows [ 1 ] . TEAM [ i ] . DRAFT . getTime ( ) , hockeyPlayers [ i ] . DRAFT . getTime ( ) ) ;
137+ }
138+
139+ for ( let i = 0 ; i < result4 . rows [ 2 ] . TEAM . length ; i ++ ) {
140+ should . strictEqual ( result4 . rows [ 2 ] . TEAM [ i ] . SHIRTNUMBER , TennisPlayers [ i ] . SHIRTNUMBER ) ;
141+ should . strictEqual ( result4 . rows [ 2 ] . TEAM [ i ] . NAME , TennisPlayers [ i ] . NAME ) ;
142+ should . strictEqual ( result4 . rows [ 2 ] . TEAM [ i ] . DRAFT . getTime ( ) , TennisPlayers [ i ] . DRAFT . getTime ( ) ) ;
143+ }
144+
145+ } catch ( err ) {
146+ should . not . exist ( err ) ;
147+ }
148+
149+ } ) ; // 215.1
150+
151+ } ) ;
0 commit comments