<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_schema_object_tree.h"
#include "midgard_error.h"
#include "midgard_core_query.h"
#include "midgard_core_object.h"
#include "midgard_core_object_class.h"
#include "midgard_reflector_object.h"
#include "midgard_reflector_property.h"
#include "midgard_datatypes.h"

gboolean _midgard_tree_exists(MidgardConnection *mgd, 
		const gchar *table, const gchar *upfield, 
		gint root, gint id)
{
	g_return_val_if_fail(mgd != NULL, FALSE);
	g_return_val_if_fail(table != NULL, FALSE);
	g_return_val_if_fail(upfield != NULL, FALSE);

	if(id == 0) {
		g_warning("tree_exists: expected id > 0");
		return FALSE;
	}
	
	gint _id = id;
	gint _root = root;
	gboolean _exists = FALSE;

	do {

		GString *where = g_string_new("");
		g_string_append_printf(where, "id = %d ", _id);

		GValue *value = 
			midgard_core_query_get_field_value(mgd, 
					upfield, table, where->str);
		g_string_free(where, TRUE);

		if(G_IS_VALUE(value)) {
			if(G_VALUE_HOLDS_INT(value))
				_id = g_value_get_int(value);
			if(G_VALUE_HOLDS_UINT(value))
				_id = g_value_get_uint(value);
		}

		if(_root == _id)
			return TRUE;

		if(_id == 0)
			return FALSE;

	} while (_id > 0);

	return _exists;
}

gboolean midgard_object_is_in_parent_tree(MidgardObject *self, guint rootid, guint id)
{
	g_return_val_if_fail(self != NULL, FALSE);

	if (!MGD_OBJECT_CNC (self)) {
		g_warning("Object has no connection pointer!");
		return FALSE;
	}

	MidgardConnection *mgd = MGD_OBJECT_CNC (self);

	MidgardObjectClass *parent_class = 
		MIDGARD_OBJECT_GET_CLASS_BY_NAME (midgard_schema_object_tree_get_parent_name (self));

	const gchar *parent_table, *up_field;
	const gchar *parent_field;

	MgdSchemaTypeAttr *type_attr = 
		midgard_core_class_get_type_attr(MIDGARD_DBOBJECT_GET_CLASS(self));
	parent_field = type_attr->parentfield;

	if(parent_field == NULL) {
		parent_field = type_attr->upfield;
	}

	parent_table = 
		midgard_core_class_get_table(MIDGARD_DBOBJECT_CLASS(parent_class));
	up_field = MIDGARD_DBOBJECT_CLASS (parent_class)->dbpriv->storage_data->upfield;

	GString *where = g_string_new("");
	g_string_append_printf(where, "id = %d ", id);
	
	GValue *value =
		midgard_core_query_get_field_value(mgd,
				parent_field , type_attr->table, where->str);
	g_string_free(where, TRUE);
	
	guint _id = 0;

	if(G_IS_VALUE(value)) {
		if(G_VALUE_HOLDS_INT(value))
			_id = g_value_get_int(value);
		if(G_VALUE_HOLDS_UINT(value))
			_id = g_value_get_uint(value);
	}

	if(!parent_table)
		return FALSE;
	if(!up_field)
		return FALSE;

	if(_id == 0)
		return FALSE;

	gboolean rv = 
		_midgard_tree_exists(MGD_OBJECT_CNC (self), parent_table, 
				up_field, rootid, _id);

	return rv;
}

gboolean midgard_object_is_in_tree(MidgardObject *self, guint rootid, guint id)
{
	g_return_val_if_fail(self != NULL, FALSE);
	
	if(!MGD_OBJECT_CNC (self)) {
		g_warning("Object has no connection pointer!");
		return FALSE;
	}

	const gchar *table = 
		midgard_core_class_get_table(MIDGARD_DBOBJECT_GET_CLASS(self));
	const gchar *upfield = MIDGARD_DBOBJECT (self)->dbpriv->storage_data->upfield;

	if(table == NULL || upfield == NULL)
		return FALSE;

	gboolean rv =
		_midgard_tree_exists(MGD_OBJECT_CNC (self), table,
				upfield, rootid, id);
	
	return rv;
}<commit_msg>is_in_parent_tree: return true is object is directly under parent one<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_schema_object_tree.h"
#include "midgard_error.h"
#include "midgard_core_query.h"
#include "midgard_core_object.h"
#include "midgard_core_object_class.h"
#include "midgard_reflector_object.h"
#include "midgard_reflector_property.h"
#include "midgard_datatypes.h"

