'Build the SELECT query to be executed.'

def _BuildQuery(self, subject, attribute=None, timestamp=None, limit=None, is_prefix=False):

    args = []
    subject = utils.SmartUnicode(subject)
    criteria = 'WHERE    aff4.subject_hash=unhex(md5(%s))'
    args.append(subject)
    sorting = ''
    tables = 'FROM    aff4'
    if (attribute is not None):
        if is_prefix:
            tables += '    JOIN    attributes    ON    aff4.attribute_hash=attributes.hash'
            prefix = (attribute + '%')
            criteria += '    AND    attributes.attribute    like    %s'
            args.append(prefix)
        else:
            criteria += '    AND    aff4.attribute_hash=unhex(md5(%s))'
            args.append(attribute)
    if isinstance(timestamp, (tuple, list)):
        criteria += '    AND    aff4.timestamp    >=    %s    AND    aff4.timestamp    <=    %s'
        args.append(int(timestamp[0]))
        args.append(int(timestamp[1]))
    fields = 'aff4.value,    aff4.timestamp'
    if is_prefix:
        fields += ',    attributes.attribute'
    if ((timestamp is None) or (timestamp == self.NEWEST_TIMESTAMP)):
        tables += ('    JOIN    (SELECT    attribute_hash,    MAX(timestamp)    timestamp    %s    %s    GROUP    BY    attribute_hash)    maxtime    ON    aff4.attribute_hash=maxtime.attribute_hash    AND    aff4.timestamp=maxtime.timestamp' % (tables, criteria))
        criteria = 'WHERE    aff4.subject_hash=unhex(md5(%s))'
        args.append(subject)
    else:
        sorting = 'ORDER    BY    aff4.timestamp    DESC'
    if limit:
        sorting += ('    LIMIT    %s' % int(limit))
    query = '    '.join(['SELECT', fields, tables, criteria, sorting])
    return (query, args)
