|
| 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 | + * The node-oracledb test suite uses 'mocha', 'should' and 'async'. |
| 19 | + * See LICENSE.md for relevant licenses. |
| 20 | + * |
| 21 | + * NAME |
| 22 | + * 59. lob.js |
| 23 | + * |
| 24 | + * DESCRIPTION |
| 25 | + * |
| 26 | + * Testing Lob data and result set. |
| 27 | + * Create a table contains Lob data. Read the Lob to result set. Get |
| 28 | + * rows one by one. Read the lob data on each row. |
| 29 | + * |
| 30 | + * NUMBERING RULE |
| 31 | + * Test numbers follow this numbering rule: |
| 32 | + * 1 - 20 are reserved for basic functional tests |
| 33 | + * 21 - 50 are reserved for data type supporting tests |
| 34 | + * 51 onwards are for other tests |
| 35 | + * |
| 36 | + *****************************************************************************/ |
| 37 | +"use strict"; |
| 38 | + |
| 39 | +var oracledb = require('oracledb'); |
| 40 | +var fs = require('fs'); |
| 41 | +var async = require('async'); |
| 42 | +var should = require('should'); |
| 43 | +var stream = require('stream'); |
| 44 | +var dbConfig = require('./dbConfig.js'); |
| 45 | +var assist = require('./dataTypeAssist.js'); |
| 46 | + |
| 47 | +var inFileName = './test/clobexample.txt'; |
| 48 | + |
| 49 | +describe('59. lob.js', function() { |
| 50 | + |
| 51 | + if(dbConfig.externalAuth){ |
| 52 | + var credential = { externalAuth: true, connectString: dbConfig.connectString }; |
| 53 | + } else { |
| 54 | + var credential = dbConfig; |
| 55 | + } |
| 56 | + |
| 57 | + var connection = null; |
| 58 | + before('get one connection', function(done) { |
| 59 | + oracledb.getConnection(credential, function(err, conn) { |
| 60 | + should.not.exist(err); |
| 61 | + connection = conn; |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }) |
| 65 | + |
| 66 | + after('release connection', function(done) { |
| 67 | + connection.release( function(err) { |
| 68 | + should.not.exist(err); |
| 69 | + done(); |
| 70 | + }); |
| 71 | + }) |
| 72 | + |
| 73 | + describe('59.1 CLOB data', function() { |
| 74 | + |
| 75 | + var tableName = "oracledb_myclobs"; |
| 76 | + before('create table', function(done) { |
| 77 | + assist.createTable(connection, tableName, done); |
| 78 | + }) |
| 79 | + |
| 80 | + after(function(done) { |
| 81 | + connection.execute( |
| 82 | + "DROP table " + tableName, |
| 83 | + function(err) { |
| 84 | + should.not.exist(err); |
| 85 | + done(); |
| 86 | + } |
| 87 | + ); |
| 88 | + }) |
| 89 | + |
| 90 | + it('59.1.1 reads clob data one by one row from result set', function(done) { |
| 91 | + async.series([ |
| 92 | + function(callback) { |
| 93 | + insertClob(1, callback); |
| 94 | + }, |
| 95 | + function(callback) { |
| 96 | + insertClob(2, callback); |
| 97 | + }, |
| 98 | + function(callback) { |
| 99 | + insertClob(3, callback); |
| 100 | + }, |
| 101 | + function(callback) { |
| 102 | + connection.execute( |
| 103 | + "SELECT num, content FROM " + tableName, |
| 104 | + [], |
| 105 | + { resultSet: true }, |
| 106 | + function(err, result) { |
| 107 | + should.not.exist(err); |
| 108 | + // console.log(result); |
| 109 | + fetchOneRowFromRS(result.resultSet, callback); |
| 110 | + } |
| 111 | + ); |
| 112 | + } |
| 113 | + ], done); |
| 114 | + }) |
| 115 | + |
| 116 | + function fetchOneRowFromRS(resultSet, callback) |
| 117 | + { |
| 118 | + resultSet.getRow( function(err, row) { |
| 119 | + should.not.exist(err); |
| 120 | + if (!row) { |
| 121 | + resultSet.close( function(err) { |
| 122 | + should.not.exist(err); |
| 123 | + callback(); |
| 124 | + }); |
| 125 | + } |
| 126 | + else { |
| 127 | + var lob = row[1]; |
| 128 | + lob.setEncoding('utf8'); |
| 129 | + |
| 130 | + var text = ''; |
| 131 | + lob.on('data', function(chunk) { |
| 132 | + text += chunk; |
| 133 | + }); |
| 134 | + |
| 135 | + lob.on('end', function() { |
| 136 | + fetchOneRowFromRS(resultSet, callback); |
| 137 | + }); |
| 138 | + |
| 139 | + lob.on('error', function(err) { |
| 140 | + console.log("lob.on 'error' event"); |
| 141 | + console.error(err.message); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + function insertClob(id, cb) |
| 149 | + { |
| 150 | + connection.execute( |
| 151 | + "INSERT INTO " + tableName + " VALUES (:n, EMPTY_CLOB()) RETURNING content INTO :lobbv", |
| 152 | + { n: id, lobbv: { type: oracledb.CLOB, dir: oracledb.BIND_OUT } }, |
| 153 | + function(err, result) { |
| 154 | + should.not.exist(err); |
| 155 | + var lob = result.outBinds.lobbv[0]; |
| 156 | + var inStream = fs.createReadStream(inFileName); |
| 157 | + |
| 158 | + inStream.pipe(lob); |
| 159 | + |
| 160 | + inStream.on('end', function() { |
| 161 | + connection.commit( function(err) { |
| 162 | + should.not.exist(err); |
| 163 | + cb(); // insertion done |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + inStream.on('error', function(err) { |
| 168 | + should.not.exist(err); |
| 169 | + }); |
| 170 | + |
| 171 | + } |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + }) // 59.1 |
| 176 | + |
| 177 | +}) // 59 |
| 178 | + |
0 commit comments