@@ -334,26 +334,18 @@ internal ConnectorState State
334334 /// Returns whether the connector is open, regardless of any task it is currently performing
335335 /// </summary>
336336 bool IsConnected
337- {
338- get
337+ => State switch
339338 {
340- switch ( State )
341- {
342- case ConnectorState . Ready :
343- case ConnectorState . Executing :
344- case ConnectorState . Fetching :
345- case ConnectorState . Waiting :
346- case ConnectorState . Copy :
347- return true ;
348- case ConnectorState . Closed :
349- case ConnectorState . Connecting :
350- case ConnectorState . Broken :
351- return false ;
352- default :
353- throw new ArgumentOutOfRangeException ( "Unknown state: " + State ) ;
354- }
355- }
356- }
339+ ConnectorState . Ready => true ,
340+ ConnectorState . Executing => true ,
341+ ConnectorState . Fetching => true ,
342+ ConnectorState . Waiting => true ,
343+ ConnectorState . Copy => true ,
344+ ConnectorState . Closed => false ,
345+ ConnectorState . Connecting => false ,
346+ ConnectorState . Broken => false ,
347+ _ => throw new ArgumentOutOfRangeException ( "Unknown state: " + State )
348+ } ;
357349
358350 internal bool IsReady => State == ConnectorState . Ready ;
359351 internal bool IsClosed => State == ConnectorState . Closed ;
@@ -1008,55 +1000,41 @@ internal ValueTask<IBackendMessage> ReadMessage(bool async, DataRowLoadingMode d
10081000
10091001 case BackendMessageCode. AuthenticationRequest:
10101002 var authType = ( AuthenticationRequestType) buf. ReadInt32( ) ;
1011- switch ( authType)
1003+ return authType switch
10121004 {
1013- case AuthenticationRequestType. AuthenticationOk:
1014- return AuthenticationOkMessage. Instance;
1015- case AuthenticationRequestType. AuthenticationCleartextPassword:
1016- return AuthenticationCleartextPasswordMessage. Instance;
1017- case AuthenticationRequestType. AuthenticationMD5Password:
1018- return AuthenticationMD5PasswordMessage. Load( buf) ;
1019- case AuthenticationRequestType. AuthenticationGSS:
1020- return AuthenticationGSSMessage. Instance;
1021- case AuthenticationRequestType. AuthenticationSSPI:
1022- return AuthenticationSSPIMessage. Instance;
1023- case AuthenticationRequestType. AuthenticationGSSContinue:
1024- return AuthenticationGSSContinueMessage. Load( buf, len) ;
1025- case AuthenticationRequestType. AuthenticationSASL:
1026- return new AuthenticationSASLMessage( buf) ;
1027- case AuthenticationRequestType. AuthenticationSASLContinue:
1028- return new AuthenticationSASLContinueMessage( buf, len - 4 ) ;
1029- case AuthenticationRequestType. AuthenticationSASLFinal:
1030- return new AuthenticationSASLFinalMessage( buf, len - 4 ) ;
1031- default :
1032- throw new NotSupportedException( $"Authentication method not supported ( Received: { authType} ) ") ;
1033- }
1005+ AuthenticationRequestType. AuthenticationOk => ( AuthenticationRequestMessage) AuthenticationOkMessage. Instance,
1006+ AuthenticationRequestType. AuthenticationCleartextPassword => AuthenticationCleartextPasswordMessage. Instance,
1007+ AuthenticationRequestType. AuthenticationMD5Password => AuthenticationMD5PasswordMessage. Load( buf) ,
1008+ AuthenticationRequestType. AuthenticationGSS => AuthenticationGSSMessage. Instance,
1009+ AuthenticationRequestType. AuthenticationSSPI => AuthenticationSSPIMessage. Instance,
1010+ AuthenticationRequestType. AuthenticationGSSContinue => AuthenticationGSSContinueMessage. Load( buf, len) ,
1011+ AuthenticationRequestType. AuthenticationSASL => new AuthenticationSASLMessage( buf) ,
1012+ AuthenticationRequestType. AuthenticationSASLContinue => new AuthenticationSASLContinueMessage( buf, len - 4 ) ,
1013+ AuthenticationRequestType. AuthenticationSASLFinal => new AuthenticationSASLFinalMessage( buf, len - 4 ) ,
1014+ _ => throw new NotSupportedException( $"Authentication method not supported ( Received: { authType} ) ")
1015+ } ;
10341016
10351017 case BackendMessageCode. BackendKeyData:
10361018 return new BackendKeyDataMessage( buf) ;
10371019
10381020 case BackendMessageCode. CopyInResponse:
1039- _copyInResponseMessage ??= new CopyInResponseMessage( ) ;
1040- return _copyInResponseMessage. Load( ReadBuffer) ;
1041-
1021+ return ( _copyInResponseMessage ??= new CopyInResponseMessage( ) ) . Load( ReadBuffer) ;
10421022 case BackendMessageCode. CopyOutResponse:
1043- _copyOutResponseMessage ??= new CopyOutResponseMessage( ) ;
1044- return _copyOutResponseMessage. Load( ReadBuffer) ;
1045-
1023+ return ( _copyOutResponseMessage ??= new CopyOutResponseMessage( ) ) . Load( ReadBuffer) ;
10461024 case BackendMessageCode. CopyData:
1047- _copyDataMessage ??= new CopyDataMessage( ) ;
1048- return _copyDataMessage. Load( len) ;
1049-
1025+ return ( _copyDataMessage ??= new CopyDataMessage( ) ) . Load( len) ;
10501026 case BackendMessageCode. CopyDone:
10511027 return CopyDoneMessage. Instance;
10521028
10531029 case BackendMessageCode. PortalSuspended:
10541030 throw new NpgsqlException( "Unimplemented message: " + code) ;
10551031 case BackendMessageCode. ErrorResponse:
10561032 return null ;
1033+
10571034 case BackendMessageCode. FunctionCallResponse:
10581035 // We don't use the obsolete function call protocol
10591036 throw new NpgsqlException( "Unexpected backend message: " + code) ;
1037+
10601038 default :
10611039 throw new InvalidOperationException( $"Internal Npgsql bug: unexpected value { code} of enum { nameof( BackendMessageCode) } . Please file a bug. ") ;
10621040 }
@@ -1093,22 +1071,15 @@ internal Task Rollback(bool async)
10931071 }
10941072
10951073 internal bool InTransaction
1096- {
1097- get
1074+ => TransactionStatus switch
10981075 {
1099- switch ( TransactionStatus)
1100- {
1101- case TransactionStatus. Idle:
1102- return false;
1103- case TransactionStatus. Pending:
1104- case TransactionStatus. InTransactionBlock:
1105- case TransactionStatus. InFailedTransactionBlock:
1106- return true;
1107- default :
1108- throw new InvalidOperationException( $"Internal Npgsql bug: unexpected value { TransactionStatus} of enum { nameof( TransactionStatus) } . Please file a bug. ") ;
1109- }
1110- }
1111- }
1076+ TransactionStatus. Idle => false,
1077+ TransactionStatus. Pending => true,
1078+ TransactionStatus. InTransactionBlock => true,
1079+ TransactionStatus. InFailedTransactionBlock => true,
1080+ _ => throw new InvalidOperationException( $"Internal Npgsql bug: unexpected value { TransactionStatus} of enum { nameof( TransactionStatus) } . Please file a bug. ")
1081+ } ;
1082+
11121083 /// <summary>
11131084 /// Handles a new transaction indicator received on a ReadyForQuery message
11141085 /// </summary>
@@ -1117,17 +1088,14 @@ void ProcessNewTransactionStatus(TransactionStatus newStatus)
11171088 if ( newStatus == TransactionStatus)
11181089 return ;
11191090
1120- switch ( newStatus) {
1121- case TransactionStatus. Idle:
1122- case TransactionStatus. InTransactionBlock:
1123- case TransactionStatus. InFailedTransactionBlock:
1124- TransactionStatus = newStatus;
1125- return ;
1126- case TransactionStatus. Pending:
1127- throw new Exception( "Invalid TransactionStatus ( should be frontend- only) ") ;
1128- default :
1129- throw new InvalidOperationException( $"Internal Npgsql bug: unexpected value { newStatus} of enum { nameof( TransactionStatus) } . Please file a bug. ") ;
1130- }
1091+ TransactionStatus = newStatus switch
1092+ {
1093+ TransactionStatus. Idle => newStatus,
1094+ TransactionStatus. InTransactionBlock => newStatus,
1095+ TransactionStatus. InFailedTransactionBlock => newStatus,
1096+ TransactionStatus. Pending => throw new Exception( "Invalid TransactionStatus ( should be frontend- only) ") ,
1097+ _ => throw new InvalidOperationException( $"Internal Npgsql bug: unexpected value { newStatus} of enum { nameof( TransactionStatus) } . Please file a bug. ")
1098+ } ;
11311099 }
11321100
11331101 void ClearTransaction( )
0 commit comments