Skip to content

Commit 92672ed

Browse files
committed
Update examples for node-oracledb 1.0
1 parent 9b5f15b commit 92672ed

29 files changed

+1298
-73
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ core
99
npm-debug.log
1010
node_modules
1111
/build/*
12+
/examples/clobstream1out.txt
13+
/examples/clobstream2out.txt
14+
/examples/blobstream1out.jpg
15+
/examples/blobstream2out.jpg
1216

1317
# Oracle Ignores
1418
sqlnet.log

examples/blobhttp.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright (c) 2015, 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+
* Use demo.sql to create the required table or do:
24+
* DROP TABLE mylobs;
25+
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
26+
* Run blobinsert1.js to load an image before running this example.
27+
* Start the listener with 'node blobhttp.js' and then use a browser
28+
* to load http://127.0.0.1:7000/getimage
29+
*
30+
*****************************************************************************/
31+
32+
var fs = require('fs');
33+
var http = require('http');
34+
var url = require('url');
35+
36+
var oracledb = require('oracledb');
37+
var dbConfig = require('./dbconfig.js');
38+
39+
var portid = 7000;
40+
41+
http.createServer(function(req, res){
42+
var request = url.parse(req.url, true);
43+
var action = request.pathname;
44+
45+
if (action == '/getimage') {
46+
oracledb.getConnection(
47+
{
48+
user : dbConfig.user,
49+
password : dbConfig.password,
50+
connectString : dbConfig.connectString
51+
},
52+
function(err, connection)
53+
{
54+
if (err) { console.error(err.message); return; }
55+
56+
connection.execute(
57+
"SELECT b FROM mylobs WHERE id = :id",
58+
{ id: 2 },
59+
function(err, result)
60+
{
61+
if (err) { console.error(err.message); return; }
62+
if (result.rows.length === 0) { console.log("No results"); return; }
63+
64+
var lob = result.rows[0][0];
65+
if (lob === null) { console.log("BLOB was NULL"); return; }
66+
67+
lob.on(
68+
'end',
69+
function () {
70+
console.log("lob.on 'end' event");
71+
res.end();
72+
});
73+
lob.on(
74+
'error',
75+
function(err)
76+
{
77+
console.log("lob.on 'error' event");
78+
console.error(err);
79+
});
80+
res.writeHead(200, {'Content-Type': 'image/jpeg' });
81+
lob.pipe(res);
82+
});
83+
});
84+
85+
} else {
86+
res.writeHead(200, {'Content-Type': 'text/plain' });
87+
res.end('Try requesting: http://127.0.0.1:7000/getimage\n');
88+
}
89+
}).listen(portid, '127.0.0.1');
90+
91+
console.log("Server running at http://127.0.0.1:" + portid);
92+
console.log("Try requesting: http://127.0.0.1:7000/getimage");

examples/blobinsert1.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright (c) 2015, 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+
* blobinsert1.js
20+
*
21+
* DESCRIPTION
22+
* Loads fuzzydinosaur.jpg and INSERTs it into a BLOB column.
23+
* Copy any image to example.jpg before running this example.
24+
* Use demo.sql to create the required table or do:
25+
* DROP TABLE mylobs;
26+
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
27+
*
28+
*****************************************************************************/
29+
30+
var fs = require('fs');
31+
var oracledb = require('oracledb');
32+
var dbConfig = require('./dbconfig.js');
33+
34+
var inFileName = 'fuzzydinosaur.jpg'; // contains the image to be inserted
35+
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) { console.error(err.message); return; }
45+
46+
connection.execute(
47+
"INSERT INTO mylobs (id, b) VALUES (:id, EMPTY_BLOB()) RETURNING b INTO :lobbv",
48+
{ id: 2, lobbv: {type: oracledb.BLOB, dir: oracledb.BIND_OUT} },
49+
{ autoCommit: false }, // a transaction needs to span the INSERT and pipe()
50+
function(err, result)
51+
{
52+
if (err) { console.error(err.message); return; }
53+
if (result.rowsAffected != 1 || result.outBinds.lobbv.length != 1) {
54+
console.error('Error getting a LOB locator');
55+
return;
56+
}
57+
58+
console.log('Reading from ' + inFileName);
59+
var inStream = fs.createReadStream(inFileName);
60+
inStream.on(
61+
'end',
62+
function()
63+
{
64+
console.log("inStream.on 'end' event");
65+
connection.commit(
66+
function(err)
67+
{
68+
if (err)
69+
console.error(err.message);
70+
else
71+
console.log("Image uploaded successfully.");
72+
});
73+
});
74+
inStream.on(
75+
'error',
76+
function(err)
77+
{
78+
console.log("inStream.on 'error' event");
79+
console.error(err);
80+
});
81+
82+
var lob = result.outBinds.lobbv[0];
83+
lob.on(
84+
'error',
85+
function(err)
86+
{
87+
console.log("lob.on 'error' event");
88+
console.error(err);
89+
});
90+
inStream.pipe(lob); // copies the text to the BLOB
91+
});
92+
});

