def build_where_clause(self, timestamp_range=None, port=None, protocol=None, rounding=True):
        """
        Build a WHERE SQL clause that covers basic timerange, port, and protocol filtering.
        :param timestamp_range: start and end times as unix timestamps (integers). Default is all time.
        :type timestamp_range: tuple[int, int]
        :param port: exclusively report traffic destined for this port, if specified.
        :type port: int or str
        :param protocol: exclusively report traffic using this protocol
        :type protocol: str
        :param rounding: round each time stamp to the nearest quantization mark. (db records are quantized for consiceness)
        :type rounding: bool
        :return: String SQL clause
        :rtype: str
        """
        clauses = []
        t_start = 0
        t_end = 0

        if timestamp_range:
            t_start = timestamp_range[0]
            t_end = timestamp_range[1]
            if rounding:
                # rounding to 5 minutes, for use with the Syslog table
                if t_start > 150:
                    t_start -= 150
                if t_end <= 2 ** 31 - 150:
                    t_end += 149
            if self.db.dbname == 'sqlite':
                clauses.append("timestamp BETWEEN $tstart AND $tend")
            else:
                clauses.append("timestamp BETWEEN FROM_UNIXTIME($tstart) AND FROM_UNIXTIME($tend)")

        if port:
            clauses.append("port = $port")

        if protocol:
            clauses.append("protocols LIKE $protocol")
            protocol = "%{0}%".format(protocol)

        qvars = {'tstart': t_start, 'tend': t_end, 'port': port, 'protocol': protocol}
        where = str(web.db.reparam("\n    AND ".join(clauses), qvars))
        if where:
            where = "    AND " + where
        return where