[odb-users] A question about odb view

Zhang, Xiaoguang (X.) xzhan156 at ford.com
Fri Feb 3 09:56:27 EST 2017


Hi,
This is Jack Zhang. I am from Ford Motor NA. I discovered odb when I searched ORM software. I used it in my application which is part of our interval utilities. It worked very well and I like it very much.
Recently when I started using view and I found the view I created is not working for me. I hope you can point me out where is the miscoding or something else missing.
I am using SQLite of odb. I created a file like:

#ifndef __APPS_HXX__
#define __APPS_HXX__

#include <string>
#include <cstddef>

#include <odb/core.hxx>

#pragma db object
class apps
{
public:
    apps(const int appid, std::string &desc) : appid_(appid), desc_(desc) {}

    const int appid () { return appid_; }
    const std::string & desc () const { return desc_; }

    void appid (int aid ) {
        appid_ = aid;
    }
    void desc (const std::string & d) {
        desc_ = d;
    }

private:
    friend class odb::access;

    apps () {}

    #pragma db id auto
    unsigned long id_;

    #pragma db unique
    int appid_;

    std::string desc_;
};

#pragma db view object(apps)
struct apps_count
{
    #pragma db column ("count(" + apps::appid_ + ")")
    std::size_t count;
};

#endif

And this is the way I use it:

    typedef odb::query<apps_count> cquery;
typedef odb::result<apps_count> cresult;

    apps_count ac(odb_hdlr->query_value<apps_count> (cquery::appid == 1));
    cout << "apps record number: " << ac.count << endl;
    if (!ac.count) {
         cout << "There is no record for table ID: " << myedit_appids[i].id << ", creating one now ..." << endl;
           //string s(myedit_appids[i].desc);
            apps a(myedit_appids[i].id, (std::string &)myedit_appids[i].desc);
            //apps a(myedit_appids[i].id, s);
            odb_hdlr->persist(a);
}

NOTE:
Record with appid 1 is in database. Everything ac.count got zero back.

Odb_hdlr gets initialized like this, and it is successfully created without error and exceptions:
    int c = 3;
    char *v[4];
    v[0] = strdup("myedit");
    v[1] = strdup("--database");
    v[2] = strdup(adacs_dbname);
    v[3] = NULL;
odb_hdlr = new odb::sqlite::database (c, v, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);

Thanks in advance. Looking forward for your answer!
Jack


More information about the odb-users mailing list