1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Query; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\QueryException; |
6
|
|
|
|
7
|
|
|
class WriteToFile |
8
|
|
|
{ |
9
|
|
|
/** |
|
|
|
|
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
const FORMAT_TabSeparated = 'TabSeparated'; |
|
|
|
|
13
|
|
|
const FORMAT_TabSeparatedWithNames = 'TabSeparatedWithNames'; |
|
|
|
|
14
|
|
|
const FORMAT_CSV = 'CSV'; |
|
|
|
|
15
|
|
|
const FORMAT_CSVWithNames = 'CSVWithNames'; |
|
|
|
|
16
|
|
|
const FORMAT_JSONEACHROW = 'JSONEachRow'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
private $support_format = ['TabSeparated', 'TabSeparatedWithNames', 'CSV', 'CSVWithNames', 'JSONEachRow']; |
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $file_name = null; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $format = 'CSV'; |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $gzip = false; |
33
|
|
|
/** |
34
|
|
|
* WriteToFile constructor. |
|
|
|
|
35
|
|
|
* @param string $file_name |
|
|
|
|
36
|
|
|
* @param bool $overwrite |
|
|
|
|
37
|
|
|
* @param string|null $format |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct($file_name, $overwrite = true, $format = null) { |
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
42
|
1 |
|
if (!$file_name) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
throw new QueryException('Bad file path'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if (is_file($file_name)) |
|
|
|
|
48
|
|
|
{ |
49
|
1 |
|
if (!$overwrite) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
throw new QueryException('File exists: ' . $file_name); |
|
|
|
|
52
|
|
|
} |
53
|
1 |
|
if (!unlink($file_name)) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
throw new QueryException('Can`t delete: ' . $file_name); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
1 |
|
$dir = dirname($file_name); |
|
|
|
|
59
|
1 |
|
if (!is_writable($dir)) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
throw new QueryException('Can`t writable dir: ' . $dir); |
62
|
|
|
} |
63
|
1 |
|
if (is_string($format)) |
|
|
|
|
64
|
|
|
{ |
65
|
1 |
|
$this->setFormat($format); |
66
|
|
|
} |
67
|
1 |
|
$this->file_name = $file_name; |
|
|
|
|
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
1 |
|
public function getGzip() |
|
|
|
|
74
|
|
|
{ |
75
|
1 |
|
return $this->gzip; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param bool $flag |
80
|
|
|
*/ |
81
|
|
|
public function setGzip($flag) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->gzip = $flag; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $format |
88
|
|
|
*/ |
89
|
1 |
|
public function setFormat($format) |
|
|
|
|
90
|
|
|
{ |
91
|
1 |
|
if (!in_array($format, $this->support_format)) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
throw new QueryException('Unsupport format: ' . $format); |
94
|
|
|
} |
95
|
1 |
|
$this->format = $format; |
96
|
1 |
|
} |
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
1 |
|
public function size() |
|
|
|
|
101
|
|
|
{ |
102
|
1 |
|
return filesize($this->file_name); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
1 |
|
public function fetchFile() |
|
|
|
|
109
|
|
|
{ |
110
|
1 |
|
return $this->file_name; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
1 |
|
public function fetchFormat() |
|
|
|
|
117
|
|
|
{ |
118
|
1 |
|
return $this->format; |
119
|
|
|
} |
|
|
|
|
120
|
|
|
|
121
|
|
|
} |
|
|
|
|
122
|
|
|
|