Skip to content

Commit 8809a4e

Browse files
committed
Query Logs Apps: fixed page number related bug introduced in PR #1702 that caused the last page navigation feature, which uses "-1" value, to stop working.
1 parent 7e375ba commit 8809a4e

3 files changed

Lines changed: 145 additions & 162 deletions

File tree

Apps/QueryLogsMySqlApp/App.cs

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = @"
791787
SELECT
792788
dlid,
793789
timestamp,
@@ -806,70 +802,69 @@ public async Task<DnsLogPage> QueryLogsAsync(long pageNumber, int entriesPerPage
806802
ORDER BY dlid" + (descendingOrder ? " DESC" : "") + @"
807803
LIMIT @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
}

Apps/QueryLogsSqlServerApp/App.cs

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public Task InsertLogAsync(DateTime timestamp, DnsDatagram request, IPEndPoint r
579579

580580
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)
581581
{
582-
if (pageNumber < 1)
582+
if (pageNumber == 0)
583583
pageNumber = 1;
584584

585585
if (qname is not null)
@@ -665,26 +665,21 @@ public async Task<DnsLogPage> QueryLogsAsync(long pageNumber, int entriesPerPage
665665
if (qclass is not null)
666666
command.Parameters.AddWithValue("@qclass", (short)qclass);
667667

668-
totalEntries = Convert.ToInt64(await command.ExecuteScalarAsync() ?? 0L);
668+
totalEntries = Convert.ToInt64(await command.ExecuteScalarAsync());
669669
}
670670

671671
long totalPages = (totalEntries / entriesPerPage) + (totalEntries % entriesPerPage > 0 ? 1 : 0);
672672

673673
if ((pageNumber > totalPages) || (pageNumber < 0))
674674
pageNumber = totalPages;
675675

676-
if (pageNumber < 1)
677-
pageNumber = 1;
678-
679676
long offset = (pageNumber - 1) * entriesPerPage;
680677

681678
List<DnsLogEntry> entries = new List<DnsLogEntry>(entriesPerPage);
682679

683-
if (totalEntries > 0)
680+
await using (SqlCommand command = connection.CreateCommand())
684681
{
685-
await using (SqlCommand command = connection.CreateCommand())
686-
{
687-
command.CommandText = @"
682+
command.CommandText = @"
688683
SELECT
689684
dlid,
690685
timestamp,
@@ -704,70 +699,69 @@ public async Task<DnsLogPage> QueryLogsAsync(long pageNumber, int entriesPerPage
704699
OFFSET @offset ROWS
705700
FETCH NEXT @limit ROWS ONLY";
706701

707-
command.Parameters.AddWithValue("@limit", entriesPerPage);
708-
command.Parameters.AddWithValue("@offset", offset);
702+
command.Parameters.AddWithValue("@limit", entriesPerPage);
703+
command.Parameters.AddWithValue("@offset", offset);
709704

710-
if (start is not null)
711-
command.Parameters.AddWithValue("@start", start);
705+
if (start is not null)
706+
command.Parameters.AddWithValue("@start", start);
712707

713-
if (end is not null)
714-
command.Parameters.AddWithValue("@end", end);
708+
if (end is not null)
709+
command.Parameters.AddWithValue("@end", end);
715710

716-
if (clientIpAddress is not null)
717-
command.Parameters.AddWithValue("@client_ip", clientIpAddress.ToString());
711+
if (clientIpAddress is not null)
712+
command.Parameters.AddWithValue("@client_ip", clientIpAddress.ToString());
718713

719-
if (protocol is not null)
720-
command.Parameters.AddWithValue("@protocol", (byte)protocol);
714+
if (protocol is not null)
715+
command.Parameters.AddWithValue("@protocol", (byte)protocol);
721716

722-
if (responseType is not null)
723-
command.Parameters.AddWithValue("@response_type", (byte)responseType);
717+
if (responseType is not null)
718+
command.Parameters.AddWithValue("@response_type", (byte)responseType);
724719

725-
if (rcode is not null)
726-
command.Parameters.AddWithValue("@rcode", (byte)rcode);
720+
if (rcode is not null)
721+
command.Parameters.AddWithValue("@rcode", (byte)rcode);
727722

728-
if (qname is not null)
729-
command.Parameters.AddWithValue("@qname", qname);
723+
if (qname is not null)
724+
command.Parameters.AddWithValue("@qname", qname);
730725

731-
if (qtype is not null)
732-
command.Parameters.AddWithValue("@qtype", (short)qtype);
726+
if (qtype is not null)
727+
command.Parameters.AddWithValue("@qtype", (short)qtype);
733728

734-
if (qclass is not null)
735-
command.Parameters.AddWithValue("@qclass", (short)qclass);
729+
if (qclass is not null)
730+
command.Parameters.AddWithValue("@qclass", (short)qclass);
736731

737-
long rowNumber = descendingOrder ? totalEntries - offset : offset + 1;
732+
long rowNumber = descendingOrder ? totalEntries - offset : offset + 1;
738733

739-
await using (SqlDataReader reader = await command.ExecuteReaderAsync())
734+
await using (SqlDataReader reader = await command.ExecuteReaderAsync())
735+
{
736+
while (await reader.ReadAsync())
740737
{
741-
while (await reader.ReadAsync())
742-
{
743-
double? responseRtt;
738+
double? responseRtt;
744739

745-
if (reader.IsDBNull(5))
746-
responseRtt = null;
747-
else
748-
responseRtt = reader.GetFloat(5);
740+
if (reader.IsDBNull(5))
741+
responseRtt = null;
742+
else
743+
responseRtt = reader.GetFloat(5);
749744

750-
DnsQuestionRecord? question;
745+
DnsQuestionRecord? question;
751746

752-
if (reader.IsDBNull(7))
753-
question = null;
754-
else
755-
question = new DnsQuestionRecord(reader.GetString(7), (DnsResourceRecordType)reader.GetInt16(8), (DnsClass)reader.GetInt16(9), false);
747+
if (reader.IsDBNull(7))
748+
question = null;
749+
else
750+
question = new DnsQuestionRecord(reader.GetString(7), (DnsResourceRecordType)reader.GetInt16(8), (DnsClass)reader.GetInt16(9), false);
756751

757-
string? answer;
752+
string? answer;
758753

759-
if (reader.IsDBNull(10))
760-
answer = null;
761-
else
762-
answer = reader.GetString(10);
754+
if (reader.IsDBNull(10))
755+
answer = null;
756+
else
757+
answer = reader.GetString(10);
763758

764-
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));
759+
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));
765760

766-
if (descendingOrder)
767-
rowNumber--;
768-
else
769-
rowNumber++;
770-
}
761+
if (descendingOrder)
762+
rowNumber--;
763+
else
764+
rowNumber++;
771765
}
772766
}
773767
}

0 commit comments

Comments
 (0)