|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the DbImporter package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace DbImporter\QueryBuilder; |
|
12
|
|
|
|
|
13
|
|
|
use DbImporter\QueryBuilder\Contracts\QueryBuilderInterface; |
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractQueryBuilder implements QueryBuilderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $table; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $debug; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $mapping; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ArrayCollection |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $data; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* MySqlQueryBuilderTest constructor. |
|
40
|
|
|
* @param $table |
|
41
|
|
|
* @param $debug |
|
42
|
|
|
* @param array $mapping |
|
43
|
|
|
* @param array $data |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
$table, |
|
47
|
|
|
array $mapping, |
|
48
|
|
|
array $data, |
|
49
|
|
|
$debug |
|
50
|
|
|
) { |
|
51
|
|
|
$this->table = $table; |
|
52
|
|
|
$this->mapping = $mapping; |
|
53
|
|
|
$this->data = $data; |
|
|
|
|
|
|
54
|
|
|
$this->debug = $debug; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $index |
|
59
|
|
|
* @param $array |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function appendComma($index, $array) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($index < (count($array))) { |
|
65
|
|
|
return ', '; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $mode |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getQueriesBody($mode, $limit) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($mode == 'multiple') { |
|
76
|
|
|
return $this->getMultipleInsertQueriesBody($limit); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->getSingleInsertQueriesBody(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getMultipleInsertQueriesBody($limit) |
|
86
|
|
|
{ |
|
87
|
|
|
$sql = []; |
|
88
|
|
|
$data = array_chunk($this->data, $limit, true); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
foreach ($data as $array) { |
|
91
|
|
|
$count = count($array); |
|
92
|
|
|
$string = ''; |
|
93
|
|
|
|
|
94
|
|
|
for ($c = 1; $c <= $count; $c++) { |
|
95
|
|
|
$string .= '('.$this->getItemPlaceholders($c).')'; |
|
96
|
|
|
$string .= $this->appendComma($c, $array); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$sql[] = $string; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $sql; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getSingleInsertQueriesBody() |
|
109
|
|
|
{ |
|
110
|
|
|
$sql = []; |
|
111
|
|
|
$count = count($this->data); |
|
112
|
|
|
|
|
113
|
|
|
for ($c = 1; $c <= $count; $c++) { |
|
114
|
|
|
$sql[] = '('.$this->getItemPlaceholders().')'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $sql; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param $index |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
private function getItemPlaceholders($index = null) |
|
125
|
|
|
{ |
|
126
|
|
|
$sql = ''; |
|
127
|
|
|
$c = 1; |
|
128
|
|
|
$values = array_values($this->mapping); |
|
129
|
|
|
|
|
130
|
|
|
foreach ($values as $map) { |
|
131
|
|
|
$sql .= ($index) ? ':'.$map.'_'.$index : ':'.$map; |
|
132
|
|
|
$sql .= $this->appendComma($c, $values); |
|
133
|
|
|
$c++; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $sql; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..