Skip to content

Commit acdb0fe

Browse files
committed
Add DB Object examples to tests
1 parent 5edee35 commit acdb0fe

File tree

6 files changed

+400
-3
lines changed

6 files changed

+400
-3
lines changed

test/dbObject11.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* 210. dbObject11.js
2020
*
2121
* DESCRIPTION
22-
* DB Objects contain PL/SQL methods.
22+
* The attribute names of DB Objects contain special characters.
2323
*
2424
*****************************************************************************/
2525
'use strict';

test/dbObject12.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
* 211. dbObject12.js
20+
*
21+
* DESCRIPTION
22+
* examples/plsqlrecord.js
23+
*
24+
*****************************************************************************/
25+
'use strict';
26+
27+
const oracledb = require('oracledb');
28+
const should = require('should');
29+
const dbconfig = require('./dbconfig.js');
30+
31+
describe('211. dbObject12.js', () => {
32+
33+
let conn;
34+
35+
const PKG = 'NODB_REC_PKG';
36+
const TYPE = 'NODB_REC_TYP';
37+
38+
before(async () => {
39+
try {
40+
conn = await oracledb.getConnection(dbconfig);
41+
42+
let plsql =`
43+
CREATE OR REPLACE PACKAGE ${PKG} AS
44+
TYPE ${TYPE} IS RECORD (name VARCHAR2(40), pos NUMBER);
45+
PROCEDURE myproc (p_in IN ${TYPE}, p_out OUT ${TYPE});
46+
END ${PKG};
47+
`;
48+
await conn.execute(plsql);
49+
50+
plsql =`
51+
CREATE OR REPLACE PACKAGE BODY ${PKG} AS
52+
PROCEDURE myproc (p_in IN ${TYPE}, p_out OUT ${TYPE}) AS
53+
BEGIN
54+
p_out := p_in;
55+
p_out.pos := p_out.pos * 2;
56+
END;
57+
END ${PKG};
58+
`;
59+
await conn.execute(plsql);
60+
61+
} catch(err) {
62+
should.not.exist(err);
63+
}
64+
}); // before()
65+
66+
after(async () => {
67+
try {
68+
let sql = `DROP PACKAGE ${PKG}`;
69+
await conn.execute(sql);
70+
71+
await conn.close();
72+
} catch(err) {
73+
should.not.exist(err);
74+
}
75+
}); // after()
76+
77+
it('211.1 examples/plsqlrecord.js', async () => {
78+
try {
79+
const CALL = `CALL ${PKG}.myproc(:inbv, :outbv)`;
80+
const RecTypeClass = await conn.getDbObjectClass(`${PKG}.${TYPE}`);
81+
82+
// Using the constructor to create an object
83+
const obj1 = new RecTypeClass( { NAME: 'Ship', POS: 12 } );
84+
let binds = {
85+
inbv: obj1,
86+
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT }
87+
};
88+
const result1 = await conn.execute(CALL, binds);
89+
let out = result1.outBinds.outbv;
90+
should.strictEqual(out.NAME, obj1.NAME);
91+
should.strictEqual(out.POS, (obj1.POS * 2) );
92+
93+
// Binding the record values directly'
94+
const obj2 = { NAME: 'Plane', POS: 34 };
95+
binds = {
96+
inbv: { type: RecTypeClass, val: obj2 },
97+
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT }
98+
};
99+
const result2 = await conn.execute(CALL, binds);
100+
out = result2.outBinds.outbv;
101+
should.strictEqual(out.NAME, obj2.NAME);
102+
should.strictEqual(out.POS, (obj2.POS * 2) );
103+
104+
// Using the type name
105+
const obj3 = { NAME: 'Car', POS: 56 };
106+
binds = {
107+
inbv: { type: `${PKG}.${TYPE}`, val: obj3 },
108+
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT }
109+
};
110+
const result3 = await conn.execute(CALL, binds);
111+
out = result3.outBinds.outbv;
112+
should.strictEqual(out.NAME, obj3.NAME);
113+
should.strictEqual(out.POS, (obj3.POS * 2) );
114+
115+
// Batch exeuction with executeMany()
116+
const obj4 = [
117+
{ NAME: 'Train', POS: 78 },
118+
{ NAME: 'Bike', POS: 83 }
119+
];
120+
binds = [
121+
{ inbv: obj4[0] },
122+
{ inbv: obj4[1] }
123+
];
124+
let opts = {
125+
bindDefs: {
126+
inbv: { type: RecTypeClass },
127+
outbv: { type: RecTypeClass, dir: oracledb.BIND_OUT },
128+
}
129+
};
130+
const result4 = await conn.executeMany(CALL, binds, opts);
131+
for (let i = 0, out = result4.outBinds; i < binds.length; i++) {
132+
should.strictEqual(out[i].outbv.NAME, obj4[i].NAME);
133+
should.strictEqual(out[i].outbv.POS, (obj4[i].POS * 2) );
134+
}
135+
} catch (err) {
136+
should.not.exist(err);
137+
}
138+
}); // 211.1
139+
});

