Skip to content

Commit

Permalink
Merge pull request #1350 from tediousjs/arthur/do-not-double-validate
Browse files Browse the repository at this point in the history
fix: prevent bulkload values from being validated twice
  • Loading branch information
arthurschreiber authored Sep 28, 2021
2 parents 11b3e5f + 4017c39 commit 47aafd1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/bulk-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ class RowTransform extends Transform {
const c = this.columns[i];
let value = Array.isArray(row) ? row[i] : row[c.objName];

try {
value = c.type.validate(value, c.collation);
} catch (error: any) {
return callback(error);
if (!this.bulkLoad.firstRowWritten) {
try {
value = c.type.validate(value, c.collation);
} catch (error: any) {
return callback(error);
}
}

const parameter = {
Expand Down Expand Up @@ -699,6 +701,7 @@ class BulkLoad extends EventEmitter {
if (this.executionStarted) {
throw new Error('BulkLoad cannot be switched to streaming mode after execution has started.');
}

this.streamingMode = true;

return this.rowToPacketTransform;
Expand Down
33 changes: 33 additions & 0 deletions test/integration/collation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,39 @@ describe('Database Collation Support', function() {
{ one: '中文', two: '中文', three: '中文' }
]);
});

it('encodes values with the current database encoding (`addRow`)', function(done) {
const bulkLoad = connection.newBulkLoad('collation_test', (err) => {
if (err) {
return done(err);
}

let values: [string, string, string];
const request = new Request('SELECT * FROM collation_test', (err) => {
if (err) {
return done(err);
}

assert.deepEqual(values, ['中文', '中文 ', '中文']);

done();
});

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

connection.execSql(request);
});

bulkLoad.addColumn('one', TYPES.VarChar, { length: 255, nullable: false });
bulkLoad.addColumn('two', TYPES.Char, { length: 10, nullable: false });
bulkLoad.addColumn('three', TYPES.Text, { nullable: false });

bulkLoad.addRow({ one: '中文', two: '中文', three: '中文' });

connection.execBulkLoad(bulkLoad);
});
});

describe('TVP parameter', function() {
Expand Down

0 comments on commit 47aafd1

Please sign in to comment.