1- /* Copyright (c) 2015, 2019 , Oracle and/or its affiliates. All rights reserved. */
1+ /* Copyright (c) 2015, 2020 , Oracle and/or its affiliates. All rights reserved. */
22
33/******************************************************************************
44 *
1919 * selectjsonblob.js
2020 *
2121 * DESCRIPTION
22- * Executes sample insert and query statements using a JSON column with BLOB storage .
22+ * Shows how to use a BLOB as a JSON column store .
2323 *
24- * Requires Oracle Database 12.1.0.2, which has extensive JSON datatype support.
25- * See https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=ADJSN
24+ * Note: with Oracle Database 21c using the new JSON type is recommended
25+ * instead, see selectjson.js
2626 *
27- * This example requires node-oracledb 1.13 or later.
27+ * Requires Oracle Database 12.1.0.2 or later.
28+ * See https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=ADJSN
2829 *
2930 * This example uses Node 8's async/await syntax.
3031 *
3334const oracledb = require ( 'oracledb' ) ;
3435const dbConfig = require ( './dbconfig.js' ) ;
3536
37+ oracledb . extendedMetaData = true ;
38+
3639async function run ( ) {
3740
3841 let connection ;
@@ -42,58 +45,81 @@ async function run() {
4245 connection = await oracledb . getConnection ( dbConfig ) ;
4346
4447 if ( connection . oracleServerVersion < 1201000200 ) {
45- throw new Error ( 'This example only works with Oracle Database 12.1.0.2 or greater ' ) ;
48+ throw new Error ( 'Using JSON requires Oracle Database 12.1.0.2 or later ' ) ;
4649 }
4750
48- const stmts = [
49- `DROP TABLE no_purchaseorder_b` ,
51+ console . log ( '1. Creating Table' ) ;
5052
51- `CREATE TABLE no_purchaseorder_b (po_document BLOB CHECK (po_document IS JSON)) LOB (po_document) STORE AS (CACHE)`
52- ] ;
53+ try {
54+ await connection . execute ( `DROP TABLE no_purchaseorder_b` ) ;
55+ } catch ( e ) {
56+ if ( e . errorNum != 942 )
57+ console . error ( e ) ;
58+ }
5359
54- for ( const s of stmts ) {
55- try {
56- await connection . execute ( s ) ;
57- } catch ( e ) {
58- if ( e . errorNum != 942 )
59- console . error ( e ) ;
60- }
60+ await connection . execute (
61+ `CREATE TABLE no_purchaseorder_b
62+ (po_document BLOB CHECK (po_document IS JSON)) LOB (po_document) STORE AS (CACHE)` ) ;
63+
64+ console . log ( '2. Inserting Data' ) ;
65+
66+ const inssql = `INSERT INTO no_purchaseorder_b (po_document) VALUES (:bv)` ;
67+ const data = { "userId" : 1 , "userName" : "Anna" , "location" : "Australia" } ;
68+
69+ if ( oracledb . oracleClientVersion >= 2100000000 && connection . oracleServerVersion >= 2100000000 ) {
70+ // Take advantage of direct binding of JavaScript objects
71+ await connection . execute ( inssql , { bv : { val : data , type : oracledb . DB_TYPE_JSON } } ) ;
72+ } else {
73+ // Insert the data as a JSON string
74+ const s = JSON . stringify ( data ) ;
75+ const b = Buffer . from ( s , 'utf8' ) ;
76+ await connection . execute ( inssql , { bv : { val : b } } ) ;
6177 }
6278
63- let result ;
79+ console . log ( '3. Selecting JSON stored in a BLOB column' ) ;
6480
65- console . log ( 'Inserting Data' ) ;
66- const data = { "userId" : 2 , "userName" : "Bob" , "location" : "USA" } ;
67- const s = JSON . stringify ( data ) ;
68- const b = Buffer . from ( s , 'utf8' ) ;
69- await connection . execute (
70- `INSERT INTO no_purchaseorder_b (po_document) VALUES (:lobbv)` ,
71- { lobbv : b }
72- ) ;
81+ let result , j ;
7382
74- console . log ( 'Selecting JSON stored in a BLOB column:' ) ;
7583 result = await connection . execute (
7684 `SELECT po_document
7785 FROM no_purchaseorder_b
78- WHERE JSON_EXISTS (po_document, '$.location')` ,
79- [ ] ,
80- { fetchInfo : { "PO_DOCUMENT" : { type : oracledb . BUFFER } } } // Fetch as a Buffer instead of a Stream
86+ WHERE JSON_EXISTS (po_document, '$.location')
87+ OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY`
8188 ) ;
82- if ( result . rows . length === 0 )
83- throw new Error ( 'No results' ) ;
84- console . log ( result . rows [ 0 ] [ 0 ] . toString ( 'utf8' ) ) ;
89+ const d = await result . rows [ 0 ] [ 0 ] . getData ( ) ;
90+ j = await JSON . parse ( d ) ;
91+ console . log ( 'Query results: ' , j ) ;
92+
93+ console . log ( '4. Using JSON_VALUE to extract a value from a JSON column' ) ;
8594
86- console . log ( 'Selecting a JSON value using "dotted" notation:' ) ;
87- if ( connection . oracleServerVersion < 1202000000 ) {
88- throw new Error ( 'This example only works with Oracle Database 12.2 or greater' ) ;
89- }
9095 result = await connection . execute (
91- `SELECT pob.po_document.location
92- FROM no_purchaseorder_b pob`
96+ `SELECT JSON_VALUE(po_document, '$.location')
97+ FROM no_purchaseorder_b
98+ OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY`
9399 ) ;
94- if ( result . rows . length === 0 )
95- throw new Error ( 'No results' ) ;
96- console . log ( result . rows [ 0 ] [ 0 ] ) ;
100+ console . log ( 'Query results: ' , result . rows [ 0 ] [ 0 ] ) ; // just show first record
101+
102+ if ( connection . oracleServerVersion >= 1202000000 ) {
103+
104+ console . log ( '5. Using dot-notation to extract a value from a JSON column' ) ;
105+
106+ result = await connection . execute (
107+ `SELECT po.po_document.location
108+ FROM no_purchaseorder_b po
109+ OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY`
110+ ) ;
111+ console . log ( 'Query results: ' , result . rows [ 0 ] [ 0 ] ) ;
112+
113+ console . log ( '6. Using JSON_OBJECT to extract relational data as JSON' ) ;
114+
115+ result = await connection . execute (
116+ `SELECT JSON_OBJECT('key' IS d.dummy) dummy
117+ FROM dual d`
118+ ) ;
119+ for ( let row of result . rows ) {
120+ console . log ( 'Query results: ' , row [ 0 ] ) ;
121+ }
122+ }
97123
98124 } catch ( err ) {
99125 console . error ( err ) ;
0 commit comments