#include "stdafx.h"

#include "mysql_sql_facade.h"
#include "mysql_sql_parser.h"
#include "mysql_sql_normalizer.h"
#include "mysql_sql_inserts_loader.h"
#include "mysql_sql_schema_rename.h"
#include "mysql_sql_syntax_check.h"
#include "mysql_sql_semantic_check.h"
#include "mysql_invalid_sql_parser.h"
#include "mysql_sql_script_splitter.h"
#include "mysql_sql_statement_decomposer.h"


using namespace bec;


int MysqlSqlFacade::splitSqlScript(const std::string &sql, std::list<std::string> &statements)
{
  return Mysql_sql_script_splitter().process(sql, statements);
}


std::auto_ptr<Sql_parser> MysqlSqlFacade::sqlParser()
{
  return std::auto_ptr<Sql_parser>(new Mysql_sql_parser(get_grt()));
}


int MysqlSqlFacade::parseSqlScriptString(db_CatalogRef catalog, const std::string sql)
{
  return parseSqlScriptStringEx(catalog, sql, DictRef());
}


int MysqlSqlFacade::parseSqlScriptStringEx(db_CatalogRef catalog, const std::string sql, const grt::DictRef options)
{
  return Mysql_sql_parser(get_grt()).parse_sql_script(
    db_mysql_CatalogRef::cast_from(catalog), sql, options);
}


int MysqlSqlFacade::parseSqlScriptFile(db_CatalogRef catalog, const std::string filename)
{
  return parseSqlScriptFileEx(catalog, filename, DictRef());
}


int MysqlSqlFacade::parseSqlScriptFileEx(db_CatalogRef catalog, const std::string filename, const grt::DictRef options)
{
  return Mysql_sql_parser(get_grt()).parse_sql_script_file(
    db_mysql_CatalogRef::cast_from(catalog), filename, options);
}


std::auto_ptr<Invalid_sql_parser> MysqlSqlFacade::invalidSqlParser()
{
  return std::auto_ptr<Invalid_sql_parser>(new Mysql_invalid_sql_parser(get_grt()));
}


int MysqlSqlFacade::parseInserts(db_TableRef table, const std::string sql)
{
  return Mysql_invalid_sql_parser(get_grt()).parse_inserts(db_mysql_TableRef::cast_from(table), sql);
}


int MysqlSqlFacade::parseTriggers(db_TableRef table, const std::string sql)
{
  return Mysql_invalid_sql_parser(get_grt()).parse_triggers(db_mysql_TableRef::cast_from(table), sql);
}


int MysqlSqlFacade::parseRoutine(db_RoutineRef routine, const std::string sql)
{
  return Mysql_invalid_sql_parser(get_grt()).parse_routine(db_mysql_RoutineRef::cast_from(routine), sql);
}


int MysqlSqlFacade::parseRoutines(db_RoutineGroupRef routineGroup, const std::string sql)
{
  return Mysql_invalid_sql_parser(get_grt()).parse_routines(db_mysql_RoutineGroupRef::cast_from(routineGroup), sql);
}


int MysqlSqlFacade::parseView(db_ViewRef view, const std::string sql)
{
  return Mysql_invalid_sql_parser(get_grt()).parse_view(db_mysql_ViewRef::cast_from(view), sql);
}


std::auto_ptr<Sql_syntax_check> MysqlSqlFacade::sqlSyntaxCheck()
{
  return std::auto_ptr<Sql_syntax_check>(new Mysql_sql_syntax_check(get_grt()));
}


int MysqlSqlFacade::checkSqlSyntax(const std::string sql)
{
  return Mysql_sql_syntax_check(get_grt()).check_sql(sql);
}


int MysqlSqlFacade::checkTriggerSyntax(const std::string sql)
{
  return Mysql_sql_syntax_check(get_grt()).check_trigger(sql);
}


int MysqlSqlFacade::checkViewSyntax(const std::string sql)
{
  return Mysql_sql_syntax_check(get_grt()).check_view(sql);
}


int MysqlSqlFacade::checkRoutineSyntax(const std::string sql)
{
  return Mysql_sql_syntax_check(get_grt()).check_routine(sql);
}


std::auto_ptr<Sql_semantic_check> MysqlSqlFacade::sqlSemanticCheck()
{
  return std::auto_ptr<Sql_semantic_check>(new Mysql_sql_semantic_check(get_grt()));
}


std::auto_ptr<Sql_normalizer> MysqlSqlFacade::sqlNormalizer()
{
  return std::auto_ptr<Sql_normalizer>(new Mysql_sql_normalizer(get_grt()));
}


std::string MysqlSqlFacade::normalizeSqlStatement(const std::string sql, const std::string schema_name)
{
  return Mysql_sql_normalizer(get_grt()).normalize(sql, schema_name);
}


std::auto_ptr<Sql_inserts_loader> MysqlSqlFacade::sqlInsertsLoader()
{
  return std::auto_ptr<Sql_inserts_loader>(new Mysql_sql_inserts_loader(get_grt()));
}


int MysqlSqlFacade::renameSchemaReferences(db_CatalogRef catalog, const std::string old_schema_name, const std::string new_schema_name)
{
  return Mysql_sql_schema_rename(get_grt()).rename_schema_references(
    db_mysql_CatalogRef::cast_from(catalog), old_schema_name, new_schema_name);
}


std::auto_ptr<Sql_statement_decomposer> MysqlSqlFacade::sqlStatementDecomposer()
{
  return std::auto_ptr<Sql_statement_decomposer>(new Mysql_sql_statement_decomposer(get_grt()));
}
