@@ -2958,41 +2958,64 @@ private function execute_show_table_status_statement( WP_Parser_Node $node ): vo
29582958 // LIKE and WHERE clauses.
29592959 $ like_or_where = $ node ->get_first_child_node ( 'likeOrWhere ' );
29602960 if ( null !== $ like_or_where ) {
2961- $ condition = $ this ->translate_show_like_or_where_condition ( $ like_or_where , 'table_name ' );
2961+ $ condition = $ this ->translate_show_like_or_where_condition ( $ like_or_where , 'Name ' );
29622962 }
29632963
2964- // Fetch table information.
2965- $ tables_tables = $ this ->information_schema_builder ->get_table_name (
2966- false , // SHOW TABLE STATUS lists only non-temporary tables.
2967- 'tables '
2964+ // SHOW TABLE STATUS lists only non-temporary tables.
2965+ $ tables_table = $ this ->information_schema_builder ->get_table_name ( false , 'tables ' );
2966+ $ columns_table = $ this ->information_schema_builder ->get_table_name ( false , 'columns ' );
2967+
2968+ // Compose a subquery to compute auto-increment values.
2969+ $ has_sequence_table = (bool ) $ this ->execute_sqlite_query (
2970+ "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'sqlite_sequence' "
2971+ )->fetchColumn ();
2972+
2973+ $ auto_increment_subquery = sprintf (
2974+ "(
2975+ SELECT COALESCE(s.seq + 1, 1)
2976+ FROM %s AS c
2977+ %s
2978+ WHERE c.extra = 'auto_increment'
2979+ AND c.table_schema = t.table_schema
2980+ AND c.table_name = t.table_name
2981+ ) " ,
2982+ $ this ->quote_sqlite_identifier ( $ columns_table ),
2983+ $ has_sequence_table
2984+ ? 'LEFT JOIN main.sqlite_sequence AS s ON s.name = c.table_name '
2985+ : 'LEFT JOIN (SELECT 0 AS seq) AS s '
29682986 );
2969- $ query = sprintf (
2970- 'SELECT
2971- table_name AS `Name`,
2972- engine AS `Engine`,
2973- version AS `Version`,
2974- row_format AS `Row_format`,
2975- table_rows AS `Rows`,
2976- avg_row_length AS `Avg_row_length`,
2977- data_length AS `Data_length`,
2978- max_data_length AS `Max_data_length`,
2979- index_length AS `Index_length`,
2980- data_free AS `Data_free`,
2981- auto_increment AS `Auto_increment`,
2982- create_time AS `Create_time`,
2983- update_time AS `Update_time`,
2984- check_time AS `Check_time`,
2985- table_collation AS `Collation`,
2986- checksum AS `Checksum`,
2987- create_options AS `Create_options`,
2988- table_comment AS `Comment`
2989- FROM %s
2990- WHERE table_schema = ? %s
2991- ORDER BY table_name ' ,
2992- $ this ->quote_sqlite_identifier ( $ tables_tables ),
2987+
2988+ $ query = sprintf (
2989+ 'SELECT * FROM (
2990+ SELECT
2991+ table_name AS `Name`,
2992+ engine AS `Engine`,
2993+ version AS `Version`,
2994+ row_format AS `Row_format`,
2995+ table_rows AS `Rows`,
2996+ avg_row_length AS `Avg_row_length`,
2997+ data_length AS `Data_length`,
2998+ max_data_length AS `Max_data_length`,
2999+ index_length AS `Index_length`,
3000+ data_free AS `Data_free`,
3001+ %s AS `Auto_increment`,
3002+ create_time AS `Create_time`,
3003+ update_time AS `Update_time`,
3004+ check_time AS `Check_time`,
3005+ table_collation AS `Collation`,
3006+ checksum AS `Checksum`,
3007+ create_options AS `Create_options`,
3008+ table_comment AS `Comment`
3009+ FROM %s AS t
3010+ WHERE table_schema = ?
3011+ )
3012+ WHERE 1 %s
3013+ ORDER BY `Name` ' ,
3014+ $ auto_increment_subquery ,
3015+ $ this ->quote_sqlite_identifier ( $ tables_table ),
29933016 $ condition ?? ''
29943017 );
2995- $ params = array (
3018+ $ params = array (
29963019 $ this ->get_saved_db_name ( $ database ),
29973020 );
29983021
@@ -4697,7 +4720,7 @@ public function translate_table_ref( WP_Parser_Node $node ): string {
46974720 $ table_name = $ this ->unquote_sqlite_identifier ( $ this ->translate ( $ table ) );
46984721
46994722 // When the table reference targets an information schema table,
4700- // we need to inject the configured database name dynamically.
4723+ // we need to inject some additional values dynamically.
47014724 if (
47024725 ( null === $ schema_name && 'information_schema ' === $ this ->db_name )
47034726 || ( null !== $ schema_name && 'information_schema ' === strtolower ( $ schema_name ) )
@@ -4743,14 +4766,40 @@ public function translate_table_ref( WP_Parser_Node $node ): string {
47434766 $ expanded_list = array ();
47444767 foreach ( $ columns as $ column ) {
47454768 $ quoted_column = $ this ->quote_sqlite_identifier ( $ column );
4746- if ( isset ( $ information_schema_db_column_map [ strtoupper ( $ column ) ] ) ) {
4769+ if ( isset ( $ information_schema_db_column_map [ $ column ] ) ) {
4770+ // Replace the database name with the configured database name.
47474771 $ expanded_list [] = sprintf (
47484772 "CASE WHEN %s = 'information_schema' THEN %s ELSE %s END AS %s " ,
47494773 $ quoted_column ,
47504774 $ quoted_column ,
47514775 $ this ->quote_sqlite_value ( $ this ->main_db_name ),
4752- strtoupper ( $ quoted_column )
4776+ $ quoted_column
4777+ );
4778+ } elseif ( 'tables ' === $ table_name && 'AUTO_INCREMENT ' === $ column ) {
4779+ // Inject the auto-increment values.
4780+ $ columns_table = $ this ->information_schema_builder ->get_table_name ( false , 'columns ' );
4781+ $ has_sequence_table = (bool ) $ this ->execute_sqlite_query (
4782+ "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'sqlite_sequence' "
4783+ )->fetchColumn ();
4784+
4785+ $ auto_increment_subquery = sprintf (
4786+ "(
4787+ SELECT COALESCE(s.seq + 1, 1)
4788+ FROM %s AS c
4789+ %s
4790+ WHERE c.extra = 'auto_increment'
4791+ AND c.table_schema = %s.table_schema
4792+ AND c.table_name = %s.table_name
4793+ ) " ,
4794+ $ this ->quote_sqlite_identifier ( $ columns_table ),
4795+ $ has_sequence_table
4796+ ? 'LEFT JOIN main.sqlite_sequence AS s ON s.name = c.table_name '
4797+ : 'LEFT JOIN (SELECT 0 AS seq) AS s ' ,
4798+ $ this ->quote_sqlite_identifier ( $ table_name ),
4799+ $ this ->quote_sqlite_identifier ( $ table_name )
47534800 );
4801+
4802+ $ expanded_list [] = sprintf ( '%s AS %s ' , $ auto_increment_subquery , $ quoted_column );
47544803 } else {
47554804 $ expanded_list [] = $ quoted_column ;
47564805 }
@@ -6303,7 +6352,8 @@ private function get_mysql_create_table_statement( bool $table_is_temporary, str
63036352 )->fetchAll ( PDO ::FETCH_ASSOC );
63046353
63056354 // 6. Generate CREATE TABLE statement columns.
6306- $ rows = array ();
6355+ $ rows = array ();
6356+ $ has_auto_increment = false ;
63076357 foreach ( $ column_info as $ column ) {
63086358 $ sql = ' ' ;
63096359 $ sql .= $ this ->quote_mysql_identifier ( $ column ['COLUMN_NAME ' ] );
@@ -6315,7 +6365,8 @@ private function get_mysql_create_table_statement( bool $table_is_temporary, str
63156365 $ sql .= ' NULL ' ;
63166366 }
63176367 if ( 'auto_increment ' === $ column ['EXTRA ' ] ) {
6318- $ sql .= ' AUTO_INCREMENT ' ;
6368+ $ has_auto_increment = true ;
6369+ $ sql .= ' AUTO_INCREMENT ' ;
63196370 }
63206371
63216372 // Handle DEFAULT CURRENT_TIMESTAMP. This works only with timestamp
@@ -6454,6 +6505,28 @@ function ( $column ) {
64546505 $ sql .= implode ( ", \n" , $ rows );
64556506 $ sql .= "\n) " ;
64566507 $ sql .= sprintf ( ' ENGINE=%s ' , $ table_info ['ENGINE ' ] );
6508+
6509+ // Add "AUTO_INCREMENT=N" if a sequence exists and has been advanced.
6510+ if ( $ has_auto_increment ) {
6511+ try {
6512+ $ seq = (int ) $ this ->execute_sqlite_query (
6513+ sprintf (
6514+ 'SELECT seq FROM %s.sqlite_sequence WHERE name = ? ' ,
6515+ $ table_is_temporary ? 'temp ' : 'main '
6516+ ),
6517+ array ( $ table_name )
6518+ )->fetchColumn ();
6519+ } catch ( PDOException $ e ) {
6520+ if ( ! str_contains ( $ e ->getMessage (), 'no such table ' ) ) {
6521+ throw $ e ;
6522+ }
6523+ $ seq = 0 ;
6524+ }
6525+ if ( $ seq > 0 ) {
6526+ $ sql .= sprintf ( ' AUTO_INCREMENT=%d ' , $ seq + 1 );
6527+ }
6528+ }
6529+
64576530 $ sql .= sprintf ( ' DEFAULT CHARSET=%s ' , $ charset );
64586531 $ sql .= sprintf ( ' COLLATE=%s ' , $ collation );
64596532 if ( '' !== $ table_info ['TABLE_COMMENT ' ] ) {
0 commit comments