mirror of
https://github.com/lingble/clickhouse.git
synced 2025-11-02 12:08:04 +00:00
Merge pull request #90 from francescorivola/feature/raw-insert-support
Provide raw string support in insert function
This commit is contained in:
2
index.d.ts
vendored
2
index.d.ts
vendored
@@ -13,7 +13,7 @@ declare module 'clickhouse' {
|
||||
}
|
||||
|
||||
export class WriteStream extends Stream.Transform {
|
||||
writeRow(data: Array<any>): Promise<void>;
|
||||
writeRow(data: Array<any> | string): Promise<void>;
|
||||
exec(): Promise<{}>;
|
||||
}
|
||||
|
||||
|
||||
17
index.js
17
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();
|
||||
}
|
||||
|
||||
56
test/test.js
56
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();
|
||||
|
||||
Reference in New Issue
Block a user