With libpqxx, is it possible for one prepared statement that exec
s but has not yet been commit
ted to store results in a result
for use in later prepared statements?
If so, how can this be done?
I've stripped it down for readability, but this is essentially what I'm trying to do:
void prepare_write_parent_table(connection_base &c){
try
{
c.prepare("write_parent_table",
"INSERT INTO parent_table (column_1) "
"SELECT $1 "
"RETURNING id"
)
("character", pqxx::prepare::treat_string);
}
catch (const exception &e)
{
cerr << e.what() << endl;
}
}
string write_parent_table(transaction_base &t, string data){
try
{
result parent_table_result = t.prepared("write_parent_table")(data).exec();
return parent_table_result[0][0].c_str();
}
catch (const exception &e)
{
cerr << e.what() << endl;
return "";
}
}
void prepare_write_child_table(connection_base &c){
try
{
c.prepare("write_child_table",
"INSERT INTO child_table (parent_table_id, column_a) "
"SELECT $1, $2 "
)
("character", pqxx::prepare::treat_string)
("character", pqxx::prepare::treat_string);
}
catch (const exception &e)
{
cerr << e.what() << endl;
}
}
The return
from write_parent_table
is checked to see if( == "")
. If it isn't, it proceeds; otherwise, I will commit
there to let it fail or more preferably cancel the transaction if possible; however, I don't yet know how to do that if it is even possible.
There will always be an uncertain amount of INSERT
s into child_table
per parent_table
INSERT
.
Simplify the operation by using a single SQL statement for both inserts using a data-modifying CTE. This is much faster than storing intermediary states in the client.
The INSERT
in the child table only happens if the first INSERT
in the parent table is successful and returns an id
:
void prepare_write_both_tables(connection_base &c){
try
{
c.prepare("write_both_tables",
"WITH p AS ("
"INSERT INTO parent_table (column_1) "
"SELECT $1 "
"RETURNING id) "
"INSERT INTO child_table (parent_table_id, column_a) "
"SELECT p.id, $2 "
"FROM p"
)
("character", pqxx::prepare::treat_string)
("character", pqxx::prepare::treat_string);
}
catch (const exception &e)
{
cerr << e.what() << endl;
}
}
Search for [postgres] & "data-modifying CTE" for more examples.
Also called "writable CTE" (or "writeable CTE").
For a single parent and 0 to many children:
void prepare_write_both_tables(connection_base &c){
try
{
c.prepare("write_both_tables",
"WITH p AS ("
"INSERT INTO parent_table (column_1) "
"SELECT $1 "
"RETURNING id) "
"INSERT INTO child_table (parent_table_id, column_a) "
"SELECT p.id, a "
"FROM p, unnest($2::text[]) AS a"
)
("character", pqxx::prepare::treat_string)
("character", pqxx::prepare::treat_string);
}
catch (const exception &e)
{
cerr << e.what() << endl;
}
}
Where The second parameter is an array of text
in text representation. Example:
{foo,bar,baz}
This inserts as many rows as there are elements in the text array. For 0 children pass NULL
or an empty array {}
.