@@ -206,6 +206,7 @@ static void dbDisconnect(char *host);
206
206
static void DBerror (MYSQL * mysql , const char * when );
207
207
static void safe_exit (int error );
208
208
static void print_result ();
209
+ static uint fixed_name_length (const char * name );
209
210
static char * fix_table_name (char * dest , char * src );
210
211
int what_to_do = 0 ;
211
212
@@ -438,14 +439,14 @@ static int process_selected_tables(char *db, char **table_names, int tables)
438
439
{
439
440
/*
440
441
We need table list in form `a`, `b`, `c`
441
- that's why we need 4 more chars added to to each table name
442
+ that's why we need 2 more chars added to to each table name
442
443
space is for more readable output in logs and in case of error
443
444
*/
444
445
char * table_names_comma_sep , * end ;
445
446
int i , tot_length = 0 ;
446
447
447
448
for (i = 0 ; i < tables ; i ++ )
448
- tot_length += strlen (* (table_names + i )) + 4 ;
449
+ tot_length += fixed_name_length (* (table_names + i )) + 2 ;
449
450
450
451
if (!(table_names_comma_sep = (char * )
451
452
my_malloc ((sizeof (char ) * tot_length ) + 4 , MYF (MY_WME ))))
@@ -463,23 +464,46 @@ static int process_selected_tables(char *db, char **table_names, int tables)
463
464
}
464
465
else
465
466
for (; tables > 0 ; tables -- , table_names ++ )
466
- handle_request_for_tables (* table_names , strlen (* table_names ));
467
+ handle_request_for_tables (* table_names , fixed_name_length (* table_names ));
467
468
return 0 ;
468
469
} /* process_selected_tables */
469
470
470
471
471
- static char * fix_table_name ( char * dest , char * src )
472
+ static uint fixed_name_length ( const char * name )
472
473
{
473
- char * db_sep ;
474
+ const char * p ;
475
+ uint extra_length = 2 ; /* count the first/last backticks */
476
+
477
+ for (p = name ; * p ; p ++ )
478
+ {
479
+ if (* p == '`' )
480
+ extra_length ++ ;
481
+ else if (* p == '.' )
482
+ extra_length += 2 ;
483
+ }
484
+ return (p - name ) + extra_length ;
485
+ }
486
+
474
487
488
+ static char * fix_table_name (char * dest , char * src )
489
+ {
475
490
* dest ++ = '`' ;
476
- if (( db_sep = strchr ( src , '.' )) )
491
+ for (; * src ; src ++ )
477
492
{
478
- dest = strmake (dest , src , (uint ) (db_sep - src ));
479
- dest = strmov (dest , "`.`" );
480
- src = db_sep + 1 ;
493
+ switch (* src ) {
494
+ case '.' : /* add backticks around '.' */
495
+ * dest ++ = '`' ;
496
+ * dest ++ = '.' ;
497
+ * dest ++ = '`' ;
498
+ break ;
499
+ case '`' : /* escape backtick character */
500
+ * dest ++ = '`' ;
501
+ /* fall through */
502
+ default :
503
+ * dest ++ = * src ;
504
+ }
481
505
}
482
- dest = strxmov ( dest , src , "`" , NullS ) ;
506
+ * dest ++ = '`' ;
483
507
return dest ;
484
508
}
485
509
@@ -500,15 +524,15 @@ static int process_all_tables_in_db(char *database)
500
524
{
501
525
/*
502
526
We need table list in form `a`, `b`, `c`
503
- that's why we need 4 more chars added to to each table name
527
+ that's why we need 2 more chars added to to each table name
504
528
space is for more readable output in logs and in case of error
505
529
*/
506
530
507
531
char * tables , * end ;
508
532
uint tot_length = 0 ;
509
533
510
534
while ((row = mysql_fetch_row (res )))
511
- tot_length += strlen (row [0 ]) + 4 ;
535
+ tot_length += fixed_name_length (row [0 ]) + 2 ;
512
536
mysql_data_seek (res , 0 );
513
537
514
538
if (!(tables = (char * ) my_malloc (sizeof (char )* tot_length + 4 , MYF (MY_WME ))))
@@ -536,7 +560,7 @@ static int process_all_tables_in_db(char *database)
536
560
/* Skip tables with an engine of NULL (probably a view). */
537
561
if (row [1 ])
538
562
{
539
- handle_request_for_tables (row [0 ], strlen (row [0 ]));
563
+ handle_request_for_tables (row [0 ], fixed_name_length (row [0 ]));
540
564
}
541
565
}
542
566
mysql_free_result (res );
@@ -826,7 +850,7 @@ int main(int argc, char **argv)
826
850
for (i = 0 ; i < tables4repair .elements ; i ++ )
827
851
{
828
852
char * name = (char * ) dynamic_array_ptr (& tables4repair , i );
829
- handle_request_for_tables (name , strlen (name ));
853
+ handle_request_for_tables (name , fixed_name_length (name ));
830
854
}
831
855
}
832
856
end :
0 commit comments