#ifndef _MYSQL_SQL_PARSER_FE_H_
#define _MYSQL_SQL_PARSER_FE_H_


#include "myx_sql_tree_item.h"
#include "mysql_sql_parser_public_interface.h"
#include "grtpp.h"

using namespace mysql_parser;

/** Splits provided SQL script into statements and parses them in turn providing generated AST to given callback.
 * 
 * @ingroup sqlparser
 */
class MYSQL_SQL_PARSER_PUBLIC_FUNC Mysql_sql_parser_fe
{
public:
  Mysql_sql_parser_fe(grt::GRT *grt);

  typedef int (* fe_process_sql_statement_callback)(void* user_data, const MyxStatementParser *splitter, const char *sql, const MyxSQLTreeItem *tree, int lineno, int err_tok_line_pos, int err_tok_len, const std::string &err_msg);
  int parse_sql_script(const std::string &sql, fe_process_sql_statement_callback cb, void *user_data);
  int parse_sql_script_file(const std::string &filename, fe_process_sql_statement_callback cb, void *user_data);

private:
  grt::GRT *_grt;

public:
  bool ignore_dml;
  bool processing_create_statements;
  bool processing_alter_statements;
  bool processing_drop_statements;
  struct MYSQL_SQL_PARSER_PUBLIC_FUNC SqlMode
  {
    SqlMode()
      :
      MODE_ANSI_QUOTES(false),
      MODE_HIGH_NOT_PRECEDENCE(false),
      MODE_PIPES_AS_CONCAT(false),
      MODE_NO_BACKSLASH_ESCAPES(false),
      MODE_IGNORE_SPACE(false)
    {}
    bool MODE_ANSI_QUOTES;
    bool MODE_HIGH_NOT_PRECEDENCE;
    bool MODE_PIPES_AS_CONCAT;
    bool MODE_NO_BACKSLASH_ESCAPES;
    bool MODE_IGNORE_SPACE;
  };
  SqlMode sql_mode;

  static void determine_token_position(const MyxSQLTreeItem *item, const MyxStatementParser *splitter, const char *statement, int &lineno, int &token_line_pos, int &token_len);

private:
  static int process_sql_statement_cb(const MyxStatementParser *splitter, const char *sql, void *context);
  void reset();
};


#endif // _MYSQL_SQL_PARSER_FE_H_
