Automattic\WooCommerce\Database\Migrations

MetaToCustomTableMigrator::build_entity_table_query()privateWC 1.0

Helper method to build query used to fetch data from core source table.

Method of the class: MetaToCustomTableMigrator{}

No Hooks.

Return

String. Query that can be used to fetch data.

Usage

// private - for code of main (parent) class only
$result = $this->build_entity_table_query( $entity_ids ): string;
$entity_ids(array) (required)
List of entity IDs to fetch.

MetaToCustomTableMigrator::build_entity_table_query() code WC 7.1.0

private function build_entity_table_query( array $entity_ids ): string {
	global $wpdb;

	$source_entity_table       = $this->schema_config['source']['entity']['table_name'];
	$source_meta_rel_id_column = "`$source_entity_table`.`{$this->schema_config['source']['entity']['meta_rel_column']}`";
	$source_primary_key_column = "`$source_entity_table`.`{$this->schema_config['source']['entity']['primary_key']}`";

	$where_clause = "$source_primary_key_column IN (" . implode( ',', array_fill( 0, count( $entity_ids ), '%d' ) ) . ')';
	$entity_keys  = array();
	foreach ( $this->core_column_mapping as $column_name => $column_schema ) {
		if ( isset( $column_schema['select_clause'] ) ) {
			$select_clause = $column_schema['select_clause'];
			$entity_keys[] = "$select_clause AS $column_name";
		} else {
			$entity_keys[] = "$source_entity_table.$column_name";
		}
	}
	$entity_column_string = implode( ', ', $entity_keys );
	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $source_meta_rel_id_column, $source_destination_rel_id_column etc is escaped for backticks. $where clause and $order_by should already be escaped.
	$query = $wpdb->prepare(
		"
SELECT
$source_meta_rel_id_column as entity_meta_rel_id,
    $source_primary_key_column as primary_key_id,
$entity_column_string
FROM `$source_entity_table`
WHERE $where_clause;
",
		$entity_ids
	);

	// phpcs:enable

	return $query;
}