Skip to content

Commit dbf54c8

Browse files
committed
Implement ANALYZE TABLE statement
1 parent 0d989b6 commit dbf54c8

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,9 @@ private function execute_mysql_query( WP_Parser_Node $node ): void {
786786
);
787787
}
788788
break;
789+
case 'tableAdministrationStatement':
790+
$this->execute_administration_statement( $node );
791+
break;
789792
default:
790793
throw $this->new_not_supported_exception(
791794
sprintf( 'statement type: "%s"', $node->rule_name )
@@ -1657,6 +1660,64 @@ private function execute_use_statement( WP_Parser_Node $node ): void {
16571660
}
16581661
}
16591662

1663+
/**
1664+
* Translate and execute a MySQL administration statement in SQLite.
1665+
*
1666+
* This emulates the following MySQL statements:
1667+
* - ANALYZE TABLE
1668+
*
1669+
* @param WP_Parser_Node $node A "tableAdministrationStatement" AST node.
1670+
* @throws WP_SQLite_Driver_Exception When the query execution fails.
1671+
*/
1672+
private function execute_administration_statement( WP_Parser_Node $node ): void {
1673+
$first_token = $node->get_first_child_token();
1674+
$table_ref_list = $node->get_first_child_node( 'tableRefList' );
1675+
$results = array();
1676+
foreach ( $table_ref_list->get_child_nodes( 'tableRef' ) as $table_ref ) {
1677+
$table_name = $this->unquote_sqlite_identifier( $this->translate( $table_ref ) );
1678+
$quoted_table_name = $this->quote_sqlite_identifier( $table_name );
1679+
try {
1680+
switch ( $first_token->id ) {
1681+
case WP_MySQL_Lexer::ANALYZE_SYMBOL:
1682+
$stmt = $this->execute_sqlite_query( "ANALYZE $quoted_table_name" );
1683+
$errors = $stmt->fetchAll( PDO::FETCH_COLUMN );
1684+
break;
1685+
default:
1686+
throw $this->new_not_supported_exception(
1687+
sprintf(
1688+
'statement type: "%s" > "%s"',
1689+
$node->rule_name,
1690+
$first_token->value
1691+
)
1692+
);
1693+
}
1694+
} catch ( PDOException $e ) {
1695+
if ( 'HY000' === $e->getCode() ) {
1696+
$errors = array( "Table '$table_name' doesn't exist" );
1697+
} else {
1698+
$errors = array( $e->getMessage() );
1699+
}
1700+
}
1701+
1702+
$operation = strtolower( $first_token->value );
1703+
foreach ( $errors as $error ) {
1704+
$results[] = (object) array(
1705+
'Table' => $this->db_name . '.' . $table_name,
1706+
'Op' => $operation,
1707+
'Msg_type' => 'Error',
1708+
'Msg_text' => $error,
1709+
);
1710+
}
1711+
$results[] = (object) array(
1712+
'Table' => $this->db_name . '.' . $table_name,
1713+
'Op' => $operation,
1714+
'Msg_type' => 'status',
1715+
'Msg_text' => count( $errors ) > 0 ? 'Operation failed' : 'OK',
1716+
);
1717+
}
1718+
$this->set_results_from_fetched_data( $results );
1719+
}
1720+
16601721
/**
16611722
* Translate a MySQL AST node or token to an SQLite query fragment.
16621723
*

0 commit comments

Comments
 (0)