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