Skip to content

Commit 4b0305b

Browse files
committed
* Fix format json
* add GetRaw()
1 parent 2bbbd38 commit 4b0305b

File tree

6 files changed

+168
-13
lines changed

6 files changed

+168
-13
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,11 @@ MIT
610610

611611
ChangeLog
612612
---------
613+
### 2017-12-28
614+
615+
* Fix `FORMAT JSON` if set FORMAT in sql
616+
* GetRaw() - result raw response if not json ``SELECT number as format_id FROM system.numbers LIMIT 3 FORMAT CSVWithNames``
617+
613618
### 2017-12-22
614619

615620
* progressFunction()

src/Query/Query.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,48 @@ public function setFormat($format)
4646
$this->format = $format;
4747
}
4848

49+
50+
private function applyFormatQuery()
51+
{
52+
// FORMAT\s(\w)*$
53+
if (null === $this->format) return false;
54+
$supportFormats=
55+
"FORMAT\\s+TSV|FORMAT\\s+TSVRaw|FORMAT\\s+TSVWithNames|FORMAT\\s+TSVWithNamesAndTypes|FORMAT\\s+Vertical|FORMAT\\s+JSONCompact|FORMAT\\s+JSONEachRow|FORMAT\\s+TSKV|FORMAT\\s+TabSeparatedWithNames|FORMAT\\s+TabSeparatedWithNamesAndTypes|FORMAT\\s+TabSeparatedRaw|FORMAT\\s+BlockTabSeparated|FORMAT\\s+CSVWithNames|FORMAT\\s+CSV|FORMAT\\s+JSON|FORMAT\\s+TabSeparated";
56+
57+
$matches=[];
58+
if (preg_match_all('%('.$supportFormats.')%ius',$this->sql,$matches)){
59+
60+
// skip add "format json"
61+
if (isset($matches[0]))
62+
{
63+
$format=trim(str_ireplace('format','',$matches[0][0]));
64+
$this->format=$format;
65+
66+
}
67+
}
68+
else {
69+
$this->sql = $this->sql . ' FORMAT ' . $this->format;
70+
}
71+
72+
73+
74+
75+
76+
77+
}
78+
public function getFormat()
79+
{
80+
81+
return $this->format;
82+
}
83+
4984
/**
5085
* @return string
5186
*/
5287
public function toSql()
5388
{
5489
if (null !== $this->format) {
55-
if (stripos($this->sql, 'FORMAT') < 1) {
56-
$this->sql = $this->sql . ' FORMAT ' . $this->format;
57-
}
90+
$this->applyFormatQuery();
5891
}
5992

6093
if (sizeof($this->degenerations))

src/Statement.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ class Statement
7171
*/
7272
private $rows_before_limit_at_least = false;
7373

74-
/**
75-
* @var
76-
*/
77-
private $rawResult;
78-
7974
/**
8075
* @var array
8176
*/
@@ -94,6 +89,7 @@ class Statement
9489
public function __construct(Request $request)
9590
{
9691
$this->_request = $request;
92+
$this->format = $this->_request->getRequestExtendedInfo('format');
9793
$this->query = $this->_request->getRequestExtendedInfo('query');
9894
$this->sql = $this->_request->getRequestExtendedInfo('sql');
9995
}
@@ -214,7 +210,7 @@ private function init()
214210
$this->check();
215211

216212

217-
$this->_rawData = $this->response()->json();
213+
$this->_rawData = $this->response()->rawDataOrJson($this->format);
218214

219215
if (!$this->_rawData) {
220216
$this->_init = true;
@@ -368,7 +364,7 @@ public function rawData()
368364

369365
$this->check();
370366

371-
return $this->response()->json();
367+
return $this->response()->rawDataOrJson($this->format);
372368
}
373369
/**
374370
* @param bool $key
@@ -446,6 +442,15 @@ public function info()
446442
];
447443
}
448444

445+
/**
446+
* get format in sql
447+
* @return mixed
448+
*/
449+
public function getFormat()
450+
{
451+
return $this->format;
452+
}
453+
449454
/**
450455
* @return array
451456
*/

src/Transport/CurlerResponse.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,17 @@ public function json($key = null)
287287

288288
return $d[$key];
289289
}
290+
291+
/**
292+
* @return mixed
293+
*/
294+
public function rawDataOrJson($format)
295+
{
296+
if (stripos($format,'json')!==false)
297+
{
298+
return $this->json();
299+
}
300+
return $this->body();
301+
302+
}
290303
}

