@@ -290,7 +290,8 @@ public function receive_bytes( string $data ): ?string {
290290 $ this ->close ( self ::CLOSE_CLIENT_QUIT );
291291 return '' ;
292292 case MySQL_Protocol::COM_INIT_DB :
293- return $ this ->process_query ( $ this ->build_use_query ( substr ( $ payload , 1 ) ) );
293+ return $ this ->use_database ( substr ( $ payload , 1 ) )
294+ ?? MySQL_Protocol ::build_ok_packet ( $ this ->packet_id ++, $ this ->status_flags );
294295 case MySQL_Protocol::COM_QUERY :
295296 return $ this ->process_query ( substr ( $ payload , 1 ) );
296297 case MySQL_Protocol::COM_PING :
@@ -499,20 +500,10 @@ private function complete_authentication( string $username, string $plugin, stri
499500
500501 // No adapter calls are allowed until the credentials have been verified.
501502 if ( '' !== $ database ) {
502- try {
503- $ result = $ this ->adapter ->handle_query ( $ this ->build_use_query ( $ database ) );
504- $ failed = (bool ) $ result ->error_info ;
505- } catch ( Throwable $ e ) {
506- $ failed = true ;
507- }
508- if ( $ failed ) {
503+ $ error = $ this ->use_database ( $ database );
504+ if ( null !== $ error ) {
509505 $ this ->close ( self ::CLOSE_LOGIN_FAILED );
510- return MySQL_Protocol ::build_err_packet (
511- $ this ->packet_id ++,
512- 1049 ,
513- '42000 ' ,
514- sprintf ( "Unknown database: '%s' " , $ database )
515- );
506+ return $ error ;
516507 }
517508 }
518509
@@ -550,13 +541,36 @@ private function close( string $reason ): void {
550541 }
551542
552543 /**
553- * Build a USE statement for a client-supplied database name.
544+ * Select a client-supplied database, for COM_INIT_DB and the login.
545+ *
546+ * The name is quoted as an identifier. Like MySQL, any failure is reported
547+ * as an unknown database. The adapter's reason goes to the log.
554548 *
555549 * @param string $database The database name.
556- * @return string The USE statement with a quoted identifier .
550+ * @return string|null The error packet, or null when the database is selected .
557551 */
558- private function build_use_query ( string $ database ): string {
559- return 'USE ` ' . str_replace ( '` ' , '`` ' , $ database ) . '` ' ;
552+ private function use_database ( string $ database ): ?string {
553+ try {
554+ $ result = $ this ->adapter ->handle_query ( 'USE ` ' . str_replace ( '` ' , '`` ' , $ database ) . '` ' );
555+ if ( ! $ result ->error_info ) {
556+ return null ;
557+ }
558+ $ this ->logger ->warning (
559+ 'Client [{client_id}]: cannot select a database: {error} ' ,
560+ array (
561+ 'client_id ' => $ this ->connection_id ,
562+ 'error ' => $ result ->error_info [2 ],
563+ )
564+ );
565+ } catch ( Throwable $ e ) {
566+ $ this ->log_internal_error ( $ e );
567+ }
568+ return MySQL_Protocol ::build_err_packet (
569+ $ this ->packet_id ++,
570+ 1049 ,
571+ '42000 ' ,
572+ sprintf ( "Unknown database '%s' " , $ database )
573+ );
560574 }
561575
562576 /**
@@ -595,18 +609,28 @@ private function process_query( string $query ): string {
595609 $ result ->last_insert_id
596610 );
597611 } catch ( Throwable $ e ) {
598- // Keep internal details, such as file paths, out of the client's error.
599- $ this ->logger ->error (
600- 'Client [{client_id}]: internal error: {error} ' ,
601- array (
602- 'client_id ' => $ this ->connection_id ,
603- 'error ' => $ e ->getMessage (),
604- )
605- );
612+ $ this ->log_internal_error ( $ e );
606613 return MySQL_Protocol ::build_err_packet ( $ this ->packet_id ++, 1105 , 'HY000 ' , 'Unknown error ' );
607614 }
608615 }
609616
617+ /**
618+ * Log an internal error, which must not reach the client.
619+ *
620+ * Its message can contain internal details, such as file paths.
621+ *
622+ * @param Throwable $error The internal error.
623+ */
624+ private function log_internal_error ( Throwable $ error ): void {
625+ $ this ->logger ->error (
626+ 'Client [{client_id}]: internal error: {error} ' ,
627+ array (
628+ 'client_id ' => $ this ->connection_id ,
629+ 'error ' => $ error ->getMessage (),
630+ )
631+ );
632+ }
633+
610634 /**
611635 * Build the result set packets for a MySQL query.
612636 *
0 commit comments