Hi, I've been trying to use boost::tuple with a boost::optional element and
I get a bad::cast exception. Attached to this mail you can find the
complete test case but here is small code snippet:

That works:
row_type get_last_row(soci::session& sql)
{
  unsigned int id = 0;
  std::string str1;
  boost::optional<std::string> str2;

  sql << "SELECT id, str1, str2 from example ORDER BY id DESC LIMIT 1",
      soci::into(id), soci::into(str1), soci::into(str2);

  return row_type(id, str1, str2);
}

That does not:
row_type get_last_row2(soci::session& sql)
{
  row_type myrow;

  try
  {
    sql << "SELECT id, str1, str2 from example ORDER BY id DESC LIMIT 1",
        soci::into(myrow);
  }
  catch(const std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return myrow;
}

Probably i'm missing something, but I can't find it.

Thanks in advance.
#include <boost/tuple/tuple.hpp>
#include <boost/optional.hpp>

#include <iostream>
#include <exception>

#define SOCI_USE_BOOST
#include "soci.h"
#include "boost-optional.h"
#include "boost-tuple.h"
#include "sqlite3/soci-sqlite3.h"

typedef boost::tuple<
    unsigned int,                 
    std::string,                  
    boost::optional<std::string>  
  > row_type;

row_type get_last_row(soci::session& sql)
{
  unsigned int id = 0;
  std::string str1;
  boost::optional<std::string> str2;

  sql << "SELECT id, str1, str2 from example ORDER BY id DESC LIMIT 1",
      soci::into(id), soci::into(str1), soci::into(str2);

  return row_type(id, str1, str2);
}

row_type get_last_row2(soci::session& sql)
{
  row_type myrow;

  try
  {
    sql << "SELECT id, str1, str2 from example ORDER BY id DESC LIMIT 1",
        soci::into(myrow);
  }
  catch(const std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return myrow;
}

int main()
{
  soci::session sql(*soci::factory_sqlite3(), ":memory:");

  sql << "CREATE TEMP TABLE example("
      " id INTEGER PRIMARY KEY AUTOINCREMENT,"
      " str1 TEXT,"
      " str2 TEXT DEFAULT NULL"
    ")";

  sql << "INSERT INTO example(id, str1, str2)"
      " VALUES(NULL, datetime('now'), NULL)";

  row_type myrow = get_last_row(sql);
  std::cout << myrow.get<0>() << std::endl;
  std::cout << myrow.get<1>() << std::endl;
  std::cout << std::boolalpha << myrow.get<2>().is_initialized() << std::endl;
  
  row_type myrow2 = get_last_row2(sql);
  std::cout << myrow2.get<0>() << std::endl;
  std::cout << myrow2.get<1>() << std::endl;
  std::cout << std::boolalpha << myrow2.get<2>().is_initialized() << std::endl;

  return 0;
}
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users

Reply via email to