Exemple #1
0
def _as_sql_rpad(self, compiler, connection):
    i = iter(self.get_source_expressions())
    expression, expression_arg = compiler.compile(next(i))
    length, length_arg = compiler.compile(next(i))
    fill_text, fill_text_arg = compiler.compile(next(i))
    params = []
    params.extend(expression_arg)
    params.extend(fill_text_arg)
    params.extend(length_arg)
    params.extend(length_arg)
    template = 'LEFT(%(expression)s + REPLICATE(%(fill_text)s, %(length)s), %(length)s)'
    return template % {'expression': expression, 'length': length, 'fill_text': fill_text}, params
def where_node_as_ldap(where, compiler, connection):
    """Parse a django.db.models.sql.where.WhereNode.

    Returns:
        (clause, [params]): the filter clause, with a list of unescaped parameters.
    """
    bits, params = [], []
    for item in where.children:
        if isinstance(item, WhereNode):
            clause, clause_params = compiler.compile(item)
        else:
            clause, clause_params = item.as_sql(compiler, connection)

        bits.append(clause)
        params.extend(clause_params)

    if not bits:
        return '', []

    # FIXME(rbarrois): shouldn't we flatten recursive AND / OR?
    if len(bits) == 1:
        clause = bits[0]
    elif where.connector == AND:
        clause = '&' + ''.join('(%s)' % bit for bit in bits)
    elif where.connector == OR:
        clause = '|' + ''.join('(%s)' % bit for bit in bits)
    else:
        raise LdapDBError("Unhandled WHERE connector: %s" % where.connector)

    if where.negated:
        clause = ('!(%s)' % clause)

    return clause, params
Exemple #3
0
def _as_sql_lpad(self, compiler, connection):
    i = iter(self.get_source_expressions())
    expression, expression_arg = compiler.compile(next(i))
    length, length_arg = compiler.compile(next(i))
    fill_text, fill_text_arg = compiler.compile(next(i))
    params = []
    params.extend(fill_text_arg)
    params.extend(length_arg)
    params.extend(length_arg)
    params.extend(expression_arg)
    params.extend(length_arg)
    params.extend(expression_arg)
    params.extend(expression_arg)
    template = ('LEFT(REPLICATE(%(fill_text)s, %(length)s), CASE WHEN %(length)s > LEN(%(expression)s) '
                'THEN %(length)s - LEN(%(expression)s) ELSE 0 END) + %(expression)s')
    return template % {'expression': expression, 'length': length, 'fill_text': fill_text}, params
Exemple #4
0
 def as_sql(self,
            compiler,
            connection,
            function=None,
            template=None,
            arg_joiner=None,
            **extra_context):
     connection.ops.check_expression_support(self)
     sql_parts = []
     params = []
     for arg in self.source_expressions:
         arg_sql, arg_params = compiler.compile(arg)
         sql_parts.append(arg_sql)
         params.extend(arg_params)
     data = {**self.extra, **extra_context}
     # Use the first supplied value in this order: the parameter to this
     # method, a value supplied in __init__()'s **extra (the value in
     # `data`), or the value defined on the class.
     if function is not None:
         data['function'] = function
     else:
         data.setdefault('function', self.function)
     template = template or data.get('template', self.template)
     arg_joiner = arg_joiner or data.get('arg_joiner', self.arg_joiner)
     arg_sql = ""
     for i in range(0, len(sql_parts), 2):
         if i > 0:
             arg_sql += ','
         arg_sql += " KEY '%s' VALUE %s " % (sql_parts[i], sql_parts[i + 1])
     data['expressions'] = data['field'] = arg_sql
     sql = template % data
     return sql % tuple(params), []
Exemple #5
0
def where_node_as_ldap(where, compiler, connection):
    """Parse a django.db.models.sql.where.WhereNode.

    Returns:
        (clause, [params]): the filter clause, with a list of unescaped parameters.
    """
    bits, params = [], []
    for item in where.children:
        if isinstance(item, WhereNode):
            clause, clause_params = compiler.compile(item)
        else:
            clause, clause_params = item.as_sql(compiler, connection)

        bits.append(clause)
        params.extend(clause_params)

    if not bits:
        return '', []

    # FIXME(rbarrois): shouldn't we flatten recursive AND / OR?
    if len(bits) == 1:
        clause = bits[0]
    elif where.connector == AND:
        clause = '&' + ''.join('(%s)' % bit for bit in bits)
    elif where.connector == OR:
        clause = '|' + ''.join('(%s)' % bit for bit in bits)
    else:
        raise LdapDBError("Unhandled WHERE connector: %s" % where.connector)

    if where.negated:
        clause = ('!(%s)' % clause)

    return clause, params
