/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "stdafx.h"

#include "recordset_be.h"
#include "recordset_data_storage_be.h"


using namespace bec;


Recordset::Recordset()
:
_data_storage(NULL)
{
}


Recordset::~Recordset()
{
}


void Recordset::commit(Recordset_data_storage *data_storage)
{
  if (data_storage)
    data_storage->serialize(this);
}


void Recordset::reset(Recordset_data_storage *data_storage)
{
  _field_names= std::vector<std::string>();
  _rs= std::vector<std::string>();

  if (data_storage)
    data_storage->unserialize(_field_names, _rs);
}


void Recordset::commit()
{
  commit(_data_storage);
}


void Recordset::reset()
{
  reset(_data_storage);
}


void Recordset::refresh()
{
}


int Recordset::count()
{
  int res= _field_names.size();
  if (0 < res)
    res= _rs.size() / res;
  return res;
}


bool Recordset::set_field(const NodeId &node, int column, const std::string &value)
{
  return false;
}


bool Recordset::set_field(const NodeId &node, int column, int value)
{
  return false;
}


bool Recordset::get_field_grt(const NodeId &node, int column, grt::ValueRef &value)
{
  if (!node.is_valid())
  {
    return false;
  }
  else if (node[0] < count())
  {
    value= grt::StringRef(_rs.at(node[0] * _field_names.size() + column));
    return true;
  }
  return false;
}


int Recordset::field_count()
{
  return _field_names.size();
}


std::string Recordset::field_name(int index)
{
  return _field_names.at(index);
}
