Automattic\WooCommerce\Database\Migrations
MetaToMetaTableMigrator::generate_update_sql_for_batch
Generate update SQL for given batch.
Method of the class: MetaToMetaTableMigrator{}
No Hooks.
Returns
String. Query to update batch records.
Usage
// private - for code of main (parent) class only $result = $this->generate_update_sql_for_batch( $batch ): string;
- $batch(array) (required)
- List of data to generate update SQL for. Should be in same format as output of
$this->fetch_data_for_migration_for_ids.
MetaToMetaTableMigrator::generate_update_sql_for_batch() MetaToMetaTableMigrator::generate update sql for batch code WC 10.5.0
private function generate_update_sql_for_batch( array $batch ): string {
global $wpdb;
$table = $this->schema_config['destination']['meta']['table_name'];
$meta_id_column = $this->schema_config['destination']['meta']['meta_id_column'];
$meta_key_column = $this->schema_config['destination']['meta']['meta_key_column'];
$meta_value_column = $this->schema_config['destination']['meta']['meta_value_column'];
$entity_id_column = $this->schema_config['destination']['meta']['entity_id_column'];
$columns = array( $meta_id_column, $entity_id_column, $meta_key_column, $meta_value_column );
$columns_sql = implode( '`, `', $columns );
$entity_id_column_placeholder = MigrationHelper::get_wpdb_placeholder_for_type( $this->schema_config['destination']['meta']['entity_id_type'] );
$placeholder_string = "%d, $entity_id_column_placeholder, %s, %s";
$values = array();
foreach ( $batch as $entity_id => $rows ) {
foreach ( $rows as $meta_key => $meta_details ) {
// phpcs:disable WordPress.DB.PreparedSQL, WordPress.DB.PreparedSQLPlaceholders
$values[] = $wpdb->prepare(
"( $placeholder_string )",
array( $meta_details['id'], $entity_id, $meta_key, $meta_details['meta_value'] )
);
// phpcs:enable
}
}
$value_sql = implode( ',', $values );
$on_duplicate_key_clause = MigrationHelper::generate_on_duplicate_statement_clause( $columns );
return "INSERT INTO $table ( `$columns_sql` ) VALUES $value_sql $on_duplicate_key_clause";
}