// -*- C++ -*- //

/* statement.h
 *
 * Copyright 2006 libgdamm Development Team
 *
 * 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.1 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <libgdamm/set.h>
#include <libgdamm/holder.h>
#include <libgdamm/sqlstatement.h>

_DEFS(libgdamm,libgda)
_PINCLUDE(glibmm/private/object_p.h)
_PINCLUDE(libgda/gda-connection.h)

namespace Gnome
{

namespace Gda
{

class Connection;

_WRAP_ENUM(StatementModelUsage, GdaStatementModelUsage)
_WRAP_ENUM(StatementSqlFlag, GdaStatementSqlFlag)

_WRAP_GERROR(StatementError, GdaStatementError, GDA_STATEMENT_ERROR)

/** Single SQL statement.
 *
 * The Gda::Statement represents a single SQL statement (multiple statements
 * can be grouped in a Batch object).
 *
 * A Gda::Statement can either be built "manually" by building a Gda::SqlStatement
 * structure, or from an SQL statement using a Gda::SqlParser object.
 * A GdaConnection can use a GdaStatement to:
 *
 *  - prepare it for a future execution, the preparation step involves converting the
 *     GdaStatement object into a structure used by the database's own API, see
 *     Gda::Connection::statement_prepare()
 *  - execute it using Gda::Connection::statement_execute_select() if it is known
 *     that the statement is a selection statement, Gda::Connection::statement_execute_non_select()
 *     if it is not a selection statement, or Gda::Connection::statement_execute()
 *     when the type of expected result is unknown.
 * Note that it is possible to use the same Gda::Statement object at the same time
 * with several Gda::Connection objects.
 *
 * @ingroup Connection
 */
class Statement : public Glib::Object
{
  _CLASS_GOBJECT(Statement, GdaStatement, GDA_STATEMENT, Glib::Object, GObject)
protected:
  _CTOR_DEFAULT()

public:
  _WRAP_CREATE()
  _WRAP_METHOD(Glib::RefPtr<Statement> copy() const, gda_statement_copy)

  _WRAP_METHOD(Glib::ustring serialize() const, gda_statement_serialize)

  //TODO: Should this be const? Probably not, because the caller could change Set. murrayc.
  _WRAP_METHOD_DOCS_ONLY(gda_statement_get_parameters)
  bool get_parameters(Glib::RefPtr<Set>& out_params);
  _IGNORE(gda_statement_get_parameters)

  //TODO: Documentation:
  Glib::ustring to_sql() const;

  //TODO: Documentation:
  Glib::ustring to_sql(const Glib::RefPtr<const Set>& params) const;
  _IGNORE(gda_statement_to_sql, gda_statement_to_sql_real)

  typedef std::vector< const Glib::RefPtr<Holder> > HolderVector;

  //TODO: Add an overload without the params?
  _WRAP_METHOD_DOCS_ONLY(gda_statement_to_sql_extended)
  Glib::ustring to_sql(const Glib::RefPtr<Connection>& cnc, const Glib::RefPtr<const Set>& params,
                                StatementSqlFlag flags, const HolderVector& params_used) const;

  _WRAP_METHOD(SqlStatementType get_statement_type() const, gda_statement_get_statement_type)
  _WRAP_METHOD(bool is_useless() const, gda_statement_is_useless)
  _WRAP_METHOD(bool check_structure() const, gda_statement_check_structure, errthrow)
  _WRAP_METHOD(bool check_validity(const Glib::RefPtr<Connection>& cnc) const, gda_statement_check_validity, errthrow)
  _WRAP_METHOD(bool normalize(const Glib::RefPtr<Connection>& cnc), gda_statement_normalize, errthrow)

  //TODO: Use a real GType in libgda?
  _WRAP_PROPERTY("structure", gpointer)

  //The m4 conversion is here because it is only for signals:
#m4 _CONVERSION(`GdaConnection*',`const Glib::RefPtr<Connection>&',`Glib::wrap($3, true)')

  _WRAP_SIGNAL(void checked(const Glib::RefPtr<Connection>& cnc, bool valid), "checked")
  _WRAP_SIGNAL(void reset(), "reset")
};

} // namespace Gda
} // namespace Gnome
