Automattic\WooCommerce\Database\Migrations

MetaToMetaTableMigrator::generate_insert_sql_for_batch()privateWC 1.0

Generate insert sql queries for batches.

{} It's a method of the class: MetaToMetaTableMigrator{}

No Hooks.

Return

String. Insert SQL query.

Usage

// private - for code of main (parent) class only
$result = $this->generate_insert_sql_for_batch( $batch ): string;
$batch(array) (required)
Data to generate queries for.

Code of MetaToMetaTableMigrator::generate_insert_sql_for_batch() WC 7.1.0

private function generate_insert_sql_for_batch( array $batch ): string {
	global $wpdb;

	$table             = $this->schema_config['destination']['meta']['table_name'];
	$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'];
	$column_sql        = "(`$entity_id_column`, `$meta_key_column`, `$meta_value_column`)";

	$entity_id_column_placeholder = MigrationHelper::get_wpdb_placeholder_for_type( $this->schema_config['destination']['meta']['entity_id_type'] );
	$placeholder_string           = "$entity_id_column_placeholder, %s, %s";
	$values                       = array();
	foreach ( $batch as $entity_id => $rows ) {
		foreach ( $rows as $meta_key => $meta_values ) {
			foreach ( $meta_values as $meta_value ) {
				$query_params = array(
					$entity_id,
					$meta_key,
					$meta_value,
				);
				// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders
				$value_sql = $wpdb->prepare( "$placeholder_string", $query_params );
				$values[]  = $value_sql;
			}
		}
	}

	$values_sql = implode( '), (', $values );

	return "INSERT IGNORE INTO $table $column_sql VALUES ($values_sql)";
}