Пример #1
0
gchar *midgard_query_builder_get_object_select(
        MidgardQueryBuilder *builder, guint select_type){

        g_assert(builder != NULL);
        g_assert(builder->priv->type);
	
	MidgardObjectClass *klass = (MidgardObjectClass*) g_type_class_peek(builder->priv->type);
	const gchar *table = midgard_core_class_get_table(MIDGARD_DBOBJECT_CLASS(klass));

        if (!table){
                g_warning("Object '%s' has no table or storage defined!", 
                                g_type_name(builder->priv->type));
                return NULL;
        }
		
        GString *select = g_string_new("");

	/* We are getting only guids */
	if(select_type == MQB_SELECT_GUID){
		
		g_string_append(select, "COUNT(*) ");
		return g_string_free(select, FALSE);
	}
	
        /* guid hardcoded ( inherited from MidgardObjectClass )
         * metadata properties hardcoded ( defined in midgard_metadata )
         */ 
	/* FIXME, move this to particular class implementation, so we can reuse MidgardDBObject derived class data only */
	if (MIDGARD_IS_OBJECT_CLASS (klass))
			 g_string_append_printf(select, "%s.guid, ", table);

	if (MIDGARD_DBOBJECT_CLASS(klass)->dbpriv->has_metadata) {

		g_string_append_printf(select, 	
				"%s.metadata_creator, "
				"%s.metadata_created, "
				"%s.metadata_revisor, "
				"%s.metadata_revised, "
				"%s.metadata_revision, "
				"%s.metadata_locker, "
				"%s.metadata_locked, "
				"%s.metadata_approver, "
				"%s.metadata_approved, "
				"%s.metadata_authors, "
				"%s.metadata_owner, "
				"%s.metadata_schedule_start, "
				"%s.metadata_schedule_end, "
				"%s.metadata_hidden, "
				"%s.metadata_nav_noentry, "
				"%s.metadata_size, "
				"%s.metadata_published, "
				"%s.metadata_exported, "
				"%s.metadata_imported, "
				"%s.metadata_deleted, "
				"%s.metadata_score, "
				"%s.metadata_islocked, "
				"%s.metadata_isapproved "
				", ", 
			table, table, table, table, table, table, table, table, table,
			table, table, table, table, table, table, table, table, table,
			table, table, table, table, table); 
		/* TODO, Set select which suits better */	
	} 

	if(builder->priv->schema->sql_select_full != NULL) {
		g_string_append_printf(select, " %s", 
				builder->priv->schema->sql_select_full);
	}

        return g_string_free(select, FALSE);        
}
Пример #2
0
/* Create GdaSqlSelectField for every property registered for the class. */
void
_add_fields_to_select_statement (MidgardDBObjectClass *klass, MidgardConnection *mgd, GdaSqlStatementSelect *select, const gchar *table_name)
{
	guint n_prop;
	guint i;
	GdaSqlSelectField *select_field;
	GdaSqlExpr *expr;
	GValue *val;
	gchar *table_field;
	GdaConnection *cnc = mgd->priv->connection;

	GParamSpec **pspecs = g_object_class_list_properties (G_OBJECT_CLASS (klass), &n_prop);
	if (!pspecs)
		return;

	const gchar *property_table = NULL;

	for (i = 0; i < n_prop; i++) {

		const gchar *property = pspecs[i]->name;
		const gchar *property_field = midgard_core_class_get_property_colname (klass, property);
		property_table = midgard_core_class_get_property_table (klass, property); 
		if (property_table && table_name)
			property_table = table_name;

		/* Ignore properties with NULL storage and those of object type */
		if (!property_table || pspecs[i]->value_type == G_TYPE_OBJECT)
			continue;

       		select_field = gda_sql_select_field_new (GDA_SQL_ANY_PART (select));
		/*select_field->field_name = g_strdup (property_field);
		select_field->table_name = g_strdup (property_table);*/
		select_field->as = gda_connection_quote_sql_identifier (cnc, property);
		select->expr_list = g_slist_append (select->expr_list, select_field);
		expr = gda_sql_expr_new (GDA_SQL_ANY_PART (select_field));
		val = g_new0 (GValue, 1);
		g_value_init (val, G_TYPE_STRING);
		gchar *q_table = gda_connection_quote_sql_identifier (cnc, property_table);
		gchar *q_field = gda_connection_quote_sql_identifier (cnc, property_field);
		table_field = g_strconcat (q_table, ".", q_field, NULL);
		g_value_set_string (val, table_field);
		g_free (q_table);
		g_free (q_field);
		g_free (table_field);
		expr->value = val;
		select_field->expr = expr;
	}	

	g_free (pspecs);

	if (!klass->dbpriv->has_metadata)
		return;

	/* Check if metadata provides own method to add fields. If not, use given class storage. */
	if (MIDGARD_IS_OBJECT_CLASS (klass)) {

		MidgardMetadataClass *mklass = MGD_DBCLASS_METADATA_CLASS (klass);
		if (!mklass)
			return;

		if (MIDGARD_DBOBJECT_CLASS (mklass)->dbpriv->add_fields_to_select_statement) {
			MIDGARD_DBOBJECT_CLASS (mklass)->dbpriv->add_fields_to_select_statement (MIDGARD_DBOBJECT_CLASS (mklass), mgd, select, table_name);
			return;
		}

		const gchar *table = midgard_core_class_get_table (klass);
		if (table_name)
			table = table_name;

		/* TODO, Once we stabilize use case, refactor this below to minimize code usage */
		GParamSpec **pspecs = g_object_class_list_properties (G_OBJECT_CLASS (mklass), &n_prop);
		if (!pspecs)
			return;
		
		for (i = 0; i < n_prop; i++) {

			const gchar *property = pspecs[i]->name;
			const gchar *property_field = midgard_core_class_get_property_colname (MIDGARD_DBOBJECT_CLASS (mklass), property);	

			if (pspecs[i]->value_type == G_TYPE_OBJECT || !property_field)
				continue;
			
			select_field = gda_sql_select_field_new (GDA_SQL_ANY_PART (select));
			/*select_field->field_name = g_strdup (property_field);
			select_field->table_name = g_strdup (table);*/
			select_field->as = g_strconcat ("metadata_", property, NULL);
			select->expr_list = g_slist_append (select->expr_list, select_field);
			expr = gda_sql_expr_new (GDA_SQL_ANY_PART (select_field));
			val = g_new0 (GValue, 1);
			g_value_init (val, G_TYPE_STRING);
			table_field = g_strconcat (table, ".", property_field, NULL);
			g_value_set_string (val, table_field);
			g_free (table_field);
			expr->value = val;
			select_field->expr = expr;
		}

		g_free (pspecs);
	}

	return;
}