Skip to content

Commit bb55e55

Browse files
committed
Convert JSON examples to use async waterfall
1 parent 83b8fc7 commit bb55e55

File tree

3 files changed

+134
-112
lines changed

3 files changed

+134
-112
lines changed

examples/demo.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ DROP TABLE j_purchaseorder;
111111
-- Note if your applications always insert valid JSON, you may delete
112112
-- the IS JSON check to remove its additional validation overhead.
113113
CREATE TABLE j_purchaseorder (po_document VARCHAR2(4000) CHECK (po_document IS JSON));
114-
INSERT INTO j_purchaseorder (po_document) VALUES ('{"userId":3,"userName":"Alison"}');
114+
INSERT INTO j_purchaseorder (po_document) VALUES ('{"userId":3,"userName":"Alison","location":"Australia"}');
115115
COMMIT;
116116

117117
-- For selectjsonclob.js example of JSON datatype. Requires Oracle Database 12.1.0.2
@@ -123,7 +123,7 @@ DROP TABLE j_purchaseorder_c;
123123
-- USER_JSON_COLUMNS. EMPTY_CLOB() is currently needed by
124124
-- node-oracledb for inserting CLOB data.
125125
CREATE TABLE j_purchaseorder_c (po_document CLOB CHECK (po_document IS JSON or length(po_document) = 0));
126-
INSERT INTO j_purchaseorder_c (po_document) VALUES ('{"userId":4,"userName":"Changjie"}');
126+
INSERT INTO j_purchaseorder_c (po_document) VALUES ('{"userId":4,"userName":"Changjie","location":"China"}');
127127
COMMIT;
128128

129129
-- For DML RETURNING aka RETURNING INTO examples

examples/selectjson.js

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. */
1+
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
22

33
/******************************************************************************
44
*
@@ -30,62 +30,85 @@
3030
*
3131
*****************************************************************************/
3232

33+
var async = require('async');
3334
var oracledb = require('oracledb');
3435
var dbConfig = require('./dbconfig.js');
3536

36-
oracledb.getConnection(
37-
{
38-
user : dbConfig.user,
39-
password : dbConfig.password,
40-
connectString : dbConfig.connectString
41-
},
42-
function(err, connection)
43-
{
44-
if (err) {
45-
console.error(err.message);
46-
return;
47-
}
37+
var doconnect = function(cb) {
38+
oracledb.getConnection(dbConfig, cb);
39+
};
4840

49-
if (connection.oracleServerVersion < 1201000200) {
50-
console.error('This example only works with Oracle Database 12.1.0.2 or greater');
51-
process.exit(1);
52-
}
41+
var dorelease = function(conn) {
42+
conn.release(function (err) {
43+
if (err)
44+
console.error(err.message);
45+
});
46+
};
5347

54-
var data = { "userId": 1, "userName": "Chris", "location": "Australia" };
55-
var s = JSON.stringify(data);
48+
var checkver = function (conn, cb) {
49+
if (conn.oracleServerVersion < 1201000200) {
50+
return cb(new Error('This example only works with Oracle Database 12.1.0.2 or greater'), conn);
51+
} else {
52+
return cb(null, conn);
53+
}
54+
}
5655

57-
connection.execute(
58-
"INSERT INTO j_purchaseorder (po_document) VALUES (:bv)",
59-
[s], // bind the JSON string for inserting into the JSON column.
60-
{ autoCommit: true },
61-
function (err) {
62-
if (err) {
63-
console.error(err.message);
64-
doRelease(connection);
65-
return;
66-
}
56+
var doinsert = function (conn, cb) {
57+
var data = { "userId": 1, "userName": "Chris", "location": "Australia" };
58+
var s = JSON.stringify(data);
59+
conn.execute(
60+
"INSERT INTO j_purchaseorder (po_document) VALUES (:bv)",
61+
[s], // bind the JSON string for inserting into the JSON column.
62+
{ autoCommit: true },
63+
function (err) {
64+
if (err) {
65+
return cb(err, conn);
66+
} else {
6767
console.log("Data inserted successfully.");
68-
connection.execute(
69-
"SELECT po_document FROM j_purchaseorder WHERE JSON_EXISTS (po_document, '$.location')",
70-
function(err, result)
71-
{
72-
if (err) {
73-
console.error(err.message);
74-
} else {
75-
var js = JSON.parse(result.rows[0][0]); // just show first record
76-
console.log('Query results: ', js);
77-
}
78-
doRelease(connection);
79-
});
80-
});
81-
});
68+
return cb(null, conn);
69+
}
70+
});
71+
}
72+
73+
var dojsonquery = function (conn, cb) {
74+
conn.execute(
75+
"SELECT po_document FROM j_purchaseorder WHERE JSON_EXISTS (po_document, '$.location')",
76+
function(err, result)
77+
{
78+
if (err) {
79+
return cb(err, conn);
80+
} else {
81+
var js = JSON.parse(result.rows[0][0]); // just show first record
82+
console.log('Query results: ', js);
83+
return cb(null, conn);
84+
}
85+
});
86+
}
8287

