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