PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
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 6 years ago AddColumns.php 6 years ago AddIndex.php 6 years ago AddPrimaryKey.php 6 years ago AddUniqueKey.php 6 years ago BatchInsert.php 6 years ago BoundSql.php 6 years ago ChangeColumn.php 6 years ago ChangeColumnType.php 6 years ago ChangeColumnTypes.php 6 years ago CreateTable.php 6 years ago DropColumn.php 6 years ago DropIndex.php 6 years ago DropPrimaryKey.php 6 years ago DropTable.php 6 years ago Factory.php 6 years ago Insert.php 6 years ago Sql.php 6 years ago
Factory.php
399 lines
1 <?php
2 /**
3 * Piwik - 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 * Changes the column name and column type of an existing database table column.
181 *
182 * @param string $table Unprefixed database table name, eg 'log_visit'.
183 * @param string $oldColumnName The current name of the column that shall be renamed/changed, eg 'column_name'.
184 * @param string $newColumnName The new name of the column, eg 'new_column_name'.
185 * @param string $columnType The updated type the new column should have, eg 'VARCHAR(200) NOT NULL'.
186 *
187 * @return ChangeColumn
188 */
189 public function changeColumn($table, $oldColumnName, $newColumnName, $columnType)
190 {
191 $table = $this->prefixTable($table);
192
193 return $this->container->make('Piwik\Updater\Migration\Db\ChangeColumn', array(
194 'table' => $table, 'oldColumnName' => $oldColumnName,
195 'newColumnName' => $newColumnName, 'columnType' => $columnType
196 ));
197 }
198
199 /**
200 * Changes the type of an existing database table column.
201 *
202 * @param string $table Unprefixed database table name, eg 'log_visit'.
203 * @param string $columnName The name of the column that shall be changed, eg 'my_column_name'.
204 * @param string $columnType The updated type the column should have, eg 'VARCHAR(200) NOT NULL'.
205 *
206 * @return ChangeColumnType
207 */
208 public function changeColumnType($table, $columnName, $columnType)
209 {
210 $table = $this->prefixTable($table);
211
212 return $this->container->make('Piwik\Updater\Migration\Db\ChangeColumnType', array(
213 'table' => $table, 'columnName' => $columnName, 'columnType' => $columnType
214 ));
215 }
216
217 /**
218 * Changes the type of multiple existing database table columns at the same time.
219 *
220 * Changing multiple columns at the same time can lead to performance improvements compared to changing the type
221 * of each column separately.
222 *
223 * @param string $table Unprefixed database table name, eg 'log_visit'.
224 * @param array $columns An array of column name to column type pairs,
225 * eg array('my_column_name' => 'VARCHAR(200) NOT NULL', 'column2' => '...')
226 *
227 * @return ChangeColumnTypes
228 */
229 public function changeColumnTypes($table, $columns)
230 {
231 $table = $this->prefixTable($table);
232
233 return $this->container->make('Piwik\Updater\Migration\Db\ChangeColumnTypes', array(
234 'table' => $table, 'columns' => $columns
235 ));
236 }
237
238 /**
239 * Adds an index to an existing database table.
240 *
241 * This is equivalent to an `ADD INDEX indexname (column_name_1, column_name_2)` in SQL.
242 * It adds a normal index, no unique index.
243 *
244 * Note: If no indexName is specified, it will automatically generate a name for this index if which is basically:
245 * `'index_' . implode('_', $columnNames)`. If a column name is eg `column1(10)` then only the first part (`column1`)
246 * will be used. For example when using columns `array('column1', 'column2(10)')` then the index name will be
247 * `index_column1_column2`.
248 *
249 * @param string $table Unprefixed database table name, eg 'log_visit'.
250 * @param string[]|string $columnNames Either one or multiple column names, eg array('column_name_1', 'column_name_2').
251 * A column name can be appended by a number bracket eg "column_name_1(10)".
252 * @param string $indexName If specified, the given index name will be used instead of the automatically generated one.
253 * @return AddIndex
254 */
255 public function addIndex($table, $columnNames, $indexName = '')
256 {
257 $table = $this->prefixTable($table);
258
259 if (!is_array($columnNames)) {
260 $columnNames = array($columnNames);
261 }
262
263 return $this->container->make('Piwik\Updater\Migration\Db\AddIndex', array(
264 'table' => $table, 'columnNames' => $columnNames, 'indexName' => $indexName
265 ));
266 }
267
268 /**
269 * Adds a unique key to an existing database table.
270 *
271 * This is equivalent to an `ADD UNIQUE KEY indexname (column_name_1, column_name_2)` in SQL.
272 *
273 * Note: If no indexName is specified, it will automatically generate a name for this index if which is basically:
274 * `'index_' . implode('_', $columnNames)`. If a column name is eg `column1(10)` then only the first part (`column1`)
275 * will be used. For example when using columns `array('column1', 'column2(10)')` then the index name will be
276 * `index_column1_column2`.
277 *
278 * @param string $table Unprefixed database table name, eg 'log_visit'.
279 * @param string[]|string $columnNames Either one or multiple column names, eg array('column_name_1', 'column_name_2').
280 * A column name can be appended by a number bracket eg "column_name_1(10)".
281 * @param string $indexName If specified, the given unique key name will be used instead of the automatically generated one.
282 * @return AddIndex
283 */
284 public function addUniqueKey($table, $columnNames, $indexName = '')
285 {
286 $table = $this->prefixTable($table);
287
288 if (!is_array($columnNames)) {
289 $columnNames = array($columnNames);
290 }
291
292 return $this->container->make('Piwik\Updater\Migration\Db\AddUniqueKey', array(
293 'table' => $table, 'columnNames' => $columnNames, 'indexName' => $indexName
294 ));
295 }
296
297 /**
298 * Drops an existing index from a database table.
299 *
300 * @param string $table Unprefixed database table name, eg 'log_visit'.
301 * @param string $indexName The name of the index that shall be dropped.
302 * @return DropIndex
303 */
304 public function dropIndex($table, $indexName)
305 {
306 $table = $this->prefixTable($table);
307
308 return $this->container->make('Piwik\Updater\Migration\Db\DropIndex', array(
309 'table' => $table, '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 * @return DropIndex
318 */
319 public function dropPrimaryKey($table)
320 {
321 $table = $this->prefixTable($table);
322
323 return $this->container->make('Piwik\Updater\Migration\Db\DropPrimaryKey', array(
324 'table' => $table
325 ));
326 }
327
328 /**
329 * Adds a primary key to an existing database table.
330 *
331 * This is equivalent to an `ADD PRIMARY KEY(column_name_1, column_name_2)` in SQL.
332 *
333 * @param string $table Unprefixed database table name, eg 'log_visit'.
334 * @param string[]|string $columnNames Either one or multiple column names, eg array('column_name_1', 'column_name_2')
335 * @return AddPrimaryKey
336 */
337 public function addPrimaryKey($table, $columnNames)
338 {
339 $table = $this->prefixTable($table);
340 if (!is_array($columnNames)) {
341 $columnNames = array($columnNames);
342 }
343
344 return $this->container->make('Piwik\Updater\Migration\Db\AddPrimaryKey', array(
345 'table' => $table, 'columnNames' => $columnNames
346 ));
347 }
348
349 /**
350 * Inserts a new record / row into an existing database table.
351 *
352 * Make sure to specify all columns that need to be defined in order to insert a value successfully. There could
353 * be for example columns that are not nullable and therefore need a value.
354 *
355 * @param string $table Unprefixed database table name, eg 'log_visit'.
356 * @param array $columnValuePairs An array containing column => value pairs. For example:
357 * array('column_name_1' => 'value1', 'column_name_2' => 'value2')
358 * @return Insert
359 */
360 public function insert($table, $columnValuePairs)
361 {
362 $table = $this->prefixTable($table);
363
364 return $this->container->make('Piwik\Updater\Migration\Db\Insert', array(
365 'table' => $table, 'columnValuePairs' => $columnValuePairs
366 ));
367 }
368
369 /**
370 * Performs a batch insert into a specific table using either LOAD DATA INFILE or plain INSERTs,
371 * as a fallback. On MySQL, LOAD DATA INFILE is 20x faster than a series of plain INSERTs.
372 *
373 * Please note that queries for batch inserts are currently not shown to an end user and should therefore not be
374 * returned in an `Updates::getMigrations` method. Instead it needs to be execute directly in `Updates::doUpdate`
375 * via `$updater->executeMigration($factory->dbBatchInsert(...));`
376 *
377 * @param string $table Unprefixed database table name, eg 'log_visit'.
378 * @param string[] $columnNames An array of unquoted column names, eg array('column_name1', 'column_name_2')
379 * @param array $values An array of data to be inserted, eg array(array('row1column1', 'row1column2'),array('row2column1', 'row2column2'))
380 * @param bool $throwException Whether to throw an exception that was caught while trying LOAD DATA INFILE, or not.
381 * @param string $charset The charset to use, defaults to utf8
382 * @return BatchInsert
383 */
384 public function batchInsert($table, $columnNames, $values, $throwException = false, $charset = 'utf8')
385 {
386 $table = $this->prefixTable($table);
387
388 return $this->container->make('Piwik\Updater\Migration\Db\BatchInsert', array(
389 'table' => $table, 'columnNames' => $columnNames, 'values' => $values,
390 'throwException' => $throwException, 'charset' => $charset
391 ));
392 }
393
394 private function prefixTable($table)
395 {
396 return Common::prefixTable($table);
397 }
398 }
399