PHP Class Piwik\Updater\Migration\Db\Factory

Show file Open project: piwik/piwik

Public Methods

Method Description
__construct ( )
addColumn ( string $table, string $columnName, string $columnType, string | null $placeColumnAfter = null ) : AddColumn Adds a new database table column to an existing table.
addColumns ( string $table, array $columns, string | null $placeColumnAfter = null ) : AddColumns Adds multiple new database table columns to an existing table at once.
addIndex ( string $table, string[] | string $columnNames, string $indexName = '' ) : AddIndex Adds an index to an existing database table.
addPrimaryKey ( string $table, string[] | string $columnNames ) : AddPrimaryKey Adds a primary key to an existing database table.
addUniqueKey ( string $table, string[] | string $columnNames, string $indexName = '' ) : AddIndex Adds a unique key to an existing database table.
batchInsert ( string $table, string[] $columnNames, array $values, boolean $throwException = false, string $charset = 'utf8' ) : BatchInsert Performs a batch insert into a specific table using either LOAD DATA INFILE or plain INSERTs, as a fallback. On MySQL, LOAD DATA INFILE is 20x faster than a series of plain INSERTs.
boundSql ( string $sql, array $bind, integer | int[] $errorCodesToIgnore = [] ) : BoundSql Performs a custom SQL query that uses bound parameters during the update.
changeColumn ( string $table, string $oldColumnName, string $newColumnName, string $columnType ) : ChangeColumn Changes the column name and column type of an existing database table column.
changeColumnType ( string $table, string $columnName, string $columnType ) : ChangeColumnType Changes the type of an existing database table column.
changeColumnTypes ( string $table, array $columns ) : ChangeColumnTypes Changes the type of multiple existing database table columns at the same time.
createTable ( string $table, array $columnNames, string | string[] $primaryKey = [] ) : CreateTable Creates a new database table.
dropColumn ( string $table, string $columnName ) : DropColumn Drops an existing database table column.
dropIndex ( string $table, string $indexName ) : DropIndex Drops an existing index from a database table.
dropTable ( string $table ) : DropTable Drops an existing database table.
insert ( string $table, array $columnValuePairs ) : Insert Inserts a new record / row into an existing database table.
sql ( string $sql, integer | int[] $errorCodesToIgnore = [] ) : Sql Performs a custom SQL query during the update.

Private Methods

Method Description
prefixTable ( $table )

Method Details

__construct() public method

public __construct ( )

addColumn() public method

Adds a new database table column to an existing table.
public addColumn ( string $table, string $columnName, string $columnType, string | null $placeColumnAfter = null ) : AddColumn
$table string Unprefixed database table name, eg 'log_visit'.
$columnName string The name of the column that shall be added, eg 'my_column_name'.
$columnType string The column type it should have, eg 'VARCHAR(200) NOT NULL'.
$placeColumnAfter string | null If specified, the added column will be added after this specified column name. If you specify a column be sure it actually exists and can be added after this column.
return AddColumn

addColumns() public method

Adding multiple columns at the same time can lead to performance improvements compared to adding each new column separately.
public addColumns ( string $table, array $columns, string | null $placeColumnAfter = null ) : AddColumns
$table string Unprefixed database table name, eg 'log_visit'.
$columns array An array of column name to column type pairs, eg array('my_column_name' => 'VARCHAR(200) NOT NULL', 'column2' => '...')
$placeColumnAfter string | null If specified, the first added column will be added after this specified column name. All following columns will be added after the previous specified in $columns. If you specify a column be sure it actually exists and can be added after this column.
return AddColumns

addIndex() public method

This is equivalent to an ADD INDEX indexname (column_name_1, column_name_2) in SQL. It adds a normal index, no unique index. Note: If no indexName is specified, it will automatically generate a name for this index if which is basically: 'index_' . implode('_', $columnNames). If a column name is eg column1(10) then only the first part (column1) will be used. For example when using columns array('column1', 'column2(10)') then the index name will be index_column1_column2.
public addIndex ( string $table, string[] | string $columnNames, string $indexName = '' ) : AddIndex
$table string Unprefixed database table name, eg 'log_visit'.
$columnNames string[] | string Either one or multiple column names, eg array('column_name_1', 'column_name_2'). A column name can be appended by a number bracket eg "column_name_1(10)".
$indexName string If specified, the given index name will be used instead of the automatically generated one.
return AddIndex

addPrimaryKey() public method

This is equivalent to an ADD PRIMARY KEY(column_name_1, column_name_2) in SQL.
public addPrimaryKey ( string $table, string[] | string $columnNames ) : AddPrimaryKey
$table string Unprefixed database table name, eg 'log_visit'.
$columnNames string[] | string Either one or multiple column names, eg array('column_name_1', 'column_name_2')
return AddPrimaryKey

addUniqueKey() public method

This is equivalent to an ADD UNIQUE KEY indexname (column_name_1, column_name_2) in SQL. Note: If no indexName is specified, it will automatically generate a name for this index if which is basically: 'index_' . implode('_', $columnNames). If a column name is eg column1(10) then only the first part (column1) will be used. For example when using columns array('column1', 'column2(10)') then the index name will be index_column1_column2.
public addUniqueKey ( string $table, string[] | string $columnNames, string $indexName = '' ) : AddIndex
$table string Unprefixed database table name, eg 'log_visit'.
$columnNames string[] | string Either one or multiple column names, eg array('column_name_1', 'column_name_2'). A column name can be appended by a number bracket eg "column_name_1(10)".
$indexName string If specified, the given unique key name will be used instead of the automatically generated one.
return AddIndex

