def test_or(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND) where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR) self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(givenName=bar))", []))
def get_extra_restriction_citus(self, alias, related_alias): """ Return a pair condition used for joining and subquery pushdown. The condition is something that responds to as_sql(compiler, connection) method. Note that currently referring both the 'alias' and 'related_alias' will not work in some conditions, like subquery pushdown. A parallel method is get_extra_descriptor_filter() which is used in instance.fieldname related object fetching. """ if not (related_alias and alias): return None # Fetch tenant column names for both sides of the relation lhs_model = self.model rhs_model = self.related_model lhs_tenant_id = get_tenant_column(lhs_model) rhs_tenant_id = get_tenant_column(rhs_model) # Fetch tenant fields for both sides of the relation lhs_tenant_field = lhs_model._meta.get_field(lhs_tenant_id) rhs_tenant_field = rhs_model._meta.get_field(rhs_tenant_id) # Get references to both tenant columns lookup_lhs = lhs_tenant_field.get_col(related_alias) lookup_rhs = rhs_tenant_field.get_col(alias) # Create "AND lhs.tenant_id = rhs.tenant_id" as a new condition lookup = lhs_tenant_field.get_lookup("exact")(lookup_lhs, lookup_rhs) condition = WhereNode() condition.add(lookup, "AND") return condition
def test_char_field_contains(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=*test*)", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND) self.assertEquals(where_as_ldap(where), ("(cn=*te\\2ast*)", []))
def test_char_field_in(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND) self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(cn=bar))", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND) self.assertEquals(where_as_ldap(where), ("(|(cn=\\28foo\\29)(cn=\\28bar\\29))", []))
def test_list_field_startswith(self): where = WhereNode() where.add((Constraint("cn", "cn", ListField()), 'startswith', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=test*)", [])) where = WhereNode() where.add((Constraint("cn", "cn", ListField()), 'startswith', "te*st"), AND) self.assertEquals(where_as_ldap(where), ("(cn=te\\2ast*)", []))
def test_char_field_endswith(self): where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "test"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*test)") where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "te*st"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*te\\2ast)")
def test_list_field_contains(self): where = WhereNode() where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND) self.assertEquals(where_as_ldap(where), ("(memberUid=foouser)", [])) where = WhereNode() where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', '(foouser)'), AND) self.assertEquals(where_as_ldap(where), ("(memberUid=\\28foouser\\29)", []))
def test_char_field_contains(self): where = WhereNode() where.add(self._build_lookup("cn", 'contains', "test"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*test*)") where = WhereNode() where.add(self._build_lookup("cn", 'contains', "te*st"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*te\\2ast*)")
def test_or(self): where = WhereNode() where.add(self._build_lookup("cn", 'exact', "foo", field=CharField), AND) where.add( self._build_lookup("givenName", 'exact', "bar", field=CharField), OR) self.assertEqual(where_as_ldap(where), "(|(cn=foo)(givenName=bar))")
def test_date_field(self): where = WhereNode() where.add( self._build_lookup("birthday", 'exact', '2013-09-03', field=DateField), AND) self.assertEqual(self._where_as_ldap(where), "(birthday=2013-09-03)")
def test_list_field_contains(self): where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', 'foouser', field=ListField), AND) self.assertEqual(where_as_ldap(where), "(memberUid=foouser)") where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', '(foouser)', field=ListField), AND) self.assertEqual(where_as_ldap(where), "(memberUid=\\28foouser\\29)")
def test_char_field_exact(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=test)", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND) self.assertEquals(where_as_ldap(where), ("(cn=\\28test\\29)", []))
def test_char_field_in(self): where = WhereNode() where.add(self._build_lookup("cn", 'in', ["foo", "bar"]), AND) self.assertEqual(where_as_ldap(where), "(|(cn=foo)(cn=bar))") where = WhereNode() where.add(self._build_lookup("cn", 'in', ["(foo)", "(bar)"]), AND) self.assertEqual(where_as_ldap(where), "(|(cn=\\28foo\\29)(cn=\\28bar\\29))")
def test_char_field_exact(self): where = WhereNode() where.add(self._build_lookup('cn', 'exact', "test"), AND) self.assertEqual(where_as_ldap(where), "(cn=test)") where = WhereNode() where.add(self._build_lookup('cn', 'exact', "(test)"), AND) self.assertEqual(where_as_ldap(where), "(cn=\\28test\\29)")
def test_char_field_contains(self): where = WhereNode() where.add(self._build_lookup("cn", 'contains', "test"), AND) self.assertEqual(where_as_ldap(where), "(cn=*test*)") where = WhereNode() where.add(self._build_lookup("cn", 'contains', "te*st"), AND) self.assertEqual(where_as_ldap(where), "(cn=*te\\2ast*)")
def test_char_field_endswith(self): where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "test"), AND) self.assertEqual(where_as_ldap(where), "(cn=*test)") where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "te*st"), AND) self.assertEqual(where_as_ldap(where), "(cn=*te\\2ast)")
def test_list_field_contains(self): where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', 'foouser', field=fields.ListField), AND) self.assertEqual(self._where_as_ldap(where), "(memberUid=foouser)") where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', '(foouser)', field=fields.ListField), AND) self.assertEqual(self._where_as_ldap(where), "(memberUid=\\28foouser\\29)")
def test_char_field_in(self): where = WhereNode() where.add(self._build_lookup("cn", 'in', ["foo", "bar"]), AND) self.assertEqual(self._where_as_ldap(where), "(|(cn=foo)(cn=bar))") where = WhereNode() where.add(self._build_lookup("cn", 'in', ["(foo)", "(bar)"]), AND) self.assertEqual(self._where_as_ldap(where), "(|(cn=\\28foo\\29)(cn=\\28bar\\29))")
def test_char_field_exact(self): where = WhereNode() where.add(self._build_lookup('cn', 'exact', "test"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=test)") where = WhereNode() where.add(self._build_lookup('cn', 'exact', "(test)"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=\\28test\\29)")
def test_char_field_endswith(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND) self.assertEqual(where_as_ldap(where), ("(cn=*test)", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND) self.assertEqual(where_as_ldap(where), ("(cn=*te\\2ast)", []))
def test_timestamp_field(self): dt = datetime.datetime(2018, 6, 25, 20, 21, 22, tzinfo=UTC) where = WhereNode() where.add( self._build_lookup("shadowLastChange", 'exact', dt, field=fields.TimestampField), AND) self.assertEqual(self._where_as_ldap(where), "(shadowLastChange=1529958082)")
def get_extra_restriction(self, where_class, alias, related_alias): constraint = WhereNode(connector=AND) for remote, local in self._raw_fields.items(): lookup = local.get_lookup(self, self.related_model._meta.get_field(remote), alias) if lookup: constraint.add(lookup, AND) if constraint.children: return constraint else: return None
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): assert self.rhs_is_direct_value() self.rhs = get_normalized_value(self.rhs, self.lhs) from django.db.models.sql.where import WhereNode, AND root_constraint = WhereNode() for target, source, val in zip(self.lhs.targets, self.lhs.sources, self.rhs): lookup_class = target.get_lookup(self.lookup_name) root_constraint.add( lookup_class(target.get_col(self.lhs.alias, source), val), AND) return root_constraint.as_sql(compiler, connection) return super().as_sql(compiler, connection)
def test_boolean_field(self): where = WhereNode() where.add( self._build_lookup("isSuperuser", 'exact', True, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=TRUE)") where = WhereNode() where.add( self._build_lookup("isSuperuser", 'exact', False, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=FALSE)") where = WhereNode() where.add( self._build_lookup("isSuperuser", 'exact', 1, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=TRUE)") where = WhereNode() where.add( self._build_lookup("isSuperuser", 'exact', 0, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=FALSE)")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or a OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import WhereNode, SubqueryConstraint, AND, OR root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [ get_normalized_value(value, self.lhs) for value in self.rhs ] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class( target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: return super(RelatedIn, self).as_sql(compiler, connection)
def test_datetime_field(self): dt = datetime.datetime(2018, 6, 25, 20, 21, 22, tzinfo=UTC) where = WhereNode() where.add( self._build_lookup( "modifyTimestamp", 'exact', dt, field=fields.DateTimeField, ), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp=20180625202122.000000Z)") where = WhereNode() where.add( self._build_lookup( "modifyTimestamp", 'lte', dt, field=fields.DateTimeField, ), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp<=20180625202122.000000Z)") where = WhereNode() where.add( self._build_lookup( "modifyTimestamp", 'gte', dt, field=fields.DateTimeField, ), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp>=20180625202122.000000Z)")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or a OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import WhereNode, SubqueryConstraint, AND, OR root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [get_normalized_value(value, self.lhs) for value in self.rhs] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class(target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: return super(RelatedIn, self).as_sql(compiler, connection)
def test_integer_field(self): where = WhereNode() where.add(self._build_lookup("uid", 'exact', 1, field=fields.IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(uid=1)") where = WhereNode() where.add(self._build_lookup("uid", 'gte', 1, field=fields.IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(uid>=1)") where = WhereNode() where.add(self._build_lookup("uid", 'lte', 1, field=fields.IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(uid<=1)") where = WhereNode() where.add(self._build_lookup("uid", 'in', [1, 2], field=fields.IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(|(uid=1)(uid=2))")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or an OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import ( WhereNode, SubqueryConstraint, AND, OR, ) root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [get_normalized_value(value, self.lhs) for value in self.rhs] for value in values: value_constraint = WhereNode() for source, target, val in zip( self.lhs.sources, self.lhs.targets, value ): lookup_class = target.get_lookup("exact") lookup = lookup_class( target.get_col(self.lhs.alias, source), val ) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs, ), AND, ) return root_constraint.as_sql(compiler, connection) else: if not getattr(self.rhs, "has_select_fields", True) and not getattr( self.lhs.field.target_field, "primary_key", False ): self.rhs.clear_select_clause() if ( getattr(self.lhs.output_field, "primary_key", False) and self.lhs.output_field.model == self.rhs.model ): # A case like Restaurant.objects.filter(place__in=restaurant_qs), # where place is a OneToOneField and the primary key of # Restaurant. target_field = self.lhs.field.name else: target_field = self.lhs.field.target_field.name self.rhs.add_fields([target_field], True) return super().as_sql(compiler, connection)
def test_float_field(self): where = WhereNode() where.add(self._build_lookup("uid", 'exact', 1.2, field=FloatField), AND) self.assertEqual(where_as_ldap(where), "(uid=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'gte', 1.2, field=FloatField), AND) self.assertEqual(where_as_ldap(where), "(uid>=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'lte', 1.2, field=FloatField), AND) self.assertEqual(where_as_ldap(where), "(uid<=1.2)")
def test_float_field(self): where = WhereNode() where.add(self._build_lookup("uid", 'exact', 1.2, field=fields.FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'gte', 1.2, field=fields.FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid>=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'lte', 1.2, field=fields.FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid<=1.2)")
def test_integer_field(self): where = WhereNode() where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND) self.assertEquals(where_as_ldap(where), ("(uid=1)", [])) where = WhereNode() where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND) self.assertEquals(where_as_ldap(where), ("(uid>=1)", [])) where = WhereNode() where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND) self.assertEquals(where_as_ldap(where), ("(uid<=1)", []))
def test_float_field(self): where = WhereNode() where.add((Constraint("uid", "uid", FloatField()), 'exact', 1.2), AND) self.assertEquals(where_as_ldap(where), ("(uid=1.2)", [])) where = WhereNode() where.add((Constraint("uid", "uid", FloatField()), 'gte', 1.2), AND) self.assertEquals(where_as_ldap(where), ("(uid>=1.2)", [])) where = WhereNode() where.add((Constraint("uid", "uid", FloatField()), 'lte', 1.2), AND) self.assertEquals(where_as_ldap(where), ("(uid<=1.2)", []))
def test_datetime_field(self): dt = datetime.datetime(2018, 6, 25, 20, 21, 22, tzinfo=UTC) where = WhereNode() where.add(self._build_lookup("modifyTimestamp", 'exact', dt, field=fields.DateTimeField,), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp=20180625202122.000000Z)") where = WhereNode() where.add(self._build_lookup("modifyTimestamp", 'lte', dt, field=fields.DateTimeField,), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp<=20180625202122.000000Z)") where = WhereNode() where.add(self._build_lookup("modifyTimestamp", 'gte', dt, field=fields.DateTimeField,), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp>=20180625202122.000000Z)")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or an OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import WhereNode, SubqueryConstraint, AND, OR root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [get_normalized_value(value, self.lhs) for value in self.rhs] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class(target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: if (not getattr(self.rhs, 'has_select_fields', True) and not getattr(self.lhs.field.target_field, 'primary_key', False)): self.rhs.clear_select_clause() if (getattr(self.lhs.output_field, 'primary_key', False) and self.lhs.output_field.model == self.rhs.model): # A case like Restaurant.objects.filter(place__in=restaurant_qs), # where place is a OneToOneField and the primary key of # Restaurant. target_field = self.lhs.field.name else: target_field = self.lhs.field.target_field.name self.rhs.add_fields([target_field], True) return super().as_sql(compiler, connection)
<<<<<<< HEAD # SQL) or a OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. ======= # SQL) or an OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 from django.db.models.sql.where import WhereNode, SubqueryConstraint, AND, OR root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [get_normalized_value(value, self.lhs) for value in self.rhs] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class(target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: <<<<<<< HEAD if (getattr(self.rhs, '_forced_pk', False) and ======= if (not getattr(self.rhs, 'has_select_fields', True) and >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 not getattr(self.lhs.field.target_field, 'primary_key', False)):
def test_timestamp_field(self): dt = datetime.datetime(2018, 6, 25, 20, 21, 22, tzinfo=UTC) where = WhereNode() where.add(self._build_lookup("shadowLastChange", 'exact', dt, field=fields.TimestampField), AND) self.assertEqual(self._where_as_ldap(where), "(shadowLastChange=1529958082)")
def test_date_field(self): where = WhereNode() where.add(self._build_lookup("birthday", 'exact', '2013-09-03', field=DateField), AND) self.assertEqual(where_as_ldap(where), "(birthday=2013-09-03)")
def test_or(self): where = WhereNode() where.add(self._build_lookup("cn", 'exact', "foo", field=CharField), AND) where.add(self._build_lookup("givenName", 'exact', "bar", field=CharField), OR) self.assertEqual(where_as_ldap(where), "(|(cn=foo)(givenName=bar))")
from django.db.models.lookups import (
def test_date_field(self): where = WhereNode() where.add((Constraint("birthday", "birthday", DateField()), 'exact', '2013-09-03'), AND) self.assertEquals(where_as_ldap(where), ("(birthday=2013-09-03)", []))
def get_extra_restriction(self, alias, remote_alias): cond = WhereNode() cond.add(self.get_content_type_lookup(alias, remote_alias), 'AND') cond.add(self.get_object_id_lookup(alias, remote_alias), 'AND') return cond
def test_list_field_exact(self): where = WhereNode() where.add((Constraint("cn", "cn", ListField()), 'exact', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=test)", []))