examples/blobstream1.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* Copyright (c) 2015, 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+
* blobstream1.js
20+
*
21+
* DESCRIPTION
22+
* SELECTs an image from a BLOB and streams it to a file, blobstream1out.jpg
23+
* Use demo.sql to create the required table or do:
24+
* DROP TABLE mylobs;
25+
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
26+
* Run blobinsert1.js to load an image before running this example.
27+
*
28+
*****************************************************************************/
29+
30+
var fs = require('fs');
31+
var oracledb = require('oracledb');
32+
var dbConfig = require('./dbconfig.js');
33+
34+
var outFileName = 'blobstream1out.jpg';
35+
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) { console.error(err.message); return; }
45+
46+
connection.execute(
47+
"SELECT b FROM mylobs WHERE id = :id",
48+
{ id: 2 },
49+
function(err, result)
50+
{
51+
if (err) { console.error(err.message); return; }
52+
if (result.rows.length === 0) { console.log("No results"); return; }
53+
54+
var lob = result.rows[0][0];
55+
if (lob === null) { console.log("BLOB was NULL"); return; }
56+
57+
lob.on(
58+
'error',
59+
function(err)
60+
{
61+
console.log("lob.on 'error' event");
62+
console.error(err);
63+
});
64+
65+
console.log('Writing to ' + outFileName);
66+
var outStream = fs.createWriteStream(outFileName);
67+
outStream.on(
68+
'error',
69+
function(err)
70+
{
71+
console.log("outStream.on 'error' event");
72+
console.error(err);
73+
});
74+
lob.pipe(outStream);
75+
});
76+
});

examples/blobstream2.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Copyright (c) 2015, 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+
* blobstream2.js
20+
*
21+
* DESCRIPTION
22+
* SELECTs a BLOB and writes it to a file, blobstream2out.jpg
23+
* Use demo.sql to create the required table or do:
24+
* DROP TABLE mylobs;
25+
* CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
26+
* Run blobinsert1.js to load an image before running this example.
27+
*
28+
*****************************************************************************/
29+
30+
var fs = require('fs');
31+
var oracledb = require('oracledb');
32+
var dbConfig = require('./dbconfig.js');
33+
34+
var outFileName = 'blobstream2out.jpg';
35+
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) { console.error(err.message); return; }
45+
46+
connection.execute(
47+
"SELECT b FROM mylobs WHERE id = :id",
48+
{ id: 2 },
49+
function(err, result)
50+
{
51+
if (err) { console.error(err.message); return; }
52+
if (result.rows.length === 0) { console.log("No results"); return; }
53+
54+
var blob = Buffer(0);
55+
var blobLength = 0;
56+
var lob = result.rows[0][0];
57+
58+
if (lob === null) { console.log("BLOB was NULL"); return; }
59+
60+
console.log('BLOB length is ' + lob.length);
61+
console.log("BLOB chunkSize is", lob.chunkSize);
62+
63+
// pieceSize is the number of bytes retrieved for each readable 'data' event.
64+
// The default is lob.chunkSize. The recommendation is for it to be a multiple of chunkSize.
65+
// lob.pieceSize = 100; // fetch smaller chunks to show repeated 'data' events
66+
67+
lob.on('data',
68+
function(chunk)
69+
{
70+
console.log("lob.on 'data' event");
71+
console.log(' - got %d bytes of data', chunk.length);
72+
blobLength = blobLength + chunk.length;
73+
blob = Buffer.concat([blob, chunk], blobLength);
74+
// an alternative (not shown) would be to write each chunk to the file
75+
});
76+
lob.on('end',
77+
function()
78+
{
79+
console.log("lob.on 'end' event");
80+
console.log("blob size is " + blob.length);
81+
fs.writeFile(outFileName, blob, function(err) {
82+
if (err) {
83+
console.error(err);
84+
} else {
85+
console.log("Completed write to " + outFileName);
86+
}
87+
});
88+
});
89+
lob.on('error',
90+
function(err)
91+
{
92+
console.log("lob.on 'error' event");
93+
console.error(err);
94+
});
95+
});
96+
});

examples/clobexample.txt

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/node_js/index.html

0 commit comments

Comments
 (0)