array.hxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** Handling of SQL arrays.
  2. *
  3. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/field instead.
  4. *
  5. * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
  6. *
  7. * See COPYING for copyright license. If you did not receive a file called
  8. * COPYING with this source code, please notify the distributor of this mistake,
  9. * or contact the author.
  10. */
  11. #ifndef PQXX_H_ARRAY
  12. #define PQXX_H_ARRAY
  13. #include "pqxx/compiler-public.hxx"
  14. #include "pqxx/compiler-internal-pre.hxx"
  15. #include "pqxx/internal/encoding_group.hxx"
  16. #include "pqxx/internal/encodings.hxx"
  17. #include <stdexcept>
  18. #include <string>
  19. #include <utility>
  20. namespace pqxx
  21. {
  22. /// Low-level array parser.
  23. /** Use this to read an array field retrieved from the database.
  24. *
  25. * This parser will only work reliably if your client encoding is UTF-8, ASCII,
  26. * or a single-byte encoding which is a superset of ASCII (such as Latin-1).
  27. *
  28. * Also, the parser only supports array element types which use either a comma
  29. * or a semicolon ("," or ";") as the separator between array elements. All
  30. * built-in types use comma, except for one which uses semicolon, but some
  31. * custom types may not work.
  32. *
  33. * The input is a C-style string containing the textual representation of an
  34. * array, as returned by the database. The parser reads this representation
  35. * on the fly. The string must remain in memory until parsing is done.
  36. *
  37. * Parse the array by making calls to @c get_next until it returns a
  38. * @c juncture of "done". The @c juncture tells you what the parser found in
  39. * that step: did the array "nest" to a deeper level, or "un-nest" back up?
  40. */
  41. class PQXX_LIBEXPORT array_parser
  42. {
  43. public:
  44. /// What's the latest thing found in the array?
  45. enum juncture
  46. {
  47. /// Starting a new row.
  48. row_start,
  49. /// Ending the current row.
  50. row_end,
  51. /// Found a NULL value.
  52. null_value,
  53. /// Found a string value.
  54. string_value,
  55. /// Parsing has completed.
  56. done,
  57. };
  58. // XXX: Actually _pass_ encoding group!
  59. /// Constructor. You don't need this; use @c field::as_array instead.
  60. explicit array_parser(
  61. const char input[],
  62. internal::encoding_group=internal::encoding_group::MONOBYTE);
  63. /// Parse the next step in the array.
  64. /** Returns what it found. If the juncture is @c string_value, the string
  65. * will contain the value. Otherwise, it will be empty.
  66. *
  67. * Call this until the @c juncture it returns is @c done.
  68. */
  69. std::pair<juncture, std::string> get_next();
  70. private:
  71. const char *const m_input;
  72. const std::string::size_type m_end;
  73. internal::glyph_scanner_func *const m_scan;
  74. /// Current parsing position in the input.
  75. std::string::size_type m_pos;
  76. std::string::size_type scan_single_quoted_string() const;
  77. std::string parse_single_quoted_string(std::string::size_type end) const;
  78. std::string::size_type scan_double_quoted_string() const;
  79. std::string parse_double_quoted_string(std::string::size_type end) const;
  80. std::string::size_type scan_unquoted_string() const;
  81. std::string parse_unquoted_string(std::string::size_type end) const;
  82. std::string::size_type scan_glyph(std::string::size_type pos) const;
  83. std::string::size_type scan_glyph(
  84. std::string::size_type pos,
  85. std::string::size_type end) const;
  86. };
  87. } // namespace pqxx
  88. #include "pqxx/compiler-internal-post.hxx"
  89. #endif