#ifndef _MYSQL_SQL_PARSER_BASE_H_
#define _MYSQL_SQL_PARSER_BASE_H_


#include "mysql_sql_parser_public_interface.h"
#include "grtsqlparser/sql_parser_base.h"
#include "grts/structs.db.mysql.h"


class Mysql_sql_parser_fe;
namespace mysql_parser
{
class MyxStatementParser;
class MyxSQLTreeItem;
}
using namespace mysql_parser;

#ifdef _WIN32
#pragma warning(disable:4250)
//warning C4250: 'Mysql_sql_parser' : inherits 'Mysql_sql_parser_base::Mysql_sql_parser_base::total_line_count' via dominance
#endif

/** Provides DBMS specific functionality for processing of SQL statements/scripts.
 * 
 * @ingroup sqlparser
 */
class MYSQL_SQL_PARSER_PUBLIC_FUNC Mysql_sql_parser_base : virtual public Sql_parser_base
{
private:
  static int process_sql_statement(
    void *sql_parser_ptr,
    const MyxStatementParser *splitter,
    const char *sql_statement,
    const MyxSQLTreeItem *tree,
    int lineno,
    int _err_tok_line_pos,
    int _err_tok_len,
    const std::string &err_msg); //callback
  friend int process_sql_statement(void *, const MyxStatementParser *, const char *, const MyxSQLTreeItem *, int, const std::string &);

protected:
  Mysql_sql_parser_base(grt::GRT *grt);
  virtual ~Mysql_sql_parser_base() {}

  // parse tree helpers
  void process_obj_full_name_item(const MyxSQLTreeItem *item, std::string &schema_name, std::string &obj_name);

  // error reporting
  void report_semantic_error(const MyxSQLTreeItem *item, const std::string &err_msg, int entry_type);

  // misc
  virtual int total_line_count();

  // aux types
  typedef sigc::slot<int, const MyxSQLTreeItem *> Process_sql_statement;

  // basic functionality
  int parse_sql_script(Mysql_sql_parser_fe &sql_parser_fe, const std::string &sql);
  int parse_sql_script_file(Mysql_sql_parser_fe &sql_parser_fe, const std::string &filename);

  // data members
  const MyxStatementParser *_splitter;
  Process_sql_statement _process_sql_statement;
  db_mysql_CatalogRef _catalog;
  db_mysql_SchemaRef _active_schema;
  int _lineno;
  int _err_tok_line_pos;
  int _err_tok_len;
  std::string _err_msg;

  class Null_state_keeper : public Sql_parser_base::Null_state_keeper
  {
  public:
    Null_state_keeper(Mysql_sql_parser_base *sql_parser)
      : Sql_parser_base::Null_state_keeper(sql_parser), _sql_parser(sql_parser) {}
    virtual ~Null_state_keeper();
  private:
    Mysql_sql_parser_base *_sql_parser;
  };
  friend class Null_state_keeper;
};


#endif // _MYSQL_SQL_PARSER_BASE_H_
