Skip to content

Commit e0ea62c

Browse files
committed
Add example and test updates
1 parent e77bc32 commit e0ea62c

File tree

4 files changed

+140
-5
lines changed

4 files changed

+140
-5
lines changed

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ File Name | Description
103103
[`lobstream2.js`](lobstream2.js) | Shows using Stream data events to fetch a CLOB
104104
[`lowercasecolumns.js`](lowercasecolumns.js) | Shows how a type handler can convert column names to lower case
105105
[`metadata.js`](metadata.js) | Shows the metadata available after executing SELECT statements
106+
[`nesteddbobject.js](nesteddbobject.js) | Shows binding of nested Oracle Database Object types
106107
[`ociConfigProvider.js`](ociConfigProvider.js) | Show how to connect to Oracle Database using OCI Config Provider
107108
[`ociConfigProviderPool.js`](ociConfigProviderPool.js) | Show how to connect with custom pool settings to Oracle Database using OCI Config Provider
108109
[`plsqlarray.js`](plsqlarray.js) | Examples of binding PL/SQL "INDEX BY" tables

examples/nesteddbobject.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* Copyright (c) 2025, Oracle and/or its affiliates. */
2+
3+
/******************************************************************************
4+
*
5+
* This software is dual-licensed to you under the Universal Permissive License
6+
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
7+
* 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
8+
* either license.
9+
*
10+
* If you elect to accept the software under the Apache License, Version 2.0,
11+
* the following applies:
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* https://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*
25+
* NAME
26+
* nesteddbobject.js
27+
*
28+
* DESCRIPTION
29+
* Shows binding of nested DbObject types
30+
*
31+
*****************************************************************************/
32+
33+
'use strict';
34+
35+
Error.stackTraceLimit = 50;
36+
37+
const oracledb = require('oracledb');
38+
const dbConfig = require('./dbconfig.js');
39+
40+
// This example runs in both node-oracledb Thin and Thick modes.
41+
//
42+
// Optionally run in node-oracledb Thick mode
43+
if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') {
44+
45+
// Thick mode requires Oracle Client or Oracle Instant Client libraries.
46+
// On Windows and macOS Intel you can specify the directory containing the
47+
// libraries at runtime or before Node.js starts. On other platforms (where
48+
// Oracle libraries are available) the system library search path must always
49+
// include the Oracle library path before Node.js starts. If the search path
50+
// is not correct, you will get a DPI-1047 error. See the node-oracledb
51+
// installation documentation.
52+
let clientOpts = {};
53+
// On Windows and macOS Intel platforms, set the environment
54+
// variable NODE_ORACLEDB_CLIENT_LIB_DIR to the Oracle Client library path
55+
if (process.platform === 'win32' || process.platform === 'darwin') {
56+
clientOpts = { libDir: process.env.NODE_ORACLEDB_CLIENT_LIB_DIR };
57+
}
58+
oracledb.initOracleClient(clientOpts); // enable node-oracledb Thick mode
59+
}
60+
61+
console.log(oracledb.thin ? 'Running in thin mode' : 'Running in thick mode');
62+
63+
async function run() {
64+
65+
const conn = await oracledb.getConnection(dbConfig);
66+
67+
const stmts = [
68+
`drop table my_shapes purge`,
69+
`drop type shape_type`,
70+
`drop type point_type`
71+
];
72+
for (const s of stmts) {
73+
try {
74+
await conn.execute(s);
75+
} catch (e) {
76+
if (e.errorNum != 942 && e.errorNum != 4043)
77+
throw (e);
78+
}
79+
}
80+
81+
// CREATE TYPES
82+
let result = await conn.execute(
83+
`CREATE TYPE point_type AS OBJECT (
84+
x NUMBER,
85+
y NUMBER
86+
)`
87+
);
88+
conn.commit();
89+
90+
result = await conn.execute(
91+
`CREATE TYPE shape_type AS OBJECT (
92+
name VARCHAR2(50),
93+
point point_type
94+
)`
95+
);
96+
conn.commit();
97+
98+
// CREATE TABLE
99+
result = await conn.execute(
100+
`CREATE TABLE my_shapes(id NUMBER, shape SHAPE_TYPE)`
101+
);
102+
conn.commit();
103+
104+
// Define the shape type and values
105+
const shapeType = await conn.getDbObjectClass('SHAPE_TYPE');
106+
const pointType = await conn.getDbObjectClass('POINT_TYPE');
107+
// The attributes of the DbObject type must always be specified in upper
108+
// case
109+
const point = new pointType({ X: 10, Y: 20 });
110+
const shape = new shapeType({ NAME: 'My Shape', POINT: point });
111+
112+
// Insert the value using an IN BIND
113+
result = await conn.execute(
114+
`INSERT INTO my_shapes (id, shape) VALUES (:id, :shape)`,
115+
{ id: 1, shape: shape }
116+
);
117+
conn.commit();
118+
119+
console.log('Rows inserted:', result.rowsAffected);
120+
121+
// Select the value from the table
122+
result = await conn.execute(
123+
`SELECT shape FROM my_shapes WHERE id = :id`,
124+
{ id: 1 }
125+
);
126+
127+
// Print the value
128+
console.log('The shape object is:');
129+
console.log(JSON.stringify(result.rows[0][0]));
130+
131+
await conn.close();
132+
}
133+
134+
run();

test/binding.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, 2024, Oracle and/or its affiliates. */
1+
/* Copyright (c) 2015, 2025, Oracle and/or its affiliates. */
22

33
/******************************************************************************
44
*
@@ -551,7 +551,7 @@ describe('4. binding.js', function() {
551551

552552
describe('4.6 PL/SQL block with empty outBinds', function() {
553553

554-
it('4.6.1 ', async function() {
554+
it('4.6.1 Empty result', async function() {
555555

556556
const sql = "begin execute immediate 'drop table does_not_exist purge'; "
557557
+ "exception when others then "
@@ -566,7 +566,7 @@ describe('4. binding.js', function() {
566566
});
567567

568568
// Test cases involving JSON value as input
569-
describe ('4.7 Value as JSON named/unamed test cases', function() {
569+
describe ('4.7 Value as JSON named/unnamed test cases', function() {
570570
let connection;
571571

572572
before(async function() {

test/list.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ Overview of node-oracledb functional tests
196196
4.5.1 DML default bind
197197
4.5.2 negative DML invalid bind direction
198198
4.6 PL/SQL block with empty outBinds
199-
4.6.1
200-
4.7 Value as JSON named/unamed test cases
199+
4.6.1 Empty result
200+
4.7 Value as JSON named/unnamed test cases
201201
4.7.1 valid case when numeric values are passed as it is
202202
4.7.2 Valid values when one of the value is passed as JSON
203203
4.7.3 Valid test case when one of the value is passed as JSON

0 commit comments

Comments
 (0)