gboolean _midgard_tree_exists(MidgardConnection *mgd, 
		const gchar *table, const gchar *upfield, 
		gint root, gint id)
{
	g_return_val_if_fail(mgd != NULL, FALSE);
	g_return_val_if_fail(table != NULL, FALSE);
	g_return_val_if_fail(upfield != NULL, FALSE);

	if(id == 0) {
		g_warning("tree_exists: expected id > 0");
		return FALSE;
	}

	gint _id = id;
	gint _root = root;
	gboolean _exists = FALSE;

	do {

		GString *where = g_string_new("");
		g_string_append_printf(where, "id = %d ", _id);

		GValue *value = 
			midgard_core_query_get_field_value(mgd, 
					upfield, table, where->str);
		g_string_free(where, TRUE);

		if(G_IS_VALUE(value)) {
			if(G_VALUE_HOLDS_INT(value))
				_id = g_value_get_int(value);
			if(G_VALUE_HOLDS_UINT(value))
				_id = g_value_get_uint(value);
		}

		if(_root == _id)
			return TRUE;

		if(_id == 0)
			return FALSE;

	} while (_id > 0);

	return _exists;
}

gboolean midgard_object_is_in_parent_tree(MidgardObject *self, guint rootid, guint id)
{
	g_return_val_if_fail(self != NULL, FALSE);

	if (!MGD_OBJECT_CNC (self)) {
		g_warning("Object has no connection pointer!");
		return FALSE;
	}

	MidgardConnection *mgd = MGD_OBJECT_CNC (self);

	MidgardObjectClass *parent_class = 
		MIDGARD_OBJECT_GET_CLASS_BY_NAME (midgard_schema_object_tree_get_parent_name (self));

	const gchar *parent_table, *up_field;
	const gchar *parent_field;

	MgdSchemaTypeAttr *type_attr = 
		midgard_core_class_get_type_attr(MIDGARD_DBOBJECT_GET_CLASS(self));
	parent_field = type_attr->parentfield;

	if(parent_field == NULL) {
		parent_field = type_attr->upfield;
	}

	parent_table = 
		midgard_core_class_get_table(MIDGARD_DBOBJECT_CLASS(parent_class));
	up_field = MIDGARD_DBOBJECT_CLASS (parent_class)->dbpriv->storage_data->upfield;

	GString *where = g_string_new("");
	g_string_append_printf(where, "id = %d ", id);
	
	GValue *value =
		midgard_core_query_get_field_value(mgd,
				parent_field , type_attr->table, where->str);
	g_string_free(where, TRUE);
	
	guint _id = 0;

	if(G_IS_VALUE(value)) {
		if(G_VALUE_HOLDS_INT(value))
			_id = g_value_get_int(value);
		if(G_VALUE_HOLDS_UINT(value))
			_id = g_value_get_uint(value);
	}

	if(!parent_table)
		return FALSE;
	if(!up_field)
		return FALSE;

	if(_id == 0)
		return FALSE;

	if (_id == rootid)
		return TRUE;

	gboolean rv = 
		_midgard_tree_exists(MGD_OBJECT_CNC (self), parent_table, 
				up_field, rootid, _id);

	return rv;
}

gboolean midgard_object_is_in_tree(MidgardObject *self, guint rootid, guint id)
{
	g_return_val_if_fail(self != NULL, FALSE);
	
	if(!MGD_OBJECT_CNC (self)) {
		g_warning("Object has no connection pointer!");
		return FALSE;
	}

	const gchar *table = 
		midgard_core_class_get_table(MIDGARD_DBOBJECT_GET_CLASS(self));
	const gchar *upfield = MIDGARD_DBOBJECT (self)->dbpriv->storage_data->upfield;

	if(table == NULL || upfield == NULL)
		return FALSE;

	gboolean rv =
		_midgard_tree_exists(MGD_OBJECT_CNC (self), table,
				upfield, rootid, id);
	
	return rv;
}
{'fraction_non_alphanumeric': 0.10553442140139895, 'fraction_numerical': 0.0061357221744999385, 'mean_word_length': 4.944566010211524, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, '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': '2114989', 'n_tokens_mistral': 3282, 'n_tokens_neox': 2965, 'n_words': 714}