[odb-users] How to cache the queried object pointer?

Justin Huang yohuang at nvidia.com
Wed Oct 30 22:40:29 EDT 2019


Hi,

I'd like to load sqlite3 DB back, construct C++ objects, and save the object pointers for later usage.

I have 3 Module entries in the Module table, the code below is used. It is weird that the 'ptr' of the 1st Module is destructed before looping the 2nd Module, but 2nd/3rd Module ptr are not destructed.

I wonder how should we cache the queried object pointers for later use? I don't want to create new Module using copy constructor from the result::iterator, because the Modules can have cross references (I mean point to each other) in class definition and creating new Module objects would require updating all the cross references.

Thanks,
Justin


class DB {
public:
     void load_db(int argc, char* argv[]);
private:
std::vector<Module *> modules;
};

void DB::load_db(int argc, char* argv[])
{
try
{
session s;
db = std::unique_ptr<odb::database>(create_database (argc, argv));

     // load all modules in Module table back into C++ objects
     {
          odb::transaction t (db->begin());

           typedef odb::result<Module> result;
           result all_modules (db->query<Module> ());

           int count = 0;
           for (result::iterator i(all_modules.begin()); i != all_modules.end(); ++i) {
                auto ptr = i.operator->();
              modules.push_back(ptr);
}
           }
}
}


-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------


More information about the odb-users mailing list