PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Updater / Migration / Db / Factory.php
matomo / app / core / Updater / Migration / Db Last commit date
AddColumn.php 5 years ago AddColumns.php 5 years ago AddIndex.php 5 years ago AddPrimaryKey.php 5 years ago AddUniqueKey.php 5 years ago BatchInsert.php 5 years ago BoundSql.php 5 years ago ChangeColumn.php 5 years ago ChangeColumnType.php 5 years ago ChangeColumnTypes.php 5 years ago CreateTable.php 5 years ago DropColumn.php 5 years ago DropColumns.php 5 years ago DropIndex.php 5 years ago DropPrimaryKey.php 5 years ago DropTable.php 5 years ago Factory.php 5 years ago Insert.php 5 years ago Sql.php 5 years ago
Factory.php
415 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 */
8 namespace Piwik\Updater\Migration\Db;
9
10 use Piwik\Common;
11 use Piwik\Container\StaticContainer;
12
13 /**
14 * Provides database migrations.
15 *
16 * @api
17 */
18 class Factory
19 {
20 /**
21 * @var \DI\Container
22 */
23 private $container;
24
25 /**
26 * @ignore
27 */
28 public function __construct()
29 {
30 $this->container = StaticContainer::getContainer();
31 }
32
33 /**
34 * Performs a custom SQL query during the update.
35 *
36 * Example:
37 * $factory->sql("DELETE * FROM table_name WHERE plugin_name = 'MyPluginName'");
38 *
39 * @param string $sql The SQL query that should be executed. Make sure to prefix a table name via
40 * {@link Piwik\Commin::prefixTable()}.
41 * @param int|int[] $errorCodesToIgnore Any given MySQL server error code will be ignored. For a list of all
42 * possible error codes have a look at {@link \Piwik\Updater\Migration\Db}.
43 * If no error should be ignored use an empty array or `false`.
44 * @return Sql
45 */
46 public function sql($sql, $errorCodesToIgnore = array())
47 {
48 if ($errorCodesToIgnore === false) {
49 $errorCodesToIgnore = array();
50 }
51
52 return $this->container->make('Piwik\Updater\Migration\Db\Sql', array(
53 'sql' => $sql, 'errorCodesToIgnore' => $errorCodesToIgnore
54 ));
55 }
56
57 /**
58 * Performs a custom SQL query that uses bound parameters during the update.
59 *
60 * You can replace values with a question mark and then pass the actual value via `$bind` for better security.
61 *
62 * Example:
63 * $factory->boundSql('DELETE * FROM table_name WHERE idsite = ?, array($idSite = 1));
64 *
65 * @param string $sql The SQL query that should be executed. Make sure to prefix a table name via
66 * {@link Piwik\Commin::prefixTable()}.
67 * @param array $bind An array of values that need to be replaced with the question marks in the SQL query.
68 * @param int|int[] $errorCodesToIgnore Any given MySQL server error code will be ignored. For a list of all
69 * possible error codes have a look at {@link \Piwik\Updater\Migration\Db}.
70 * If no error should be ignored use `false`.
71 * @return BoundSql
72 */
73 public function boundSql($sql, $bind, $errorCodesToIgnore = array())
74 {
75 if ($errorCodesToIgnore === false) {
76 $errorCodesToIgnore = array();
77 }
78
79 return $this->container->make('Piwik\Updater\Migration\Db\BoundSql', array(
80 'sql' => $sql, 'errorCodesToIgnore' => $errorCodesToIgnore, 'bind' => $bind
81 ));
82 }
83
84 /**
85 * Creates a new database table.
86 * @param string $table Unprefixed database table name, eg 'log_visit'.
87 * @param array $columnNames An array of column names and their type they should use. For example:
88 * array('column_name_1' => 'VARCHAR(200) NOT NULL', 'column_name_2' => 'INT(10) DEFAULT 0')
89 * @param string|string[] $primaryKey Optional. One or multiple columns that shall define the primary key.
90 * @return CreateTable
91 */
92 public function createTable($table, $columnNames, $primaryKey = array())
93 {
94 $table = $this->prefixTable($table);
95
96 if (!empty($primaryKey) && !is_array($primaryKey)) {
97 $primaryKey = array($primaryKey);
98 }
99
100 return $this->container->make('Piwik\Updater\Migration\Db\CreateTable', array(
101 'table' => $table, 'columnNames' => $columnNames, 'primaryKey' => $primaryKey
102 ));
103 }
104
105 /**
106 * Drops an existing database table.
107 * @param string $table Unprefixed database table name, eg 'log_visit'.
108 * @return DropTable
109 */
110 public function dropTable($table)
111 {
112 $table = $this->prefixTable($table);
113
114 return $this->container->make('Piwik\Updater\Migration\Db\DropTable', array(
115 'table' => $table
116 ));
117 }
118
119 /**
120 * Adds a new database table column to an existing table.
121 *
122 * @param string $table Unprefixed database table name, eg 'log_visit'.
123 * @param string $columnName The name of the column that shall be added, eg 'my_column_name'.
124 * @param string $columnType The column type it should have, eg 'VARCHAR(200) NOT NULL'.
125 * @param string|null $placeColumnAfter If specified, the added column will be added after this specified column
126 * name. If you specify a column be sure it actually exists and can be added
127 * after this column.
128 * @return AddColumn
129 */
130 public function addColumn($table, $columnName, $columnType, $placeColumnAfter = null)
131 {
132 $table = $this->prefixTable($table);
133
134 return $this->container->make('Piwik\Updater\Migration\Db\AddColumn', array(
135 'table' => $table, 'columnName' => $columnName, 'columnType' => $columnType, 'placeColumnAfter' => $placeColumnAfter
136 ));
137 }
138
139 /**
140 * Adds multiple new database table columns to an existing table at once.
141 *
142 * Adding multiple columns at the same time can lead to performance improvements compared to adding each new column
143 * separately.
144 *
145 * @param string $table Unprefixed database table name, eg 'log_visit'.
146 * @param array $columns An array of column name to column type pairs,
147 * eg array('my_column_name' => 'VARCHAR(200) NOT NULL', 'column2' => '...')
148 * @param string|null $placeColumnAfter If specified, the first added column will be added after this specified column
149 * name. All following columns will be added after the previous specified in
150 * $columns. If you specify a column be sure it actually exists and can be added
151 * after this column.
152 * @return AddColumns
153 */
154 public function addColumns($table, $columns, $placeColumnAfter = null)
155 {
156 $table = $this->prefixTable($table);
157
158 return $this->container->make('Piwik\Updater\Migration\Db\AddColumns', array(
159 'table' => $table, 'columns' => $columns, 'placeColumnAfter' => $placeColumnAfter
160 ));
161 }
162
163 /**
164 * Drops an existing database table column.
165 *
166 * @param string $table Unprefixed database table name, eg 'log_visit'.
167 * @param string $columnName The name of the column that shall be dropped, eg 'my_column_name'.
168 * @return DropColumn
169 */
170 public function dropColumn($table, $columnName)
171 {
172 $table = $this->prefixTable($table);
173
174 return $this->container->make('Piwik\Updater\Migration\Db\DropColumn', array(
175 'table' => $table, 'columnName' => $columnName
176 ));
177 }
178
179 /**
180 * Drops an existing database table column.
181 *
182 * @param string $table Unprefixed database table name, eg 'log_visit'.
183 * @param array $columnName An array of column names that should be dropped eg ['column1', 'column2'].
184 * @return DropColumns
185 */
186 public function dropColumns($table, $columnNames)
187 {
188 $table = $this->prefixTable($table);
189
190 return $this->container->make('Piwik\Updater\Migration\Db\DropColumns', array(
191 'tableName' => $table, 'columnNames' => $columnNames
192 ));
193 }
194
195 /**
196 * Changes the column name and column type of an existing database table column.
197 *
198 * @param string $table Unprefixed database table name, eg 'log_visit'.
199 * @param string $oldColumnName The current name of the column that shall be renamed/changed, eg 'column_name'.
200 * @param string $newColumnName The new name of the column, eg 'new_column_name'.
201 * @param string $columnType The updated type the new column should have, eg 'VARCHAR(200) NOT NULL'.
202 *
203 * @return ChangeColumn
204 */
205 public function changeColumn($table, $oldColumnName, $newColumnName, $columnType)
206 {
207 $table = $this->prefixTable($table);
208
209 return $this->container->make('Piwik\Updater\Migration\Db\ChangeColumn', array(
210 'table' => $table, 'oldColumnName' => $oldColumnName,
211 'newColumnName' => $newColumnName, 'columnType' => $columnType
212 ));
213 }
214
215 /**
216 * Changes the type of an existing database table column.
217 *
218 * @param string $table Unprefixed database table name, eg 'log_visit'.
219 * @param string $columnName The name of the column that shall be changed, eg 'my_column_name'.
220 * @param string $columnType The updated type the column should have, eg 'VARCHAR(200) NOT NULL'.
221 *
222 * @return ChangeColumnType
223 */
224 public function changeColumnType($table, $columnName, $columnType)
225 {
226 $table = $this->prefixTable($table);
227
228 return $this->container->make('Piwik\Updater\Migration\Db\ChangeColumnType', array(
229 'table' => $table, 'columnName' => $columnName, 'columnType' => $columnType
230 ));
231 }
232
233 /**
234 * Changes the type of multiple existing database table columns at the same time.
235 *
236 * Changing multiple columns at the same time can lead to performance improvements compared to changing the type
237 * of each column separately.
238 *
239 * @param string $table Unprefixed database table name, eg 'log_visit'.
240 * @param array $columns An array of column name to column type pairs,
241 * eg array('my_column_name' => 'VARCHAR(200) NOT NULL', 'column2' => '...')
242 *
243 * @return ChangeColumnTypes
244 */
245 public function changeColumnTypes($table, $columns)
246 {
247 $table = $this->prefixTable($table);
248
249 return $this->container->make('Piwik\Updater\Migration\Db\ChangeColumnTypes', array(
250 'table' => $table, 'columns' => $columns
251 ));
252 }
253
254 /**
255 * Adds an index to an existing database table.
256 *
257 * This is equivalent to an `ADD INDEX indexname (column_name_1, column_name_2)` in SQL.
258 * It adds a normal index, no unique index.
259 *
260 * Note: If no indexName is specified, it will automatically generate a name for this index if which is basically:
261 * `'index_' . implode('_', $columnNames)`. If a column name is eg `column1(10)` then only the first part (`column1`)
262 * will be used. For example when using columns `array('column1', 'column2(10)')` then the index name will be
263 * `index_column1_column2`.
264 *
265 * @param string $table Unprefixed database table name, eg 'log_visit'.
266 * @param string[]|string $columnNames Either one or multiple column names, eg array('column_name_1', 'column_name_2').
267 * A column name can be appended by a number bracket eg "column_name_1(10)".
268 * @param string $indexName If specified, the given index name will be used instead of the automatically generated one.
269 * @return AddIndex
270 */
271 public function addIndex($table, $columnNames, $indexName = '')
272 {
273 $table = $this->prefixTable($table);
274
275 if (!is_array($columnNames)) {
276 $columnNames = array($columnNames);
277 }
278
279 return $this->container->make('Piwik\Updater\Migration\Db\AddIndex', array(
280 'table' => $table, 'columnNames' => $columnNames, 'indexName' => $indexName
281 ));
282 }
283
284 /**
285 * Adds a unique key to an existing database table.
286 *
287 * This is equivalent to an `ADD UNIQUE KEY indexname (column_name_1, column_name_2)` in SQL.
288 *
289 * Note: If no indexName is specified, it will automatically generate a name for this index if which is basically:
290 * `'index_' . implode('_', $columnNames)`. If a column name is eg `column1(10)` then only the first part (`column1`)
291 * will be used. For example when using columns `array('column1', 'column2(10)')` then the index name will be
292 * `index_column1_column2`.
293 *
294 * @param string $table Unprefixed database table name, eg 'log_visit'.
295 * @param string[]|string $columnNames Either one or multiple column names, eg array('column_name_1', 'column_name_2').
296 * A column name can be appended by a number bracket eg "column_name_1(10)".
297 * @param string $indexName If specified, the given unique key name will be used instead of the automatically generated one.
298 * @return AddIndex
299 */
300 public function addUniqueKey($table, $columnNames, $indexName = '')
301 {
302 $table = $this->prefixTable($table);
303
304 if (!is_array($columnNames)) {
305 $columnNames = array($columnNames);
306 }
307
308 return $this->container->make('Piwik\Updater\Migration\Db\AddUniqueKey', array(
309 'table' => $table, 'columnNames' => $columnNames, 'indexName' => $indexName
310 ));
311 }
312
313 /**
314 * Drops an existing index from a database table.
315 *
316 * @param string $table Unprefixed database table name, eg 'log_visit'.
317 * @param string $indexName The name of the index that shall be dropped.
318 * @return DropIndex
319 */
320 public function dropIndex($table, $indexName)
321 {
322 $table = $this->prefixTable($table);
323
324 return $this->container->make('Piwik\Updater\Migration\Db\DropIndex', array(
325 'table' => $table, 'indexName' => $indexName
326 ));
327 }
328
329 /**
330 * Drops an existing index from a database table.
331 *
332 * @param string $table Unprefixed database table name, eg 'log_visit'.
333 * @return DropIndex
334 */
335 public function dropPrimaryKey($table)
336 {
337 $table = $this->prefixTable($table);
338
339 return $this->container->make('Piwik\Updater\Migration\Db\DropPrimaryKey', array(
340 'table' => $table
341 ));
342 }
343
344 /**
345 * Adds a primary key to an existing database table.
346 *
347 * This is equivalent to an `ADD PRIMARY KEY(column_name_1, column_name_2)` in SQL.
348 *
349 * @param string $table Unprefixed database table name, eg 'log_visit'.
350 * @param string[]|string $columnNames Either one or multiple column names, eg array('column_name_1', 'column_name_2')
351 * @return AddPrimaryKey
352 */
353 public function addPrimaryKey($table, $columnNames)
354 {
355 $table = $this->prefixTable($table);
356 if (!is_array($columnNames)) {
357 $columnNames = array($columnNames);
358 }
359
360 return $this->container->make('Piwik\Updater\Migration\Db\AddPrimaryKey', array(
361 'table' => $table, 'columnNames' => $columnNames
362 ));
363 }
364
365 /**
366 * Inserts a new record / row into an existing database table.
367 *
368 * Make sure to specify all columns that need to be defined in order to insert a value successfully. There could
369 * be for example columns that are not nullable and therefore need a value.
370 *
371 * @param string $table Unprefixed database table name, eg 'log_visit'.
372 * @param array $columnValuePairs An array containing column => value pairs. For example:
373 * array('column_name_1' => 'value1', 'column_name_2' => 'value2')
374 * @return Insert
375 */
376 public function insert($table, $columnValuePairs)
377 {
378 $table = $this->prefixTable($table);
379
380 return $this->container->make('Piwik\Updater\Migration\Db\Insert', array(
381 'table' => $table, 'columnValuePairs' => $columnValuePairs
382 ));
383 }
384
385 /**
386 * Performs a batch insert into a specific table using either LOAD DATA INFILE or plain INSERTs,
387 * as a fallback. On MySQL, LOAD DATA INFILE is 20x faster than a series of plain INSERTs.
388 *
389 * Please note that queries for batch inserts are currently not shown to an end user and should therefore not be
390 * returned in an `Updates::getMigrations` method. Instead it needs to be execute directly in `Updates::doUpdate`
391 * via `$updater->executeMigration($factory->dbBatchInsert(...));`
392 *
393 * @param string $table Unprefixed database table name, eg 'log_visit'.
394 * @param string[] $columnNames An array of unquoted column names, eg array('column_name1', 'column_name_2')
395 * @param array $values An array of data to be inserted, eg array(array('row1column1', 'row1column2'),array('row2column1', 'row2column2'))
396 * @param bool $throwException Whether to throw an exception that was caught while trying LOAD DATA INFILE, or not.
397 * @param string $charset The charset to use, defaults to utf8
398 * @return BatchInsert
399 */
400 public function batchInsert($table, $columnNames, $values, $throwException = false, $charset = 'utf8')
401 {
402 $table = $this->prefixTable($table);
403
404 return $this->container->make('Piwik\Updater\Migration\Db\BatchInsert', array(
405 'table' => $table, 'columnNames' => $columnNames, 'values' => $values,
406 'throwException' => $throwException, 'charset' => $charset
407 ));
408 }
409
410 private function prefixTable($table)
411 {
412 return Common::prefixTable($table);
413 }
414 }
415