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?
Code
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
.