simple-oracledb
Advanced tools
Comparing version 0.0.3 to 0.0.4
117
docs/api.md
@@ -10,5 +10,11 @@ ## Classes | ||
<dl> | ||
<dt><a href="#Constants">Constants</a> : <code>object</code></dt> | ||
<dd><p>Library constants.</p> | ||
</dd> | ||
<dt><a href="#RecordReader">RecordReader</a> : <code>object</code></dt> | ||
<dd><p>Record reading helper functions.</p> | ||
</dd> | ||
<dt><a href="#RecordWriter">RecordWriter</a> : <code>object</code></dt> | ||
<dd><p>Record writing helper functions.</p> | ||
</dd> | ||
<dt><a href="#ResultSetReader">ResultSetReader</a> : <code>object</code></dt> | ||
@@ -45,2 +51,3 @@ <dd><p>ResultSet object reading helper functions.</p> | ||
* [#query([params])](#Connection+query) | ||
* [#insert([params])](#Connection+insert) | ||
* [#release([callback])](#Connection+release) | ||
@@ -71,3 +78,3 @@ * _static_ | ||
This function is not recommended for huge results sets or huge LOB values as it will consume a lot of memory.<br> | ||
The function arguments used to execute the query are exactly as defined in the oracledb connection.execute function. | ||
The function arguments used to execute the 'query' are exactly as defined in the oracledb connection.execute function. | ||
@@ -91,2 +98,30 @@ **Access:** public | ||
``` | ||
<a name="Connection+insert"></a> | ||
### Connection#insert([params]) | ||
Provides simpler interface than the original oracledb connection.execute function to enable simple insert invocation with LOB support.<br> | ||
The callback output will be the same as oracledb conection.execute.<br> | ||
All LOBs will be written to the DB via streams and only after all LOBs are written the callback will be called.<br> | ||
The function arguments used to execute the 'insert' are exactly as defined in the oracledb connection.execute function, however the options are mandatory. | ||
**Access:** public | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| [params] | <code>\*</code> | See oracledb connection.execute function | | ||
**Example** | ||
```js | ||
connection.insert('INSERT INTO mylobs (id, clob_column1, blob_column2) VALUES (:id, EMPTY_CLOB(), EMPTY_BLOB())', { //no need to specify the RETURNING clause in the SQL | ||
id: 110, | ||
clobText1: 'some long clob string', //add bind variable with LOB column name and text content (need to map that name in the options) | ||
blobBuffer2: new Buffer('some blob content, can be binary...') //add bind variable with LOB column name and text content (need to map that name in the options) | ||
}, { | ||
lobMetaInfo: { //if LOBs are provided, this data structure must be provided in the options object and the bind variables parameter must be an object (not array) | ||
clob_column1: 'clobText1', //map oracle column name to bind variable name | ||
blob_column2: 'blobBuffer2' | ||
} | ||
}, function onResults(error, output) { | ||
//continue flow... | ||
}); | ||
``` | ||
<a name="Connection+release"></a> | ||
@@ -172,2 +207,29 @@ ### Connection#release([callback]) | ||
<a name="Constants"></a> | ||
## Constants : <code>object</code> | ||
Library constants. | ||
**Kind**: global namespace | ||
**Author:** Sagie Gur-Ari | ||
* [Constants](#Constants) : <code>object</code> | ||
* [.clobType](#Constants.clobType) : <code>number</code> | ||
* [.blobType](#Constants.blobType) : <code>number</code> | ||
* [.bindOut](#Constants.bindOut) : <code>number</code> | ||
<a name="Constants.clobType"></a> | ||
### Constants.clobType : <code>number</code> | ||
Holds the CLOB type. | ||
**Access:** public | ||
<a name="Constants.blobType"></a> | ||
### Constants.blobType : <code>number</code> | ||
Holds the BLOB type. | ||
**Access:** public | ||
<a name="Constants.bindOut"></a> | ||
### Constants.bindOut : <code>number</code> | ||
Holds the BIND_OUT value. | ||
**Access:** public | ||
<a name="RecordReader"></a> | ||
@@ -181,13 +243,6 @@ ## RecordReader : <code>object</code> | ||
* [RecordReader](#RecordReader) : <code>object</code> | ||
* [.blobType](#RecordReader.blobType) : <code>number</code> | ||
* _static_ | ||
* [.getValue(field, callback)](#RecordReader.getValue) ℗ | ||
* [.createFieldHandler(jsObject, columnName, value)](#RecordReader.createFieldHandler) ⇒ <code>function</code> ℗ | ||
* [.read(columnNames, row, callback)](#RecordReader.read) | ||
* [.getValue(field, callback)](#RecordReader.getValue) ℗ | ||
* [.createFieldHandler(jsObject, columnName, value)](#RecordReader.createFieldHandler) ⇒ <code>function</code> ℗ | ||
* [.read(columnNames, row, callback)](#RecordReader.read) | ||
<a name="RecordReader.blobType"></a> | ||
### RecordReader.blobType : <code>number</code> | ||
Holds the BLOB type. | ||
**Access:** public | ||
<a name="RecordReader.getValue"></a> | ||
@@ -232,2 +287,21 @@ ### RecordReader.getValue(field, callback) ℗ | ||
<a name="RecordWriter"></a> | ||
## RecordWriter : <code>object</code> | ||
Record writing helper functions. | ||
**Kind**: global namespace | ||
**Author:** Sagie Gur-Ari | ||
<a name="RecordWriter.write"></a> | ||
### RecordWriter.write(outBindings, lobData, callback) | ||
Writes all LOBs columns via out bindings of the INSERT/UPDATE command. | ||
**Kind**: static method of <code>[RecordWriter](#RecordWriter)</code> | ||
**Access:** public | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| outBindings | <code>object</code> | The output bindings of the INSERT/UPDATE result | | ||
| lobData | <code>object</code> | The LOB data holder (key column name, value column value) | | ||
| callback | <code>[AsyncCallback](#AsyncCallback)</code> | called when the row is fully written to or in case of an error | | ||
<a name="ResultSetReader"></a> | ||
@@ -359,6 +433,10 @@ ## ResultSetReader : <code>object</code> | ||
**Author:** Sagie Gur-Ari | ||
* [Stream](#Stream) : <code>object</code> | ||
* [.readFully(readableStream, binary, callback)](#Stream.readFully) | ||
* [.write(writableStream, data, callback)](#Stream.write) | ||
<a name="Stream.readFully"></a> | ||
### Stream.readFully(readableStream, binary, callback) | ||
Reads all data from the provided stream.<br> | ||
The stream data is expected to be UTF-8 string. | ||
Reads all data from the provided stream. | ||
@@ -374,2 +452,15 @@ **Kind**: static method of <code>[Stream](#Stream)</code> | ||
<a name="Stream.write"></a> | ||
### Stream.write(writableStream, data, callback) | ||
Writes the provided data to the stream. | ||
**Kind**: static method of <code>[Stream](#Stream)</code> | ||
**Access:** public | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| writableStream | <code>object</code> | The writable stream | | ||
| data | <code>Buffer</code> | <code>string</code> | The text of binary data to write | | ||
| callback | <code>[AsyncCallback](#AsyncCallback)</code> | called when the data is fully written to the provided stream | | ||
<a name="AsyncCallback"></a> | ||
@@ -376,0 +467,0 @@ ## AsyncCallback : <code>function</code> |
@@ -5,2 +5,4 @@ 'use strict'; | ||
var resultSetReader = require('./resultset-reader'); | ||
var recordWriter = require('./record-writer'); | ||
var constants = require('./constants'); | ||
@@ -47,3 +49,3 @@ /*jslint debug: true */ | ||
* This function is not recommended for huge results sets or huge LOB values as it will consume a lot of memory.<br> | ||
* The function arguments used to execute the query are exactly as defined in the oracledb connection.execute function. | ||
* The function arguments used to execute the 'query' are exactly as defined in the oracledb connection.execute function. | ||
* | ||
@@ -72,3 +74,3 @@ * @function | ||
var callback = argumentsArray.pop(); | ||
argumentsArray.push(function onConnection(error, results) { | ||
argumentsArray.push(function onExecute(error, results) { | ||
if (error || (!results)) { | ||
@@ -87,2 +89,114 @@ callback(error, results); | ||
/** | ||
* Provides simpler interface than the original oracledb connection.execute function to enable simple insert invocation with LOB support.<br> | ||
* The callback output will be the same as oracledb conection.execute.<br> | ||
* All LOBs will be written to the DB via streams and only after all LOBs are written the callback will be called.<br> | ||
* The function arguments used to execute the 'insert' are exactly as defined in the oracledb connection.execute function, however the options are mandatory. | ||
* | ||
* @function | ||
* @memberof! Connection | ||
* @public | ||
* @param {*} [params] - See oracledb connection.execute function | ||
* @example | ||
* ```js | ||
* connection.insert('INSERT INTO mylobs (id, clob_column1, blob_column2) VALUES (:id, EMPTY_CLOB(), EMPTY_BLOB())', { //no need to specify the RETURNING clause in the SQL | ||
* id: 110, | ||
* clobText1: 'some long clob string', //add bind variable with LOB column name and text content (need to map that name in the options) | ||
* blobBuffer2: new Buffer('some blob content, can be binary...') //add bind variable with LOB column name and text content (need to map that name in the options) | ||
* }, { | ||
* lobMetaInfo: { //if LOBs are provided, this data structure must be provided in the options object and the bind variables parameter must be an object (not array) | ||
* clob_column1: 'clobText1', //map oracle column name to bind variable name | ||
* blob_column2: 'blobBuffer2' | ||
* } | ||
* }, function onResults(error, output) { | ||
* //continue flow... | ||
* }); | ||
* ``` | ||
*/ | ||
Connection.prototype.insert = function () { | ||
var self = this; | ||
var argumentsArray = Array.prototype.slice.call(arguments, 0); | ||
var lobMetaInfo; | ||
var lobColumns; | ||
var lobData; | ||
if (argumentsArray.length === 4) { | ||
lobMetaInfo = argumentsArray[2].lobMetaInfo || {}; | ||
lobColumns = Object.keys(lobMetaInfo); | ||
//modify SQL by adding RETURNING clause and modify bind variables definition | ||
if (lobColumns && lobColumns.length) { | ||
var sql = [argumentsArray[0]]; | ||
var bindVariables = argumentsArray[1]; | ||
lobData = {}; | ||
sql.push(' RETURNING '); | ||
var index; | ||
var columnName; | ||
var lobValue; | ||
var bindVariableName; | ||
for (index = 0; index < lobColumns.length; index++) { | ||
if (index > 0) { | ||
sql.push(', '); | ||
} | ||
columnName = lobColumns[index]; | ||
sql.push(columnName); | ||
bindVariableName = lobMetaInfo[columnName]; | ||
lobValue = bindVariables[bindVariableName]; | ||
lobData[bindVariableName] = lobValue; | ||
if (typeof lobValue === 'string') { //CLOB | ||
bindVariables[bindVariableName] = { | ||
type: constants.clobType, | ||
dir: constants.bindOut | ||
}; | ||
} else { //BLOB | ||
bindVariables[bindVariableName] = { | ||
type: constants.blobType, | ||
dir: constants.bindOut | ||
}; | ||
} | ||
} | ||
sql.push(' INTO '); | ||
for (index = 0; index < lobColumns.length; index++) { | ||
if (index > 0) { | ||
sql.push(', '); | ||
} | ||
sql.push(':'); | ||
columnName = lobColumns[index]; | ||
sql.push(lobMetaInfo[columnName]); | ||
} | ||
argumentsArray[0] = sql.join(''); | ||
} | ||
} | ||
var callback = argumentsArray.pop(); | ||
argumentsArray.push(function onExecute(error, results) { | ||
if (error || (!results)) { | ||
callback(error, results); | ||
} else if ((results.rowsAffected === 1) && lobColumns && lobColumns.length) { | ||
recordWriter.write(results.outBinds, lobData, function onWrite(writeError) { | ||
if (writeError) { | ||
callback(writeError); | ||
} else { | ||
callback(null, results); | ||
} | ||
}); | ||
} else { | ||
callback(error, results); | ||
} | ||
}); | ||
self.execute.apply(self, argumentsArray); | ||
}; | ||
/** | ||
* This function modifies the existing connection.release function by enabling the input | ||
@@ -89,0 +203,0 @@ * callback to be an optional parameter.<br> |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var stream = require('./stream'); | ||
var constants = require('./constants'); | ||
@@ -15,11 +16,2 @@ /** | ||
/** | ||
* Holds the BLOB type. | ||
* | ||
* @member {number} | ||
* @alias RecordReader.blobType | ||
* @memberof! RecordReader | ||
* @public | ||
*/ | ||
blobType: 2007, | ||
/** | ||
* Returns the value of the field from the row. | ||
@@ -46,3 +38,3 @@ * | ||
} else if (field.type !== undefined) { //LOB | ||
var binary = (field.type === this.blobType); | ||
var binary = (field.type === constants.blobType); | ||
stream.readFully(field, binary, callback); | ||
@@ -49,0 +41,0 @@ } else { |
@@ -13,3 +13,3 @@ 'use strict'; | ||
var Connection = require('./connection'); | ||
var RecordReader = require('./record-reader'); | ||
var constants = require('./constants'); | ||
@@ -57,5 +57,13 @@ /** | ||
oracle.simplified = true; | ||
//update type meta info | ||
if (oracle.BLOB !== undefined) { | ||
RecordReader.blobType = oracle.BLOB; | ||
constants.blobType = oracle.BLOB; | ||
} | ||
if (oracle.CLOB !== undefined) { | ||
constants.clobType = oracle.CLOB; | ||
} | ||
if (oracle.BIND_OUT !== undefined) { | ||
constants.bindOut = oracle.BIND_OUT; | ||
} | ||
@@ -62,0 +70,0 @@ var getConnectionOrg = oracle.getConnection; |
@@ -11,4 +11,3 @@ 'use strict'; | ||
/** | ||
* Reads all data from the provided stream.<br> | ||
* The stream data is expected to be UTF-8 string. | ||
* Reads all data from the provided stream. | ||
* | ||
@@ -87,3 +86,41 @@ * @function | ||
}); | ||
}, | ||
/** | ||
* Writes the provided data to the stream. | ||
* | ||
* @function | ||
* @memberof! Stream | ||
* @public | ||
* @param {object} writableStream - The writable stream | ||
* @param {Buffer|string} data - The text of binary data to write | ||
* @param {AsyncCallback} callback - called when the data is fully written to the provided stream | ||
*/ | ||
write: function write(writableStream, data, callback) { | ||
if (writableStream && data) { | ||
var errorDetected = false; | ||
var onError = function onError(error) { | ||
errorDetected = true; | ||
callback(error); | ||
}; | ||
writableStream.once('error', onError); | ||
var onWrite = function onWrite() { | ||
writableStream.removeListener('error', onError); | ||
if (!errorDetected) { | ||
callback(); | ||
} | ||
}; | ||
if (typeof data === 'string') { | ||
writableStream.end(data, 'utf8', onWrite); | ||
} else { //Buffer | ||
writableStream.end(data, onWrite); | ||
} | ||
} else { | ||
callback(); | ||
} | ||
} | ||
}; |
{ | ||
"name": "simple-oracledb", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Extend capabilities of oracledb with simplified API for quicker development.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,2 +12,3 @@ # simple-oracledb | ||
* [query](#usage-query) | ||
* [insert](#usage-insert) | ||
* [release](#usage-release) | ||
@@ -22,3 +23,3 @@ * [Installation](#installation) | ||
## Overview | ||
This library enables to modify the oracledb main object, oracledb pool and oracledb connection.<br> | ||
This library enables to modify the oracledb main object, oracledb pool and oracledb connection of the [official oracle node.js driver](https://github.com/oracle/node-oracledb).<br> | ||
The main goal is to provide an extended oracledb connection which provides more functionality for most use cases.<br> | ||
@@ -101,3 +102,3 @@ The new functionality aim is to be simpler and more strait forward to enable quicker development. | ||
This function is not recommended for huge results sets or huge LOB values as it will consume a lot of memory.<br> | ||
The function arguments used to execute the query are exactly as defined in the oracledb connection.execute function. | ||
The function arguments used to execute the 'query' are exactly as defined in the oracledb connection.execute function. | ||
@@ -115,2 +116,24 @@ ```js | ||
<a name="usage-insert"></a> | ||
## 'connection.insert(sql, bindVariables, options, callback)' | ||
Provides simpler interface than the original oracledb connection.execute function to enable simple insert invocation with LOB support.<br> | ||
The callback output will be the same as oracledb conection.execute.<br> | ||
All LOBs will be written to the DB via streams and only after all LOBs are written the callback will be called.<br> | ||
The function arguments used to execute the 'insert' are exactly as defined in the oracledb connection.execute function, however the options are mandatory. | ||
```js | ||
connection.insert('INSERT INTO mylobs (id, clob_column1, blob_column2) VALUES (:id, EMPTY_CLOB(), EMPTY_BLOB())', { //no need to specify the RETURNING clause in the SQL | ||
id: 110, | ||
clobText1: 'some long clob string', //add bind variable with LOB column name and text content (need to map that name in the options) | ||
blobBuffer2: new Buffer('some blob content, can be binary...') //add bind variable with LOB column name and text content (need to map that name in the options) | ||
}, { | ||
lobMetaInfo: { //if LOBs are provided, this data structure must be provided in the options object and the bind variables parameter must be an object (not array) | ||
clob_column1: 'clobText1', //map oracle column name to bind variable name | ||
blob_column2: 'blobBuffer2' | ||
} | ||
}, function onResults(error, output) { | ||
//continue flow... | ||
}); | ||
``` | ||
<a name="usage-release"></a> | ||
@@ -159,2 +182,3 @@ ## 'connection.release([callback])' | ||
| ----------- | ------- | ----------- | | ||
| 2015-10-18 | v0.0.4 | Added connection.insert support | | ||
| 2015-10-16 | v0.0.3 | Maintenance | | ||
@@ -161,0 +185,0 @@ | 2015-10-15 | v0.0.1 | Initial release. | |
@@ -46,7 +46,16 @@ 'use strict'; | ||
var testStream = new EventEmitter(); | ||
testStream.type = 10; | ||
testStream.type = require('../../lib/constants').clobType; | ||
testStream.setEncoding = function (encoding) { | ||
assert.equal(encoding, 'utf8'); | ||
}; | ||
testStream.end = function (data, encoding, callback) { | ||
assert.deepEqual(data, testStream.testData); | ||
assert.equal(encoding, 'utf8'); | ||
assert.isFunction(callback); | ||
this.emit('end'); | ||
callback(); | ||
}; | ||
return testStream; | ||
@@ -56,6 +65,14 @@ }, | ||
var testStream = new EventEmitter(); | ||
testStream.type = require('../../lib/record-reader').blobType; | ||
testStream.type = require('../../lib/constants').blobType; | ||
testStream.end = function (data, callback) { | ||
assert.deepEqual(data, new Buffer(testStream.testData)); | ||
assert.isFunction(callback); | ||
this.emit('end'); | ||
callback(); | ||
}; | ||
return testStream; | ||
} | ||
}; |
@@ -12,3 +12,4 @@ 'use strict'; | ||
try { | ||
oracledb = require('oracledb');} catch (error) { | ||
oracledb = require('oracledb'); | ||
} catch (error) { | ||
integrated = false; | ||
@@ -316,3 +317,92 @@ } | ||
}); | ||
describe('insert', function () { | ||
var columnNames = [ | ||
{ | ||
name: 'COL1' | ||
}, | ||
{ | ||
name: 'COL2' | ||
}, | ||
{ | ||
name: 'COL3' | ||
}, | ||
{ | ||
name: 'COL4' | ||
}, | ||
{ | ||
name: 'LOB1' | ||
}, | ||
{ | ||
name: 'LOB2' | ||
} | ||
]; | ||
it('error', function (done) { | ||
var table = 'TEST_ORA_INST1'; | ||
initDB(table, null, function (pool) { | ||
pool.getConnection(function (err, connection) { | ||
assert.isUndefined(err); | ||
connection.insert('INSERT INTO TEST_NOTHING (SOMEFIELD), (:value)', { | ||
value: 'valid' | ||
}, {}, function (error) { | ||
assert.isDefined(error); | ||
end(done, connection); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('insert - simple data', function (done) { | ||
var table = 'TEST_ORA_INST2'; | ||
initDB(table, [], function (pool) { | ||
pool.getConnection(function (err, connection) { | ||
assert.isUndefined(err); | ||
connection.insert('INSERT INTO ' + table + ' (COL1, COL2) values (:value1, :value2)', { | ||
value1: 'test', | ||
value2: 123 | ||
}, {}, function (error, results) { | ||
assert.isUndefined(error); | ||
assert.equal(1, results.rowsAffected); | ||
end(done, connection); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('insert - LOB data', function (done) { | ||
var table = 'TEST_ORA_INST3'; | ||
var longClobText = 'this is a really long line of test data\n'; | ||
var index; | ||
var buffer = []; | ||
for (index = 0; index < 1000; index++) { | ||
buffer.push(longClobText); | ||
} | ||
longClobText = buffer.join(''); | ||
initDB(table, [], function (pool) { | ||
pool.getConnection(function (err, connection) { | ||
assert.isUndefined(err); | ||
connection.insert('INSERT INTO ' + table + ' (COL1, COL2, LOB1, LOB2) values (:value1, :value2, :clob1, :blob2)', { | ||
value1: 'test', | ||
value2: 123, | ||
clob1: longClobText, | ||
blob2: new Buffer('blob text here') | ||
}, {}, function (error, results) { | ||
assert.isUndefined(error); | ||
assert.equal(1, results.rowsAffected); | ||
end(done, connection); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var helper = require('../helpers/test-oracledb'); | ||
var constants = require('../../lib/constants'); | ||
var Connection = require('../../lib/connection'); | ||
@@ -288,2 +289,237 @@ | ||
describe('insert', function () { | ||
it('no lobs', function (done) { | ||
var connection = {}; | ||
Connection.extend(connection); | ||
connection.execute = function (sql, bindVars, options, callback) { | ||
assert.equal(sql, 'INSERT INTO nolobs (id, id2) VALUES (:id1, :id2)'); | ||
assert.deepEqual(bindVars, { | ||
id1: 1, | ||
id2: 2 | ||
}); | ||
assert.deepEqual(options, { | ||
lobMetaInfo: {} | ||
}); | ||
callback(null, { | ||
rowsAffected: 1, | ||
outBinds: {} | ||
}); | ||
}; | ||
connection.insert('INSERT INTO nolobs (id, id2) VALUES (:id1, :id2)', { | ||
id1: 1, | ||
id2: 2 | ||
}, { | ||
lobMetaInfo: {} | ||
}, function (error, results) { | ||
assert.isNull(error); | ||
assert.equal(results.rowsAffected, 1); | ||
done(); | ||
}); | ||
}); | ||
it('multiple lobs', function (done) { | ||
var connection = {}; | ||
Connection.extend(connection); | ||
var lobsWritten = 0; | ||
connection.execute = function (sql, bindVars, options, callback) { | ||
assert.equal(sql, 'INSERT INTO mylobs (id, c1, c2, b) VALUES (:id, EMPTY_CLOB(), EMPTY_CLOB(), EMPTY_CLOB()) RETURNING c1, c2, b INTO :lob1, :lob2, :lob3'); | ||
assert.deepEqual(bindVars, { | ||
id: 1, | ||
lob1: { | ||
type: constants.clobType, | ||
dir: constants.bindOut | ||
}, | ||
lob2: { | ||
type: constants.clobType, | ||
dir: constants.bindOut | ||
}, | ||
lob3: { | ||
type: constants.blobType, | ||
dir: constants.bindOut | ||
} | ||
}); | ||
assert.deepEqual(options, { | ||
lobMetaInfo: { | ||
c1: 'lob1', | ||
c2: 'lob2', | ||
b: 'lob3' | ||
} | ||
}); | ||
var lob1 = helper.createCLOB(); | ||
lob1.testData = 'clob text'; | ||
lob1.once('end', function () { | ||
lobsWritten++; | ||
}); | ||
var lob2 = helper.createCLOB(); | ||
lob2.testData = 'second clob text'; | ||
lob2.once('end', function () { | ||
lobsWritten++; | ||
}); | ||
var lob3 = helper.createBLOB(); | ||
lob3.testData = 'binary data'; | ||
lob3.once('end', function () { | ||
lobsWritten++; | ||
}); | ||
callback(null, { | ||
rowsAffected: 1, | ||
outBinds: { | ||
lob1: [lob1], | ||
lob2: [lob2], | ||
lob3: [lob3] | ||
} | ||
}); | ||
}; | ||
connection.insert('INSERT INTO mylobs (id, c1, c2, b) VALUES (:id, EMPTY_CLOB(), EMPTY_CLOB(), EMPTY_CLOB())', { | ||
id: 1, | ||
lob1: 'clob text', | ||
lob2: 'second clob text', | ||
lob3: new Buffer('binary data') | ||
}, { | ||
lobMetaInfo: { | ||
c1: 'lob1', | ||
c2: 'lob2', | ||
b: 'lob3' | ||
} | ||
}, function (error, results) { | ||
assert.isNull(error); | ||
assert.equal(results.rowsAffected, 1); | ||
assert.equal(lobsWritten, 3); | ||
done(); | ||
}); | ||
}); | ||
it('error while writing lobs', function (done) { | ||
var connection = {}; | ||
Connection.extend(connection); | ||
connection.execute = function (sql, bindVars, options, callback) { | ||
assert.equal(sql, 'INSERT INTO mylobs (id, c1, c2, b) VALUES (:id, EMPTY_CLOB(), EMPTY_CLOB(), EMPTY_CLOB()) RETURNING c1, c2, b INTO :lob1, :lob2, :lob3'); | ||
assert.deepEqual(bindVars, { | ||
id: 1, | ||
lob1: { | ||
type: constants.clobType, | ||
dir: constants.bindOut | ||
}, | ||
lob2: { | ||
type: constants.clobType, | ||
dir: constants.bindOut | ||
}, | ||
lob3: { | ||
type: constants.blobType, | ||
dir: constants.bindOut | ||
} | ||
}); | ||
assert.deepEqual(options, { | ||
lobMetaInfo: { | ||
c1: 'lob1', | ||
c2: 'lob2', | ||
b: 'lob3' | ||
} | ||
}); | ||
var lobsWritten; | ||
var lob1 = helper.createCLOB(); | ||
lob1.testData = 'clob text'; | ||
var lob2 = helper.createCLOB(); | ||
lob2.testData = 'second clob text'; | ||
lob2.end = function (data, encoding, cb) { | ||
lob2.emit('error', new Error('lob test error')); | ||
setTimeout(cb, 10); | ||
}; | ||
var lob3 = helper.createBLOB(); | ||
lob3.testData = 'binary data'; | ||
callback(null, { | ||
rowsAffected: 1, | ||
outBinds: { | ||
lob1: [lob1], | ||
lob2: [lob2], | ||
lob3: [lob3] | ||
} | ||
}); | ||
}; | ||
connection.insert('INSERT INTO mylobs (id, c1, c2, b) VALUES (:id, EMPTY_CLOB(), EMPTY_CLOB(), EMPTY_CLOB())', { | ||
id: 1, | ||
lob1: 'clob text', | ||
lob2: 'second clob text', | ||
lob3: new Buffer('binary data') | ||
}, { | ||
lobMetaInfo: { | ||
c1: 'lob1', | ||
c2: 'lob2', | ||
b: 'lob3' | ||
} | ||
}, function (error, results) { | ||
assert.isDefined(error); | ||
assert.equal(error.message, 'lob test error'); | ||
assert.isUndefined(results); | ||
done(); | ||
}); | ||
}); | ||
it('error on execute', function (done) { | ||
var connection = {}; | ||
Connection.extend(connection); | ||
connection.execute = function (sql, bindVars, options, callback) { | ||
assert.equal(sql, 'INSERT INTO mylobs (id, c1, c2, b) VALUES (:id, EMPTY_CLOB(), EMPTY_CLOB(), EMPTY_CLOB()) RETURNING c1, c2, b INTO :lob1, :lob2, :lob3'); | ||
assert.deepEqual(bindVars, { | ||
id: 1, | ||
lob1: { | ||
type: constants.clobType, | ||
dir: constants.bindOut | ||
}, | ||
lob2: { | ||
type: constants.clobType, | ||
dir: constants.bindOut | ||
}, | ||
lob3: { | ||
type: constants.blobType, | ||
dir: constants.bindOut | ||
} | ||
}); | ||
assert.deepEqual(options, { | ||
lobMetaInfo: { | ||
c1: 'lob1', | ||
c2: 'lob2', | ||
b: 'lob3' | ||
} | ||
}); | ||
callback(new Error('execute error test')); | ||
}; | ||
connection.insert('INSERT INTO mylobs (id, c1, c2, b) VALUES (:id, EMPTY_CLOB(), EMPTY_CLOB(), EMPTY_CLOB())', { | ||
id: 1, | ||
lob1: 'clob text', | ||
lob2: 'second clob text', | ||
lob3: new Buffer('binary data') | ||
}, { | ||
lobMetaInfo: { | ||
c1: 'lob1', | ||
c2: 'lob2', | ||
b: 'lob3' | ||
} | ||
}, function (error, results) { | ||
assert.isDefined(error); | ||
assert.equal(error.message, 'execute error test'); | ||
assert.isUndefined(results); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('release', function () { | ||
@@ -290,0 +526,0 @@ it('callback provided', function (done) { |
@@ -10,2 +10,4 @@ 'use strict'; | ||
oracledb.BLOB = 2007; | ||
oracledb.CLOB = 2006; | ||
oracledb.BIND_OUT = 3003; | ||
var simpleOracleDB = require('../../'); | ||
@@ -12,0 +14,0 @@ simpleOracleDB.extend(oracledb); |
@@ -92,2 +92,100 @@ 'use strict'; | ||
}); | ||
describe('write Tests', function () { | ||
it('write string test', function (done) { | ||
var writable = new EventEmitter(); | ||
writable.end = function (data, encoding, callback) { | ||
assert.equal(data, 'TEST STRING DATA'); | ||
assert.equal(encoding, 'utf8'); | ||
assert.isFunction(callback); | ||
callback(); | ||
}; | ||
stream.write(writable, 'TEST STRING DATA', function (error) { | ||
assert.isUndefined(error); | ||
assert.equal(0, writable.listeners('error').length); | ||
done(); | ||
}); | ||
}); | ||
it('write Buffer test', function (done) { | ||
var writable = new EventEmitter(); | ||
writable.end = function (data, callback) { | ||
assert.deepEqual(data, new Buffer('TEST STRING DATA')); | ||
assert.isFunction(callback); | ||
callback(); | ||
}; | ||
stream.write(writable, new Buffer('TEST STRING DATA'), function (error) { | ||
assert.isUndefined(error); | ||
assert.equal(0, writable.listeners('error').length); | ||
done(); | ||
}); | ||
}); | ||
it('write null data test', function (done) { | ||
var writable = new EventEmitter(); | ||
writable.end = function () { | ||
assert.fail(); | ||
}; | ||
stream.write(writable, null, function (error) { | ||
assert.isUndefined(error); | ||
assert.equal(0, writable.listeners('error').length); | ||
done(); | ||
}); | ||
}); | ||
it('write undefined data test', function (done) { | ||
var writable = new EventEmitter(); | ||
writable.end = function () { | ||
assert.fail(); | ||
}; | ||
stream.write(writable, undefined, function (error) { | ||
assert.isUndefined(error); | ||
assert.equal(0, writable.listeners('error').length); | ||
done(); | ||
}); | ||
}); | ||
it('write null writable test', function (done) { | ||
stream.write(null, 'data', function (error) { | ||
assert.isUndefined(error); | ||
done(); | ||
}); | ||
}); | ||
it('write undefined writable test', function (done) { | ||
stream.write(undefined, 'data', function (error) { | ||
assert.isUndefined(error); | ||
done(); | ||
}); | ||
}); | ||
it('write error test', function (done) { | ||
var writable = new EventEmitter(); | ||
writable.end = function (data, encoding, callback) { | ||
writable.emit('error', new Error('test')); | ||
setTimeout(callback, 10); | ||
}; | ||
stream.write(writable, 'TEST STRING DATA', function (error) { | ||
assert.isDefined(error); | ||
assert.equal(error.message, 'test'); | ||
assert.equal(0, writable.listeners('error').length); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
165745
33
3516
185