示例#1
0
 def test_repr(self):
     query = Query(Item)
     compiler = query.get_compiler(DEFAULT_DB_ALIAS, connection)
     self.assertEqual(
         repr(compiler), f"<SQLCompiler model=Item connection="
         f"<DatabaseWrapper vendor={connection.vendor!r} alias='default'> "
         f"using='default'>")
示例#2
0
    def check(self, against, using=DEFAULT_DB_ALIAS):
        """
        Do a database query to check if the expressions of the Q instance
        matches against the expressions.
        """
        # Avoid circular imports.
        from django.db.models import Value
        from django.db.models.sql import Query
        from django.db.models.sql.constants import SINGLE

        query = Query(None)
        for name, value in against.items():
            if not hasattr(value, "resolve_expression"):
                value = Value(value)
            query.add_annotation(value, name, select=False)
        query.add_annotation(Value(1), "_check")
        # This will raise a FieldError if a field is missing in "against".
        query.add_q(self)
        compiler = query.get_compiler(using=using)
        try:
            return compiler.execute_sql(SINGLE) is not None
        except DatabaseError as e:
            logger.warning("Got a database error calling check() on %r: %s",
                           self, e)
            return True
示例#3
0
 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return None
     query = Query(model=model, alias_cols=False)
     where = query.build_where(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     sql, params = where.as_sql(compiler, schema_editor.connection)
     return sql % tuple(schema_editor.quote_value(p) for p in params)
示例#4
0
 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return ''
     query = Query(model=model)
     query.add_q(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     # Only the WhereNode is of interest for the partial index.
     sql, params = query.where.as_sql(compiler=compiler, connection=schema_editor.connection)
     # BaseDatabaseSchemaEditor does the same map on the params, but since
     # it's handled outside of that class, the work is done here.
     return ' WHERE ' + (sql % tuple(map(schema_editor.quote_value, params)))
示例#5
0
 def constraint_sql(self, model, schema_editor):
     query = Query(model)
     compiler = query.get_compiler(connection=schema_editor.connection)
     expressions = self._get_expression_sql(compiler, schema_editor.connection, query)
     condition = self._get_condition_sql(compiler, schema_editor, query)
     return self.template % {
         'name': schema_editor.quote_name(self.name),
         'index_type': self.index_type,
         'expressions': ', '.join(expressions),
         'where': ' WHERE (%s)' % condition if condition else '',
     }
示例#6
0
 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return ''
     query = Query(model=model)
     query.add_q(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     # Only the WhereNode is of interest for the partial index.
     sql, params = query.where.as_sql(compiler=compiler, connection=schema_editor.connection)
     # BaseDatabaseSchemaEditor does the same map on the params, but since
     # it's handled outside of that class, the work is done here.
     return ' WHERE ' + (sql % tuple(map(schema_editor.quote_value, params)))
示例#7
0
 def constraint_sql(self, model, schema_editor):
     query = Query(model)
     compiler = query.get_compiler(connection=schema_editor.connection)
     expressions = self._get_expression_sql(compiler,
                                            schema_editor.connection, query)
     condition = self._get_condition_sql(compiler, schema_editor, query)
     return self.template % {
         "name": schema_editor.quote_name(self.name),
         "index_type": self.index_type,
         "expressions": ", ".join(expressions),
         "where": " WHERE (%s)" % condition if condition else "",
     }
示例#8
0
    def _get_check_sql(self, model, schema_editor):
        query = Query(model=model)

        # Add annotations
        for k, v in self.annotations.items():
            query.add_annotation(v, k)

        where = query.build_where(self.check)

        compiler = query.get_compiler(connection=schema_editor.connection)

        sql, params = where.as_sql(compiler, schema_editor.connection)

        return sql % tuple(schema_editor.quote_value(p) for p in params)
示例#9
0
 def constraint_sql(self, model, schema_editor):
     query = Query(model, alias_cols=False)
     compiler = query.get_compiler(connection=schema_editor.connection)
     expressions = self._get_expression_sql(compiler, schema_editor.connection, query)
     condition = self._get_condition_sql(compiler, schema_editor, query)
     include = [model._meta.get_field(field_name).column for field_name in self.include]
     return self.template % {
         'name': schema_editor.quote_name(self.name),
         'index_type': self.index_type,
         'expressions': ', '.join(expressions),
         'include': schema_editor._index_include_sql(model, include),
         'where': ' WHERE (%s)' % condition if condition else '',
         'deferrable': schema_editor._deferrable_constraint_sql(self.deferrable),
     }
示例#10
0
 def get_related_where(self, fk_name, using, type):
     related_where = ["%s=%s.%s" % (self.model._meta.pk.get_attname_column()[1], type, fk_name)]
     related_query = Query(self.manager.related.model)
     for name, value in self.filter.iteritems():
         related_query.add_filter((name, value))
     related_query.add_extra(None, None,
         ["%s=%s.%s" % (self.model._meta.pk.get_attname_column()[1], type, self.manager.related.field.m2m_column_name())],
         None, None, None)
     related_query.add_count_column()
     related_query.clear_ordering(force_empty=True)
     related_query.default_cols = False
     related_filter_where, related_where_params = related_query.get_compiler(using=using,
         connection=connection).as_sql()
     if related_filter_where is not None:
         related_where.append('(' + related_filter_where + ') > 0')
     return related_where, related_where_params
示例#11
0
 def get_related_where(self, fk_name, using, type):
     related_where = [
         "%s=%s.%s" %
         (self.model._meta.pk.get_attname_column()[1], type, fk_name)
     ]
     related_query = Query(self.manager.related.model)
     for name, value in self.filter.iteritems():
         related_query.add_filter((name, value))
     related_query.add_extra(None, None, [
         "%s=%s.%s" % (self.model._meta.pk.get_attname_column()[1], type,
                       self.manager.related.field.m2m_column_name())
     ], None, None, None)
     related_query.add_count_column()
     related_query.clear_ordering(force_empty=True)
     related_query.default_cols = False
     related_filter_where, related_where_params = related_query.get_compiler(
         using=using, connection=connection).as_sql()
     if related_filter_where is not None:
         related_where.append('(' + related_filter_where + ') > 0')
     return related_where, related_where_params
示例#12
0
 def constraint_sql(self, model, schema_editor):
     query = Query(model, alias_cols=False)
     compiler = query.get_compiler(connection=schema_editor.connection)
     expressions = self._get_expressions(schema_editor, query)
     table = model._meta.db_table
     condition = self._get_condition_sql(compiler, schema_editor, query)
     include = [
         model._meta.get_field(field_name).column for field_name in self.include
     ]
     return Statement(
         self.template,
         table=Table(table, schema_editor.quote_name),
         name=schema_editor.quote_name(self.name),
         index_type=self.index_type,
         expressions=Expressions(
             table, expressions, compiler, schema_editor.quote_value
         ),
         where=" WHERE (%s)" % condition if condition else "",
         include=schema_editor._index_include_sql(model, include),
         deferrable=schema_editor._deferrable_constraint_sql(self.deferrable),
     )
示例#13
0
 def constraint_sql(self, model, schema_editor):
     query = Query(model, alias_cols=False)
     compiler = query.get_compiler(connection=schema_editor.connection)
     expressions = self._get_expression_sql(compiler, schema_editor, query)
     condition = self._get_condition_sql(compiler, schema_editor, query)
     include = [
         model._meta.get_field(field_name).column
         for field_name in self.include
     ]
     return self.template % {
         "name":
         schema_editor.quote_name(self.name),
         "index_type":
         self.index_type,
         "expressions":
         ", ".join(expressions),
         "include":
         schema_editor._index_include_sql(model, include),
         "where":
         " WHERE (%s)" % condition if condition else "",
         "deferrable":
         schema_editor._deferrable_constraint_sql(self.deferrable),
     }