Automattic\WooCommerce\Database\Migrations

MetaToCustomTableMigrator::generate_column_clauses()privateWC 1.0

Generate values and columns clauses to be used in INSERT and INSERT..ON DUPLICATE KEY UPDATE statements.

Метод класса: MetaToCustomTableMigrator{}

No Hooks.

Return

Array. SQL clause for values, columns placeholders, and columns.

Usage

// private - for code of main (parent) class only
$result = $this->generate_column_clauses( $columns_schema, $batch ): array;
$columns_schema(array) (required)
Columns config for destination table.
$batch(array) (required)
Actual data to migrate as returned by data in fetch_data_for_migration_for_ids method.

Code of MetaToCustomTableMigrator::generate_column_clauses() WC 7.1.0

private function generate_column_clauses( array $columns_schema, array $batch ): array {
	global $wpdb;

	$columns      = array();
	$placeholders = array();
	foreach ( $columns_schema as $prev_column => $schema ) {
		if ( in_array( $schema['destination'], $columns, true ) ) {
			continue;
		}
		$columns[]      = $schema['destination'];
		$placeholders[] = MigrationHelper::get_wpdb_placeholder_for_type( $schema['type'] );
	}
	$placeholders = "'" . implode( "', '", $placeholders ) . "'";

	$values = array();
	foreach ( array_values( $batch ) as $row ) {
		$query_params = array();
		foreach ( $columns as $column ) {
			$query_params[] = $row[ $column ] ?? null;
		}
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $placeholders can only contain combination of placeholders described in MigrationHelper::get_wpdb_placeholder_for_type
		$value_string = '(' . $wpdb->prepare( $placeholders, $query_params ) . ')';
		$values[]     = $value_string;
	}

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

	$column_sql = implode( '`, `', $columns );

	return array( $value_sql, $column_sql, $columns );
}