|
| 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(); |
0 commit comments