Automattic\WooCommerce\Database\Migrations

MetaToMetaTableMigrator::build_meta_table_query()privateWC 1.0

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

{} Это метод класса: MetaToMetaTableMigrator{}

Хуков нет.

Возвращает

Строку. Query that can be used to fetch data.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->build_meta_table_query( $entity_ids ): string;
$entity_ids(массив) (обязательный)
List of entity IDs to build meta query for.

Код MetaToMetaTableMigrator::build_meta_table_query() WC 6.8.0

private function build_meta_table_query( array $entity_ids ): string {
	global $wpdb;
	$source_meta_table        = $this->schema_config['source']['meta']['table_name'];
	$source_meta_key_column   = $this->schema_config['source']['meta']['meta_key_column'];
	$source_meta_value_column = $this->schema_config['source']['meta']['meta_value_column'];
	$source_entity_id_column  = $this->schema_config['source']['meta']['entity_id_column'];
	$order_by                 = "source.$source_entity_id_column ASC";

	$where_clause = "source.`$source_entity_id_column` IN (" . implode( ', ', array_fill( 0, count( $entity_ids ), '%d' ) ) . ')';

	$entity_table                  = $this->schema_config['source']['entity']['table_name'];
	$entity_id_column              = $this->schema_config['source']['entity']['id_column'];
	$entity_meta_id_mapping_column = $this->schema_config['source']['entity']['source_id_column'];

	if ( $this->schema_config['source']['excluded_keys'] ) {
		$key_placeholder = implode( ',', array_fill( 0, count( $this->schema_config['source']['excluded_keys'] ), '%s' ) );
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $source_meta_key_column is escaped for backticks, $key_placeholder is hardcoded.
		$exclude_clause = $wpdb->prepare( "source.$source_meta_key_column NOT IN ( $key_placeholder )", $this->schema_config['source']['excluded_keys'] );
		$where_clause   = "$where_clause AND $exclude_clause";
	}

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
	return $wpdb->prepare(
		"
SELECT
source.`$source_entity_id_column` as source_entity_id,
entity.`$entity_id_column` as entity_id,
source.`$source_meta_key_column` as meta_key,
source.`$source_meta_value_column` as meta_value
FROM `$source_meta_table` source
JOIN `$entity_table` entity ON entity.`$entity_meta_id_mapping_column` = source.`$source_entity_id_column`
WHERE $where_clause ORDER BY $order_by
",
		$entity_ids
	);
	// phpcs:enable
}