[odb-users] Querying entity's container

Adnan RIHAN axel50397 at gmail.com
Wed Mar 25 06:06:26 EDT 2015


Hi Boris,

On 13 mars 2015 at 16:06:16, Boris Kolpackov (boris at codesynthesis.com(mailto:boris at codesynthesis.com)) wrote:

> Now we can use this view to load a product that matches a barcode:
>  
> typedef odb::query query;
>  
> db.query (query::barcode::value == "123456");

Thanks a lot for that help. But I had compiler issues.

Here is how I had to implement it (I only show what seems relevant):
Product.cpp
// *SP means: QSharedPointer<*>
class Product : public BaseModel
{
    private:
        CategorySP      m_category;
        QSet<QString>   m_barcodes;
};

typedef QSharedPointer<Product> ProductSP;

struct product_barcode
{
    ProductSP   product_id; # <-- shared_ptr instead of ptr directly
    QString     barcode;
};

struct product_view
{
    ProductSP   p;
};

# ifdef     ODB_COMPILER
#   pragma  db  model                           version(1, 1, open)
#   pragma  db  object(Product)                 table("products")
#   pragma  db  member(Product::m_category)     not_null                    column("category_id")
#   pragma  db  member(Product::m_barcodes)     table("product_barcodes")   id_column("product_id") value_column("barcode")

#   pragma  db  object(product_barcode)         table("product_barcodes")   no_id   abstract
#   pragma  db  member(product_barcode::product_id) column("product_id")
#   pragma  db  member(product_barcode::barcode)    column("barcode")
#   pragma  db  view(product_view)              object(Product) object(product_barcode = barcode)   query(distinct)
# endif
// END

And this is the compiler error I had:
/Users/Max13/Dev/System/LGC/Desktop/apps/LGC/src/Models/Product/Product-odb.cxx:1790:13: error: cannot cast from type 'typename object_traits<Product>::pointer_type' (aka 'QSharedPointer< ::Product>') to pointer type 'ptr_traits::pointer_type' (aka 'Product *')
        v = ptr_traits::pointer_type (
            ^~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

So I thought it was just a typo. It’s good thanks.
===================
I was using « query() » before 2.4.0 update. Now I know query_one exists, I wanted to give a try. But then, I have a more complicated compiler issue:
In file included from /Users/Max13/Dev/System/LGC/Desktop/apps/LGC/src/CashRegister/CashRegisterWidget.cpp:5:
In file included from /usr/local/include/odb/qt/list.hxx:8:
In file included from /usr/local/include/odb/qt/containers/list.hxx:22:
In file included from /usr/local/include/odb/vector-impl.hxx:14:
In file included from /usr/local/include/odb/transaction.hxx:263:
In file included from /usr/local/include/odb/transaction.ixx:5:
In file included from /usr/local/include/odb/connection.hxx:19:
In file included from /usr/local/include/odb/prepared-query.hxx:12:
In file included from /usr/local/include/odb/result.hxx:242:
/usr/local/include/odb/result.txx:19:20: error: no viable conversion from 'typename view_traits<product_view>::pointer_type' (aka 'QSharedPointer< ::product_view>') to 'pointer_type' (aka 'product_view *')
      pointer_type o (i.load ());
                   ^  ~~~~~~~~~
/usr/local/include/odb/database.ixx:837:14: note: in instantiation of member function 'odb::result<product_view>::one' requested here
    return r.one ();
             ^
/usr/local/include/odb/database.ixx:647:12: note: in instantiation of function template specialization 'odb::database::query_one_<product_view, 5, odb::query<product_view, odb::sqlite::query_base> >' requested here
    return query_one_<T, id_common> (q);
           ^
/Users/Max13/Dev/System/LGC/Desktop/apps/LGC/src/Controllers/EntityManager/EntityManager.hpp:247:39: note: in instantiation of function template specialization 'odb::database::query_one<product_view>' requested here
                one.reset(this->m_db->query_one<T>(query));
                                      ^
/Users/Max13/Dev/System/LGC/Desktop/apps/LGC/src/CashRegister/CashRegisterWidget.cpp:156:72: note: in instantiation of function template specialization 'EntityManager::findOne2<product_view>' requested here
    QSharedPointer<product_view>    pv(MasterController::inst()->em()->findOne2<product_view>(odb::query<product_view>::barcode::barcode == scan));
                                                                       ^
/Users/Max13/Qt/5.4/clang_64/lib/QtCore.framework/Headers/qsharedpointer_impl.h:299:12: note: candidate function
    inline operator RestrictedBool() const { return isNull() ? 0 : &QSharedPointer::value; }
           ^
1 error generated.

It may be another thread, but I didn’t try it with another call. It seems that « query_one » returns a raw pointer, while it tries to assign it from a shared_ptr. The error doesn’t change if I set the product_view::p to raw pointer.

Is it a bug?
--  
Cordialement, Adnan RIHAN.
Directeur-Gérant de Eolis-Software, société de services informatiques.  

GPG: 5675-62BA (https://keybase.io/max13/key.asc)
-> Si vous n'utilisez pas GPG mais souhaitez quand même m’envoyer un e-mail chiffré: (https://encrypt.to/0x567562BA).



More information about the odb-users mailing list