Skip to content

Commit 00cb1f9

Browse files
committed
Use async waterfall to avoid nesting. Fix comment
1 parent a0a77d8 commit 00cb1f9

File tree

3 files changed

+297
-193
lines changed

3 files changed

+297
-193
lines changed

examples/insert1.js

Lines changed: 105 additions & 57 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
*
@@ -23,69 +23,117 @@
2323
*
2424
*****************************************************************************/
2525

26+
var async = require('async');
2627
var oracledb = require('oracledb');
2728
var dbConfig = require('./dbconfig.js');
2829

29-
oracledb.getConnection(
30-
{
31-
user : dbConfig.user,
32-
password : dbConfig.password,
33-
connectString : dbConfig.connectString
34-
},
35-
function(err, connection)
36-
{
37-
if (err) {
30+
var doconnect = function(cb) {
31+
oracledb.getConnection(
32+
{
33+
user : dbConfig.user,
34+
password : dbConfig.password,
35+
connectString : dbConfig.connectString
36+
},
37+
cb);
38+
};
39+
40+
var dorelease = function(conn) {
41+
conn.release(function (err) {
42+
if (err)
3843
console.error(err.message);
39-
return;
40-
}
41-
connection.execute(
42-
"CREATE TABLE test (id NUMBER, name VARCHAR2(20))",
43-
function(err)
44-
{
45-
if (err) {
46-
console.error(err.message);
47-
return;
48-
}
49-
console.log("Table created");
44+
});
45+
};
5046

51-
connection.execute(
52-
"INSERT INTO test VALUES (:id, :nm)",
53-
[1, 'Chris'], // Bind values
54-
function(err, result)
55-
{
56-
if (err) { console.error(err.message); return; }
57-
console.log("Rows inserted: " + result.rowsAffected); // 1
47+
var dodrop = function (conn, cb) {
48+
conn.execute(
49+
"BEGIN "
50+
+ " EXECUTE IMMEDIATE 'DROP TABLE test'; "
51+
+ " EXCEPTION WHEN OTHERS THEN "
52+
+ " IF SQLCODE <> -942 THEN "
53+
+ " RAISE; "
54+
+ " END IF; "
55+
+ "END;",
56+
function(err, result)
57+
{
58+
if (err) {
59+
return cb(err, conn);
60+
} else {
61+
console.log("Table dropped");
62+
return cb(null, conn);
63+
}
64+
});
65+
};
66+
67+
var docreate = function (conn, cb) {
68+
conn.execute(
69+
"CREATE TABLE test (id NUMBER, name VARCHAR2(20))",
70+
function(err, result)
71+
{
72+
if (err) {
73+
return cb(err, conn);
74+
} else {
75+
console.log("Table created");
76+
return cb(null, conn);
77+
}
78+
});
79+
};
5880

59-
connection.execute(
60-
"INSERT INTO test VALUES (:id, :nm)",
61-
[2, 'Alison'], // Bind values
62-
function(err, result)
63-
{
64-
if (err) { console.error(err.message); return; }
65-
console.log("Rows inserted: " + result.rowsAffected); // 1
81+
var doinsert1 = function (conn, cb) {
82+
conn.execute(
83+
"INSERT INTO test VALUES (:id, :nm)",
84+
[1, 'Chris'], // Bind values
85+
function(err, result)
86+
{
87+
if (err) {
88+
return cb(err, conn);
89+
} else {
90+
console.log("Rows inserted: " + result.rowsAffected); // 1
91+
return cb(null, conn);
92+
}
93+
});
94+
};
6695

67-
connection.execute(
68-
"UPDATE test SET name = 'Bambi'",
69-
function(err, result)
70-
{
71-
if (err) { console.error(err.message); return; }
72-
console.log("Rows updated: " + result.rowsAffected); // 2
96+
var doinsert2 = function (conn, cb) {
97+
conn.execute(
98+
"INSERT INTO test VALUES (:id, :nm)",
99+
[2, 'Alison'], // Bind values
100+
function(err, result)
101+
{
102+
if (err) {
103+
return cb(err, conn);
104+
} else {
105+
console.log("Rows inserted: " + result.rowsAffected); // 1
106+
return cb(null, conn);
107+
}
108+
});
109+
};
73110

74-
connection.execute(
75-
"DROP TABLE test",
76-
function(err)
77-
{
78-
if (err) { console.error(err.message); return; }
79-
console.log("Table dropped");
111+
var doupdate = function (conn, cb) {
112+
conn.execute(
113+
"UPDATE test SET name = 'Bambi'",
114+
function(err, result)
115+
{
116+
if (err) {
117+
return cb(err, conn);
118+
} else {
119+
console.log("Rows updated: " + result.rowsAffected); // 2
120+
return cb(null, conn);
121+
}
122+
});
123+
};
80124

81-
connection.release(
82-
function(err)
83-
{
84-
if (err) { console.error(err.message); return; }
85-
});
86-
});
87-
});
88-
});
89-
});
90-
});
125+
async.waterfall(
126+
[
127+
doconnect,
128+
dodrop,
129+
docreate,
130+
doinsert1,
131+
doinsert2,
132+
doupdate,
133+
dodrop
134+
],
135+
function (err, conn) {
136+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
137+
if (conn)
138+
dorelease(conn);
91139
});

0 commit comments

Comments
 (0)