[odb-users] strange lazy section
Патрушев Данил Андреевич
d.patrushev at prosoftsystems.ru
Wed Nov 8 05:20:21 EST 2017
Hi, we are observing strange behavior of lazy loaded sections. The content of a lazy loaded QByteArray, in case its length exceeds 256 bytes, seems to be corrupted. With eager loading enabled, the section is loaded as expected.
Code:
#pragma db object
struct DbObject
{
#pragma db id auto
quint64 id;
#pragma db load(lazy)
odb::section extras_;
#pragma db section(extras_)
QByteArray content;
};
void persist(DbObject& obj1, DbObject& obj2)
{
odb::pgsql::database connection(//database connection details);
odb::transaction tran(connection.begin());
connection.persist(obj1);
connection.persist(obj2);
tran.commit();
}
void read()
{
odb::pgsql::database connection(//database connection details);
odb::transaction tran(connection.begin());
odb::result<DbObject> result(connection.query<DbObject>());
std::vector<DbObject> object_list;
for(const auto& obj : result)
object_list.push_back(obj);
tran.commit();
//load section, print size, first and last byte of array
// short array (less than 256) - 's' first, 't' last
//long array (over 256) - should be 'a' first, 'z' last, but it's not the case
auto load_section_func = [&connection](DbObject& b){
odb::transaction tran(connection.begin());
connection.load(b, b.extras_);
qDebug()<<b.content.size()<<b.content[0]<<b.content[b.content.size()-1];
tran.commit();
};
for(auto& b: object_list)
load_section_func(b);
}
void erase(DbObject& obj1, DbObject& obj2)
{
odb::pgsql::database connection(//database connection details);
odb::transaction tran(connection.begin());
connection.erase(obj1);
connection.erase(obj2);
tran.commit();
}
int main(int, char*[])
{
DbObject short_object;
DbObject long_object;
short_object.content = "short object content";// 's' first, 't' last
long_object.content.resize(257);
long_object.content[0] = 'a';
long_object.content[long_object.content.size()-1] = 'z';// 'a' first, 'z' last
persist(short_object, long_object);
read();
erase(short_object, long_object);
return 0;
}
Another observation is that if we modify the above code to use a single database instance throughout the "persist - read - erase" cycle then the section is loaded as expected.
More information about the odb-users
mailing list