/* gda-query.h
 *
 * Copyright (C) 2003 - 2005 Vivien Malerba
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


#ifndef __GDA_QUERY_H_
#define __GDA_QUERY_H_

#include <libgda/gda-query-object.h>
#include "gda-decl.h"
#include "gda-enums.h"

G_BEGIN_DECLS

#define GDA_TYPE_QUERY          (gda_query_get_type())
#define GDA_QUERY(obj)          G_TYPE_CHECK_INSTANCE_CAST (obj, gda_query_get_type(), GdaQuery)
#define GDA_QUERY_CLASS(klass)  G_TYPE_CHECK_CLASS_CAST (klass, gda_query_get_type (), GdaQueryClass)
#define GDA_IS_QUERY(obj)       G_TYPE_CHECK_INSTANCE_TYPE (obj, gda_query_get_type ())

/* error reporting */
extern GQuark gda_query_error_quark (void);
#define GDA_QUERY_ERROR gda_query_error_quark ()

/* different possible types for a query */
typedef enum {
        GDA_QUERY_TYPE_SELECT,
	GDA_QUERY_TYPE_INSERT,
	GDA_QUERY_TYPE_UPDATE,
	GDA_QUERY_TYPE_DELETE,
        GDA_QUERY_TYPE_UNION,
        GDA_QUERY_TYPE_INTERSECT,
	GDA_QUERY_TYPE_EXCEPT,
        GDA_QUERY_TYPE_NON_PARSED_SQL
} GdaQueryType;

enum
{
	GDA_QUERY_XML_LOAD_ERROR,
	GDA_QUERY_META_DATA_UPDATE,
	GDA_QUERY_FIELDS_ERROR,
	GDA_QUERY_TARGETS_ERROR,
	GDA_QUERY_RENDER_ERROR,
	GDA_QUERY_PARSE_ERROR,
	GDA_QUERY_SYNTAX_ERROR,
	GDA_QUERY_STRUCTURE_ERROR,
	GDA_QUERY_SQL_ANALYSE_ERROR
};


/* struct for the object's data */
struct _GdaQuery
{
	GdaQueryObject         object;
	GdaQueryPrivate       *priv;
};

/* struct for the object's class */
struct _GdaQueryClass
{
	GdaQueryObjectClass    parent_class;

	/* signals */
	void   (*type_changed)         (GdaQuery *query);
	void   (*condition_changed)    (GdaQuery *query);

	void   (*target_added)         (GdaQuery *query, GdaQueryTarget *target);
	void   (*target_removed)       (GdaQuery *query, GdaQueryTarget *target);
	void   (*target_updated)       (GdaQuery *query, GdaQueryTarget *target);

	void   (*join_added)           (GdaQuery *query, GdaQueryJoin *join);
	void   (*join_removed)         (GdaQuery *query, GdaQueryJoin *join);
	void   (*join_updated)         (GdaQuery *query, GdaQueryJoin *join);
	
	void   (*sub_query_added)      (GdaQuery *query, GdaQuery *sub_query);
	void   (*sub_query_removed)    (GdaQuery *query, GdaQuery *sub_query);
	void   (*sub_query_updated)    (GdaQuery *query, GdaQuery *sub_query);
};

GType              gda_query_get_type               (void);
GObject           *gda_query_new                    (GdaDict *dict);
GObject           *gda_query_new_copy               (GdaQuery *orig, GHashTable *replacements);
GObject           *gda_query_new_from_sql           (GdaDict *dict, const gchar *sql, GError **error);

void               gda_query_set_query_type         (GdaQuery *query, GdaQueryType type);
GdaQueryType       gda_query_get_query_type         (GdaQuery *query);
const gchar       *gda_query_get_query_type_string  (GdaQuery *query);
gboolean           gda_query_is_select_query        (GdaQuery *query);
gboolean           gda_query_is_insert_query        (GdaQuery *query);
gboolean           gda_query_is_update_query        (GdaQuery *query);
gboolean           gda_query_is_delete_query        (GdaQuery *query);
gboolean           gda_query_is_modif_query         (GdaQuery *query);
GdaQuery          *gda_query_get_parent_query       (GdaQuery *query);

/* if SQL queries */
void               gda_query_set_sql_text           (GdaQuery *query, const gchar *sql, GError **error);
const gchar       *gda_query_get_sql_text           (GdaQuery *query);

/* for other types of queries */
GSList            *gda_query_get_sub_queries        (GdaQuery *query);
void               gda_query_add_sub_query          (GdaQuery *query, GdaQuery *sub_query);
void               gda_query_del_sub_query          (GdaQuery *query, GdaQuery *sub_query);
 
void               gda_query_add_param_source       (GdaQuery *query, GdaDataModel *param_source);
void               gda_query_del_param_source       (GdaQuery *query, GdaDataModel *param_source);
const GSList      *gda_query_get_param_sources      (GdaQuery *query);

gboolean           gda_query_add_target             (GdaQuery *query, GdaQueryTarget *target, GError **error);
void               gda_query_del_target             (GdaQuery *query, GdaQueryTarget *target);
GSList            *gda_query_get_targets            (GdaQuery *query);
GdaQueryTarget    *gda_query_get_target_by_xml_id   (GdaQuery *query, const gchar *xml_id);
GdaQueryTarget    *gda_query_get_target_by_alias    (GdaQuery *query, const gchar *alias_or_name);
GSList            *gda_query_get_target_pkfields    (GdaQuery *query, GdaQueryTarget *target);

GSList            *gda_query_get_joins              (GdaQuery *query);
GdaQueryJoin      *gda_query_get_join_by_targets    (GdaQuery *query, GdaQueryTarget *target1, GdaQueryTarget *target2);
gboolean           gda_query_add_join               (GdaQuery *query, GdaQueryJoin *join);
void               gda_query_del_join               (GdaQuery *query, GdaQueryJoin *join);

void               gda_query_set_condition          (GdaQuery *query, GdaQueryCondition *cond);
GdaQueryCondition *gda_query_get_condition          (GdaQuery *query);
GSList            *gda_query_get_main_conditions    (GdaQuery *query);
void               gda_query_append_condition       (GdaQuery *query, GdaQueryCondition *cond, gboolean append_as_and);

void               gda_query_set_order_by_field     (GdaQuery *query, GdaQueryField *field, gint order, gboolean ascendant);
gint               gda_query_get_order_by_field     (GdaQuery *query, GdaQueryField *field, gboolean *ascendant);

/* utility functions */
GdaQueryField     *gda_query_add_field_from_sql     (GdaQuery *query, const gchar *field, GError **error);

GSList            *gda_query_get_all_fields         (GdaQuery *query);
GdaQueryField     *gda_query_get_field_by_sql_naming        (GdaQuery *query, const gchar *sql_naming);
GdaQueryField     *gda_query_get_field_by_ref_field         (GdaQuery *query, GdaQueryTarget *target, GdaEntityField *ref_field, 
								 GdaEntityFieldState field_state);
GdaQueryField     *gda_query_get_first_field_for_target     (GdaQuery *query, GdaQueryTarget *target);
GSList            *gda_query_expand_all_field               (GdaQuery *query, GdaQueryTarget *target);
void               gda_query_order_fields_using_join_conds  (GdaQuery *query);
GSList            *gda_query_get_fields_by_target           (GdaQuery *query, GdaQueryTarget *target, gboolean visible_fields_only);

void               gda_query_declare_condition      (GdaQuery *query, GdaQueryCondition *cond);
void               gda_query_undeclare_condition    (GdaQuery *query, GdaQueryCondition *cond);

G_END_DECLS

#endif
