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