/* 
 * © 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 "sql_editor_be.h"
#include "grtsqlparser/sql_facade.h"
#include "sqlide.h"


using namespace bec;
using namespace grt;


std::auto_ptr<Sql_editor> Sql_editor::create_sql_editor(db_mgmt_RdbmsRef rdbms)
{
  std::auto_ptr<Sql_editor> sql_editor;

  const char *def_module_name= "Sql";
  std::string module_name= rdbms->name().repr() + def_module_name;
  Sql *sql_module= dynamic_cast<Sql*>(rdbms->get_grt()->get_module(module_name));
  if (!sql_module) // fallback to db agnostic sql module
    sql_module= dynamic_cast<Sql*>(rdbms->get_grt()->get_module(def_module_name));
  if (sql_module)
    sql_editor= sql_module->getSqlEditor(rdbms);
  return sql_editor;
}


Sql_editor::Sql_editor(db_mgmt_RdbmsRef rdbms)
:
_rdbms(rdbms)
{
  GRTManager *grtm= GRTManager::get_instance_for(rdbms->get_grt());
  GRT *grt= grtm->get_grt();
  _options= DictRef::cast_from(grt->unserialize(make_path(grtm->get_basedir(), "modules/data/" + _rdbms->id() + ".sql.editor.xml")));

  SqlFacade *sql_facade= SqlFacade::instance_for_rdbms(rdbms);
  _sql_checker= sql_facade->sqlSemanticCheck();
}


Sql_editor::~Sql_editor()
{
}


int Sql_editor::int_option(std::string name)
{
  return IntegerRef::cast_from(_options[name]);
}


std::string Sql_editor::string_option(std::string name)
{
  return StringRef::cast_from(_options[name]);
}


void Sql_editor::sql_parser_err_cb(Sql_editor::Sql_parser_err_cb cb)
{
  _sql_checker->parse_error_cb(cb);
}


void Sql_editor::check_sql()
{
  _sql_checker->check_sql(_sql);
}
