Skip to content

Commit

Permalink
Merge pull request #1309 from tediousjs/fix-873
Browse files Browse the repository at this point in the history
fix: fix handling of `textsize` connection option
  • Loading branch information
arthurschreiber committed Aug 5, 2021
2 parents 3fa7d87 + ebe75f1 commit 3ecb650
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const DEFAULT_PACKET_SIZE = 4 * 1024;
/**
* @private
*/
const DEFAULT_TEXTSIZE = '2147483647';
const DEFAULT_TEXTSIZE = 2147483647;
/**
* @private
*/
Expand Down Expand Up @@ -379,7 +379,7 @@ export interface InternalConnectionOptions {
serverName: undefined | string;
serverSupportsColumnEncryption: boolean;
tdsVersion: string;
textsize: string;
textsize: number;
trustedServerNameAE?: string;
trustServerCertificate: boolean;
useColumnNames: boolean;
Expand Down Expand Up @@ -1644,7 +1644,13 @@ class Connection extends EventEmitter {
throw new TypeError('The "config.options.textsize" property must be of type number or null.');
}

this.config.options.textsize = config.options.textsize;
if (config.options.textsize > 2147483647) {
throw new TypeError('The "config.options.textsize" can\'t be greater than 2147483647.');
} else if (config.options.textsize < -1) {
throw new TypeError('The "config.options.textsize" can\'t be smaller than -1.');
}

this.config.options.textsize = config.options.textsize | 0;
}

if (config.options.trustServerCertificate !== undefined) {
Expand Down
188 changes: 188 additions & 0 deletions test/integration/connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,194 @@ describe('Language Insert Test', function() {
});
});

describe('custom textsize value', function() {
it('should set the textsize to the given value', function(done) {
const config = getConfig();
config.options.textsize = 123456;

const connection = new Connection(config);
connection.connect((err) => {
/**
* @type {number | undefined}
*/
let textsize;

const request = new Request('SELECT @@TEXTSIZE', () => {
if (err) {
return done(err);
}

assert.strictEqual(textsize, 123456);

connection.close();

done();
});

request.on('row', (row) => {
textsize = row[0].value;
});

connection.execSql(request);
});
});

it('should fail if the textsize is below -1', function() {
const config = getConfig();
config.options.textsize = -2;

assert.throws(() => {
new Connection(config);
}, TypeError, 'The "config.options.textsize" can\'t be smaller than -1.');
});

it('should fail if the textsize is above 2147483647', function() {
const config = getConfig();
config.options.textsize = 2147483648;

assert.throws(() => {
new Connection(config);
}, TypeError, 'The "config.options.textsize" can\'t be greater than 2147483647.');
});

it('should fail if the textsize is not a number', function() {
const config = getConfig();
config.options.textsize = 'textSize';

assert.throws(() => {
new Connection(config);
}, TypeError, 'The "config.options.textsize" property must be of type number or null.');
});

it('should default to 2147483647', function(done) {
const config = getConfig();
config.options.textsize = undefined;

const connection = new Connection(config);
connection.connect((err) => {
/**
* @type {number | undefined}
*/
let textsize;

const request = new Request('SELECT @@TEXTSIZE', () => {
if (err) {
return done(err);
}

assert.strictEqual(textsize, 2147483647);

connection.close();

done();
});

request.on('row', (row) => {
textsize = row[0].value;
});

connection.execSql(request);
});
});

it('should allow setting it to -1', function(done) {
const config = getConfig();
config.options.textsize = -1;

const connection = new Connection(config);
connection.connect((err) => {
/**
* @type {number | undefined}
*/
let textsize;

const request = new Request('SELECT @@TEXTSIZE', () => {
if (err) {
return done(err);
}

if (connection.config.options.tdsVersion <= '7_2') {
assert.strictEqual(textsize, 2147483647);
} else {
assert.strictEqual(textsize, -1);
}

connection.close();

done();
});

request.on('row', (row) => {
textsize = row[0].value;
});

connection.execSql(request);
});
});

it('should allow setting it to 0 and reset to server defaults', function(done) {
const config = getConfig();
config.options.textsize = 0;

const connection = new Connection(config);
connection.connect((err) => {
/**
* @type {number | undefined}
*/
let textsize;

const request = new Request('SELECT @@TEXTSIZE', () => {
if (err) {
return done(err);
}

assert.strictEqual(textsize, 4096);

connection.close();

done();
});

request.on('row', (row) => {
textsize = row[0].value;
});

connection.execSql(request);
});
});

it('truncates floating point numbers', function(done) {
const config = getConfig();
config.options.textsize = 1000.0123;

const connection = new Connection(config);
connection.connect((err) => {
/**
* @type {number | undefined}
*/
let textsize;

const request = new Request('SELECT @@TEXTSIZE', () => {
if (err) {
return done(err);
}

assert.strictEqual(textsize, 1000);

connection.close();

done();
});

request.on('row', (row) => {
textsize = row[0].value;
});

connection.execSql(request);
});
});
});

describe('should test date format', function() {
/**
* @param {Mocha.Done} done
Expand Down

0 comments on commit 3ecb650

Please sign in to comment.