<commit_before>/* 
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * 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 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
 */

#include "midgard_query_constraint.h"
#include "midgard_query_property.h"
#include "midgard_query_storage.h"
#include "midgard_query_holder.h"
#include "midgard_query_constraint_simple.h"
#include "midgard_dbobject.h"
#include "midgard_core_query.h"
#include "midgard_core_object_class.h"
#include "midgard_core_object.h"

struct _MidgardQueryConstraintPrivate {
	GObject  parent;
	MidgardQueryProperty *property_value;
	gchar *op;
	GdaSqlOperatorType op_type;
	MidgardQueryStorage *storage;
	MidgardQueryHolder *holder;
};

static struct {
	const gchar *name;
	GdaSqlOperatorType op_type;
} valid_operators[] = {
	{ "=", GDA_SQL_OPERATOR_TYPE_EQ },
	{ ">", GDA_SQL_OPERATOR_TYPE_GT },
	{ "<", GDA_SQL_OPERATOR_TYPE_LT },
	{ "!=", GDA_SQL_OPERATOR_TYPE_DIFF },
	{ "<>", GDA_SQL_OPERATOR_TYPE_DIFF },
	{ "<=", GDA_SQL_OPERATOR_TYPE_LEQ },
	{ ">=", GDA_SQL_OPERATOR_TYPE_GEQ },
	{ "LIKE", GDA_SQL_OPERATOR_TYPE_LIKE },
	{ "NOT LIKE", GDA_SQL_OPERATOR_TYPE_GT },
	{ "IN", GDA_SQL_OPERATOR_TYPE_IN },
	{ "NOT IN", GDA_SQL_OPERATOR_TYPE_NOTIN },
	{ NULL, 0 }
};

gboolean __query_constraint_operator_is_valid(const gchar *op, GdaSqlOperatorType *op_type)
{
	guint i;
	for (i = 0; valid_operators[i].name != NULL; i++) {
		if (g_str_equal(op, valid_operators[i].name)) {
			*op_type = valid_operators[i].op_type;
			return TRUE;
		}
	}

	if (valid_operators[i].name == NULL) {
		g_warning("Invalid comparison operator %s", op);
		return FALSE;
	}

	return TRUE;
}


MidgardQueryConstraint *
midgard_query_constraint_new (MidgardQueryProperty *property, const gchar *op, 
		MidgardQueryHolder *holder, MidgardQueryStorage *storage)
{
	g_return_val_if_fail (property != NULL, NULL);
	g_return_val_if_fail (op != NULL, NULL);
	g_return_val_if_fail (holder != NULL, NULL);

	GdaSqlOperatorType op_type;
	if (!__query_constraint_operator_is_valid (op, &op_type)) 
		return NULL;	

	MidgardQueryConstraint *self = g_object_new (MIDGARD_TYPE_QUERY_CONSTRAINT, NULL);
	self->priv->property_value = property;
	self->priv->op = g_strdup (op);
	self->priv->op_type = op_type;
	self->priv->holder = holder;

	/* Allow NULL storage */
	if (storage)
		self->priv->storage = storage;
	
	return self;
}

void
midgard_query_constraint_get_value (MidgardQueryConstraint *self, GValue *value)
{
	return;
}

gboolean
midgard_query_constraint_set_value (MidgardQueryConstraint *self, const GValue *value)
{
	return FALSE;
}

MidgardQueryStorage *
midgard_query_constraint_get_storage (MidgardQueryConstraint *self)
{
	return NULL;
}

gboolean          
midgard_query_constraint_set_storage (MidgardQueryConstraint *self, MidgardQueryStorage *storage)
{
	return FALSE;
}

MidgardQueryProperty *
midgard_query_constraint_get_property (MidgardQueryConstraint *self)
{
	return NULL;
}

gboolean                
midgard_query_constraint_set_property (MidgardQueryConstraint *self, MidgardQueryProperty *property)
{
	return FALSE;
}

const gchar *
midgard_query_constraint_get_operator (MidgardQueryConstraint *self)
{
	return NULL;
}

