# Path: ref/models/instances.py
# class ComponentInstance(models.Model):
#     """Instances! Usually used through its proxy object"""
# 
#     ## Base data for all components
#     instanciates = models.ForeignKey('ComponentImplementationClass', null=True, blank=True, verbose_name=u'implémentation de ', related_name='instances', on_delete=models.CASCADE)
#     description = models.ForeignKey('ImplementationDescription', related_name='instance_set', verbose_name=u'décrit par l\'implémentation', on_delete=models.CASCADE)
#     deleted = models.BooleanField(default=False)
#     include_in_envt_backup = models.BooleanField(default=False)
# 
#     ## Environments
#     environments = models.ManyToManyField(Environment, blank=True, verbose_name='environnements ', related_name='component_instances')
# 
#     ## Connections
#     #TODO: symmetrical
#     relationships = models.ManyToManyField('self', verbose_name='relations', through=ComponentInstanceRelation, symmetrical=False, related_name='reverse_relationships')
# 
#     ## Proxy object for easier handling
#     __proxy = None
#     def build_proxy(self, force=False):
#         if self.description_id is None:
#             return
#         if self.__proxy is None or force:
#             self.__proxy = self.description.proxy_class()(base_instance=self)
#         return self.__proxy
#     proxy = property(build_proxy)
# 
#     ## First environment (helper)
#     def first_environment(self):
#         if self.environments.count() > 0:
#             return self.environments.all()[0]
#         return None
#     first_environment.short_description = u'notamment dans'
#     first_environment.admin_order_field = 'environments__name'
# 
#     def _environments_str(self):
#         if not self.environments or self.environments.count() == 0:
#             return ""
#         return ','.join([e.name for e in self.environments.all()])
#     environments_str = property(_environments_str)
# 
#     ## Pretty print
#     def __str__(self):
#         if self.description:
#             return '%s' % self.description.resolve_self_description(self)
#         else:
#             return '%s' % self.pk
#     name = property(__str__)
# 
#     ## Pretty admin deleted field
#     def active(self):
#         return not self.deleted
#     active.admin_order_field = 'deleted'
#     active.boolean = True
# 
#     class Meta:
#         permissions = (('allfields_componentinstance', 'access all fields including restricted ones'),)
#         verbose_name = 'instance de composant'
#         verbose_name_plural = 'instances de composant'
# 
# class ComponentInstanceField(models.Model):
#     objects = RichManager()
# 
#     value = models.CharField(max_length=255, verbose_name='valeur', db_index=True)
#     field = models.ForeignKey('ImplementationFieldDescription', verbose_name=u'champ implémenté', on_delete=models.CASCADE)
#     instance = models.ForeignKey('ComponentInstance', verbose_name=u'instance de composant', related_name='field_set', on_delete=models.CASCADE)
# 
#     class Meta:
#         verbose_name = u'valeur de champ'
#         verbose_name_plural = u'valeurs des champs'
# 
#     def __str__(self):
#         return 'valeur de %s' % self.field.name
# 
# class ComponentInstanceRelation(models.Model):
#     source = models.ForeignKey('ComponentInstance', related_name='rel_target_set', verbose_name='instance source', on_delete=models.CASCADE)
#     target = models.ForeignKey('ComponentInstance', related_name='rel_targeted_by_set', verbose_name='instance cible', on_delete=models.CASCADE)
#     field = models.ForeignKey('ImplementationRelationDescription', verbose_name=u'champ implémenté', related_name='field_set', on_delete=models.CASCADE)
# 
#     class Meta:
#         verbose_name = u'valeur de relation'
#         verbose_name_plural = u'valeurs des relations'
# 
#     def __str__(self):
#         return 'valeur de %s' % self.field.name


from pyparsing import Forward, Group, Suppress, Optional, ZeroOrMore, Combine, \
    Word, alphas, nums, QuotedString, CaselessLiteral, FollowedBy
