/*
	Copyright (C) 2012 - Juan Ferrer Toribio

	This program 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 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
	Lesser General Public License for more details.

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

#include <glib-object.h>
#include <gvn/gvn-param.h>
#include "db-model.h"

#define DB_CALC_LOG_DOMAIN		(g_quark_from_string ("DbCalc"))

#define DB_TYPE_CALC			(db_calc_get_type ())
#define DB_CALC(self)			(G_TYPE_CHECK_INSTANCE_CAST (self, DB_TYPE_CALC, DbCalc))
#define DB_IS_CALC(self)		(G_TYPE_CHECK_INSTANCE_TYPE (self, DB_TYPE_CALC))
#define DB_CALC_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((self), DB_TYPE_CALC, DbCalcClass))
#define DB_IS_CALC_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), DB_TYPE_CLASS))
#define DB_CALC_GET_CLASS(self)	(G_TYPE_INSTANCE_GET_CLASS ((self), DB_TYPE_CALC, DbCalcClass))

#define DB_TYPE_CALC_TYPE		(db_calc_type_get_type ())

typedef struct _DbCalc DbCalc;
typedef struct _DbCalcClass DbCalcClass;

/**
 * DbCalcFunc:
 * @self: a #DbCalc
 * @iter: a #DbIter pointing to a row of the model
 * @out: the return position for the resulting value
 * @user_data: (closure): the data passed to the function
 * 
 * The prototype of the functions used by the #DbCalc to perform operations
 * on more than one column or to perform additional operations to a column,
 * previously to add them to the calculations performed by the @DbCalc itself.
 **/ 
typedef void (*DbCalcFunc)	(DbCalc * self, DbIter * iter, GValue * out, gpointer user_data);

/**
 * DbCalcType:
 * @DB_CALC_TYPE_SUM: sumatory of the values
 * @DB_CALC_TYPE_AVG: average of the values 
 * 
 * Defines which type of operation will be performed over the column values.
 **/
typedef enum
{
	DB_CALC_TYPE_SUM,
	DB_CALC_TYPE_AVG
}
DbCalcType;

struct _DbCalc
{
	GvnParam parent;

	DbModel * model;
	DbCalcType type;
	DbCalcFunc func;
	gpointer data;
	guint column_index;
	gchar * column_name;
	guint count;
};

struct _DbCalcClass
{
	/* <private> */
	GvnParamClass klass;
};

GType		db_calc_get_type		();
GType		db_calc_type_get_type   ();

DbCalc *	db_calc_new				(DbModel * model
									,DbCalcType type
									,const gchar * column_name);
DbCalc *	db_calc_new_with_func	(DbModel * model
									,DbCalcType type
									,DbCalcFunc func
									,gpointer data);

#endif