Skip to content

Commit c2c33e0

Browse files
TertiumOrganum1alexiprof
authored andcommitted
feat docs: add an example for retrieving binary data from a 'bytea' PostgreSQL field
The main part is ```cpp row["data"].To(pg::Bytea(tgt_bin_str)) ``` Tests: протестировано CI 1c95306de4f096f725f66307f939094defaaecf4 Pull Request resolved: #590
1 parent bc6d877 commit c2c33e0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

postgresql/include/userver/storages/postgres/io/supported_types.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,23 @@
127127
/// @code{.cpp}
128128
/// namespace pg = storages::postgres;
129129
/// using namespace std::string_literals;
130+
/// // using a "binary string"
130131
/// std::string s = "\0\xff\x0afoobar"s;
131-
/// trx.Execute("select $1", pg::Bytea(tp));
132+
/// std::vector<std::uint8_t> tgt_bin_str;
133+
/// // note: pg::Bytea(const T &) is used
134+
/// trx.Execute("select $1", pg::Bytea(s));
135+
/// // storing a byte array:
136+
/// std::vector<std::uint8_t> bin_str{1, 2, 3, 4, 5, 6, 7, 8, 9};
137+
/// trx.Execute("INSERT INTO mytable (data) VALUES ($1)", pg::Bytea(bin_str));
138+
/// // note: pg::Bytea(const T &) is used
139+
/// @endcode
140+
///
141+
/// To read data from bytea field:
142+
/// @code{.cpp}
143+
/// // SQL: data BYTEA {NOT} NULL
144+
/// auto res = trx.Execute("SELECT id, data FROM mytable WHERE id = $1", id);
145+
/// std::vector<std::uint8_t> tgt_bin_str; const auto& row = res.Front();
146+
/// row["data"].To(pg::Bytea(tgt_bin_str)); // note: pg::Bytea(T &) is used
132147
/// @endcode
133148
///
134149
/// @par Network types

0 commit comments

Comments
 (0)