Skip to content

Commit 4928143

Browse files
author
Suraiya Hameed
committed
feat: add flag parsing and tests for returnvalue token
1 parent afdb800 commit 4928143

File tree

3 files changed

+47
-92
lines changed

3 files changed

+47
-92
lines changed

src/tokens/returnvalue/index.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ class ReturnValueToken extends Token {
88
paramName: ?string
99
status: ?number
1010
userType: ?number
11-
// TODO: parser flag
12-
flags: {
13-
nullable: ?boolean,
14-
caseSensitive: ?boolean,
15-
updateable: ?boolean,
16-
identity: ?boolean,
17-
computed: ?boolean,
18-
reservedODBC: ?boolean,
19-
fixedLenCLRType: ?boolean,
20-
encrypted: ?boolean
21-
}
2211
typeInfo: ?TypeInfo
2312
valueLength: ?number
2413
value: ?any
@@ -30,16 +19,6 @@ class ReturnValueToken extends Token {
3019
this.paramName = undefined;
3120
this.status = undefined;
3221
this.userType = undefined;
33-
this.flags = {
34-
nullable: undefined,
35-
caseSensitive: undefined,
36-
updateable: undefined,
37-
identity: undefined,
38-
computed: undefined,
39-
reservedODBC: undefined,
40-
fixedLenCLRType: undefined,
41-
encrypted: undefined
42-
};
4322
this.typeInfo = undefined;
4423
this.valueLength = undefined;
4524
this.value = undefined;

src/tokens/returnvalue/read.js

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,48 +59,16 @@ function parseUserType_7_2(reader: Reader) {
5959
}
6060

6161
function parseFlags(reader: Reader) {
62-
if (reader.version < 0x72090002) {
63-
return parseFlags_7_0;
64-
} else if (reader.version < 0x74000004) {
65-
return parseFlags_7_2;
66-
} else {
67-
return parseFlags_7_4;
68-
}
69-
}
70-
71-
72-
function parseFlags_7_0(reader: Reader) {
62+
// for RETURNVALUE_TOKEN all the flags should be zero (TDS 2.2.7.18)
7363
if (!reader.bytesAvailable(2)) {
7464
return;
7565
}
7666

77-
// TODO: Implement flag parsing
7867
const flags = reader.readUInt16LE(0); // eslint-disable-line no-unused-vars
7968
reader.consumeBytes(2);
8069

81-
return parseTypeInfo;
82-
}
83-
84-
function parseFlags_7_2(reader: Reader) {
85-
if (!reader.bytesAvailable(2)) {
86-
return;
87-
}
88-
89-
// TODO: Implement flag parsing
90-
const flags = reader.readUInt16LE(0); // eslint-disable-line no-unused-vars
91-
reader.consumeBytes(2);
92-
93-
return parseTypeInfo;
94-
}
95-
96-
function parseFlags_7_4(reader: Reader) {
97-
if (!reader.bytesAvailable(2)) {
98-
return;
99-
}
100-
101-
// TODO: Implement flag parsing
102-
const flags = reader.readUInt16LE(0); // eslint-disable-line no-unused-vars
103-
reader.consumeBytes(2);
70+
if (0 != flags)
71+
throw new Error('Unknown flags in RETURNVALUE_TOKEN ');
10472

10573
return parseTypeInfo;
10674
}

test/returnValue-test.js

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ describe('Parsing a RETURNVALUE token', function() {
1515

1616
describe('in TDS 7.0 mode', function() {
1717

18-
let reader, data, paramOrdinal, paramName, status, userType, typeid, dataLength, value, offset, tempBuff;
18+
let reader, data, paramOrdinal, paramName, status, userType, flag, typeid, dataLength, value, offset, tempOffset, tempBuff;
1919

2020
before(function() {
2121
paramOrdinal = 1;
2222
paramName = '@count';
2323
status = 1;
2424
userType = 0;
25+
flag = 0;
2526
typeid = 0x26;
2627
value = 4;
27-
offset = 0;
28+
tempOffset = 0;
2829
tempBuff = Buffer.alloc(21);
2930
buildDataBuffer();
3031
});
3132

3233
beforeEach(function() {
3334
reader = new Reader(0x07000000);
35+
offset = tempOffset;
3436
});
3537

3638
function addListners(done, token) {
@@ -51,18 +53,17 @@ describe('Parsing a RETURNVALUE token', function() {
5153
}
5254

5355
function buildDataBuffer() {
54-
tempBuff.writeUInt8(0xAC, offset++);
55-
tempBuff.writeUInt16LE(paramOrdinal, offset);
56-
offset += 2;
57-
tempBuff.writeUInt8(paramName.length, offset++);
58-
tempBuff.write(paramName, offset, paramName.length * 2, 'ucs2');
59-
offset += paramName.length * 2;
60-
tempBuff.writeUInt8(status, offset++);
61-
tempBuff.writeUInt16LE(userType, offset);
62-
offset += 2;
63-
// Flag
64-
tempBuff.writeUInt16LE(0, offset);
65-
offset += 2;
56+
tempBuff.writeUInt8(0xAC, tempOffset++);
57+
tempBuff.writeUInt16LE(paramOrdinal, tempOffset);
58+
tempOffset += 2;
59+
tempBuff.writeUInt8(paramName.length, tempOffset++);
60+
tempBuff.write(paramName, tempOffset, paramName.length * 2, 'ucs2');
61+
tempOffset += paramName.length * 2;
62+
tempBuff.writeUInt8(status, tempOffset++);
63+
tempBuff.writeUInt16LE(userType, tempOffset);
64+
tempOffset += 2;
65+
tempBuff.writeUInt16LE(flag, tempOffset);
66+
tempOffset += 2;
6667
}
6768

6869
it('should parse the INTNTYPE(Int) token correctly', function(done) {
@@ -82,19 +83,44 @@ describe('Parsing a RETURNVALUE token', function() {
8283
addListners(done, token);
8384
reader.end(data);
8485
});
86+
87+
it('should throw exception on receiving non-zero flag', function(done) {
88+
dataLength = 4;
89+
90+
data = Buffer.alloc(28);
91+
tempBuff.copy(data, 0, 0, offset - 2);
92+
93+
// write non-zero flag
94+
data.writeUInt16LE(56, offset - 2);
95+
96+
// TYPE_INFO
97+
data.writeUInt8(typeid, offset++);
98+
data.writeUInt8(dataLength, offset++);
99+
100+
// TYPE_VARBYTE
101+
data.writeUInt8(dataLength, offset++);
102+
data.writeUInt32LE(value, offset);
103+
console.log('data ', data);
104+
const token = {};
105+
106+
addListners(done, token);
107+
assert.throw(() => reader.end(data), Error, 'Unknown flags in RETURNVALUE_TOKEN');
108+
done();
109+
});
85110
});
86111

87112
describe('in TDS 7.2 mode', function() {
88113

89114
describe('test INTNTYPE', function() {
90115

91-
let reader, data, paramOrdinal, paramName, status, userType, typeid, dataLength, value, offset, tempBuff, tempOffset;
116+
let reader, data, paramOrdinal, paramName, status, userType, flag, typeid, dataLength, value, offset, tempBuff, tempOffset;
92117

93118
before(function() {
94119
paramOrdinal = 1;
95120
paramName = '@count';
96121
status = 1;
97122
userType = 0;
123+
flag = 0;
98124
typeid = 0x26;
99125
value = 4;
100126
tempOffset = 0;
@@ -104,6 +130,7 @@ describe('Parsing a RETURNVALUE token', function() {
104130

105131
beforeEach(function() {
106132
reader = new Reader(0x72090002);
133+
offset = tempOffset;
107134
});
108135

109136
function addListners(done, token) {
@@ -133,14 +160,12 @@ describe('Parsing a RETURNVALUE token', function() {
133160
tempBuff.writeUInt8(status, tempOffset++);
134161
tempBuff.writeUInt32LE(userType, tempOffset);
135162
tempOffset += 4;
136-
// Flag
137-
tempBuff.writeUInt16LE(0, tempOffset);
163+
tempBuff.writeUInt16LE(flag, tempOffset);
138164
tempOffset += 2;
139165
}
140166

141167
it('should parse the INTNTYPE(Tinyint) token correctly', function(done) {
142168
dataLength = 1;
143-
offset = tempOffset;
144169

145170
data = Buffer.alloc(27);
146171
tempBuff.copy(data);
@@ -159,7 +184,6 @@ describe('Parsing a RETURNVALUE token', function() {
159184

160185
it('should parse the INTNTYPE(smallint) token correctly', function(done) {
161186
dataLength = 2;
162-
offset = tempOffset;
163187

164188
data = Buffer.alloc(28);
165189
tempBuff.copy(data);
@@ -179,7 +203,6 @@ describe('Parsing a RETURNVALUE token', function() {
179203

180204
it('should parse the INTNTYPE(Int) token correctly', function(done) {
181205
dataLength = 4;
182-
offset = tempOffset;
183206

184207
data = Buffer.alloc(30);
185208
tempBuff.copy(data);
@@ -198,7 +221,6 @@ describe('Parsing a RETURNVALUE token', function() {
198221

199222
it('should parse the INTNTYPE(Bigint) token correctly', function(done) {
200223
dataLength = 8;
201-
offset = tempOffset;
202224

203225
data = Buffer.alloc(34);
204226
tempBuff.copy(data);
@@ -220,7 +242,6 @@ describe('Parsing a RETURNVALUE token', function() {
220242
it('should parse the INTNTYPE(null) token correctly', function(done) {
221243
dataLength = 8;
222244
value = null;
223-
offset = tempOffset;
224245

225246
data = Buffer.alloc(26);
226247
tempBuff.copy(data);
@@ -253,7 +274,7 @@ describe('Parsing a RETURNVALUE token', function() {
253274

254275
beforeEach(function() {
255276
reader = new Reader(0x72090002);
256-
277+
offset = tempOffset;
257278
});
258279

259280

@@ -301,7 +322,6 @@ describe('Parsing a RETURNVALUE token', function() {
301322
it('should parse the NULLTYPE token correctly', function(done) {
302323
typeid = 0x1F;
303324
value = null;
304-
offset = tempOffset;
305325

306326
data = Buffer.alloc(24);
307327
tempBuff.copy(data);
@@ -318,7 +338,6 @@ describe('Parsing a RETURNVALUE token', function() {
318338
it('should parse the INT1TYPE/TintInt token correctly', function(done) {
319339
typeid = 0x30;
320340
value = 255;
321-
offset = tempOffset;
322341

323342
data = Buffer.alloc(25);
324343
tempBuff.copy(data);
@@ -337,7 +356,6 @@ describe('Parsing a RETURNVALUE token', function() {
337356
it('should parse the BITTYPE token correctly', function(done) {
338357
typeid = 0x32;
339358
value = false;
340-
offset = tempOffset;
341359

342360
data = Buffer.alloc(25);
343361
tempBuff.copy(data);
@@ -356,7 +374,6 @@ describe('Parsing a RETURNVALUE token', function() {
356374
it('should parse the INT2TYPE/SmallInt token correctly', function(done) {
357375
typeid = 0x34;
358376
value = 32767;
359-
offset = tempOffset;
360377

361378
data = Buffer.alloc(26);
362379
tempBuff.copy(data);
@@ -375,7 +392,6 @@ describe('Parsing a RETURNVALUE token', function() {
375392
it('should parse the INT4TYPE/Int token correctly', function(done) {
376393
typeid = 0x38;
377394
value = -2147483648;
378-
offset = tempOffset;
379395

380396
data = Buffer.alloc(28);
381397
tempBuff.copy(data);
@@ -395,7 +411,6 @@ describe('Parsing a RETURNVALUE token', function() {
395411
typeid = 0x7F;
396412
// value = -2147483648;
397413
value = 147483648;
398-
offset = tempOffset;
399414

400415
data = Buffer.alloc(32);
401416
tempBuff.copy(data);
@@ -420,7 +435,6 @@ describe('Parsing a RETURNVALUE token', function() {
420435
const days = 43225; // days since 1900-01-01
421436
const minutes = 763;
422437
value = new Date('2018-05-07T12:43:00.000Z');
423-
offset = tempOffset;
424438

425439
data = Buffer.alloc(28);
426440
tempBuff.copy(data);
@@ -445,7 +459,6 @@ describe('Parsing a RETURNVALUE token', function() {
445459
const days = 43225;
446460
const minutes = 763;
447461
value = new Date('2018-05-07T12:43:00.000');
448-
offset = tempOffset;
449462

450463
data = Buffer.alloc(28);
451464
tempBuff.copy(data);
@@ -466,7 +479,6 @@ describe('Parsing a RETURNVALUE token', function() {
466479
it('should parse the FLT4TYPE/Real token correctly', function(done) {
467480
typeid = 0x3B;
468481
value = 9654.2529296875;
469-
offset = tempOffset;
470482

471483
data = Buffer.alloc(28);
472484
tempBuff.copy(data);
@@ -485,7 +497,6 @@ describe('Parsing a RETURNVALUE token', function() {
485497
it('should parse the FLT8TYPE/Float token correctly', function(done) {
486498
typeid = 0x3E;
487499
value = 9654.2546456567565767644;
488-
offset = tempOffset;
489500

490501
data = Buffer.alloc(32);
491502
tempBuff.copy(data);
@@ -504,7 +515,6 @@ describe('Parsing a RETURNVALUE token', function() {
504515
it('should parse the MONEYTYPE/Money token correctly', function(done) {
505516
typeid = 0x3C;
506517
value = 922337203.5807;
507-
offset = tempOffset;
508518

509519
const TDS_value = value * 10000;
510520
data = Buffer.alloc(32);
@@ -525,7 +535,6 @@ describe('Parsing a RETURNVALUE token', function() {
525535
it('should parse the MONEY4TYPE/SmallMoney token correctly', function(done) {
526536
typeid = 0x7A;
527537
value = -214748.3647;
528-
offset = tempOffset;
529538

530539
const TDS_value = value * 10000;
531540
data = Buffer.alloc(28);
@@ -545,7 +554,6 @@ describe('Parsing a RETURNVALUE token', function() {
545554
it('should parse the DATETIMETYPE/DateTime token correctly', function(done) {
546555
reader.options = {};
547556
reader.options.useUTC = true;
548-
offset = tempOffset;
549557

550558
typeid = 0x3D;
551559
value = new Date('2004-05-23T14:25:10.487Z');

0 commit comments

Comments
 (0)