Esempio n. 1
0
    def parse_query(self, config_entity, query=None):
        """
            Parses the stored QuerySet components of the DbEntity to create an actual QuerySet
            :param query: pass in a query instead of using self.query. Used by run_grouped_query to add the group_by
        :return:
        """
        query = query or self.query
        if not query:
            logger.error("Trying to run query for db_entity %s, which has no query defined or passed in" % self.full_name)
            return {}

        # Use the base table of the Feature class for now. We'll need to use the rel table soon.
        manager = config_entity.db_entity_feature_class(self.key, base_feature_class=True).objects
        if isinstance(query, basestring):
            # String query
            # Join the query with the base tables of the feature classes that match the db_entity_keys listed
            # as named wildcards in the query string They are in the form %(db_entity_key)
            db_entity_keys = re.findall(r'%\((.+?)\)', query)
            formatted_query = query % map_to_dict(
                lambda db_entity_key: [db_entity_key,
                                       config_entity.db_entity_feature_class(db_entity_key, base_feature_class=True)._meta.db_table],
                db_entity_keys)
            cursor = connection.cursor()
            cursor.execute(formatted_query)

            result_value = dictfetchall(cursor)
            #always return results as a list so that multiple rows can be returned
            return result_value
        else:
            # Combine the query parts
            return [accumulate(lambda manager, queryset_command: self.parse_and_exec_queryset_command(manager, queryset_command), manager, query)]
Esempio n. 2
0
    def _computed_tags(self, seen=[]):
        """
            Tags are used for categorizing, especially visually tags + the tags of the parents (recursive).
            tags always at least contain the Behavior's key as a Tag,
            so the user doesn't have to explicitly list tags that match the key.
            It might be that we never need tags that don't match a key
        """

        # Combine querysets using the | operator
        return accumulate(
            lambda previous, next: previous | next,
            self.tags.all().distinct(),
            map(lambda parent: parent._computed_tags(seen+list(self.parents.all())), filter(lambda parent: parent not in seen, self.parents.all().distinct()))).distinct()
Esempio n. 3
0
    def parse_query(self, config_entity, query=None):
        """
            Parses the stored QuerySet components of the DbEntity to create an actual QuerySet
            :param query: pass in a query instead of using self.query. Used by run_grouped_query to add the group_by
        :return:
        """
        query = query or self.query
        if not query:
            logger.error(
                "Trying to run query for db_entity %s, which has no query defined or passed in"
                % self.full_name)
            return {}

        # Use the base table of the Feature class for now. We'll need to use the rel table soon.
        manager = config_entity.db_entity_feature_class(
            self.key, base_feature_class=True).objects
        if isinstance(query, basestring):
            # String query
            # Join the query with the base tables of the feature classes that match the db_entity_keys listed
            # as named wildcards in the query string They are in the form %(db_entity_key)
            db_entity_keys = re.findall(r'%\((.+?)\)', query)
            formatted_query = query % map_to_dict(
                lambda db_entity_key: [
                    db_entity_key,
                    config_entity.db_entity_feature_class(
                        db_entity_key, base_feature_class=True)._meta.db_table
                ], db_entity_keys)
            cursor = connection.cursor()
            cursor.execute(formatted_query)

            result_value = dictfetchall(cursor)
            #always return results as a list so that multiple rows can be returned
            return result_value
        else:
            # Combine the query parts
            return [
                accumulate(
                    lambda manager, queryset_command: self.
                    parse_and_exec_queryset_command(manager, queryset_command),
                    manager, query)
            ]