83-
function doRelease(connection)
84-
{
85-
connection.release(
86-
function(err) {
88+
var dorelationalquery = function (conn, cb) {
89+
conn.execute(
90+
"SELECT JSON_VALUE(po_document, '$.location') FROM j_purchaseorder",
91+
function(err, result)
92+
{
8793
if (err) {
88-
console.error(err.message);
94+
return cb(err, conn);
95+
} else {
96+
console.log('Query results: ', result.rows[0][0]); // just show first record
97+
return cb(null, conn);
8998
}
9099
});
91100
}
101+
102+
async.waterfall(
103+
[
104+
doconnect,
105+
checkver,
106+
doinsert,
107+
dojsonquery,
108+
dorelationalquery
109+
],
110+
function (err, conn) {
111+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
112+
if (conn)
113+
dorelease(conn);
114+
});

examples/selectjsonclob.js

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,111 +27,90 @@
2727
*
2828
*****************************************************************************/
2929

30+
var async = require('async');
3031
var oracledb = require('oracledb');
3132
var dbConfig = require('./dbconfig.js');
3233
var stream = require('stream');
3334

34-
oracledb.getConnection(
35-
{
36-
user : dbConfig.user,
37-
password : dbConfig.password,
38-
connectString : dbConfig.connectString
39-
},
40-
function(err, connection)
41-
{
42-
if (err) { console.error(err.message); return; }
35+
var doconnect = function(cb) {
36+
oracledb.getConnection(dbConfig, cb);
37+
};
4338

44-
if (connection.oracleServerVersion < 1201000200) {
45-
console.error('This example only works with Oracle Database 12.1.0.2 or greater');
46-
process.exit(1);
47-
}
48-
49-
doInsert(
50-
connection,
51-
JSON.stringify({ "userId": 2, "userName": "Bob", "location": "USA" }),
52-
function(err)
53-
{
54-
if (err) {
55-
console.error(err.message);
56-
doRelease(connection);
57-
return;
58-
}
59-
60-
doQuery(
61-
connection,
62-
function(err, result)
63-
{
64-
if (err)
65-
console.error(err.message);
66-
else
67-
console.log('Query results: ', result);
68-
doRelease(connection);
69-
});
70-
});
39+
var dorelease = function(conn) {
40+
conn.release(function (err) {
41+
if (err)
42+
console.error(err.message);
7143
});
44+
};
7245

73-
function doInsert(connection, data, cb)
74-
{
75-
connection.execute(
46+
var checkver = function (conn, cb) {
47+
if (conn.oracleServerVersion < 1201000200) {
48+
return cb(new Error('This example only works with Oracle Database 12.1.0.2 or greater'), conn);
49+
} else {
50+
return cb(null, conn);
51+
}
52+
}
53+
54+
var doinsert = function (conn, cb) {
55+
var data = { "userId": 2, "userName": "Bob", "location": "USA" };
56+
var s = JSON.stringify(data);
57+
conn.execute(
7658
"INSERT INTO j_purchaseorder_c (po_document) VALUES (EMPTY_CLOB()) RETURNING po_document INTO :lobbv",
7759
{ lobbv: {type: oracledb.CLOB, dir: oracledb.BIND_OUT} },
7860
{ autoCommit: false }, // a transaction needs to span the INSERT and pipe()
7961
function(err, result)
8062
{
81-
if (err) { return cb(err); }
63+
if (err) { return cb(err, conn); }
8264
if (result.rowsAffected != 1 || result.outBinds.lobbv.length != 1) {
83-
return cb(new Error('Error getting a LOB locator'));
65+
return cb(new Error('Error getting a LOB locator'), conn);
8466
}
85-
8667
var lob = result.outBinds.lobbv[0];
8768
lob.on(
8869
'error',
8970
function(err) {
90-
return cb(err);
71+
return cb(err, conn);
9172
});
92-
9373
lob.on(
9474
'finish',
9575
function()
9676
{
97-
connection.commit(
77+
conn.commit(
9878
function(err)
9979
{
10080
if (err)
101-
return cb(err);
81+
return cb(err, conn);
10282
else {
10383
console.log("Data inserted successfully.");
104-
return cb();
84+
return cb(null, conn);
10585
}
10686
});
10787
});
10888

10989
var inStream = new stream.Readable();
11090
inStream._read = function noop() {};
111-
inStream.push(data);
91+
inStream.push(s); // Data to insert
11292
inStream.push(null);
11393

11494
inStream.on(
11595
'error',
11696
function(err) {
117-
return cb(err);
97+
return cb(err, conn);
11898
});
11999
inStream.pipe(lob);
120100
});
121101
}
122102

123-
function doQuery(connection, cb)
124-
{
125-
connection.execute(
103+
var dojsonquery = function (conn, cb) {
104+
conn.execute(
126105
"SELECT po_document FROM j_purchaseorder_c WHERE JSON_EXISTS (po_document, '$.location')",
127106
function(err, result)
128107
{
129-
if (err) { return cb(err); }
130-
if (result.rows.length === 0) { return cb(new Error('No results')); }
108+
if (err) { return cb(err, conn); }
109+
if (result.rows.length === 0) { return cb(new Error('No results'), conn); }
131110

132111
var clob = '';
133112
var lob = result.rows[0][0]; // just show first record
134-
if (lob === null) { return cb(new Error('CLOB was NULL')); }
113+
if (lob === null) { return cb(new Error('CLOB was NULL'), conn); }
135114
lob.setEncoding('utf8'); // set the encoding so we get a 'string' not a 'buffer'
136115
lob.on('data',
137116
function(chunk)
@@ -141,22 +120,42 @@ function doQuery(connection, cb)
141120
lob.on('close',
142121
function()
143122
{
144-
return cb(null, JSON.parse(clob));
123+
var js = JSON.parse(clob);
124+
console.log('Query results: ', js);
125+
return cb(null, conn);
145126
});
146127
lob.on('error',
147128
function(err)
148129
{
149-
return cb(err);
130+
return cb(err, conn);
150131
});
151132
});
152133
}
153134

154-
function doRelease(connection)
155-
{
156-
connection.release(
157-
function(err) {
135+
var dorelationalquery = function (conn, cb) {
136+
conn.execute(
137+
"SELECT JSON_VALUE(po_document, '$.location') FROM j_purchaseorder_c",
138+
function(err, result)
139+
{
158140
if (err) {
159-
console.error(err.message);
141+
return cb(err, conn);
142+
} else {
143+
console.log('Query results: ', result.rows[0][0]); // just show first record
144+
return cb(null, conn);
160145
}
161146
});
162147
}
148+
149+
async.waterfall(
150+
[
151+
doconnect,
152+
checkver,
153+
doinsert,
154+
dojsonquery,
155+
dorelationalquery
156+
],
157+
function (err, conn) {
158+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
159+
if (conn)
160+
dorelease(conn);
161+
});

0 commit comments

Comments
 (0)