Skip to content

Commit

Permalink
Escape values in arrays
Browse files Browse the repository at this point in the history
Fixes #85
  • Loading branch information
simPod committed Aug 22, 2018
1 parent fcfea16 commit b0178fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/Quote/StrictQuoteLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
namespace ClickHouseDB\Quote;

use ClickHouseDB\Exception\QueryException;
use function array_map;
use function is_string;
use function preg_replace;
use function str_replace;

class StrictQuoteLine
{
Expand Down Expand Up @@ -79,7 +83,7 @@ public function quoteValue($row)
}

$value = strval($value);
$value = preg_replace('/(' . $enclosure_esc . '|' . $encode_esc . ')/', $encode_esc . '\1', $value);
$value = $this->encodeString($value, $enclosure_esc, $encode_esc);
return $enclosure . $value . $enclosure;
}

Expand All @@ -88,7 +92,12 @@ public function quoteValue($row)
// Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
// single quotation marks with the same screening rules as above.
// as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes.
$value = $this->escapeDoubleQoutes($value);
$value = array_map(
function ($v) use ($enclosure_esc, $encode_esc) {
return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v;
},
$value
);
$result_array = FormatLine::Insert($value);

return $encodeArray . '[' . $result_array . ']' . $encodeArray;
Expand All @@ -104,11 +113,11 @@ public function quoteValue($row)
return array_map($quote, $row);
}

public function escapeDoubleQoutes(array $arr)
/**
* @return string
*/
public function encodeString(string $value, string $enclosureEsc, string $encodeEsc)
{
return array_map(function($v) {
return is_string($v) ? str_replace('"', '""', $v) : $v;
}, $arr);
return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value);
}

}
13 changes: 13 additions & 0 deletions tests/StrictQuoteLineTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Tests;

use ClickHouseDB\Quote\StrictQuoteLine;
use PHPUnit\Framework\TestCase;
use function array_diff;
use function array_map;
use function file_put_contents;
use function unlink;
use const FILE_APPEND;

class StrictQuoteLineTest extends TestCase
{
use WithClient;

/**
* @return void
*/
public function setUp()
{
$this->client->write('DROP TABLE IF EXISTS cities');
Expand All @@ -25,6 +35,8 @@ public function setUp()

/**
* @group test
*
* @return void
*/
public function testQuoteValueCSV()
{
Expand All @@ -35,6 +47,7 @@ public function testQuoteValueCSV()
['2018-04-02', 'That works', ['\""That does not\""', '"\'\""That works"""\"'], [1, 0]],
['2018-04-03', 'That works', ['\"\"That does not"\'""', '""""That works""""'], [9, 121]],
];

$fileName = $this->tmpPath . '__test_quote_value.csv';

@unlink($fileName);
Expand Down

0 comments on commit b0178fe

Please sign in to comment.