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+ * 216. dbObject17.js
20+ *
21+ * DESCRIPTION
22+ * Test DB Object collection with columns TIMESTAMP, TIMESTAMP WITH TIME ZONE
23+ * and TIMESTAMP WITH LOCAL TIME ZONE.
24+ *
25+ *****************************************************************************/
26+ 'use strict' ;
27+
28+ const oracledb = require ( 'oracledb' ) ;
29+ const should = require ( 'should' ) ;
30+ const dbconfig = require ( './dbconfig.js' ) ;
31+ const testsUtil = require ( './testsUtil.js' ) ;
32+
33+ describe ( '216. dbObject17.js' , ( ) => {
34+
35+ let conn ;
36+
37+ const TABLE = 'NODB_TAB_SPORTS' ;
38+ const PLAYER_T = 'NODB_TYP_PLAYER_17' ;
39+ const TEAM_T = 'NODB_TYP_TEAM_17' ;
40+
41+ before ( async ( ) => {
42+ try {
43+ conn = await oracledb . getConnection ( dbconfig ) ;
44+
45+ let plsql = `
46+ CREATE OR REPLACE TYPE ${ PLAYER_T } AS OBJECT (
47+ shirtnumber NUMBER,
48+ name VARCHAR2(20),
49+ ts TIMESTAMP,
50+ tsz TIMESTAMP WITH TIME ZONE,
51+ ltz TIMESTAMP WITH LOCAL TIME ZONE
52+ );
53+ ` ;
54+ await conn . execute ( plsql ) ;
55+
56+ plsql = `
57+ CREATE OR REPLACE TYPE ${ TEAM_T } AS VARRAY(10) OF ${ PLAYER_T } ;
58+ ` ;
59+ await conn . execute ( plsql ) ;
60+
61+ let sql = `
62+ CREATE TABLE ${ TABLE } (sportname VARCHAR2(20), team ${ TEAM_T } )
63+ ` ;
64+ plsql = testsUtil . sqlCreateTable ( TABLE , sql ) ;
65+ await conn . execute ( plsql ) ;
66+
67+ } catch ( err ) {
68+ should . not . exist ( err ) ;
69+ }
70+ } ) ; // before()
71+
72+ after ( async ( ) => {
73+ try {
74+
75+ let sql = `DROP TABLE ${ TABLE } PURGE` ;
76+ await conn . execute ( sql ) ;
77+
78+ sql = `DROP TYPE ${ TEAM_T } FORCE` ;
79+ await conn . execute ( sql ) ;
80+
81+ sql = `DROP TYPE ${ PLAYER_T } FORCE` ;
82+ await conn . execute ( sql ) ;
83+
84+ await conn . close ( ) ;
85+ } catch ( err ) {
86+ should . not . exist ( err ) ;
87+ }
88+ } ) ; // after()
89+
90+ it ( '216.1 VARRAY Collection. Object columns contain TS, TSZ and LTZ' , async ( ) => {
91+ try {
92+ const TeamTypeClass = await conn . getDbObjectClass ( TEAM_T ) ;
93+
94+ // Insert with explicit constructor
95+ const FrisbeePlayers = [
96+ {
97+ SHIRTNUMBER : 11 ,
98+ NAME : 'Elizabeth' ,
99+ TS : new Date ( 1986 , 8 , 18 , 12 , 14 , 27 , 0 ) ,
100+ TSZ : new Date ( 1989 , 3 , 4 , 10 , 27 , 16 , 201 ) ,
101+ LTZ : new Date ( 1999 , 5 , 4 , 11 , 23 , 5 , 45 )
102+ } ,
103+ {
104+ SHIRTNUMBER : 22 ,
105+ NAME : 'Frank' ,
106+ TS : new Date ( 1987 , 8 , 18 , 12 , 14 , 27 , 0 ) ,
107+ TSZ : new Date ( 1990 , 3 , 4 , 10 , 27 , 16 , 201 ) ,
108+ LTZ : new Date ( 2000 , 5 , 4 , 11 , 23 , 5 , 45 )
109+ }
110+ ] ;
111+ const FrisbeeTeam = new TeamTypeClass ( FrisbeePlayers ) ;
112+
113+ let sql = `INSERT INTO ${ TABLE } VALUES (:sn, :t)` ;
114+ let binds = { sn : "Frisbee" , t : FrisbeeTeam } ;
115+ const result1 = await conn . execute ( sql , binds ) ;
116+ should . strictEqual ( result1 . rowsAffected , 1 ) ;
117+
118+ sql = `SELECT * FROM ${ TABLE } ` ;
119+ const result = await conn . execute ( sql , [ ] , { outFormat :oracledb . OUT_FORMAT_OBJECT } ) ;
120+
121+ should . strictEqual ( result . rows [ 0 ] . SPORTNAME , 'Frisbee' ) ;
122+
123+ for ( let i = 0 ; i < result . rows [ 0 ] . TEAM . length ; i ++ ) {
124+ should . strictEqual ( result . rows [ 0 ] . TEAM [ i ] . SHIRTNUMBER , FrisbeePlayers [ i ] . SHIRTNUMBER ) ;
125+ should . strictEqual ( result . rows [ 0 ] . TEAM [ i ] . NAME , FrisbeePlayers [ i ] . NAME ) ;
126+ // should.strictEqual(result.rows[0].TEAM[i].TS.getTime(), FrisbeePlayers[i].TS.getTime());
127+ should . strictEqual ( result . rows [ 0 ] . TEAM [ i ] . TSZ . getTime ( ) , FrisbeePlayers [ i ] . TSZ . getTime ( ) ) ;
128+ should . strictEqual ( result . rows [ 0 ] . TEAM [ i ] . LTZ . getTime ( ) , FrisbeePlayers [ i ] . LTZ . getTime ( ) ) ;
129+ }
130+ } catch ( err ) {
131+ should . not . exist ( err ) ;
132+ }
133+ } ) ; // 216.1
134+ } ) ;
0 commit comments