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
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function getMultipleInsertQueriesBody($limit) |
73
|
|
|
{ |
74
|
|
|
$sql = []; |
75
|
|
|
$data = array_chunk($this->data, $limit, true); |
|
|
|
|
76
|
|
|
|
77
|
|
|
foreach ($data as $array) { |
78
|
|
|
$count = count($array); |
79
|
|
|
$string = ''; |
80
|
|
|
|
81
|
|
|
for ($c = 1; $c <= $count; $c++) { |
82
|
|
|
$string .= '('.$this->getItemPlaceholders($c).')'; |
83
|
|
|
$string .= $this->appendComma($c, $array); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$sql[] = $string; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $sql; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function getSingleInsertQueriesBody() |
96
|
|
|
{ |
97
|
|
|
$sql = []; |
98
|
|
|
$count = count($this->data); |
99
|
|
|
|
100
|
|
|
for ($c = 1; $c <= $count; $c++) { |
101
|
|
|
$sql[] = '('.$this->getItemPlaceholders().')'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $sql; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $mode |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
protected function getQueriesBody($mode) |
112
|
|
|
{ |
113
|
|
|
if($mode == 'multiple'){ |
114
|
|
|
return $this->getMultipleInsertQueriesBody(self::MULTIPLE_QUERY_IMPORT_LIMIT); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->getSingleInsertQueriesBody();; |
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..