1 | <?php |
||
35 | class BulkInsertQuery |
||
36 | { |
||
37 | /** |
||
38 | * @var string[] |
||
39 | */ |
||
40 | private $columns; |
||
41 | |||
42 | /** |
||
43 | * @var Connection |
||
44 | */ |
||
45 | private $connection; |
||
46 | |||
47 | /** |
||
48 | * @var Identifier |
||
49 | */ |
||
50 | private $table; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $parameters = array(); |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $types = array(); |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $values = array(); |
||
66 | |||
67 | /** |
||
68 | * Constructor. |
||
69 | * |
||
70 | * @param Connection $connection The connection to use for query execution. |
||
71 | * @param string $table The name of the table to insert rows into. |
||
72 | * @param string[] $columns The names of the columns to insert values into. |
||
73 | * Can be left empty to allow arbitrary table row inserts |
||
74 | * based on the table's column order. |
||
75 | */ |
||
76 | 13 | public function __construct(Connection $connection, string $table, array $columns = array()) |
|
82 | |||
83 | /** |
||
84 | * Adds a set of values to the bulk insert query to be inserted as a row into the specified table. |
||
85 | * |
||
86 | * @param array $values The set of values to be inserted as a row into the table. |
||
87 | * If no columns have been specified for insertion, this can be |
||
88 | * an arbitrary list of values to be inserted into the table. |
||
89 | * Otherwise the values' keys have to match either one of the |
||
90 | * specified column names or indexes. |
||
91 | * @param array $types The types for the given values to bind to the query. |
||
92 | * If no columns have been specified for insertion, the types' |
||
93 | * keys will be matched against the given values' keys. |
||
94 | * Otherwise the types' keys will be matched against the |
||
95 | * specified column names and indexes. |
||
96 | * Non-matching keys will be discarded, missing keys will not |
||
97 | * be bound to a specific type. |
||
98 | * |
||
99 | * @throws \InvalidArgumentException if columns were specified for this query |
||
100 | * and either no value for one of the specified |
||
101 | * columns is given or multiple values are given |
||
102 | * for a single column (named and indexed) or |
||
103 | * multiple types are given for a single column |
||
104 | * (named and indexed). |
||
105 | * |
||
106 | * @todo add support for expressions. |
||
107 | */ |
||
108 | 12 | public function addValues(array $values, array $types = array()) |
|
116 | |||
117 | /** |
||
118 | * Executes this INSERT query using the bound parameters and their types. |
||
119 | * |
||
120 | * @return integer The number of affected rows. |
||
121 | * |
||
122 | * @throws \LogicException if this query contains more rows than acceptable |
||
123 | * for a single INSERT statement by the underlying platform. |
||
124 | */ |
||
125 | 2 | public function execute(): int |
|
149 | |||
150 | /** |
||
151 | * Returns the parameters for this INSERT query being constructed indexed by parameter index. |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | 8 | public function getParameters(): array |
|
159 | |||
160 | /** |
||
161 | * Returns the parameter types for this INSERT query being constructed indexed by parameter index. |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | 8 | public function getParameterTypes(): array |
|
169 | |||
170 | /** |
||
171 | * Returns the SQL formed by the current specifications of this INSERT query. |
||
172 | * |
||
173 | * @return string |
||
174 | * |
||
175 | * @throws \LogicException if no values have been specified yet. |
||
176 | */ |
||
177 | 8 | public function getSQL(): string |
|
185 | |||
186 | /** |
||
187 | * This method handles the situation when user did not specified any columns at all |
||
188 | * |
||
189 | * @param array $values Values to be inserted |
||
190 | * @param array $types Corresponding value types |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 4 | private function addValuesInDbOrder(array $values, array $types): void |
|
205 | |||
206 | /** |
||
207 | * Handles adding values when user has specified column names |
||
208 | * |
||
209 | * @param array $values Values to be inserted |
||
210 | * @param array $types Corresponding value types |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | 8 | private function addValuesByColumnDefinition(array $values, array $types): void |
|
247 | |||
248 | /** |
||
249 | * Return query for given list of values |
||
250 | * |
||
251 | * @param array $values Values to be inserted |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 8 | private function getSqlForValues(array $values): string |
|
292 | } |
||
293 |