1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Query; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\QueryException; |
6
|
|
|
|
7
|
|
|
class WhereInFile |
8
|
|
|
{ |
9
|
|
|
/** |
|
|
|
|
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
const FORMAT_TabSeparated = 'TabSeparated'; |
|
|
|
|
13
|
|
|
const FORMAT_TabSeparatedWithNames = 'TabSeparatedWithNames'; |
|
|
|
|
14
|
|
|
const FORMAT_CSV = 'CSV'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* @var array |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
private $_files = []; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* WhereInFile constructor. |
|
|
|
|
24
|
|
|
*/ |
25
|
1 |
|
public function __construct() {} |
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $file_name |
30
|
|
|
* @param string $table_name |
31
|
|
|
* @param string $structure |
32
|
|
|
* @param string $format |
33
|
|
|
*/ |
34
|
1 |
|
public function attachFile($file_name, $table_name, $structure, $format = 'CSV') |
|
|
|
|
35
|
|
|
{ |
36
|
1 |
|
if (!is_readable($file_name)) { |
|
|
|
|
37
|
|
|
throw new QueryException('Can`t read file: ' . $file_name); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
$this->_files[$table_name] = [ |
|
|
|
|
41
|
1 |
|
'filename' => $file_name, |
|
|
|
|
42
|
1 |
|
'structure' => $structure, |
43
|
1 |
|
'format' => $format |
|
|
|
|
44
|
|
|
]; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
1 |
|
public function size() |
|
|
|
|
51
|
|
|
{ |
52
|
1 |
|
return sizeof($this->_files); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return array |
|
|
|
|
57
|
|
|
*/ |
58
|
1 |
|
public function fetchFiles() |
|
|
|
|
59
|
|
|
{ |
60
|
1 |
|
$out = []; |
61
|
1 |
|
foreach ($this->_files as $table => $data) { |
62
|
1 |
|
$out[$table] = realpath($data['filename']); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $out; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $table |
|
|
|
|
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
public function fetchStructure($table) |
|
|
|
|
73
|
|
|
{ |
74
|
1 |
|
$structure = $this->_files[$table]['structure']; |
75
|
|
|
|
76
|
1 |
|
$out = []; |
77
|
1 |
|
foreach ($structure as $name => $type) { |
78
|
1 |
|
$out[] = $name . ' ' . $type; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return implode(',', $out); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
|
|
|
|
86
|
|
|
*/ |
87
|
1 |
|
public function fetchUrlParams() |
|
|
|
|
88
|
|
|
{ |
89
|
1 |
|
$out = []; |
90
|
1 |
|
foreach ($this->_files as $table => $data) { |
91
|
1 |
|
$out[$table . '_structure'] = $this->fetchStructure($table); |
92
|
1 |
|
$out[$table . '_format'] = $data['format']; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $out; |
96
|
|
|
} |
|
|
|
|
97
|
|
|
|
98
|
|
|
} |
|
|
|
|
99
|
|
|
|