Hello! i've found a extrange behaviour related to column names and
binding by name. You can find the code attached to this mail.

I'm using postgresql backend and soci lib compiled from sourceforge git repo:

ricardo@bugge:~/devel/git/soci(master)$ git show
commit 631977adad434bcbdf0b8ee30314fa886757e4b0
Merge: 901e1ba 6e45bdb
Author: Mateusz Loskot <[email protected]>
Date:   Sat Feb 25 09:22:10 2012 -0800

I have a table with 4 columns: id, firstName, lastName, idcard and
streeet. When i try to update a row with:

st = (sql.prepare << "UPDATE persons SET firstName = :firstName,"
    " lastName = :lastName, idcard = :idcard, street = :street"
    " WHERE id = :id",
    use(p));

And execute the code i get an error: "Error: Missing use element for
bind by name (id)"

If i change all occurrences of "idcard" by "card" in the code, it works.

Its a bug or i'm doing something wrong?
-- 
Ricardo Muñoz Fernández
Warp Networks S.L. - http://www.warp.es
Zentyal - http://www.zentyal.com
#include "soci.h"
#include "soci-postgresql.h"
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <exception>

using namespace soci;
using namespace std;

struct Person
{
    int id;
    std::string firstName;
    std::string lastName;
    std::string idcard;
    std::string street;
};

namespace soci
{
template<> struct type_conversion<Person>
{
    typedef values base_type;
    static void from_base(values const & v, indicator /* ind */, Person & p)
    {
        p.id = v.get<int>("id");
        p.firstName = v.get<std::string>("firstName");
        p.lastName = v.get<std::string>("lastName");
        p.idcard = v.get<std::string>("idcard");
        p.idcard = v.get<std::string>("street");
    }
    static void to_base(const Person & p, values & v, indicator & ind)
    {
        v.set("id", p.id);
        v.set("firstName", p.firstName);
        v.set("lastName", p.lastName);
        v.set("idcard", p.idcard);
        v.set("street", p.street);
    }
};
}

int main()
{
    try
    {
        session sql(postgresql, "dbname=foo");

        try
        {
          cout << "creating table..." << endl;
          sql << "CREATE TABLE persons ("
              " id INTEGER NOT NULL,"
              " firstName VARCHAR(255),"
              " lastName VARCHAR(255),"
              " idcard VARCHAR(255),"
              " street VARCHAR(255))";
          sql << "CREATE SEQUENCE persons_id_seq START WITH 1 INCREMENT BY 1"
              " NO MAXVALUE NO MINVALUE CACHE 1";
          sql << "ALTER TABLE persons ALTER COLUMN id"
              " SET DEFAULT nextval('persons_id_seq'::regclass)";

          Person p;
          p.firstName = "John";
          p.lastName = "Doe";
          p.idcard = "12345678A";
          p.street = "Foo st.";
          int id = -1;
          cout << "preparing statement..." << endl;
          statement st = (sql.prepare << "INSERT INTO persons(firstName,"
              " lastName, idcard, street) VALUES (:firstName, :lastName,"
              " :idcard, :street) RETURNING id",
              use(p),
              into(id)
            );

          cout << "executing statement..." << endl;
          st.execute();
          cout << "fetching statement..." << endl;
          st.fetch();

          p.id = id;

          cout << "Person: " << p.firstName << p.lastName << ", id: " << p.id << endl;

          p.firstName = "Johnny";
          p.lastName = "Mnemonic";
          p.idcard = "87654321B";
          p.street = "Bar st.";
          cout << "preparing update statement..." << endl;
          st = (sql.prepare << "UPDATE persons SET firstName = :firstName,"
              " lastName = :lastName, idcard = :idcard, street = :street"
              " WHERE id = :id",
              use(p)
            );

          cout << "executing update statement..." << endl;
          st.execute();
          /*

          Person p2;
          cout << "preparing select statement..." << endl;
          st = (sql.prepare << "SELECT * FROM persons WHERE id = :id",
              use(p.id),
              into(p2)
            );

          cout << "executing select statement..." << endl;
          st.execute();
          cout << "fetching select statement..." << endl;
          st.fetch();

          cout << "Assert..." << endl;

          assert((p.firstName == p2.firstName) 
              && (p.lastName == p2.lastName)
              && (p.idcard == p2.idcard)
            );*/

        }
        catch (exception const &e)
        {
            cerr << "Error: " << e.what() << '\n';
        }

        sql << "DROP SEQUENCE IF EXISTS persons_id_seq CASCADE";
        sql << "DROP TABLE IF EXISTS persons";

    }
    catch (exception const &e)
    {
        cerr << "Error creating session or dropping: " << e.what() << '\n';
    }
}
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users

Reply via email to