src/Transport/Http.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ private function makeRequest(Query $query, $urlParams = [], $query_as_string = f
214214

215215
$extendinfo = [
216216
'sql' => $sql,
217-
'query' => $query
217+
'query' => $query,
218+
'format'=> $query->getFormat()
218219
];
219220

220221
$new = $this->newRequest($extendinfo);
@@ -249,7 +250,8 @@ public function writeStreamData($sql)
249250

250251
$extendinfo = [
251252
'sql' => $sql,
252-
'query' => $query
253+
'query' => $query,
254+
'format'=> $query->getFormat()
253255
];
254256

255257
$request = $this->newRequest($extendinfo);
@@ -274,7 +276,8 @@ public function writeAsyncCSV($sql, $file_name)
274276

275277
$extendinfo = [
276278
'sql' => $sql,
277-
'query' => $query
279+
'query' => $query,
280+
'format'=> $query->getFormat()
278281
];
279282

280283
$request = $this->newRequest($extendinfo);

tests/00_FormatTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
/**
6+
* Class ClientTest
7+
*/
8+
class FormatQueryTest extends TestCase
9+
{
10+
/**
11+
* @var \ClickHouseDB\Client
12+
*/
13+
private $db;
14+
15+
/**
16+
* @var
17+
*/
18+
private $tmp_path;
19+
20+
/**
21+
* @throws Exception
22+
*/
23+
public function setUp()
24+
{
25+
date_default_timezone_set('Europe/Moscow');
26+
27+
if (!defined('phpunit_clickhouse_host')) {
28+
throw new Exception("Not set phpunit_clickhouse_host in phpUnit.xml");
29+
}
30+
31+
$tmp_path = rtrim(phpunit_clickhouse_tmp_path, '/') . '/';
32+
33+
if (!is_dir($tmp_path)) {
34+
throw new Exception("Not dir in phpunit_clickhouse_tmp_path");
35+
}
36+
37+
$this->tmp_path = $tmp_path;
38+
39+
$config = [
40+
'host' => phpunit_clickhouse_host,
41+
'port' => phpunit_clickhouse_port,
42+
'username' => phpunit_clickhouse_user,
43+
'password' => phpunit_clickhouse_pass,
44+
45+
];
46+
47+
$this->db = new ClickHouseDB\Client($config);
48+
49+
50+
$this->db->ping();
51+
}
52+
53+
/**
54+
*
55+
*/
56+
public function tearDown()
57+
{
58+
//
59+
}
60+
61+
62+
63+
public function testCreateTableTEMPORARYNoSession()
64+
{
65+
$this->setUp();
66+
67+
68+
69+
$query="SELECT 2*number as FORMAT FROM system.numbers LIMIT 1,1 format TSV";
70+
$st = $this->db->select($query);
71+
$this->assertEquals($query, $st->sql());
72+
$this->assertEquals('TSV', $st->getFormat());
73+
$this->assertEquals("2\n", $st->rawData());
74+
75+
76+
77+
$query="SELECT number as format_id FROM system.numbers LIMIT 3 FORMAT CSVWithNames";
78+
$st = $this->db->select($query);
79+
$this->assertEquals($query, $st->sql());
80+
$this->assertEquals('CSVWithNames', $st->getFormat());
81+
$this->assertEquals("\"format_id\"\n0\n1\n2\n", $st->rawData());
82+
83+
84+
85+
$query="SELECT number as format_id FROM system.numbers LIMIT 1,1 FORMAT CSV";
86+
$st = $this->db->select($query);
87+
$this->assertEquals($query, $st->sql());
88+
$this->assertEquals('CSV', $st->getFormat());
89+
90+
//
91+
92+
93+
}
94+
95+
96+
}

0 commit comments

Comments
 (0)