Skip to content

Commit d32e84f

Browse files
committed
Initial commit for node-oracledb files
1 parent 8ca5898 commit d32e84f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4928
-0
lines changed

javascript/node-oracledb/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
These are examples for the [node-oracledb 1.13 driver](https://www.npmjs.com/package/oracledb)
2+
3+
The node-oracledb add-on for Node.js powers high performance Oracle Database applications.
4+
5+
[node-oracledb installation instructions](https://github.com/oracle/node-oracledb/blob/master/INSTALL.md)
6+
7+
[Node-oracledb documentation](https://github.com/oracle/node-oracledb/blob/master/doc/api.md)
8+
9+
[Issues and questions](https://github.com/oracle/node-oracledb/issues)
10+
11+
After installing node-oracledb, the demos can be run by editing
12+
dbconfig.js to set your database credentials, creating the schema
13+
objects with demo.sql, and then executing each JavaScript file with
14+
Node.js.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
2+
3+
/******************************************************************************
4+
*
5+
* You may not use the identified files except in compliance with the Apache
6+
* License, Version 2.0 (the "License.")
7+
*
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* NAME
19+
* blobhttp.js
20+
*
21+
* DESCRIPTION
22+
* Listens for an HTTP request and returns an image queried from a BLOB column
23+
* Also shows the connection pool's caching using a 'default' pool.
24+
*
25+
* Use demo.sql to create the required table or do:
26+
* DROP TABLE mylobs;
27+
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
28+
*
29+
* Run lobinsert1.js to load an image before running this example.
30+
*
31+
* Start the listener with 'node blobhttp.js' and then use a browser
32+
* to load http://localhost:7000/getimage
33+
*
34+
*****************************************************************************/
35+
36+
var url = require('url');
37+
var http = require('http');
38+
var oracledb = require('oracledb');
39+
var dbConfig = require('./dbconfig.js');
40+
41+
var httpPort = 7000;
42+
43+
// Main entry point. Creates a connection pool which becomes the
44+
// 'default' pool. The callback creates an HTTP server.
45+
function init() {
46+
oracledb.createPool(
47+
{
48+
user: dbConfig.user,
49+
password: dbConfig.password,
50+
connectString: dbConfig.connectString
51+
},
52+
function(err) {
53+
if (err) {
54+
console.error("createPool() error: " + err.message);
55+
return;
56+
}
57+
58+
// Create HTTP server and listen on port 'httpPort'
59+
http
60+
.createServer(function(request, response) {
61+
handleRequest(request, response);
62+
})
63+
.listen(httpPort);
64+
65+
console.log("Server running. Try requesting: http://localhost:" + httpPort + "/getimage");
66+
});
67+
}
68+
69+
// Handles each web request
70+
function handleRequest(request, response) {
71+
72+
var requrl = url.parse(request.url, true);
73+
var action = requrl.pathname;
74+
75+
if (action == '/getimage') {
76+
oracledb.getConnection( // gets a connection from the 'default' connection pool
77+
function(err, connection)
78+
{
79+
if (err) {
80+
console.error(err.message);
81+
return;
82+
}
83+
84+
connection.execute(
85+
"SELECT b FROM mylobs WHERE id = :id", // get the image
86+
{ id: 2 },
87+
function(err, result)
88+
{
89+
if (err) {
90+
console.error(err.message);
91+
return;
92+
}
93+
94+
if (result.rows.length === 0) {
95+
console.error("No results. Did you run lobinsert1.js?");
96+
return;
97+
}
98+
99+
var lob = result.rows[0][0];
100+
if (lob === null) {
101+
console.log("BLOB was NULL");
102+
return;
103+
}
104+
105+
lob.on(
106+
'end',
107+
function()
108+
{
109+
console.log("lob.on 'end' event");
110+
response.end();
111+
});
112+
lob.on(
113+
'close',
114+
function()
115+
{
116+
console.log("lob.on 'close' event");
117+
connection.close(function(err) {
118+
if (err) console.error(err);
119+
});
120+
});
121+
lob.on(
122+
'error',
123+
function(err)
124+
{
125+
console.log("lob.on 'error' event");
126+
console.error(err);
127+
connection.close(function(err) {
128+
if (err) console.error(err);
129+
});
130+
});
131+
response.writeHead(200, {'Content-Type': 'image/jpeg' });
132+
lob.pipe(response); // write the image out
133+
});
134+
});
135+
136+
} else {
137+
response.writeHead(200, {'Content-Type': 'text/plain' });
138+
response.end("Try requesting: http://localhost:" + httpPort + "/getimage\n");
139+
}
140+
}
141+
142+
process
143+
.on('SIGTERM', function() {
144+
console.log("\nTerminating");
145+
process.exit(0);
146+
})
147+
.on('SIGINT', function() {
148+
console.log("\nTerminating");
149+
process.exit(0);
150+
});
151+
152+
init();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is example text used for node-oracledb CLOB examples.
2+
3+
The Oracle Database Node.js driver powers high performance Node.js applications.
4+
5+
The node-oracledb home page is at http://www.oracle.com/technetwork/database/database-technologies/scripting-languages/node_js/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
2+
3+
/******************************************************************************
4+
*
5+
* You may not use the identified files except in compliance with the Apache
6+
* License, Version 2.0 (the "License.")
7+
*
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* NAME
19+
* connect.js
20+
*
21+
* DESCRIPTION
22+
* Tests a basic connection to the database.
23+
* See dbconfig.js for information on connectString formats.
24+
*
25+
*****************************************************************************/
26+
27+
var oracledb = require('oracledb');
28+
var dbConfig = require('./dbconfig.js');
29+
30+
oracledb.getConnection(
31+
{
32+
user : dbConfig.user,
33+
password : dbConfig.password,
34+
connectString : dbConfig.connectString
35+
},
36+
function(err, connection)
37+
{
38+
if (err) {
39+
console.error(err.message);
40+
return;
41+
}
42+
console.log('Connection was successful!');
43+
44+
connection.close(
45+
function(err)
46+
{
47+
if (err) {
48+
console.error(err.message);
49+
return;
50+
}
51+
});
52+
});

javascript/node-oracledb/date.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */
2+
3+
/******************************************************************************
4+
*
5+
* You may not use the identified files except in compliance with the Apache
6+
* License, Version 2.0 (the "License.")
7+
*
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* NAME
19+
* date.js
20+
*
21+
* DESCRIPTION
22+
* Insert and query DATE and TIMESTAMP columns.
23+
*
24+
* When bound in an INSERT, JavaScript Dates are inserted using
25+
* TIMESTAMP WITH LOCAL TIMEZONE. Similarly for queries, TIMESTAMP
26+
* and DATE columns are fetched as TIMESTAMP WITH LOCAL TIMEZONE.
27+
*
28+
*****************************************************************************///
29+
30+
var async = require('async');
31+
var oracledb = require('oracledb');
32+
var dbConfig = require('./dbconfig.js');
33+
34+
var doconnect = function(cb) {
35+
oracledb.getConnection(
36+
{
37+
user : dbConfig.user,
38+
password : dbConfig.password,
39+
connectString : dbConfig.connectString
40+
},
41+
cb);
42+
};
43+
44+
var dorelease = function(conn) {
45+
conn.close(function (err) {
46+
if (err)
47+
console.error(err.message);
48+
});
49+
};
50+
51+
var docleanup = function (conn, cb) {
52+
conn.execute(
53+
"BEGIN " +
54+
" DECLARE" +
55+
" e_table_exists EXCEPTION;" +
56+
" PRAGMA EXCEPTION_INIT(e_table_exists, -00942);" +
57+
" BEGIN" +
58+
" EXECUTE IMMEDIATE ('DROP TABLE datetest');" +
59+
" EXCEPTION" +
60+
" WHEN e_table_exists" +
61+
" THEN NULL;" +
62+
" END; " +
63+
"END;",
64+
function(err) {
65+
return cb(err, conn);
66+
});
67+
};
68+
69+
var docreate = function(conn, cb) {
70+
conn.execute(
71+
"CREATE TABLE datetest(timestampcol TIMESTAMP, datecol DATE)",
72+
function(err) {
73+
return cb(err, conn);
74+
});
75+
};
76+
77+
// Setting a local timezone in applications is recommended.
78+
// Note setting the environment variable ORA_SDTZ is an efficient alternative.
79+
var doalter = function(conn, cb) {
80+
console.log('Altering session time zone');
81+
conn.execute(
82+
"ALTER SESSION SET TIME_ZONE='UTC'",
83+
function(err) {
84+
return cb(err, conn);
85+
});
86+
};
87+
88+
// When bound, JavaScript Dates are inserted using TIMESTAMP WITH LOCAL TIMEZONE
89+
var doinsert = function(conn, cb) {
90+
var date = new Date();
91+
92+
console.log("Inserting JavaScript date: " + date);
93+
94+
conn.execute(
95+
"INSERT INTO datetest (timestampcol, datecol) VALUES (:ts, :td)",
96+
{ ts: date,
97+
td: date },
98+
function(err, result) {
99+
if (err)
100+
return cb(err, conn);
101+
102+
console.log('Rows inserted: ' + result.rowsAffected );
103+
return cb(null, conn);
104+
});
105+
};
106+
107+
108+
// Fetch the dates
109+
var doselect = function(conn, cb) {
110+
conn.execute(
111+
"SELECT timestampcol, datecol FROM datetest",
112+
function(err, result) {
113+
if (err) {
114+
return cb(err, conn);
115+
}
116+
117+
console.log("Query Results:");
118+
console.log(result.rows);
119+
120+
// Show the queried dates are of type Date
121+
console.log("Result Manipulation in JavaScript:");
122+
var ts = result.rows[0][0];
123+
ts.setDate(ts.getDate() + 5);
124+
console.log(ts);
125+
126+
var d = result.rows[0][1];
127+
d.setDate(d.getDate() - 5);
128+
console.log(d);
129+
130+
return cb(null, conn);
131+
});
132+
};
133+
134+
// Main routine
135+
async.waterfall([
136+
doconnect,
137+
docleanup,
138+
docreate,
139+
doinsert,
140+
141+
doselect,
142+
doalter,
143+
doselect,
144+
145+
docleanup
146+
],
147+
function (err, conn) {
148+
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
149+
if (conn)
150+
dorelease(conn);
151+
});

0 commit comments

Comments
 (0)