test/dbObject13.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
* 212. dbObject13.js
20+
*
21+
* DESCRIPTION
22+
* examples/plsqlvarrayrecord.js
23+
*
24+
*****************************************************************************/
25+
'use strict';
26+
27+
const oracledb = require('oracledb');
28+
const should = require('should');
29+
const dbconfig = require('./dbconfig.js');
30+
31+
describe('212. dbObject13.js', () => {
32+
33+
let conn;
34+
const PKG = 'NODB_NETBALL_PKG';
35+
36+
before(async () => {
37+
try {
38+
conn = await oracledb.getConnection(dbconfig);
39+
40+
let plsql =`
41+
CREATE OR REPLACE PACKAGE ${PKG} AS
42+
TYPE playerType IS RECORD (name VARCHAR2(40), position VARCHAR2(20), shirtnumber NUMBER);
43+
TYPE teamType IS VARRAY(10) OF playerType;
44+
PROCEDURE assignShirtNumbers (t_in IN teamType, t_out OUT teamType);
45+
END ${PKG};
46+
`;
47+
await conn.execute(plsql);
48+
49+
plsql =`
50+
CREATE OR REPLACE PACKAGE BODY ${PKG} AS
51+
PROCEDURE assignShirtNumbers (t_in IN teamType, t_out OUT teamType) AS
52+
p teamType := teamType();
53+
BEGIN
54+
FOR i in 1..t_in.COUNT LOOP
55+
p.EXTEND;
56+
p(i) := t_in(i);
57+
p(i).SHIRTNUMBER := i;
58+
END LOOP;
59+
t_out := p;
60+
END;
61+
62+
END ${PKG};
63+
`;
64+
await conn.execute(plsql);
65+
66+
} catch(err) {
67+
should.not.exist(err);
68+
}
69+
}); // before()
70+
71+
after(async () => {
72+
try {
73+
let sql = `DROP PACKAGE ${PKG}`;
74+
await conn.execute(sql);
75+
76+
await conn.close();
77+
} catch(err) {
78+
should.not.exist(err);
79+
}
80+
}); // after()
81+
82+
it('212.1 examples/plsqlvarrayrecord.js', async () => {
83+
try {
84+
const CALL = `CALL ${PKG}.assignShirtNumbers(:inbv, :outbv)`;
85+
86+
const players = [
87+
{ NAME: 'Jay', POSITION: 'GOAL ATTACK', SHIRTNUMBER: 1 },
88+
{ NAME: 'Leslie', POSITION: 'CENTRE', SHIRTNUMBER: 2 },
89+
{ NAME: 'Chris', POSITION: 'WING DEFENCE', SHIRTNUMBER: 3 }
90+
];
91+
const binds = {
92+
inbv:
93+
{
94+
type: `${PKG}.TEAMTYPE`,
95+
val: players
96+
},
97+
outbv:
98+
{
99+
type: `${PKG}.TEAMTYPE`,
100+
dir: oracledb.BIND_OUT
101+
}
102+
};
103+
const result = await conn.execute(CALL, binds);
104+
105+
for (let i = 0, out = result.outBinds.outbv; i < players.length; i++) {
106+
should.strictEqual(out[i].NAME, players[i].NAME);
107+
should.strictEqual(out[i].POSITION, players[i].POSITION);
108+
should.strictEqual(out[i].SHIRTNUMBER, players[i].SHIRTNUMBER);
109+
}
110+
} catch (err) {
111+
should.not.exist(err);
112+
}
113+
}); // 212.1
114+
115+
});