def query_as_ldap(query, compiler, connection):
    """Convert a django.db.models.sql.query.Query to a LdapLookup."""
    # starting with django 1.6 we can receive empty querysets
    if hasattr(query, 'is_empty') and query.is_empty():
        return

    # FIXME(rbarrois): this could be an extra Where clause
    filterstr = ''.join(['(objectClass=%s)' % cls for cls in
                         query.model.object_classes])

    # FIXME(rbarrois): Remove this code as part of #101
    if (len(query.where.children) == 1
            and not isinstance(query.where.children[0], WhereNode)
            and query.where.children[0].lhs.target.column == 'dn'):

        lookup = query.where.children[0]
        if lookup.lookup_name != 'exact':
            raise LdapDBError("Unsupported dn lookup: %s" % lookup.lookup_name)

        return LdapLookup(
            base=lookup.rhs,
            scope=ldap.SCOPE_BASE,
            filterstr='(&%s)' % filterstr,
        )

    sql, params = compiler.compile(query.where)
    if sql:
        filterstr += '(%s)' % (sql % tuple(escape_ldap_filter(param) for param in params))
    return LdapLookup(
        base=query.model.base_dn,
        scope=query.model.search_scope,
        filterstr='(&%s)' % filterstr,
    )
Exemple #7
0
def query_as_ldap(query, compiler, connection):
    """Convert a django.db.models.sql.query.Query to a LdapLookup."""
    if query.is_empty():
        return

    if query.model._meta.model_name == 'migration' and not hasattr(query.model, 'object_classes'):
        # FIXME(rbarrois): Support migrations
        return

    # FIXME(rbarrois): this could be an extra Where clause
    filterstr = ''.join(['(objectClass=%s)' % cls for cls in
                         query.model.object_classes])

    # FIXME(rbarrois): Remove this code as part of #101
    if (len(query.where.children) == 1
            and not isinstance(query.where.children[0], WhereNode)
            and query.where.children[0].lhs.target.column == 'dn'):

        lookup = query.where.children[0]
        if not isinstance(lookup, ExactLookup):
            # TODO: follow #101
            # raise LdapDBError("Unsupported dn lookup: %s" % lookup.lookup_name)
            # PATCH that fixes Admin Action: delete entries
            kwargs = copy.copy(lookup.__dict__)
            kwargs.pop('bilateral_transforms')
            kwargs.pop('contains_aggregate')
            kwargs['rhs'] = kwargs['rhs'][0]
            lookup = ExactLookup(**kwargs)

        return LdapLookup(
            base=lookup.rhs,
            scope=ldap.SCOPE_BASE,
            filterstr='(&%s)' % filterstr,
        )

    sql, params = compiler.compile(query.where)
    if sql:
        filterstr += '(%s)' % (sql % tuple(escape_ldap_filter(param) for param in params))
    return LdapLookup(
        base=query.model.base_dn,
        scope=query.model.search_scope,
        filterstr='(&%s)' % filterstr,
    )
Exemple #8
0
def query_as_ldap(query, compiler, connection):
    """Convert a django.db.models.sql.query.Query to a LdapLookup."""
    if query.is_empty():
        return

    if query.model._meta.model_name == 'migration' and not hasattr(query.model, 'object_classes'):
        # FIXME(rbarrois): Support migrations
        return

    # FIXME(rbarrois): this could be an extra Where clause
    filterstr = ''.join(['(objectClass=%s)' % cls for cls in
                         query.model.object_classes])

    # FIXME(rbarrois): Remove this code as part of #101
    if (len(query.where.children) == 1
            and not isinstance(query.where.children[0], WhereNode)
            and query.where.children[0].lhs.target.column == 'dn'):

        lookup = query.where.children[0]
        if lookup.lookup_name != 'exact':
            raise LdapDBError("Unsupported dn lookup: %s" % lookup.lookup_name)

        return LdapLookup(
            base=lookup.rhs,
            scope=ldap.SCOPE_BASE,
            filterstr='(&%s)' % filterstr,
        )

    sql, params = compiler.compile(query.where)
    if sql:
        filterstr += '(%s)' % (sql % tuple(escape_ldap_filter(param) for param in params))
    return LdapLookup(
        base=query.model.base_dn,
        scope=query.model.search_scope,
        filterstr='(&%s)' % filterstr,
    )