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.

Method of the class: 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.

MetaToCustomTableMigrator::generate_column_clauses() code WC 8.9.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'] );
	}

	$values = array();
	foreach ( array_values( $batch ) as $row ) {
		$row_values = array();
		foreach ( $columns as $index => $column ) {
			if ( ! isset( $row[ $column ] ) || is_null( $row[ $column ] ) ) {
				$row_values[] = 'NULL';
			} else {
				// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- $placeholders is a placeholder.
				$row_values[] = $wpdb->prepare( $placeholders[ $index ], $row[ $column ] );
			}
		}

		$value_string = '(' . implode( ',', $row_values ) . ')';
		$values[]     = $value_string;
	}

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

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

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