test/dbObject14.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
* 213. dbObject14.js
20+
*
21+
* DESCRIPTION
22+
* examples/plsqlvarray.js
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('213. dbObject14.js', () => {
33+
let conn;
34+
35+
const TABLE = 'NODB_TAB_SPORTS';
36+
const PLAYER_T = 'NODB_TYP_PLAYERTYPE';
37+
const TEAM_T = 'NODB_TYP_TEAMTYPE';
38+
39+
before(async () => {
40+
try {
41+
conn = await oracledb.getConnection(dbconfig);
42+
43+
let plsql =`
44+
CREATE OR REPLACE TYPE ${PLAYER_T} AS OBJECT (
45+
shirtnumber NUMBER,
46+
name VARCHAR2(20)
47+
);
48+
`;
49+
await conn.execute(plsql);
50+
51+
plsql =`
52+
CREATE OR REPLACE TYPE ${TEAM_T} AS VARRAY(10) OF ${PLAYER_T};
53+
`;
54+
await conn.execute(plsql);
55+
56+
let sql =`
57+
CREATE TABLE ${TABLE} (sportname VARCHAR2(20), team ${TEAM_T})
58+
`;
59+
plsql = testsUtil.sqlCreateTable(TABLE, sql);
60+
await conn.execute(plsql);
61+
62+
} catch(err) {
63+
should.not.exist(err);
64+
}
65+
}); // before()
66+
67+
after(async () => {
68+
try {
69+
let sql = `DROP TABLE ${TABLE} PURGE`;
70+
await conn.execute(sql);
71+
72+
sql = `DROP TYPE ${TEAM_T} FORCE`;
73+
await conn.execute(sql);
74+
75+
sql = `DROP TYPE ${PLAYER_T} FORCE`;
76+
await conn.execute(sql);
77+
78+
await conn.close();
79+
} catch(err) {
80+
should.not.exist(err);
81+
}
82+
}); // after()
83+
84+
it('213.1 examples/selectvarray.js', async () => {
85+
86+
try {
87+
const TeamTypeClass = await conn.getDbObjectClass(TEAM_T);
88+
89+
// Insert with explicit constructor
90+
const hockeyPlayers = [
91+
{SHIRTNUMBER: 11, NAME: 'Elizabeth'},
92+
{SHIRTNUMBER: 22, NAME: 'Frank'},
93+
];
94+
const hockeyTeam = new TeamTypeClass(hockeyPlayers);
95+
96+
let sql = `INSERT INTO ${TABLE} VALUES (:sn, :t)`;
97+
let binds = { sn: "Hockey", t: hockeyTeam };
98+
const result1 = await conn.execute(sql, binds);
99+
should.strictEqual(result1.rowsAffected, 1);
100+
101+
// Insert with direct binding
102+
const badmintonPlayers = [
103+
{ SHIRTNUMBER: 10, NAME: 'Alison' },
104+
{ SHIRTNUMBER: 20, NAME: 'Bob' },
105+
{ SHIRTNUMBER: 30, NAME: 'Charlie' },
106+
{ SHIRTNUMBER: 40, NAME: 'Doug' }
107+
];
108+
binds = { sn: "Badminton", t: { type: TeamTypeClass, val: badmintonPlayers } };
109+
const result2 = await conn.execute(sql, binds);
110+
should.strictEqual(result2.rowsAffected, 1);
111+
112+
// Query the data back
113+
sql = `SELECT * FROM ${TABLE}`;
114+
const result3 = await conn.execute(sql, [], { outFormat:oracledb.OUT_FORMAT_OBJECT });
115+
should.strictEqual(result3.rows[0].SPORTNAME, 'Hockey');
116+
should.strictEqual(result3.rows[1].SPORTNAME, 'Badminton');
117+
118+
for (let i = 0; i < result3.rows[0].TEAM.length; i++) {
119+
should.strictEqual(result3.rows[0].TEAM[i].SHIRTNUMBER, hockeyPlayers[i].SHIRTNUMBER);
120+
should.strictEqual(result3.rows[0].TEAM[i].NAME, hockeyPlayers[i].NAME);
121+
}
122+
123+
for (let i = 0; i < result3.rows[1].TEAM.length; i++) {
124+
should.strictEqual(result3.rows[1].TEAM[i].SHIRTNUMBER, badmintonPlayers[i].SHIRTNUMBER);
125+
should.strictEqual(result3.rows[1].TEAM[i].NAME, badmintonPlayers[i].NAME);
126+
}
127+
} catch (err) {
128+
should.not.exist(err);
129+
}
130+
}); // 213.1
131+
});

0 commit comments

Comments
 (0)