gboolean
midgard_query_constraint_set_operator (MidgardQueryConstraint *self, const gchar *op)
{
	return FALSE;
}

/* GOBJECT ROUTINES */

static GObjectClass *parent_class = NULL;

MidgardQueryConstraintSimple**
_midgard_query_constraint_list_constraints (MidgardQueryConstraintSimple *self, guint *n_objects)
{
	return NULL;
}

static void 
__set_expression_value (GValue *dest, GValue *src)<commit_msg>Include docs. Added missed set/get implementation. Refs #1507<commit_after>/* 
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * 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 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
 */

#include "midgard_query_constraint.h"
#include "midgard_query_property.h"
#include "midgard_query_storage.h"
#include "midgard_query_holder.h"
#include "midgard_query_constraint_simple.h"
#include "midgard_dbobject.h"
#include "midgard_core_query.h"
#include "midgard_core_object_class.h"
#include "midgard_core_object.h"

struct _MidgardQueryConstraintPrivate {
	GObject  parent;
	MidgardQueryProperty *property_value;
	gchar *op;
	GdaSqlOperatorType op_type;
	MidgardQueryStorage *storage;
	MidgardQueryHolder *holder;
};

static struct {
	const gchar *name;
	GdaSqlOperatorType op_type;
} valid_operators[] = {
	{ "=", GDA_SQL_OPERATOR_TYPE_EQ },
	{ ">", GDA_SQL_OPERATOR_TYPE_GT },
	{ "<", GDA_SQL_OPERATOR_TYPE_LT },
	{ "!=", GDA_SQL_OPERATOR_TYPE_DIFF },
	{ "<>", GDA_SQL_OPERATOR_TYPE_DIFF },
	{ "<=", GDA_SQL_OPERATOR_TYPE_LEQ },
	{ ">=", GDA_SQL_OPERATOR_TYPE_GEQ },
	{ "LIKE", GDA_SQL_OPERATOR_TYPE_LIKE },
	{ "NOT LIKE", GDA_SQL_OPERATOR_TYPE_GT },
	{ "IN", GDA_SQL_OPERATOR_TYPE_IN },
	{ "NOT IN", GDA_SQL_OPERATOR_TYPE_NOTIN },
	{ NULL, 0 }
};

gboolean __query_constraint_operator_is_valid(const gchar *op, GdaSqlOperatorType *op_type)
{
	guint i;
	for (i = 0; valid_operators[i].name != NULL; i++) {
		if (g_str_equal(op, valid_operators[i].name)) {
			*op_type = valid_operators[i].op_type;
			return TRUE;
		}
	}

	if (valid_operators[i].name == NULL) {
		g_warning("Invalid comparison operator %s", op);
		return FALSE;
	}

	return TRUE;
}


/**
 * midgard_query_constraint_new:
 * @property: #MidgardQueryProperty instance
 * @op: constraint operator
 * @holder: #MidgardQueryHolder instance
 * @storage: optional #MidgardQueryStorage to use with constraint
 * 
 * Valid @op operators are: '=', '<', '>', '!=', '<>', '<=', '>=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN'
 *
 * Returns: new #MidgardQueryConstraint instance, or %NULL on failure
 * Since: 10.05
 */ 
MidgardQueryConstraint *
midgard_query_constraint_new (MidgardQueryProperty *property, const gchar *op, 
		MidgardQueryHolder *holder, MidgardQueryStorage *storage)
{
	g_return_val_if_fail (property != NULL, NULL);
	g_return_val_if_fail (op != NULL, NULL);
	g_return_val_if_fail (holder != NULL, NULL);

	GdaSqlOperatorType op_type;
	if (!__query_constraint_operator_is_valid (op, &op_type)) 
		return NULL;	

	MidgardQueryConstraint *self = g_object_new (MIDGARD_TYPE_QUERY_CONSTRAINT, NULL);
	self->priv->property_value = property;
	self->priv->op = g_strdup (op);
	self->priv->op_type = op_type;
	self->priv->holder = holder;

	/* Allow NULL storage */
	if (storage)
		self->priv->storage = storage;
	
	return self;
}

