Question

I use Soci to make database queries. Now I need to have a custom resultset class that will wrap around soci::rowset. I cannot get it work in the way my code are below simply because copy constructor is private in soci (not supported according to source file). How can I accomplish having wrapper with list of soci::rows (hereby typedef-ed to Row) without complicated code? Any design or direction on how to go about is appreciated

my header file

typedef soci::row Row; 


class ResultSet
{
public:
    ResultSet();
    ~ResultSet();
    void Copy(soci::rowset<soci::row>& rs);

    Row GetNextRow();
    bool HasRows();

private:
    soci::rowset<soci::row> m_rows;
    soci::rowset_iterator<soci::row> m_iterator;
    bool m_isAccessed;//if first access on row is done
};

source file

ResultSet::ResultSet() {
    m_isAccessed=false;
}

ResultSet::~ResultSet() {

}

Row ResultSet::GetNextRow() {
    if(m_isAccessed) {
        m_iterator++;//increment row
        m_isAccessed=true;
    }
    return *m_iterator;
}

bool ResultSet::HasRows() {
    return m_iterator!=m_rows.end();//it have not reached the end yet;
}

void ResultSet::Copy(soci::rowset<soci::row>& rs) {
    m_rows = rs;
    m_iterator = rs.begin();//put it at row 1
}

and here is how I use in ExecuteQuery function of connection. m_session is soci::session

void ConnectionMgr::ExecuteQuery(wxString& sql, ResultSet& rs) {
    try { 
        soci::rowset<soci::row> rsInternal = m_session.prepare<<sql.ToStdString();
        rs.Copy(rsInternal); 
    }
    catch (std::exception const& e) {
        m_error = wxString::Format(wxT("SqlQuery close error:%s\n"), e.what());
    }
}

example of how I want to use the class later

wxString sql = wxT("-- a query\n SHOW DATABASES;"); 
                ResultSet rs;
                m_conn->ExecuteQuery(sql, rs);
                while(rs.HasRows())
                {
                    wxString name = wxString(rs.GetNextRow().get<std::string>(0));
                    //work with name here
                }
Was it helpful?

Solution

SOCI have many limitation and undertaking such task seems to be impossible without modifying sources which would mean you have to maintain changes yourself (unless the developers accepts patches which is a probability).

Since I use wxWidgets, I decided to revive databaselayer as wxDatabase and will use that and maintain it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top