/*  libhrel - Holyrel library

    Copyright (C) 2005 Chong Kai Xiong <descender@phreaker.net>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library 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 H_RELATION_H
#define H_RELATION_H

#include <glib.h>
#include <glib-object.h>
#include <libhrel/tuple.h>
#include <libhrel/attr_map.h>

#define H_STR_NULL(str)            ((str) ? (str) : "None")

G_BEGIN_DECLS

#define H_TYPE_RELATION             (h_relation_get_type ())
#define H_RELATION(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), H_TYPE_RELATION, HRelation))
#define H_RELATION_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST    ((klass), H_TYPE_RELATION, HRelationClass))
#define H_IS_RELATION(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), H_TYPE_RELATION))
#define H_IS_RELATION_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE    ((klass), H_TYPE_RELATION))
#define H_RELATION_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS  ((obj), H_TYPE_RELATION, HRelationClass))

#define H_RELATION_FLOATING(obj)    (H_RELATION (obj)->floating)

typedef struct _HRelation      HRelation;
typedef struct _HRelationClass HRelationClass;

typedef void (*HTupleFunc) (HTuple   *tuple,
                            gpointer  user_data);

struct _HRelation
{
  GObject  gobject;
  gboolean floating;
};

struct _HRelationClass
{
  GObjectClass parent_class;

  gchar      *(*printable_str)   (HRelation *self);
  HAttribute *(*get_attr)        (HRelation *self, const gchar *name);
  guint       (*get_degree)      (HRelation *self);
  guint       (*get_cardinality) (HRelation *self);
  void        (*for_each_attr)   (HRelation *self, HAttrFunc  func, gpointer user_data);
  void        (*for_each_tuple)  (HRelation *self, HTupleFunc func, gpointer user_data);
  gboolean    (*has_tuple)       (HRelation *self, HTuple *tuple);
  void        (*insert_tuple)    (HRelation *self, HTuple *tuple);
  void        (*delete_tuple)    (HRelation *self, HTuple *tuple);
};

#define h_relation_get_n_attrs(relation)  h_relation_get_degree (relation)
#define h_relation_get_n_tuples(relation) h_relation_get_cardinality (relation)
#define h_relation_ref(relation)          g_object_ref (relation)
#define h_relation_unref(relation)        g_object_unref (relation)

GType       h_relation_get_type        (void);
void        h_relation_set_name        (HRelation   *relation,
                                        const gchar *name);
void        h_relation_set_description (HRelation   *relation,
                                        const gchar *desc);
gchar      *h_relation_get_name        (HRelation   *relation);
gchar      *h_relation_get_description (HRelation   *relation);
HAttribute *h_relation_get_attr        (HRelation   *relation,
                                        const gchar *name);
void        h_relation_print           (HRelation   *relation);
gchar      *h_relation_printable_str   (HRelation   *relation);
void        h_relation_for_each_attr   (HRelation   *relation,
                                        HAttrFunc    func,
                                        gpointer     user_data);
void        h_relation_for_each_tuple  (HRelation   *relation,
                                        HTupleFunc   func,
                                        gpointer     user_data);
guint       h_relation_get_cardinality (HRelation   *relation);
guint       h_relation_get_degree      (HRelation   *relation);
gboolean    h_relation_has_same_attrs  (HRelation   *relation1,
                                        HRelation   *relation2);
gboolean    h_relation_has_tuple       (HRelation   *relation,
                                        HTuple      *tuple);
void        h_relation_sink            (HRelation   *relation);
HRelation  *h_relation_sink_or_ref     (HRelation   *relation);

G_END_DECLS

#include <libhrel/relation_base.h>
#include <libhrel/relation_sort.h>
#include <libhrel/relation_restrict.h>
#include <libhrel/relation_project.h>
#include <libhrel/relation_union.h>
#include <libhrel/relation_diff.h>
#include <libhrel/relation_intersect.h>

#endif /* #ifndef H_RELATION_H */
