/* 
 * © 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.
 */
#ifndef _EDITOR_DBOBJECT_H_
#define _EDITOR_DBOBJECT_H_


#include "wbpublic_public_interface.h"
#include "grt/editor_base.h"

#include "grt/tree_model.h"
#include "grtsqlparser/sql_facade.h"
#include "sqlide/sql_editor_be.h"

#include "grts/structs.db.h"
#include "grts/structs.db.mgmt.h"

#include <memory>


namespace bec {

  class WBPUBLICBACKEND_PUBLIC_FUNC DBObjectEditorBE : public BaseEditor
  {
  public:
    virtual bool should_close_on_delete_of(const std::string &oid);

    virtual GrtObjectRef get_object() { return get_dbobject(); }
    virtual db_DatabaseObjectRef get_dbobject()= 0;
    virtual db_mgmt_RdbmsRef get_rdbms() { return _rdbms; }

    virtual std::string get_name();
    virtual void set_name(const std::string &name);

    virtual std::string get_comment();
    virtual void set_comment(const std::string &descr);

    virtual std::string get_sql();
    virtual bool is_sql_commented();
    virtual void set_sql_commented(bool flag);

    virtual void set_custom_data(const std::string &name, const std::string &value);
    virtual size_t count_custom_data();
    virtual void get_custom_data(size_t index, std::string &name, std::string &value);
    virtual void delete_custom_data(const std::string &name);

    virtual Sql_editor * get_sql_editor();

  public: // helper functions

    db_SchemaRef get_schema();
    virtual std::string get_schema_name();

    db_CatalogRef get_catalog();
    
    db_SchemaRef get_schema_with_name(const std::string &schema_name);

    virtual std::vector<std::string> get_all_table_names();
    virtual std::vector<std::string> get_schema_table_names();
    virtual std::vector<std::string> get_table_column_names(const std::string &table_name);
    virtual std::vector<std::string> get_table_column_names(const db_TableRef &table);

    // charsets and collations
    virtual std::vector<std::string> get_charset_collation_list();
    bool parse_charset_collation(const std::string &str, std::string &charset, std::string &collation);
    std::string format_charset_collation(const std::string &charset, const std::string &collation);

    void update_change_date();

    virtual ~DBObjectEditorBE();

  protected:
    db_mgmt_RdbmsRef _rdbms;
    std::auto_ptr<Sql_editor> _sql_editor;

    DBObjectEditorBE(GRTManager *grtm, const db_DatabaseObjectRef &object, const db_mgmt_RdbmsRef &rdbms);

    virtual std::string get_object_type();

  public:
    typedef std::vector<std::string> Log_messages;
    typedef sigc::slot<void, Log_messages> Sql_parser_log_cb;
    typedef Sql_parser_base::Parse_error_cb Sql_parser_err_cb;

    void set_sql_parser_log_cb(const Sql_parser_log_cb &cb);
    void set_sql_parser_err_cb(const Sql_parser_err_cb &cb);
    void set_sql(const std::string &sql, const db_DatabaseObjectRef &template_obj, const std::string &comment= "");

  protected:
    typedef sigc::slot<grt::ValueRef, grt::GRT*, grt::StringRef> Sql_parser_task_cb;
    void set_sql_parser_task_cb(const Sql_parser_task_cb &cb);
    std::auto_ptr<Invalid_sql_parser> _sql_parser;

  private:
    void sql_parser_task_finished_cb(grt::ValueRef value);
    void sql_parser_msg_cb(const grt::Message &msg);

    Sql_parser_task_cb _sql_parser_task_cb;
    Log_messages _sql_parser_log;
    Sql_parser_log_cb _sql_parser_log_cb;
    Sql_parser_err_cb _sql_parser_err_cb;
  };
};


#endif /* _EDITOR_DBOBJECT_H_ */