from ref.models.instances import ComponentInstance, ComponentInstanceField, ComponentInstanceRelation
from django.db.models.query import Prefetch
            compo['mage_cic_id'] = ci.instanciates_id
            compo['mage_deleted'] = ci.deleted
            compo['mage_description_id'] = ci.description_id
            compo['mage_description_name'] = ci.description.name
            compo['mage_environments'] = ','.join([e.name for e in ci.environments.all()])

            for fi in ci.field_set.all():
                if not return_sensitive_data and fi.field.sensitive:
                    continue
                compo[fi.field.name] = fi.value

            for fi in ci.rel_target_set.all():
                if not return_sensitive_data and fi.field.sensitive:
                    continue
                key = fi.field.name + '_id'
                if key in compo:
                    compo[key] = '%s,%s' % (compo[key], fi.target_id)
                else:
                    compo[key] = fi.target_id

            if use_computed_fields:
                for cf in ci.description.computed_field_set.all():
                    compo[cf.name] = cf.resolve(ci)

        return res

    else:
        if optim:
            ## Preload two levels of data
            rs = rs.prefetch_related(

# Path: ref/models/instances.py
# class ComponentInstance(models.Model):
#     """Instances! Usually used through its proxy object"""
# 
#     ## Base data for all components
#     instanciates = models.ForeignKey('ComponentImplementationClass', null=True, blank=True, verbose_name=u'implémentation de ', related_name='instances', on_delete=models.CASCADE)
#     description = models.ForeignKey('ImplementationDescription', related_name='instance_set', verbose_name=u'décrit par l\'implémentation', on_delete=models.CASCADE)
#     deleted = models.BooleanField(default=False)
#     include_in_envt_backup = models.BooleanField(default=False)
# 
#     ## Environments
#     environments = models.ManyToManyField(Environment, blank=True, verbose_name='environnements ', related_name='component_instances')
# 
#     ## Connections
#     #TODO: symmetrical
#     relationships = models.ManyToManyField('self', verbose_name='relations', through=ComponentInstanceRelation, symmetrical=False, related_name='reverse_relationships')
# 
#     ## Proxy object for easier handling
#     __proxy = None
#     def build_proxy(self, force=False):
#         if self.description_id is None:
#             return
#         if self.__proxy is None or force:
#             self.__proxy = self.description.proxy_class()(base_instance=self)
#         return self.__proxy
#     proxy = property(build_proxy)
# 
#     ## First environment (helper)
#     def first_environment(self):
#         if self.environments.count() > 0:
#             return self.environments.all()[0]
#         return None
#     first_environment.short_description = u'notamment dans'
#     first_environment.admin_order_field = 'environments__name'
# 
#     def _environments_str(self):
#         if not self.environments or self.environments.count() == 0:
#             return ""
#         return ','.join([e.name for e in self.environments.all()])
#     environments_str = property(_environments_str)
# 
#     ## Pretty print
#     def __str__(self):
#         if self.description:
#             return '%s' % self.description.resolve_self_description(self)
#         else:
#             return '%s' % self.pk
#     name = property(__str__)
# 
#     ## Pretty admin deleted field
#     def active(self):
#         return not self.deleted
#     active.admin_order_field = 'deleted'
#     active.boolean = True
# 
#     class Meta:
#         permissions = (('allfields_componentinstance', 'access all fields including restricted ones'),)
#         verbose_name = 'instance de composant'
#         verbose_name_plural = 'instances de composant'
# 
# class ComponentInstanceField(models.Model):
#     objects = RichManager()
# 
#     value = models.CharField(max_length=255, verbose_name='valeur', db_index=True)
#     field = models.ForeignKey('ImplementationFieldDescription', verbose_name=u'champ implémenté', on_delete=models.CASCADE)
#     instance = models.ForeignKey('ComponentInstance', verbose_name=u'instance de composant', related_name='field_set', on_delete=models.CASCADE)
# 
#     class Meta:
#         verbose_name = u'valeur de champ'
#         verbose_name_plural = u'valeurs des champs'
# 
#     def __str__(self):
#         return 'valeur de %s' % self.field.name
# 
# class ComponentInstanceRelation(models.Model):
#     source = models.ForeignKey('ComponentInstance', related_name='rel_target_set', verbose_name='instance source', on_delete=models.CASCADE)
#     target = models.ForeignKey('ComponentInstance', related_name='rel_targeted_by_set', verbose_name='instance cible', on_delete=models.CASCADE)
#     field = models.ForeignKey('ImplementationRelationDescription', verbose_name=u'champ implémenté', related_name='field_set', on_delete=models.CASCADE)
# 
#     class Meta:
#         verbose_name = u'valeur de relation'
#         verbose_name_plural = u'valeurs des relations'
# 
#     def __str__(self):
#         return 'valeur de %s' % self.field.name


# Path: ref/mql.py
from pyparsing import Forward, Group, Suppress, Optional, ZeroOrMore, Combine, \
    Word, alphas, nums, QuotedString, CaselessLiteral, FollowedBy
from ref.models.instances import ComponentInstance, ComponentInstanceField, ComponentInstanceRelation
from django.db.models.query import Prefetch

            compo['mage_cic_id'] = ci.instanciates_id
            compo['mage_deleted'] = ci.deleted
            compo['mage_description_id'] = ci.description_id
            compo['mage_description_name'] = ci.description.name
            compo['mage_environments'] = ','.join([e.name for e in ci.environments.all()])

            for fi in ci.field_set.all():
                if not return_sensitive_data and fi.field.sensitive:
                    continue
                compo[fi.field.name] = fi.value

            for fi in ci.rel_target_set.all():
                if not return_sensitive_data and fi.field.sensitive:
                    continue
                key = fi.field.name + '_id'
                if key in compo:
                    compo[key] = '%s,%s' % (compo[key], fi.target_id)
                else:
                    compo[key] = fi.target_id

            if use_computed_fields:
                for cf in ci.description.computed_field_set.all():
                    compo[cf.name] = cf.resolve(ci)

        return res

    else:
        if optim:
            ## Preload two levels of data
            rs = rs.prefetch_related(

                Prefetch('field_set', queryset=ComponentInstanceField.objects.select_related('field')))