dimanche 7 juin 2015

Impossible to update/remove row using Wt library with posgresql

I downloaded one of the example from official Wt libarary page http://ift.tt/1MyB9KP and change it for postgresql application. I can`t update or remove row. Another operation like e.g. adding new rows works correcly. How to change this querry

dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe");

to start working properly with postgresql? The problem is that the querry generate command something like this:

select u.id, u.version, u."name", u."password", u."role", u."karma"
    from user u
    where (name = ?)

Which is not ok. Here are some examples:

#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/backend/Sqlite3>
#include <Wt/Dbo/backend/Postgres>
#include <string>

namespace dbo = Wt::Dbo;

class User {
public:
    enum Role {
        Visitor = 0,
        Admin = 1,
        Alien = 42
    };

    std::string name;
    std::string password;
    Role        role;
    int         karma;

    template<class Action>
    void persist(Action& a)
    {
        dbo::field(a, name, "name");
        dbo::field(a, password, "password");
        dbo::field(a, role, "role");
        dbo::field(a, karma, "karma");
    }
};

void run()
{
    dbo::backend::Postgres po("host=127.0.0.1 user=postgres password=PPPP port=5432 dbname=testdbo");
    dbo::Session session;
    session.setConnection(po);

    session.mapClass<User>("user");

    try
    {
        session.createTables();
    }
    catch (...)
    {
        std::cout << "Not added" << std::endl;
    }

  {
      try
      {
          dbo::Transaction transaction(session);

          dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe");

          std::cerr << "Joe has karma: " << joe->karma << std::endl;

          dbo::ptr<User> joe2 = session.query< dbo::ptr<User> >
              ("select u from user u").where("name = ?").bind("Joe");
      }
      catch (...)
      {
          std::cout << "Not working 1" << std::endl;
      }
  }
}

int main()
{
    run();
}

Example from page works on Sqlite. I was trying to change it to:

dbo::ptr<User> joe = session.find<User>().where("name = 'Joe';");// .bind("Joe");

Aucun commentaire:

Enregistrer un commentaire