> */ private $columns = array(); /** @var array */ private $keys = array(); public function __construct( \wpdb $db ) { $this->db = $db; } /** * Base tables (not views) whose names start with $prefix, sorted. * * @return string[] */ public function tables( string $prefix ): array { $rows = $this->db->get_results( "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'", ARRAY_N ); $this->assertOk(); $names = array(); foreach ( (array) $rows as $row ) { $name = (string) $row[0]; if ( 0 !== strpos( $name, $prefix ) ) { continue; } // Our own scratch tables must never be dumped or treated as site data. if ( 0 === strpos( $name, 'omtmp_' ) || 0 === strpos( $name, 'omold_' ) ) { continue; } $names[] = $name; } sort( $names, SORT_STRING ); return $names; } /** * Approximate row counts, enough for a progress bar. * * @return array */ public function rowEstimates(): array { $rows = $this->db->get_results( 'SHOW TABLE STATUS', ARRAY_A ); $this->assertOk(); $out = array(); foreach ( (array) $rows as $row ) { $out[ (string) $row['Name'] ] = (int) ( $row['Rows'] ?? 0 ); } return $out; } /** * @return array Average row length in bytes per table. */ public function averageRowLengths(): array { $rows = $this->db->get_results( 'SHOW TABLE STATUS', ARRAY_A ); $this->assertOk(); $out = array(); foreach ( (array) $rows as $row ) { $out[ (string) $row['Name'] ] = (int) ( $row['Avg_row_length'] ?? 0 ); } return $out; } /** * Columns in table order. Generated columns are flagged because they cannot be INSERTed. * * @return array */ public function columns( string $table ): array { if ( isset( $this->columns[ $table ] ) ) { return $this->columns[ $table ]; } $rows = $this->db->get_results( 'SHOW FULL COLUMNS FROM ' . Sql::ident( $table ), ARRAY_A ); $this->assertOk(); $columns = array(); foreach ( (array) $rows as $row ) { $extra = strtoupper( (string) ( $row['Extra'] ?? '' ) ); $columns[] = array( 'name' => (string) $row['Field'], 'type' => strtolower( (string) $row['Type'] ), // "DEFAULT_GENERATED" is an ordinary column with an expression default; only // VIRTUAL/STORED/PERSISTENT columns are computed and must be skipped. 'generated' => 1 === preg_match( '/(VIRTUAL|STORED|PERSISTENT)( GENERATED)?/', $extra ) && false === strpos( $extra, 'DEFAULT_GENERATED' ), ); } $this->columns[ $table ] = $columns; return $columns; } /** * Primary key columns in key order; empty when the table has none. * * @return string[] */ public function primaryKey( string $table ): array { if ( isset( $this->keys[ $table ] ) ) { return $this->keys[ $table ]; } $rows = $this->db->get_results( 'SHOW KEYS FROM ' . Sql::ident( $table ) . " WHERE Key_name = 'PRIMARY'", ARRAY_A ); $this->assertOk(); usort( $rows, static function ( $a, $b ) { return (int) $a['Seq_in_index'] <=> (int) $b['Seq_in_index']; } ); $this->keys[ $table ] = array_map( static function ( $row ) { return (string) $row['Column_name']; }, $rows ); return $this->keys[ $table ]; } /** * @return array{name:string,type:string,generated:bool}|null */ public function column( string $table, string $name ): ?array { foreach ( $this->columns( $table ) as $column ) { if ( $column['name'] === $name ) { return $column; } } return null; } /** * @throws \RuntimeException */ private function assertOk(): void { if ( '' !== $this->db->last_error ) { throw new \RuntimeException( 'Database error: ' . esc_html( $this->db->last_error ) ); } } }