[Libpqxx-general] transactor select results?
Robert Backhaus-pqxx
pqxx at robbak.com
Tue Dec 29 14:20:19 UTC 2009
On Tue, Dec 29, 2009 at 9:16 PM, Remco Post <remco at pipsworld.nl> wrote:
> Hi all,
>
> I'm trying to get my head round the whole transactors concept. I've build a
> very simple transactor class that performs a query, now I need to be able to
> access the results of that query. Now, the docs says something about
> overloading the on_commit() member function. Now am I right in thinking that
> perform() still doesn't do anything with the value returned by on_commit()?
> How do I get the results of my query? Should I create a member function that
> eg. returns a reference to the (private) pqxx::result variable?
>
>
When you write your transactor, include private attributes to store the data
that your queries will return. In your perform() function, store the
information in those attributes. In on_commit(), you can move the data out
of the transactor.
As the pqxx::result is part of the transactor, you should not reference it
outside of the transactor.
I guess that the answer is: outside of your transactor, you should be done
with anything databasey. When the transactor is finshed, you should be left
with the objects of your program, manipulated as requred by the information
extracted from the database.
Here is one of my uses of the transactor framework. All this needed to do is
give me a integer from the database to match an address, and create a record
if one doesn't exist. Mind you, reading it now, it desprately needs some
fixing: I've got something to do tommorrow! (Amateur programers!)
class GetMachine : public transactor <>
{
public:
GetMachine() : transactor<>("GetMachine") {}
int *machine_id;
in_addr address;
string tempstring, qstring;
void operator()(argument_type &T)
{
try {
tempstring = "SELECT machinenumber from machines where address =
'";
tempstring += inet_ntoa( address );
tempstring += "';";
R = T.exec( tempstring );
// If no records returned,
if ( R.size() == 0 )
{ // Need to add new machine record
//FIXME: Generating random machineid : this is not good.
qstring = "INSERT INTO machines ( machinenumber, address,
position, owner_id ) values (";
qstring += pqxx::to_string( random() );
qstring += " , '";
qstring += inet_ntoa( address );
qstring += "', 'Laptop', 1 );";
R = T.exec( qstring );
// Get record for new machine_id. FIXME:: This is a very
backwards way: wrong even before we had insert..returning. And a random
value: messy. I guess I was relying on unique to kill an attempt to
duplicate anything. That said, this doesn't get called often.
R = T.exec( tempstring );
}
} catch (pqxx::sql_error e) {
cerr << "Sql Error Processing in GetMachine transactor." << endl <<
"Query - " << e.query() << endl <<
"Error - " << e.what() << endl;
// FIXME:What on earth was I thinking! I forgot to throw() the error so the
transactor would try again!
throw(); //At least, I hope this is right. Research tomorrow as well.
}
}
void on_commit()
{
if ( R.size() != 0 )
R[0][0].to(*machine_id); // XXX FIXME: This may barf if there is no
record!
#ifdef DEBUG
cerr << "Transactor Found Machineid " << *machine_id << " for " <<
inet_ntoa( address ) << endl;
#endif
}
private:
result R;
};
> --
>
> Met vriendelijke groeten,
>
> Remco Post
> remco at pipsworld.nl
>
> _______________________________________________
> Libpqxx-general mailing list
> Libpqxx-general at pgfoundry.org
> http://pgfoundry.org/mailman/listinfo/libpqxx-general
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://pgfoundry.org/pipermail/libpqxx-general/attachments/20091230/d85b9996/attachment.html>
More information about the Libpqxx-general
mailing list