batchInsert() public method

Please note that queries for batch inserts are currently not shown to an end user and should therefore not be returned in an Updates::getMigrations method. Instead it needs to be execute directly in Updates::doUpdate via $updater->executeMigration($factory->dbBatchInsert(...));
public batchInsert ( string $table, string[] $columnNames, array $values, boolean $throwException = false, string $charset = 'utf8' ) : BatchInsert
$table string Unprefixed database table name, eg 'log_visit'.
$columnNames string[] An array of unquoted column names, eg array('column_name1', 'column_name_2')
$values array An array of data to be inserted, eg array(array('row1column1', 'row1column2'),array('row2column1', 'row2column2'))
$throwException boolean Whether to throw an exception that was caught while trying LOAD DATA INFILE, or not.
$charset string The charset to use, defaults to utf8
return BatchInsert

boundSql() public method

You can replace values with a question mark and then pass the actual value via $bind for better security. Example: $factory->boundSql('DELETE * FROM table_name WHERE idsite = ?, array($idSite = 1));
public boundSql ( string $sql, array $bind, integer | int[] $errorCodesToIgnore = [] ) : BoundSql
$sql string The SQL query that should be executed. Make sure to prefix a table name via {@link Piwik\Commin::prefixTable()}.
$bind array An array of values that need to be replaced with the question marks in the SQL query.
$errorCodesToIgnore integer | int[] Any given MySQL server error code will be ignored. For a list of all possible error codes have a look at {@link \Piwik\Updater\Migration\Db}. If no error should be ignored use `false`.
return BoundSql

changeColumn() public method

Changes the column name and column type of an existing database table column.
public changeColumn ( string $table, string $oldColumnName, string $newColumnName, string $columnType ) : ChangeColumn
$table string Unprefixed database table name, eg 'log_visit'.
$oldColumnName string The current name of the column that shall be renamed/changed, eg 'column_name'.
$newColumnName string The new name of the column, eg 'new_column_name'.
$columnType string The updated type the new column should have, eg 'VARCHAR(200) NOT NULL'.
return ChangeColumn

changeColumnType() public method

Changes the type of an existing database table column.
public changeColumnType ( string $table, string $columnName, string $columnType ) : ChangeColumnType
$table string Unprefixed database table name, eg 'log_visit'.
$columnName string The name of the column that shall be changed, eg 'my_column_name'.
$columnType string The updated type the column should have, eg 'VARCHAR(200) NOT NULL'.
return ChangeColumnType

changeColumnTypes() public method

Changing multiple columns at the same time can lead to performance improvements compared to changing the type of each column separately.
public changeColumnTypes ( string $table, array $columns ) : ChangeColumnTypes
$table string Unprefixed database table name, eg 'log_visit'.
$columns array An array of column name to column type pairs, eg array('my_column_name' => 'VARCHAR(200) NOT NULL', 'column2' => '...')
return ChangeColumnTypes

createTable() public method

Creates a new database table.
public createTable ( string $table, array $columnNames, string | string[] $primaryKey = [] ) : CreateTable
$table string Unprefixed database table name, eg 'log_visit'.
$columnNames array An array of column names and their type they should use. For example: array('column_name_1' => 'VARCHAR(200) NOT NULL', 'column_name_2' => 'INT(10) DEFAULT 0')
$primaryKey string | string[] Optional. One or multiple columns that shall define the primary key.
return CreateTable

dropColumn() public method

Drops an existing database table column.
public dropColumn ( string $table, string $columnName ) : DropColumn
$table string Unprefixed database table name, eg 'log_visit'.
$columnName string The name of the column that shall be dropped, eg 'my_column_name'.
return DropColumn

dropIndex() public method

Drops an existing index from a database table.
public dropIndex ( string $table, string $indexName ) : DropIndex
$table string Unprefixed database table name, eg 'log_visit'.
$indexName string The name of the index that shall be dropped.
return DropIndex

dropTable() public method

Drops an existing database table.
public dropTable ( string $table ) : DropTable
$table string Unprefixed database table name, eg 'log_visit'.
return DropTable

insert() public method

Make sure to specify all columns that need to be defined in order to insert a value successfully. There could be for example columns that are not nullable and therefore need a value.
public insert ( string $table, array $columnValuePairs ) : Insert
$table string Unprefixed database table name, eg 'log_visit'.
$columnValuePairs array An array containing column => value pairs. For example: array('column_name_1' => 'value1', 'column_name_2' => 'value2')
return Insert

sql() public method

Example: $factory->sql("DELETE * FROM table_name WHERE plugin_name = 'MyPluginName'");
public sql ( string $sql, integer | int[] $errorCodesToIgnore = [] ) : Sql
$sql string The SQL query that should be executed. Make sure to prefix a table name via {@link Piwik\Commin::prefixTable()}.
$errorCodesToIgnore integer | int[] Any given MySQL server error code will be ignored. For a list of all possible error codes have a look at {@link \Piwik\Updater\Migration\Db}. If no error should be ignored use an empty array or `false`.
return Sql