/* 
 * © 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 "grt/grt_dispatcher.h"
//#include "db_mysql_statement_parser_be.h"
#include "editor_routine.h"

#define DEFAULT_PROCEDURE_TEMPLATE \
"CREATE PROCEDURE `%s`.`%s` ()\n"\
"BEGIN\n"\
"\n"\
"END//\n"


namespace bec {

RoutineEditorBE::RoutineEditorBE(GRTManager *grtm, const db_RoutineRef &routine, const db_mgmt_RdbmsRef &rdbms)
  : DBObjectEditorBE(grtm, routine, rdbms), _routine(routine), _has_syntax_error(false)
{
  if ((*_routine->sqlDefinition()).empty())
  {
    std::string template_name;
    int cursor_pos;
    std::string sql= get_sql_template(template_name, cursor_pos);
    size_t pos= sql.find_last_not_of("/\n\t ");
    if (pos != std::string::npos)
      sql= sql.substr(0, pos+1);
    _routine->sqlDefinition(sql);
  }
}

std::string RoutineEditorBE::get_sql()
{
  return _routine->sqlDefinition();
}

std::string RoutineEditorBE::get_object_type()
{
  return "routine";
}


std::string RoutineEditorBE::get_sql_definition_header()
{
  return "-- --------------------------------------------------------------------------------\n"
    "-- Routine DDL\n"
    "-- --------------------------------------------------------------------------------\n"
    "DELIMITER //\n\n";
}


std::string RoutineEditorBE::get_sql_template(const std::string &template_name, int &cursor_pos)
{
  char *sql= NULL;
  char *pos;
  if (template_name.empty() || template_name == "default")
  {
    sql= g_strdup_printf(DEFAULT_PROCEDURE_TEMPLATE, 
                         get_schema_name().c_str(), get_name().c_str());
  }
  
  pos= strstr(sql, "BEGIN");
  if (pos)
    cursor_pos= (pos - sql) + 6;
  
  std::string tmp(sql);
  g_free(sql);
  return tmp;
}


std::string RoutineEditorBE::get_formatted_sql_for_editing(int &cursor_pos)
{
  std::string sql= get_sql_definition_header();
  std::string code;

  code= get_sql();

  if (code.empty())
  {
    code= get_sql_template("", cursor_pos);
    cursor_pos+= sql.length();
    sql.append(code);
  }
  else
  {
    sql.append(code).append("//\n");
  }

  return sql;
}


void RoutineEditorBE::set_sql(const std::string &sql)
{
  if (get_sql() != sql)
  {
    set_sql_parser_task_cb(sigc::mem_fun(this, &RoutineEditorBE::parse_sql));
    DBObjectEditorBE::set_sql(sql, _routine);
  }
}


grt::ValueRef RoutineEditorBE::parse_sql(grt::GRT* grt, grt::StringRef sql)
{
  grt::AutoUndo undo(grt);

  int err_count= _sql_parser->parse_routine(_routine, sql.c_str());
  _has_syntax_error= 0 < err_count;

  undo.end(strfmt(_("Edit routine `%s`.`%s`"), get_schema_name().c_str(), get_name().c_str()));

  return grt::IntegerRef(err_count);
}


Sql_editor * RoutineEditorBE::get_sql_editor()
{
  Sql_editor * sql_editor= DBObjectEditorBE::get_sql_editor();
  if (sql_editor)
  {
    sql_editor->sql_checker()->only_object_type_of(Sql_syntax_check::ot_routine);
    sql_editor->sql_checker()->context_object(_routine);
  }
  return sql_editor;
}


} // namespace bec
