test_prepared_statement.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #include <cassert>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <list>
  5. #include <pqxx/transaction>
  6. #include "../test_helpers.hxx"
  7. // Test program for libpqxx. Define and use prepared statements.
  8. #define COMPARE_RESULTS(name, lhs, rhs) \
  9. PQXX_CHECK_EQUAL( \
  10. rhs, lhs, \
  11. "Executing " name " as prepared statement yields different results.");
  12. namespace
  13. {
  14. using namespace std::literals;
  15. template<typename T> std::string stringize(pqxx::transaction_base &t, T i)
  16. {
  17. return stringize(t, pqxx::to_string(i));
  18. }
  19. // Substitute variables in raw query. This is not likely to be very robust,
  20. // but it should do for just this test. The main shortcomings are escaping,
  21. // and not knowing when to quote the variables.
  22. // Note we do the replacement backwards (meaning forward_only iterators won't
  23. // do!) to avoid substituting e.g. "$12" as "$1" first.
  24. template<typename ITER>
  25. std::string
  26. subst(pqxx::transaction_base &t, std::string q, ITER patbegin, ITER patend)
  27. {
  28. ptrdiff_t i{distance(patbegin, patend)};
  29. for (ITER arg{patend}; i > 0; --i)
  30. {
  31. --arg;
  32. std::string const marker{"$" + pqxx::to_string(i)},
  33. var{stringize(t, *arg)};
  34. std::string::size_type const msz{std::size(marker)};
  35. while (q.find(marker) != std::string::npos)
  36. q.replace(q.find(marker), msz, var);
  37. }
  38. return q;
  39. }
  40. template<typename CNTNR>
  41. std::string
  42. subst(pqxx::transaction_base &t, std::string const &q, CNTNR const &patterns)
  43. {
  44. return subst(t, q, std::begin(patterns), std::end(patterns));
  45. }
  46. void test_registration_and_invocation()
  47. {
  48. constexpr auto count_to_5{"SELECT * FROM generate_series(1, 5)"};
  49. pqxx::connection c;
  50. pqxx::work tx1{c};
  51. // Prepare a simple statement.
  52. tx1.conn().prepare("CountToFive", count_to_5);
  53. // The statement returns exactly what you'd expect.
  54. COMPARE_RESULTS(
  55. "CountToFive", tx1.exec_prepared("CountToFive"), tx1.exec(count_to_5));
  56. // Re-preparing it is an error.
  57. PQXX_CHECK_THROWS(
  58. tx1.conn().prepare("CountToFive", count_to_5), pqxx::sql_error,
  59. "Did not report re-definition of prepared statement.");
  60. tx1.abort();
  61. pqxx::work tx2{c};
  62. // Executing a nonexistent prepared statement is also an error.
  63. PQXX_CHECK_THROWS(
  64. tx2.exec_prepared("NonexistentStatement"), pqxx::sql_error,
  65. "Did not report invocation of nonexistent prepared statement.");
  66. }
  67. void test_basic_args()
  68. {
  69. pqxx::connection c;
  70. c.prepare("EchoNum", "SELECT $1::int");
  71. pqxx::work tx{c};
  72. auto r{tx.exec_prepared("EchoNum", 7)};
  73. PQXX_CHECK_EQUAL(
  74. std::size(r), 1, "Did not get 1 row from prepared statement.");
  75. PQXX_CHECK_EQUAL(std::size(r.front()), 1, "Did not get exactly one column.");
  76. PQXX_CHECK_EQUAL(r[0][0].as<int>(), 7, "Got wrong result.");
  77. auto rw{tx.exec_prepared1("EchoNum", 8)};
  78. PQXX_CHECK_EQUAL(
  79. std::size(rw), 1, "Did not get 1 column from exec_prepared1.");
  80. PQXX_CHECK_EQUAL(rw[0].as<int>(), 8, "Got wrong result.");
  81. }
  82. void test_multiple_params()
  83. {
  84. pqxx::connection c;
  85. c.prepare("CountSeries", "SELECT * FROM generate_series($1::int, $2::int)");
  86. pqxx::work tx{c};
  87. auto r{tx.exec_prepared_n(4, "CountSeries", 7, 10)};
  88. PQXX_CHECK_EQUAL(
  89. std::size(r), 4, "Wrong number of rows, but no error raised.");
  90. PQXX_CHECK_EQUAL(r.front().front().as<int>(), 7, "Wrong $1.");
  91. PQXX_CHECK_EQUAL(r.back().front().as<int>(), 10, "Wrong $2.");
  92. c.prepare("Reversed", "SELECT * FROM generate_series($2::int, $1::int)");
  93. r = tx.exec_prepared_n(3, "Reversed", 8, 6);
  94. PQXX_CHECK_EQUAL(
  95. r.front().front().as<int>(), 6, "Did parameters get reordered?");
  96. PQXX_CHECK_EQUAL(
  97. r.back().front().as<int>(), 8, "$2 did not come through properly.");
  98. }
  99. void test_nulls()
  100. {
  101. pqxx::connection c;
  102. pqxx::work tx{c};
  103. c.prepare("EchoStr", "SELECT $1::varchar");
  104. auto rw{tx.exec_prepared1("EchoStr", nullptr)};
  105. PQXX_CHECK(rw.front().is_null(), "nullptr did not translate to null.");
  106. char const *n{nullptr};
  107. rw = tx.exec_prepared1("EchoStr", n);
  108. PQXX_CHECK(rw.front().is_null(), "Null pointer did not translate to null.");
  109. }
  110. void test_strings()
  111. {
  112. pqxx::connection c;
  113. pqxx::work tx{c};
  114. c.prepare("EchoStr", "SELECT $1::varchar");
  115. auto rw{tx.exec_prepared1("EchoStr", "foo")};
  116. PQXX_CHECK_EQUAL(
  117. rw.front().as<std::string>(), "foo", "Wrong string result.");
  118. char const nasty_string[]{R"--('\"\)--"};
  119. rw = tx.exec_prepared1("EchoStr", nasty_string);
  120. PQXX_CHECK_EQUAL(
  121. rw.front().as<std::string>(), std::string(nasty_string),
  122. "Prepared statement did not quote/escape correctly.");
  123. rw = tx.exec_prepared1("EchoStr", std::string{nasty_string});
  124. PQXX_CHECK_EQUAL(
  125. rw.front().as<std::string>(), std::string(nasty_string),
  126. "Quoting/escaping went wrong in std::string.");
  127. char nonconst[]{"non-const C string"};
  128. rw = tx.exec_prepared1("EchoStr", nonconst);
  129. PQXX_CHECK_EQUAL(
  130. rw.front().as<std::string>(), std::string(nonconst),
  131. "Non-const C string passed incorrectly.");
  132. }
  133. void test_binary()
  134. {
  135. pqxx::connection c;
  136. pqxx::work tx{c};
  137. c.prepare("EchoBin", "SELECT $1::bytea");
  138. constexpr char raw_bytes[]{"Binary\0bytes'\"with\tweird\xff bytes"};
  139. std::string const input{raw_bytes, std::size(raw_bytes)};
  140. #include "pqxx/internal/ignore-deprecated-pre.hxx"
  141. {
  142. pqxx::binarystring const bin{input};
  143. auto rw{tx.exec_prepared1("EchoBin", bin)};
  144. PQXX_CHECK_EQUAL(
  145. pqxx::binarystring(rw[0]).str(), input,
  146. "Binary string came out damaged.");
  147. }
  148. #include "pqxx/internal/ignore-deprecated-post.hxx"
  149. {
  150. std::basic_string<std::byte> bytes{
  151. reinterpret_cast<std::byte const *>(raw_bytes), std::size(raw_bytes)};
  152. auto bp{tx.exec_prepared1("EchoBin", bytes)};
  153. auto bval{bp[0].as<std::basic_string<std::byte>>()};
  154. PQXX_CHECK_EQUAL(
  155. (std::string_view{
  156. reinterpret_cast<char const *>(bval.c_str()), std::size(bval)}),
  157. input, "Binary string parameter went wrong.");
  158. }
  159. // Now try it with a complex type that ultimately uses the conversions of
  160. // std::basic_string<std::byte>, but complex enough that the call may
  161. // convert the data to a text string on the libpqxx side. Which would be
  162. // okay, except of course it's likely to be slower.
  163. {
  164. auto ptr{std::make_shared<std::basic_string<std::byte>>(
  165. reinterpret_cast<std::byte const *>(raw_bytes), std::size(raw_bytes))};
  166. auto rp{tx.exec_prepared1("EchoBin", ptr)};
  167. auto pval{rp[0].as<std::basic_string<std::byte>>()};
  168. PQXX_CHECK_EQUAL(
  169. (std::string_view{
  170. reinterpret_cast<char const *>(pval.c_str()), std::size(pval)}),
  171. input, "Binary string as shared_ptr-to-optional went wrong.");
  172. }
  173. {
  174. auto opt{std::optional<std::basic_string<std::byte>>{
  175. std::in_place, reinterpret_cast<std::byte const *>(raw_bytes),
  176. std::size(raw_bytes)}};
  177. auto op{tx.exec_prepared1("EchoBin", opt)};
  178. auto oval{op[0].as<std::basic_string<std::byte>>()};
  179. PQXX_CHECK_EQUAL(
  180. (std::string_view{
  181. reinterpret_cast<char const *>(oval.c_str()), std::size(oval)}),
  182. input, "Binary string as shared_ptr-to-optional went wrong.");
  183. }
  184. #if defined(PQXX_HAVE_CONCEPTS)
  185. // By the way, it doesn't have to be a std::basic_string. Any contiguous
  186. // range will do.
  187. {
  188. std::vector<std::byte> data{std::byte{'x'}, std::byte{'v'}};
  189. auto op{tx.exec_prepared1("EchoBin", data)};
  190. auto oval{op[0].as<std::basic_string<std::byte>>()};
  191. PQXX_CHECK_EQUAL(
  192. std::size(oval), 2u, "Binary data came back as wrong length.");
  193. PQXX_CHECK_EQUAL(static_cast<int>(oval[0]), int('x'), "Wrong data.");
  194. PQXX_CHECK_EQUAL(static_cast<int>(oval[1]), int('v'), "Wrong data.");
  195. }
  196. #endif
  197. }
  198. void test_params()
  199. {
  200. pqxx::connection c;
  201. pqxx::work tx{c};
  202. c.prepare("Concat2Numbers", "SELECT 10 * $1 + $2");
  203. std::vector<int> values{3, 9};
  204. pqxx::params params;
  205. params.reserve(std::size(values));
  206. params.append_multi(values);
  207. auto const rw39{tx.exec_prepared1("Concat2Numbers", params)};
  208. PQXX_CHECK_EQUAL(
  209. rw39.front().as<int>(), 39,
  210. "Dynamic prepared-statement parameters went wrong.");
  211. c.prepare("Concat4Numbers", "SELECT 1000*$1 + 100*$2 + 10*$3 + $4");
  212. auto const rw1396{tx.exec_prepared1("Concat4Numbers", 1, params, 6)};
  213. PQXX_CHECK_EQUAL(
  214. rw1396.front().as<int>(), 1396,
  215. "Dynamic params did not interleave with static ones properly.");
  216. }
  217. void test_optional()
  218. {
  219. pqxx::connection c;
  220. pqxx::work tx{c};
  221. c.prepare("EchoNum", "SELECT $1::int");
  222. pqxx::row rw{
  223. tx.exec_prepared1("EchoNum", std::optional<int>{std::in_place, 10})};
  224. PQXX_CHECK_EQUAL(
  225. rw.front().as<int>(), 10,
  226. "optional (with value) did not return the right value.");
  227. rw = tx.exec_prepared1("EchoNum", std::optional<int>{});
  228. PQXX_CHECK(
  229. rw.front().is_null(), "optional without value did not come out as null.");
  230. }
  231. void test_prepared_statements()
  232. {
  233. test_registration_and_invocation();
  234. test_basic_args();
  235. test_multiple_params();
  236. test_nulls();
  237. test_strings();
  238. test_binary();
  239. test_params();
  240. test_optional();
  241. }
  242. void test_placeholders_generates_names()
  243. {
  244. using pqxx::operator""_zv;
  245. pqxx::placeholders name;
  246. PQXX_CHECK_EQUAL(name.view(), "$1"_zv, "Bad placeholders initial zview.");
  247. PQXX_CHECK_EQUAL(name.view(), "$1"sv, "Bad placeholders string_view.");
  248. PQXX_CHECK_EQUAL(name.get(), "$1", "Bad placeholders::get().");
  249. name.next();
  250. PQXX_CHECK_EQUAL(name.view(), "$2"_zv, "Incorrect placeholders::next().");
  251. name.next();
  252. PQXX_CHECK_EQUAL(name.view(), "$3"_zv, "Incorrect placeholders::next().");
  253. name.next();
  254. PQXX_CHECK_EQUAL(name.view(), "$4"_zv, "Incorrect placeholders::next().");
  255. name.next();
  256. PQXX_CHECK_EQUAL(name.view(), "$5"_zv, "Incorrect placeholders::next().");
  257. name.next();
  258. PQXX_CHECK_EQUAL(name.view(), "$6"_zv, "Incorrect placeholders::next().");
  259. name.next();
  260. PQXX_CHECK_EQUAL(name.view(), "$7"_zv, "Incorrect placeholders::next().");
  261. name.next();
  262. PQXX_CHECK_EQUAL(name.view(), "$8"_zv, "Incorrect placeholders::next().");
  263. name.next();
  264. PQXX_CHECK_EQUAL(name.view(), "$9"_zv, "Incorrect placeholders::next().");
  265. name.next();
  266. PQXX_CHECK_EQUAL(name.view(), "$10"_zv, "Incorrect placeholders carry.");
  267. name.next();
  268. PQXX_CHECK_EQUAL(name.view(), "$11"_zv, "Incorrect placeholders 11.");
  269. while (name.count() < 999) name.next();
  270. PQXX_CHECK_EQUAL(name.view(), "$999"_zv, "Incorrect placeholders 999.");
  271. name.next();
  272. PQXX_CHECK_EQUAL(name.view(), "$1000"_zv, "Incorrect large placeholder.");
  273. }
  274. PQXX_REGISTER_TEST(test_prepared_statements);
  275. PQXX_REGISTER_TEST(test_placeholders_generates_names);
  276. } // namespace