Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for BINTEXT and BOOLEAN #254

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/protocol/Reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@ Reader.prototype.readAlphanum = function readAlphanum() {
return this.readBytes(false, true);
}

Reader.prototype.readBoolean = function readBoolean() {
var value = this.buffer[this.offset++];
switch (value) {
case 0x02:
return true;
case 0x01:
return null;
default:
return false;
}
}

Reader.LobDescriptor = LobDescriptor;

function LobDescriptor(type, options, charLength, byteLength, locatorId, chunk, defaultType) {
Expand Down
1 change: 1 addition & 0 deletions lib/protocol/Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ function isLob(column) {
case TypeCode.NCLOB:
case TypeCode.NLOCATOR:
case TypeCode.TEXT:
case TypeCode.BINTEXT:
return true;
default:
return false;
Expand Down
1 change: 1 addition & 0 deletions lib/protocol/ResultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ function isLob(column) {
case TypeCode.NCLOB:
case TypeCode.NLOCATOR:
case TypeCode.TEXT:
case TypeCode.BINTEXT:
return true;
default:
return false;
Expand Down
29 changes: 29 additions & 0 deletions lib/protocol/Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,35 @@ Writer.prototype[TypeCode.ST_GEOMETRY] = function writeST_GEOMETRY(value) {
}
}

Writer.prototype[TypeCode.BOOLEAN] = function writeBoolean(value) {
var buffer = new Buffer(2);
buffer[0] = TypeCode.BOOLEAN;
// 0x02 - True, 0x01 - Null, 0x00 - False
if (value === null) {
buffer[1] = 0x01;
} else if (util.isString(value)) {
if (value.toUpperCase() === 'TRUE' || value === '1') {
buffer[1] = 0x02;
} else if (value.toUpperCase() === 'FALSE' || value === '0') {
buffer[1] = 0x00;
} else if (value.toUpperCase() === 'UNKNOWN' || value.length === 0) {
buffer[1] = 0x01;
} else {
throw createInputError('BOOLEAN');
}
} else if (util.isNumber(value)) {
buffer[1] = value == 0 ? 0x00 : 0x02;
} else if (value === true) {
buffer[1] = 0x02;
} else if (value === false) {
buffer[1] = 0x00;
} else {
throw createInputError('BOOLEAN');
}

this.push(buffer);
}

function setChar(str, i, c) {
if(i >= str.length) return str;
return str.substring(0, i) + c + str.substring(i + 1);
Expand Down
4 changes: 3 additions & 1 deletion lib/protocol/common/DataFormatVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports = {
EXTENDED_DATE_TIME_SUPPORT: 3,
LEVEL4: 4,
LEVEL5: 5,
LEVEL6: 6,
LEVEL7: 7,
// Maximum data format version supported by this driver
MAX_VERSION: 5,
MAX_VERSION: 7,
};
2 changes: 2 additions & 0 deletions lib/protocol/common/NormalizedTypeCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ NormalizedTypeCode[TypeCode.SECONDDATE] = TypeCode.SECONDDATE;
// ST_GEOMETRY
NormalizedTypeCode[TypeCode.ST_GEOMETRY] = TypeCode.ST_GEOMETRY;
NormalizedTypeCode[TypeCode.ST_POINT] = TypeCode.ST_GEOMETRY;
// Boolean
NormalizedTypeCode[TypeCode.BOOLEAN] = TypeCode.BOOLEAN;
3 changes: 3 additions & 0 deletions lib/protocol/common/ReadFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var READ_DOUBLE = 'readDouble';
var READ_FLOAT = 'readFloat';
var READ_DECIMAL = 'readDecimal';
var READ_ALPHANUM = 'readAlphanum';
var READ_BOOLEAN = 'readBoolean';

ReadFunction[TypeCode.TINYINT] = READ_TINYINT;
ReadFunction[TypeCode.SMALLINT] = READ_SMALLINT;
Expand Down Expand Up @@ -67,8 +68,10 @@ ReadFunction[TypeCode.CLOB] = READ_CLOB;
ReadFunction[TypeCode.NCLOB] = READ_NCLOB;
ReadFunction[TypeCode.NLOCATOR] = READ_NCLOB;
ReadFunction[TypeCode.TEXT] = READ_NCLOB;
ReadFunction[TypeCode.BINTEXT] = READ_NCLOB;
ReadFunction[TypeCode.DOUBLE] = READ_DOUBLE;
ReadFunction[TypeCode.REAL] = READ_FLOAT;
ReadFunction[TypeCode.DECIMAL] = READ_DECIMAL;
ReadFunction[TypeCode.ST_GEOMETRY] = READ_BINARY;
ReadFunction[TypeCode.ST_POINT] = READ_BINARY;
ReadFunction[TypeCode.BOOLEAN] = READ_BOOLEAN;
2 changes: 1 addition & 1 deletion lib/protocol/common/TypeCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = {
UNUSED12: 50,
TEXT: 51,
SHORTTEXT: 52,
UNUSED15: 53,
BINTEXT: 53,
UNUSED16: 54,
ALPHANUM: 55,
UNUSED18: 56,
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/part/ConnectOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ function ConnectOptions() {
// Support for ALPHANUM, TEXT, SHORTTEXT, LONGDATE, SECONDDATE,
// DAYDATE, and SECONDTIME.
// * `5` Support for ST_GEOMETRY and ST_POINT
// * `6` Specifies to send the data type BINTEXT to the client
// * `7` Support for BOOLEAN
// TODO: Once implementation and testing for data format version 4 are
// complete, replace dataFormatVersion and dataFormatVersion2 with the
// commented-out lines.
Expand Down
69 changes: 67 additions & 2 deletions test/acceptance/db.DataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

var async = require('async');
// Set the data format version necessary for the data types
var db = require('../db')({dataFormatSupport: 5});
var db = require('../db')({dataFormatSupport: 7});
var RemoteDB = require('../db/RemoteDB');
var lorem = require('../fixtures/lorem');
var util = require('../../lib/util');
Expand Down Expand Up @@ -307,7 +307,7 @@ describe('db', function () {
});
});

describeRemoteDB('ALPHANUM, TEXT, SHORTTEXT (only tested on on-premise HANA)', function () {
describeRemoteDB('ALPHANUM, TEXT, SHORTTEXT, BINTEXT (only tested on on-premise HANA)', function () {
var skipTests = false;
before(function (done) {
var version = db.getHANAFullVersion();
Expand Down Expand Up @@ -470,6 +470,34 @@ describe('db', function () {
async.each(invalidTestData, testDataTypeError.bind(null, 'SHORTTEXT_TABLE'), done);
});
});

describe('BINTEXT', function () {
beforeEach(setUpTable('BINTEXT_TABLE', ['A BINTEXT'], 6));
afterEach(dropTable('BINTEXT_TABLE', 6));

it('should insert and return valid text via callback', function (done) {
var insertValues = [
[Buffer.from("Here is a regular string", "utf-8")],
[Buffer.alloc(0)],
[null],
];
var expected = [{A: null}, {A: Buffer.alloc(0)}, {A: Buffer.from("Here is a regular string", "utf-8")}];
testDataTypeValid('BINTEXT_TABLE', insertValues, 53, expected, done);
});

it('should insert binary data', function (done) {
var expected = [{A: Buffer.from("6162636465666768696a6b6c6d6e6fc3a1", "hex")}];
function insert(cb) {
client.exec("INSERT INTO BINTEXT_TABLE VALUES(x'6162636465666768696a6b6c6d6e6fc3a1')", function (err, rowsAffected) {
if (err) {
done(err);
}
cb();
});
}
async.waterfall([insert, validateDataSql.bind(null, "select * from BINTEXT_TABLE", 53, expected)], done);
});
});
});

describeRemoteDB('Spatial', function () {
Expand Down Expand Up @@ -721,4 +749,41 @@ describe('db', function () {
});
});
});

describeRemoteDB('BOOLEAN', function () {
before(setUpTableRemoteDB('BOOLEAN_TABLE', ['A BOOLEAN'], 7));
after(dropTableRemoteDB('BOOLEAN_TABLE', 7));

it('should add valid booleans using different parameter types', function (done) {
var insertValues = [
[true],
[null],
[false],
[1],
[10.5],
[0],
['TRUE'],
['FAlsE'],
['UNknOwn'],
['1'],
['0'],
[''],
];
// 3 null, 4 false, 5 true
var expected = [];
for (let i = 0; i < 3; i++) expected.push({A: null});
for (let i = 0; i < 4; i++) expected.push({A: false});
for (let i = 0; i < 5; i++) expected.push({A: true});
testDataTypeValid('BOOLEAN_TABLE', insertValues, 28, expected, done);
});

it('should raise input type error', function (done) {
var invalidValues = ['String not boolean', Buffer.from("01", "hex")];
// Add the same expected error message to the values
var invalidTestData = invalidValues.map(function (testValue) {
return {value: testValue, errMessage: "Cannot set parameter at row: 1. Wrong input for BOOLEAN type"};
});
async.each(invalidTestData, testDataTypeError.bind(null, 'BOOLEAN_TABLE'), done);
});
});
});
31 changes: 27 additions & 4 deletions test/fixtures/parametersData.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ exports.ALL_TYPES = {
'3E6DB3C0DB0E000000' +
'40EDB00000' +
'3DBFB3AF519B36DB08' +
// offset 195
'1b06c8000000e2000000' +
'19060b000000aa010000' +
'1a060b000000b5010000',
'1C02' +
// offset 197
'1b06c8000000e4000000' +
'19060b000000ac010000' +
'1a060b000000b7010000',
'hex'), blob, clob, nclob])
},
types: [
Expand Down Expand Up @@ -141,6 +142,7 @@ exports.ALL_TYPES = {
TypeCode.SECONDDATE,
TypeCode.SECONDTIME,
TypeCode.LONGDATE,
TypeCode.BOOLEAN,
TypeCode.BLOB,
TypeCode.CLOB,
TypeCode.NCLOB
Expand Down Expand Up @@ -175,6 +177,7 @@ exports.ALL_TYPES = {
'2023-04-04 12:34:52',
'12:34:52',
'2023-04-04 12:34:52.1357246',
true,
blob,
clob,
nclob
Expand Down Expand Up @@ -496,3 +499,23 @@ exports.DATETIME = {
'00:00:00'
]
};

exports.BOOLEAN = {
part: {
argumentCount: 1,
buffer: new Buffer(
'1C02' +
'9C' +
'1C00', 'hex')
},
types: [
TypeCode.BOOLEAN,
TypeCode.BOOLEAN,
TypeCode.BOOLEAN,
],
values: [
true,
null,
false
]
};
13 changes: 13 additions & 0 deletions test/lib.Reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@ describe('Lib', function () {
reader.readAlphanum().should.equal('123456');
reader.hasMore().should.equal(false);
});

it('should read a Boolean', function () {
var buffer = new Buffer([
0x01,
0x00,
0x02,
]);
var reader = new lib.Reader(buffer);
(reader.readBoolean() === null).should.be.ok;
reader.readBoolean().should.equal(false);
reader.readBoolean().should.equal(true);
reader.hasMore().should.equal(false);
});
});

it('should read a BLob', function () {
Expand Down
22 changes: 22 additions & 0 deletions test/lib.Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ describe('Lib', function () {
});
});

it('should write boolean type', function (done) {
var test = data.BOOLEAN;
var writer = Writer.create(test);
writer.getParameters(SIZE, function (err, buffer) {
if (err) {
return done(err);
}
buffer.should.eql(test.part.buffer);
done();
});
});

it('should get WriteLobRequest', function (done) {
var writer = new Writer([TypeCode.BLOB]);
var stream = new lib.util.stream.Readable();
Expand Down Expand Up @@ -592,6 +604,16 @@ describe('Lib', function () {
Writer.prototype.setValues.bind(writer, [false]).should.throw();
});

it('should raise wrong input type error for BINTEXT', function () {
var writer = new Writer([TypeCode.BINTEXT]);
Writer.prototype.setValues.bind(writer, [false]).should.throw();
});

it('should raise wrong input type error for BOOLEAN', function () {
var writer = new Writer([TypeCode.BOOLEAN]);
Writer.prototype.setValues.bind(writer, [Buffer.from("01", "hex")]).should.throw();
});

});

});