HttpHandler.queries.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using BeetleX.Light.Memory;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. namespace PlatformBenchmarks
  9. {
  10. public partial class HttpHandler
  11. {
  12. public async ValueTask queries(string queryString, IStreamWriter stream)
  13. {
  14. int count = 1;
  15. if (!string.IsNullOrEmpty(queryString))
  16. {
  17. var values = queryString.Split('=');
  18. if (values.Length > 1)
  19. {
  20. if (int.TryParse(values[1], out int size))
  21. {
  22. count = size;
  23. }
  24. }
  25. }
  26. if (count > 500)
  27. count = 500;
  28. if (count < 1)
  29. count = 1;
  30. ContentLengthMemory content = new ContentLengthMemory();
  31. try
  32. {
  33. var data = await DB.LoadMultipleQueriesRows(count);
  34. stream.Write(_jsonResultPreamble.Data, 0, _jsonResultPreamble.Length);
  35. content.Data = GetContentLengthMemory(stream);
  36. GMTDate.Default.Write(stream);
  37. stream.WriteSequenceNetStream.StartWriteLength();
  38. var jsonWriter = GetJsonWriter(stream);
  39. using (var unflush = stream.UnFlush())
  40. {
  41. jsonWriter.WriteStartArray();
  42. foreach (var item in data)
  43. {
  44. jsonWriter.WriteStartObject();
  45. jsonWriter.WriteNumber("Id", item.Id);
  46. jsonWriter.WriteNumber("RandomNumber", item.RandomNumber);
  47. jsonWriter.WriteEndObject();
  48. }
  49. jsonWriter.WriteEndArray();
  50. jsonWriter.Flush();
  51. }
  52. }
  53. catch (Exception e_)
  54. {
  55. Context.GetLoger(BeetleX.Light.Logs.LogLevel.Error)?.WriteException(Context, "PlatformBenchmarks", "queries", e_);
  56. stream.WriteString(e_.Message);
  57. }
  58. var len = stream.WriteSequenceNetStream.EndWriteLength();
  59. content.Full(len);
  60. }
  61. }
  62. }