diff --git a/index.d.ts b/index.d.ts index 7a87ba2..7ba52bd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,7 +13,7 @@ declare module 'clickhouse' { } export class WriteStream extends Stream.Transform { - writeRow(data: Array): Promise; + writeRow(data: Array | string): Promise; exec(): Promise<{}>; } diff --git a/index.js b/index.js index 899edb1..9e561b9 100644 --- a/index.js +++ b/index.js @@ -285,7 +285,9 @@ class Rs extends Transform { writeRow(data) { let row = ''; - if (Array.isArray(data)) { + if (typeof data === 'string') { + row = data; + } else if (Array.isArray(data)) { row = ClickHouse.mapRowAsArray(data); } else if (isObject(data)) { throw new Error('Error: Inserted data must be an array, not an object.'); @@ -399,7 +401,9 @@ class QueryCursor { fieldList = [], isFirstElObject = false; - if (Array.isArray(data) && Array.isArray(data[0])) { + if(Array.isArray(data) && data.every(d => typeof d === 'string')) { + values = data; + } else if (Array.isArray(data) && Array.isArray(data[0])) { values = data; } else if (Array.isArray(data) && isObject(data[0])) { values = data; @@ -421,6 +425,9 @@ class QueryCursor { } return values.map(row => { + if (typeof row === 'string') { + return row; + } if (isFirstElObject) { return ClickHouse.mapRowAsObject(fieldList, row); } else { @@ -515,10 +522,12 @@ class QueryCursor { } } else if (me.isInsert) { if (query.match(/values/i)) { - // + if (data && data.every(d => typeof d === 'string')) { + params['body'] = me._getBodyForInsert(); + } } else { query += ' FORMAT TabSeparated'; - + if (data) { params['body'] = me._getBodyForInsert(); } diff --git a/test/test.js b/test/test.js index 734a315..176805f 100644 --- a/test/test.js +++ b/test/test.js @@ -466,7 +466,63 @@ describe('queries', () => { ).toPromise(); expect(r2).to.be.ok(); }); + + it('insert field as raw string', async () => { + clickhouse.sessionId = Date.now(); + + const r = await clickhouse.query(` + CREATE TABLE IF NOT EXISTS test_raw_string ( + date Date, + str String, + arr Array(String), + arr2 Array(Date), + arr3 Array(UInt8), + fixedStr String + ) ENGINE=MergeTree(date, date, 8192) + `).toPromise(); + expect(r).to.be.ok(); + + const rows = [ + '(\'2018-01-01 10:00:00\',\'Вам, проживающим за оргией оргию,\',[],[\'1915-01-02 10:00:00\',\'1915-01-03 10:00:00\'],[1,2,3,4,5],unhex(\'60ed56e75bb93bd353267faa\'))', + '(\'2018-02-01 10:00:00\',\'имеющим ванную и теплый клозет!\',[\'5670000000\',\'asdas dasf\'],[\'1915-02-02 10:00:00\'],[],unhex(\'60ed56f4a88cd5dcb249d959\'))' + ]; + + const r2 = await clickhouse.insert( + 'INSERT INTO test_raw_string (date, str, arr, arr2, arr3, fixedStr) VALUES', + rows + ).toPromise(); + expect(r2).to.be.ok(); + }); + it('insert stream accept raw string', async () => { + clickhouse.sessionId = Date.now(); + + const r = await clickhouse.query(` + CREATE TABLE IF NOT EXISTS test_insert_stream_raw_string ( + date Date, + str String, + arr Array(String), + arr2 Array(Date), + arr3 Array(UInt8), + fixedStr FixedString(12) + ) ENGINE=MergeTree(date, date, 8192) + `).toPromise(); + expect(r).to.be.ok(); + + const rows = [ + '(\'2018-01-01 10:00:00\',\'Вам, проживающим за оргией оргию,\',[],[\'1915-01-02 10:00:00\',\'1915-01-03 10:00:00\'],[1,2,3,4,5],unhex(\'60ed56e75bb93bd353267faa\'))', + '(\'2018-02-01 10:00:00\',\'имеющим ванную и теплый клозет!\',[\'5670000000\',\'asdas dasf\'],[\'1915-02-02 10:00:00\'],[],unhex(\'60ed56f4a88cd5dcb249d959\'))' + ]; + + const stream = await clickhouse.insert( + 'INSERT INTO test_insert_stream_raw_string (date, str, arr, arr2, arr3, fixedStr) VALUES', + ).stream(); + stream.writeRow(rows[0]); + stream.writeRow(rows[1]); + const r2 = await stream.exec(); + expect(r2).to.be.ok(); + }); + it('select, insert and two pipes', async () => { const result = await clickhouse.query('DROP TABLE IF EXISTS session_temp').toPromise(); expect(result).to.be.ok();