Skip to content

Commit fc31027

Browse files
authored
Merge fcfea16 into 71476c9
2 parents 71476c9 + fcfea16 commit fc31027

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Quote/StrictQuoteLine.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function quoteValue($row)
8888
// Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
8989
// single quotation marks with the same screening rules as above.
9090
// as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes.
91+
$value = $this->escapeDoubleQoutes($value);
9192
$result_array = FormatLine::Insert($value);
9293

9394
return $encodeArray . '[' . $result_array . ']' . $encodeArray;
@@ -103,5 +104,11 @@ public function quoteValue($row)
103104
return array_map($quote, $row);
104105
}
105106

107+
public function escapeDoubleQoutes(array $arr)
108+
{
109+
return array_map(function($v) {
110+
return is_string($v) ? str_replace('"', '""', $v) : $v;
111+
}, $arr);
112+
}
106113

107114
}

tests/StrictQuoteLineTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace ClickHouseDB\Tests;
4+
5+
use ClickHouseDB\Quote\StrictQuoteLine;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class StrictQuoteLineTest extends TestCase
9+
{
10+
use WithClient;
11+
12+
public function setUp()
13+
{
14+
$this->client->write('DROP TABLE IF EXISTS cities');
15+
$this->client->write('
16+
CREATE TABLE IF NOT EXISTS cities (
17+
date Date,
18+
city String,
19+
keywords Array(String),
20+
nums Array(UInt8)
21+
) ENGINE = MergeTree(date, (date), 8192)
22+
');
23+
parent::setUp();
24+
}
25+
26+
/**
27+
* @group test
28+
*/
29+
public function testQuoteValueCSV()
30+
{
31+
$strict = new StrictQuoteLine('CSV');
32+
33+
$rows = [
34+
['2018-04-01', '"That works"', ['\"That does not\"', 'That works'], [8, 7]],
35+
['2018-04-02', 'That works', ['\""That does not\""', '"\'\""That works"""\"'], [1, 0]],
36+
['2018-04-03', 'That works', ['\"\"That does not"\'""', '""""That works""""'], [9, 121]],
37+
];
38+
$fileName = $this->tmpPath . '__test_quote_value.csv';
39+
40+
@unlink($fileName);
41+
foreach ($rows as $row) {
42+
file_put_contents($fileName, $strict->quoteRow($row) . "\n", FILE_APPEND);
43+
}
44+
45+
$this->client->insertBatchFiles('cities', [$fileName], ['date', 'city', 'keywords', 'nums']);
46+
$statement = $this->client->select('SELECT * FROM cities');
47+
48+
$result = array_map('array_values', $statement->rows());
49+
foreach ($result as $key => $value) {
50+
// check correct quote string
51+
$this->assertEmpty(array_diff($rows[$key][2], $value[2]));
52+
$this->assertEmpty(array_diff($rows[$key][3], $value[3]));
53+
}
54+
55+
$rows[0][2][1] = 'Not the same string';
56+
$this->assertCount(1, array_diff($rows[0][2], $result[0][2]));
57+
}
58+
}

0 commit comments

Comments
 (0)