Automattic\WooCommerce\Database\Migrations

MetaToMetaTableMigrator::generate_delete_sql_for_batchprivateWC 10.4.0

Generate delete SQL for given batch.

Method of the class: MetaToMetaTableMigrator{}

No Hooks.

Returns

String.

Usage

// private - for code of main (parent) class only
$result = $this->generate_delete_sql_for_batch( $batch ): string;
$batch(array) (required)
List of data to generate delete SQL for. Should be in same format as output of $this->fetch_data_for_migration_for_ids.

Changelog

Since 10.4.0 Introduced.

MetaToMetaTableMigrator::generate_delete_sql_for_batch() code WC 10.4.3

private function generate_delete_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'];
	$entity_id_column      = $this->schema_config['destination']['meta']['entity_id_column'];
	$entity_id_placeholder = MigrationHelper::get_wpdb_placeholder_for_type( $this->schema_config['destination']['meta']['entity_id_type'] );

	$clauses = array();

	foreach ( $batch as $entity_id => $metas ) {
		$meta_ids = array_column(
			array_reduce( $metas, 'array_merge', array() ),
			$meta_id_column
		);

		if ( ! $meta_ids ) {
			continue;
		}

		$meta_id_placeholders = implode( ',', array_fill( 0, count( $meta_ids ), '%d' ) );

		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
		$clauses[] = $wpdb->prepare(
			"( %i = {$entity_id_placeholder} AND %i IN ({$meta_id_placeholders}) )",
			$entity_id_column,
			$entity_id,
			$meta_id_column,
			...$meta_ids
		);
		// phpcs:enable
	}

	if ( ! $clauses ) {
		return '';
	}

	$clauses_sql = implode( ' OR ', $clauses );

	return "DELETE FROM {$table} WHERE {$clauses_sql}";
}