|
| 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 | + }); |
0 commit comments