void
midgard_query_constraint_get_value (MidgardQueryConstraint *self, GValue *value)
{
	return;
}

gboolean
midgard_query_constraint_set_value (MidgardQueryConstraint *self, const GValue *value)
{
	return FALSE;
}

/**
 * midgard_query_constraint_get_storage:
 * @self: #MidgardQueryStorage instance
 *
 * Returns: #MidgardQueryStorage associated with constraint or %NULL
 * Since: 10.05
 */  
MidgardQueryStorage *
midgard_query_constraint_get_storage (MidgardQueryConstraint *self)
{
	g_return_val_if_fail (self != NULL, NULL);
	return self->priv->storage;
}

/**
 * midgard_query_constraint_set_storage:
 * @self: #MidgardQueryConstraint instance
 * @storage: #MidgardQueryStorage to associate with @self constraint
 *
 * Returns: %TRUE on success, %FALSE otherwise
 * Since: 10.05
 */ 
gboolean          
midgard_query_constraint_set_storage (MidgardQueryConstraint *self, MidgardQueryStorage *storage)
{
	g_return_val_if_fail (self != NULL, FALSE);

	self->priv->storage = storage;
	return TRUE;
}

/**
 * midgard_query_constraint_get_property:
 * @self: #MidgardQueryConstraint instance
 *
 * Returns: #MidgardQueryProperty associated with @self constraint, or %NULL
 * Since: 10.05
 */ 
MidgardQueryProperty *
midgard_query_constraint_get_property (MidgardQueryConstraint *self)
{
	g_return_val_if_fail (self != NULL, NULL);

	return self->priv->property_value;
}

/**
 * midgard_query_constraint_set_property:
 * @self: #MidgardQueryConstraint instance
 * @property: #MidgardQueryProperty to associate with @self constraint
 *
 * Returns: %TRUE on success, %FALSE otherwise
 * Since: 10.05
 */ 
gboolean                
midgard_query_constraint_set_property (MidgardQueryConstraint *self, MidgardQueryProperty *property)
{
	g_return_val_if_fail (self != NULL, FALSE);

	self->priv->property_value = property;
	return TRUE;
}

/**
 * midgard_query_constraint_get_operator:
 * @self: #MidgardQueryConstraint instance
 *
 * Returns: operator type associated with @self constraint, or %NULL
 * Since: 10.05
 */
const gchar *
midgard_query_constraint_get_operator (MidgardQueryConstraint *self)
{
	g_return_val_if_fail (self != NULL, NULL);

	return self->priv->op;
}

/**
 * midgard_query_constraint_set_operator:
 * @self: #MidgardQueryConstraint instance
 * @op: operator to associate with constraint
 * 
 * Check midgard_query_constraint_new() for valid operator types.
 *
 * Returns: %TRUE on success, %FALSE otherwise
 * Since: 10.05
 */ 
gboolean
midgard_query_constraint_set_operator (MidgardQueryConstraint *self, const gchar *op)
{
	g_return_val_if_fail (self != NULL, FALSE);

 	GdaSqlOperatorType op_type;
	if (!__query_constraint_operator_is_valid (op, &op_type))
		return FALSE;

        self->priv->op = g_strdup (op);
        self->priv->op_type = op_type;
	return TRUE;
}

/* GOBJECT ROUTINES */

static GObjectClass *parent_class = NULL;

MidgardQueryConstraintSimple**
_midgard_query_constraint_list_constraints (MidgardQueryConstraintSimple *self, guint *n_objects)
{
	return NULL;
}

static void 
__set_expression_value (GValue *dest, GValue *src)
{'fraction_non_alphanumeric': 0.10555296313579095, 'fraction_numerical': 0.006906206252916472, 'mean_word_length': 5.4437763078773305, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5158625', 'n_tokens_mistral': 3855, 'n_tokens_neox': 3574, 'n_words': 882}