Automattic\WooCommerce\Blocks

Installer::maybe_create_table()protectedWC 1.0

Create database table, if it doesn't already exist.

Based on admin/install-helper.php maybe_create_table function.

Method of the class: Installer{}

No Hooks.

Return

true|false. False on error, true if already exists or success.

Usage

// protected - for code of main (parent) or child class
$result = $this->maybe_create_table( $table_name, $create_sql );
$table_name(string) (required)
Database table name.
$create_sql(string) (required)
Create database table SQL.

Installer::maybe_create_table() code WC 8.7.0

protected function maybe_create_table( $table_name, $create_sql ) {
	global $wpdb;

	if ( in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true ) ) {
		return true;
	}

	$wpdb->query( $create_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

	return in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true );
}