@@ -683,7 +683,7 @@ public Task InsertLogAsync(DateTime timestamp, DnsDatagram request, IPEndPoint r
683683
684684 public async Task < DnsLogPage > QueryLogsAsync ( long pageNumber , int entriesPerPage , bool descendingOrder , DateTime ? start , DateTime ? end , IPAddress ? clientIpAddress , DnsTransportProtocol ? protocol , DnsServerResponseType ? responseType , DnsResponseCode ? rcode , string ? qname , DnsResourceRecordType ? qtype , DnsClass ? qclass )
685685 {
686- if ( pageNumber < 1 )
686+ if ( pageNumber == 0 )
687687 pageNumber = 1 ;
688688
689689 if ( qname is not null )
@@ -727,6 +727,7 @@ public async Task<DnsLogPage> QueryLogsAsync(long pageNumber, int entriesPerPage
727727
728728 if ( qclass is not null )
729729 whereClause += "qclass = @qclass AND " ;
730+
730731 if ( ! string . IsNullOrEmpty ( whereClause ) )
731732 whereClause = whereClause . Substring ( 0 , whereClause . Length - 5 ) ;
732733
@@ -768,26 +769,21 @@ public async Task<DnsLogPage> QueryLogsAsync(long pageNumber, int entriesPerPage
768769 if ( qclass is not null )
769770 command . Parameters . AddWithValue ( "@qclass" , ( short ) qclass ) ;
770771
771- totalEntries = Convert . ToInt64 ( await command . ExecuteScalarAsync ( ) ?? 0L ) ;
772+ totalEntries = Convert . ToInt64 ( await command . ExecuteScalarAsync ( ) ) ;
772773 }
773774
774775 long totalPages = ( totalEntries / entriesPerPage ) + ( totalEntries % entriesPerPage > 0 ? 1 : 0 ) ;
775776
776777 if ( ( pageNumber > totalPages ) || ( pageNumber < 0 ) )
777778 pageNumber = totalPages ;
778779
779- if ( pageNumber < 1 )
780- pageNumber = 1 ;
781-
782780 long offset = ( pageNumber - 1 ) * entriesPerPage ;
783781
784782 List < DnsLogEntry > entries = new List < DnsLogEntry > ( entriesPerPage ) ;
785783
786- if ( totalEntries > 0 )
784+ await using ( MySqlCommand command = connection . CreateCommand ( ) )
787785 {
788- await using ( MySqlCommand command = connection . CreateCommand ( ) )
789- {
790- command . CommandText = @"
786+ command . CommandText = @"
791787SELECT
792788 dlid,
793789 timestamp,
@@ -806,70 +802,69 @@ public async Task<DnsLogPage> QueryLogsAsync(long pageNumber, int entriesPerPage
806802ORDER BY dlid" + ( descendingOrder ? " DESC" : "" ) + @"
807803LIMIT @limit OFFSET @offset" ;
808804
809- command . Parameters . AddWithValue ( "@limit" , entriesPerPage ) ;
810- command . Parameters . AddWithValue ( "@offset" , offset ) ;
805+ command . Parameters . AddWithValue ( "@limit" , entriesPerPage ) ;
806+ command . Parameters . AddWithValue ( "@offset" , offset ) ;
811807
812- if ( start is not null )
813- command . Parameters . AddWithValue ( "@start" , start ) ;
808+ if ( start is not null )
809+ command . Parameters . AddWithValue ( "@start" , start ) ;
814810
815- if ( end is not null )
816- command . Parameters . AddWithValue ( "@end" , end ) ;
811+ if ( end is not null )
812+ command . Parameters . AddWithValue ( "@end" , end ) ;
817813
818- if ( clientIpAddress is not null )
819- command . Parameters . AddWithValue ( "@client_ip" , clientIpAddress . ToString ( ) ) ;
814+ if ( clientIpAddress is not null )
815+ command . Parameters . AddWithValue ( "@client_ip" , clientIpAddress . ToString ( ) ) ;
820816
821- if ( protocol is not null )
822- command . Parameters . AddWithValue ( "@protocol" , ( byte ) protocol ) ;
817+ if ( protocol is not null )
818+ command . Parameters . AddWithValue ( "@protocol" , ( byte ) protocol ) ;
823819
824- if ( responseType is not null )
825- command . Parameters . AddWithValue ( "@response_type" , ( byte ) responseType ) ;
820+ if ( responseType is not null )
821+ command . Parameters . AddWithValue ( "@response_type" , ( byte ) responseType ) ;
826822
827- if ( rcode is not null )
828- command . Parameters . AddWithValue ( "@rcode" , ( byte ) rcode ) ;
823+ if ( rcode is not null )
824+ command . Parameters . AddWithValue ( "@rcode" , ( byte ) rcode ) ;
829825
830- if ( qname is not null )
831- command . Parameters . AddWithValue ( "@qname" , qname ) ;
826+ if ( qname is not null )
827+ command . Parameters . AddWithValue ( "@qname" , qname ) ;
832828
833- if ( qtype is not null )
834- command . Parameters . AddWithValue ( "@qtype" , ( short ) qtype ) ;
829+ if ( qtype is not null )
830+ command . Parameters . AddWithValue ( "@qtype" , ( short ) qtype ) ;
835831
836- if ( qclass is not null )
837- command . Parameters . AddWithValue ( "@qclass" , ( short ) qclass ) ;
832+ if ( qclass is not null )
833+ command . Parameters . AddWithValue ( "@qclass" , ( short ) qclass ) ;
838834
839- long rowNumber = descendingOrder ? totalEntries - offset : offset + 1 ;
835+ long rowNumber = descendingOrder ? totalEntries - offset : offset + 1 ;
840836
841- await using ( DbDataReader reader = await command . ExecuteReaderAsync ( ) )
837+ await using ( DbDataReader reader = await command . ExecuteReaderAsync ( ) )
838+ {
839+ while ( await reader . ReadAsync ( ) )
842840 {
843- while ( await reader . ReadAsync ( ) )
844- {
845- double ? responseRtt ;
841+ double ? responseRtt ;
846842
847- if ( reader . IsDBNull ( 5 ) )
848- responseRtt = null ;
849- else
850- responseRtt = reader . GetFloat ( 5 ) ;
843+ if ( reader . IsDBNull ( 5 ) )
844+ responseRtt = null ;
845+ else
846+ responseRtt = reader . GetFloat ( 5 ) ;
851847
852- DnsQuestionRecord ? question ;
848+ DnsQuestionRecord ? question ;
853849
854- if ( reader . IsDBNull ( 7 ) )
855- question = null ;
856- else
857- question = new DnsQuestionRecord ( reader . GetString ( 7 ) , ( DnsResourceRecordType ) reader . GetInt16 ( 8 ) , ( DnsClass ) reader . GetInt16 ( 9 ) , false ) ;
850+ if ( reader . IsDBNull ( 7 ) )
851+ question = null ;
852+ else
853+ question = new DnsQuestionRecord ( reader . GetString ( 7 ) , ( DnsResourceRecordType ) reader . GetInt16 ( 8 ) , ( DnsClass ) reader . GetInt16 ( 9 ) , false ) ;
858854
859- string ? answer ;
855+ string ? answer ;
860856
861- if ( reader . IsDBNull ( 10 ) )
862- answer = null ;
863- else
864- answer = reader . GetString ( 10 ) ;
857+ if ( reader . IsDBNull ( 10 ) )
858+ answer = null ;
859+ else
860+ answer = reader . GetString ( 10 ) ;
865861
866- entries . Add ( new DnsLogEntry ( rowNumber , reader . GetDateTime ( 1 ) , IPAddress . Parse ( reader . GetString ( 2 ) ) , ( DnsTransportProtocol ) reader . GetByte ( 3 ) , ( DnsServerResponseType ) reader . GetByte ( 4 ) , responseRtt , ( DnsResponseCode ) reader . GetByte ( 6 ) , question , answer ) ) ;
862+ entries . Add ( new DnsLogEntry ( rowNumber , reader . GetDateTime ( 1 ) , IPAddress . Parse ( reader . GetString ( 2 ) ) , ( DnsTransportProtocol ) reader . GetByte ( 3 ) , ( DnsServerResponseType ) reader . GetByte ( 4 ) , responseRtt , ( DnsResponseCode ) reader . GetByte ( 6 ) , question , answer ) ) ;
867863
868- if ( descendingOrder )
869- rowNumber -- ;
870- else
871- rowNumber ++ ;
872- }
864+ if ( descendingOrder )
865+ rowNumber -- ;
866+ else
867+ rowNumber ++ ;
873868 }
874869 }
875870 }
0 commit comments