From 4017c39e11b17d44f6a09f21e9a18e39dd86613a Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Tue, 28 Sep 2021 15:37:28 +0000 Subject: [PATCH] fix: prevent bulkload values from being validated twice --- src/bulk-load.ts | 11 ++++++---- test/integration/collation-test.ts | 33 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/bulk-load.ts b/src/bulk-load.ts index e68939528..379168869 100644 --- a/src/bulk-load.ts +++ b/src/bulk-load.ts @@ -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 = { @@ -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; diff --git a/test/integration/collation-test.ts b/test/integration/collation-test.ts index 146940c35..24faea805 100644 --- a/test/integration/collation-test.ts +++ b/test/integration/collation-test.ts @@ -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() {