Resending. Previous version was rejected(!) by the moderators for not coming from the subscribed address.
Begin forwarded message: > From: "Jan Mikkelsen" <[email protected]> > Date: 1 July 2009 8:17:03 AM > To: "General-purpose list for SOCI users." <[email protected] > > > Subject: Re: [SOCI-users] Question about ORM > > Hi, > > You need to define how the type conversion works from your object to > the columns in your SQL. Something like the code below. If you > have a way of extracting the column names, their types and getting > access to methods for getting and setting the values you can > generalise this function. > > Regards, > > Jan Mikkelsen > > namespace soci { > template<> struct type_conversion<Object> > { > typedef values base_type; > > static void > from_base( > values const& v, > indicator /* ind */, > Object& o) > { > o.col1_setter(v.get<std::string>("col1", "")); > o.col2_setter(v.get<int>("col2")); > } > > static void > to_base( > Object const& o, > values& v, > indicator& ind) > { > std::string const& col1_val = o.col1_getter(); > v.set("col1", col1_val, col1_val.is_empty() ? soci::i_null : > soci::i_ok); > v.set("col2", o.col2_getter()); > ind = i_ok; > } > }; > } > > ----- Original Message ----- From: "Matt Calder" <[email protected]> > To: "General-purpose list for SOCI users." <[email protected] > > > Sent: Wednesday, July 01, 2009 5:36 AM > Subject: [SOCI-users] Question about ORM > > >> Hi, >> I am having trouble with ORM. I have code like the following: >> >> // Function that fills object with values >> void FillObject(Object &obj, int i); >> >> // Start postgres session >> soci::session sql(soci::postgresql, ...); >> >> // Declare "use" object >> Object obj; >> >> // Prepare statement >> soci::statement st_put = (sql.prepare << "insert into tbl (col1, >> col2) >> values (:c1, :c2)", soci::use(obj)); >> >> // Insert 10 objects into the database >> for (int i=0; i < 10; i++) { >> // Get the data for object i >> FillObject(obj, i); >> >> // Execute SQL >> st_put.execute(true); // <--- Throws exception due to bad foreign >> key (= 0) >> >> } >> >> The exception is thrown because the table I am inserting into has a >> foreign key constraint on (say) col1. The error message says the >> value >> I am putting into col1 is 0, but I can see using the debugger that >> the >> value in obj is not 0 and is OK. The ORM works for "Object"'s, if >> instead of the st_put.execute line I put the whole statement >> construction inside the loop, so I suspect I am violating the "object >> lifetime" restrictions but I don't see how. Is there anything >> obviously wrong with what I am doing here? I can flesh out the >> example >> more if that would help. Thanks, >> >> Matt >> >> ------------------------------------------------------------------------------ >> _______________________________________________ >> Soci-users mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/soci-users ------------------------------------------------------------------------------ _______________________________________________ Soci-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/soci-users
