Automattic\WooCommerce\Internal\Utilities

DatabaseUtil::get_index_columns()publicWC 1.0

Get the columns of a given table index, or of the primary key.

Method of the class: DatabaseUtil{}

No Hooks.

Return

Array. The index columns. Empty array if the table or the index don't exist.

Usage

$DatabaseUtil = new DatabaseUtil();
$DatabaseUtil->get_index_columns( $table_name, $index_name ): array;
$table_name(string) (required)
Table name.
$index_name(string)
Index name, empty string for the primary key.
Default: ''

DatabaseUtil::get_index_columns() code WC 8.9.0

public function get_index_columns( string $table_name, string $index_name = '' ): array {
	global $wpdb;

	if ( empty( $index_name ) ) {
		$index_name = 'PRIMARY';
	}

	// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$results = $wpdb->get_results( $wpdb->prepare( "SHOW INDEX FROM $table_name WHERE Key_name = %s", $index_name ) );

	if ( empty( $results ) ) {
		return array();
	}

	return array_column( $results, 'Column_name' );
}