예제 #1
0
    def __str__(self):
        if self.function is None:
            return "{key} AS {alias}".format(key=quote_ident(self.key),
                                             alias=quote_ident(self.alias))

        return "{function}({key}) AS {alias}".format(function=self.function,
                                                     key=quote_ident(self.key),
                                                     alias=quote_ident(
                                                         self.alias))
예제 #2
0
    def __str__(self):
        if self.function is None:
            return "{key} AS {alias}".format(
                key=quote_ident(self.key),
                alias=quote_ident(self.alias))

        return "{function}({key}) AS {alias}".format(
            function=self.function,
            key=quote_ident(self.key),
            alias=quote_ident(self.alias))
예제 #3
0
    def build(self):
        """
        Returns the query as a string.

        :rtype: str
        """
        if not self.select_columns:
            raise ValueError(
                "The select statement should contain at least one value.")

        # Generate select statement
        select_columns = ', '.join(
            str(select_column)
            for select_column in self.select_columns)

        # Generate where statement
        where_statement = ''
        if self.where_conditions:
            where_statement = 'WHERE {}'.format(
                ' AND '.join(
                    '({})'.format(condition)
                    for condition in self.where_conditions))

        # Generate group by statement
        group_by_tags = []
        if self.group_by_time_interval:
            if self.group_by_time_offset:
                group_by_tags.append('time({}, {})'.format(
                    self.group_by_time_interval,
                    self.group_by_time_offset))
            else:
                group_by_tags.append('time({})'.format(
                    self.group_by_time_interval))

        for tag in self.group_by_tags:
            group_by_tags.append(quote_ident(tag))

        group_by_statement = ''
        if group_by_tags:
            group_by_statement = 'GROUP BY {}'.format(
                ', '.join(group_by_tags))

        return (
            "SELECT {select_columns} "
            "FROM {measurement} "
            "{where_statement} "
            "{group_by_statement}"
        ).format(
            select_columns=select_columns,
            measurement=quote_ident(self.measurement),
            where_statement=where_statement,
            group_by_statement=group_by_statement)
예제 #4
0
    def where_equal(self, tag, value):
        """
        Add a condition to the WHERE statement checking that the value of a tag
        is equal to a specified value.

        :param str tag: The name of the tag
        :param str value: The expected value of the tag
        """
        return self.where('{} = {}'.format(quote_ident(tag),
                                           quote_literal(value)))
예제 #5
0
    def where_in(self, tag, values):
        """
        Add a condition to the WHERE statement checking that the value of a tag
        is in a list of values.

        :param str tag: The name of the tag
        :param List[str] values: A list of values the tag should be in.
        """
        if not values:
            # The list of values is empty. No value should match this
            # condition.
            return self.where('false')

        # Build a regular expression checking that the tag matches one of the
        # values.
        regex = '^({})$'.format('|'.join(
            escape_regex(value) for value in values))
        condition = '{} =~ {}'.format(quote_ident(tag), quote_regex(regex))
        return self.where(condition)
예제 #6
0
    def where_in(self, tag, values):
        """
        Add a condition to the WHERE statement checking that the value of a tag
        is in a list of values.

        :param str tag: The name of the tag
        :param List[str] values: A list of values the tag should be in.
        """
        if not values:
            # The list of values is empty. No value should match this
            # condition.
            return self.where('false')

        # Build a regular expression checking that the tag matches one of the
        # values.
        regex = '^({})$'.format('|'.join(
            escape_regex(value)
            for value in values
        ))
        condition = '{} =~ {}'.format(
            quote_ident(tag),
            quote_regex(regex))
        return self.where(condition)
예제 #7
0
    def build(self):
        """
        Returns the query as a string.

        :rtype: str
        """
        if not self.select_columns:
            raise ValueError(
                "The select statement should contain at least one value.")

        # Generate select statement
        select_columns = ', '.join(
            str(select_column) for select_column in self.select_columns)

        # Generate from
        if isinstance(self.from_series, SelectQuery):
            from_series = '({})'.format(self.from_series.build())
        else:
            from_series = quote_ident(self.from_series)

        # Generate into statement
        into_statement = ''
        if self.into_measurement is not None:
            into_statement = 'INTO {}'.format(
                quote_ident(self.into_measurement))

        # Generate where statement
        where_statement = ''
        if self.where_conditions:
            where_statement = 'WHERE {}'.format(' AND '.join(
                '({})'.format(condition)
                for condition in self.where_conditions))

        # Generate group by statement
        group_by_tags = []
        if self.group_by_time_interval:
            if self.group_by_time_offset:
                group_by_tags.append('time({}, {})'.format(
                    self.group_by_time_interval, self.group_by_time_offset))
            else:
                group_by_tags.append('time({})'.format(
                    self.group_by_time_interval))

        for tag in self.group_by_tags:
            group_by_tags.append(quote_ident(tag))

        group_by_statement = ''
        if group_by_tags:
            group_by_statement = 'GROUP BY {}'.format(', '.join(group_by_tags))

        fill_statement = ''
        if not (self.group_by_time_interval is None
                or self.group_by_time_fill is None):
            fill_statement = 'fill({})'.format(self.group_by_time_fill)

        return ("SELECT {select_columns} "
                "{into_statement} "
                "FROM {from_series} "
                "{where_statement} "
                "{group_by_statement}"
                "{fill_statement}").format(
                    select_columns=select_columns,
                    into_statement=into_statement,
                    from_series=from_series,
                    where_statement=where_statement,
                    group_by_statement=group_by_statement,
                    fill_statement=fill_statement)
예제 #8
0
    def build(self):
        """
        Returns the query as a string.

        :rtype: str
        """
        if not self.select_columns:
            raise ValueError(
                "The select statement should contain at least one value.")

        # Generate select statement
        select_columns = ', '.join(
            str(select_column)
            for select_column in self.select_columns)

        # Generate from
        if isinstance(self.from_series, SelectQuery):
            from_series = '({})'.format(self.from_series.build())
        else:
            from_series = quote_ident(self.from_series)

        # Generate into statement
        into_statement = ''
        if self.into_measurement is not None:
            into_statement = 'INTO {}'.format(
                quote_ident(self.into_measurement))

        # Generate where statement
        where_statement = ''
        if self.where_conditions:
            where_statement = 'WHERE {}'.format(
                ' AND '.join(
                    '({})'.format(condition)
                    for condition in self.where_conditions))

        # Generate group by statement
        group_by_tags = []
        if self.group_by_time_interval:
            if self.group_by_time_offset:
                group_by_tags.append('time({}, {})'.format(
                    self.group_by_time_interval,
                    self.group_by_time_offset))
            else:
                group_by_tags.append('time({})'.format(
                    self.group_by_time_interval))

        for tag in self.group_by_tags:
            group_by_tags.append(quote_ident(tag))

        group_by_statement = ''
        if group_by_tags:
            group_by_statement = 'GROUP BY {}'.format(
                ', '.join(group_by_tags))

        fill_statement = ''
        if not (self.group_by_time_interval is None
                or self.group_by_time_fill is None):
            fill_statement = 'fill({})'.format(self.group_by_time_fill)

        return (
            "SELECT {select_columns} "
            "{into_statement} "
            "FROM {from_series} "
            "{where_statement} "
            "{group_by_statement}"
            "{fill_statement}"
        ).format(
            select_columns=select_columns,
            into_statement=into_statement,
            from_series=from_series,
            where_statement=where_statement,
            group_by_statement=group_by_statement,
            fill_statement=fill_statement)