Пример #1
0
    def as_sql(self):
        """
        Creates the SQL for this query. Returns the SQL string and list of
        parameters.
        """
        from django.db.models.base import Model

        self.pre_sql_setup()
        if not self.query.values:
            return '', ()
        table = self.query.tables[0]
        qn = self.quote_name_unless_alias
        result = ['UPDATE %s' % qn(table)]
        result.append('SET')
        values, update_params = [], []
        for field, model, val in self.query.values:
            if hasattr(val, 'prepare_database_save'):
                val = val.prepare_database_save(field)
            else:
                val = field.get_db_prep_save(val, connection=self.connection)

            # Getting the placeholder for the field.
            if hasattr(field, 'get_placeholder'):
                placeholder = field.get_placeholder(val, self.connection)
            else:
                placeholder = '%s'

            if hasattr(val, 'evaluate'):
                val = SQLEvaluator(val, self.query, allow_joins=False)
            name = field.column
            if hasattr(val, 'as_sql'):
                sql, params = val.as_sql(qn, self.connection)
                values.append('%s = %s' % (qn(name), sql))
                update_params.extend(params)
            elif val is not None:
                values.append('%s = %s' % (qn(name), placeholder))
                update_params.append(val)
            else:
                values.append('%s = NULL' % qn(name))
        if not values:
            return '', ()
        result.append(', '.join(values))
        where, params = self.query.where.as_sql(qn=qn,
                                                connection=self.connection)
        if where:
            result.append('WHERE %s' % where)
        return ' '.join(result), tuple(update_params + params)
Пример #2
0
    def as_sql(self):
        """
        Creates the SQL for this query. Returns the SQL string and list of
        parameters.
        """
        from django.db.models.base import Model

        self.pre_sql_setup()
        if not self.query.values:
            return '', ()
        table = self.query.tables[0]
        qn = self.quote_name_unless_alias
        result = ['UPDATE %s' % qn(table)]
        result.append('SET')
        values, update_params = [], []
        for field, model, val in self.query.values:
            if hasattr(val, 'prepare_database_save'):
                val = val.prepare_database_save(field)
            else:
                val = field.get_db_prep_save(val, connection=self.connection)

            # Getting the placeholder for the field.
            if hasattr(field, 'get_placeholder'):
                placeholder = field.get_placeholder(val, self.connection)
            else:
                placeholder = '%s'

            if hasattr(val, 'evaluate'):
                val = SQLEvaluator(val, self.query, allow_joins=False)
            name = field.column
            if hasattr(val, 'as_sql'):
                sql, params = val.as_sql(qn, self.connection)
                values.append('%s = %s' % (qn(name), sql))
                update_params.extend(params)
            elif val is not None:
                values.append('%s = %s' % (qn(name), placeholder))
                update_params.append(val)
            else:
                values.append('%s = NULL' % qn(name))
        if not values:
            return '', ()
        result.append(', '.join(values))
        where, params = self.query.where.as_sql(qn=qn, connection=self.connection)
        if where:
            result.append('WHERE %s' % where)
        return ' '.join(result), tuple(update_params + params)
Пример #3
0
    def as_sql(self):
        if getattr(settings, 'SPHINX_INDEX_RT', True):
            qn = self.connection.ops.quote_name
            opts = self.query.model._meta
            result = ['REPLACE INTO %s' % qn(opts.db_table)]
            # This is a bit ugly, we have to scrape information from the where clause
            # and put it into the field/values list. Sphinx will not accept an UPDATE
            # statement that includes full text data, only INSERT/REPLACE INTO.
            lvalue, lookup_type, value_annot, params_or_value = self.query.where.children[
                0].children[0]
            (table_name, column_name,
             column_type), val = lvalue.process(lookup_type, params_or_value,
                                                self.connection)
            fields, values, params = [column_name], ['%s'], [val[0]]
            # Now build the rest of the fields into our query.
            for field, model, val in self.query.values:
                if hasattr(val, 'prepare_database_save'):
                    val = val.prepare_database_save(field)
                else:
                    val = field.get_db_prep_save(val,
                                                 connection=self.connection)

                # Getting the placeholder for the field.
                if hasattr(field, 'get_placeholder'):
                    placeholder = field.get_placeholder(val, self.connection)
                else:
                    placeholder = '%s'

                if hasattr(val, 'evaluate'):
                    val = SQLEvaluator(val, self.query, allow_joins=False)
                name = field.column
                if hasattr(val, 'as_sql'):
                    sql, params = val.as_sql(qn, self.connection)
                    values.append(sql)
                    params.extend(params)
                elif val is not None:
                    values.append(placeholder)
                    params.append(val)
                else:
                    values.append('NULL')
                fields.append(name)
            result.append('(%s)' % ', '.join(fields))
            result.append('VALUES (%s)' % ', '.join(values))
            return ' '.join(result), params
        else:
            return super(SQLUpdateCompiler, self).as_sql()
Пример #4
0
    def as_sql(self):
        """
        Creates the SQL for this query. Returns the SQL string and list of
        parameters.
        """
        self.pre_sql_setup()
        if not self.query.values:
            return "", ()
        table = self.query.tables[0]
        qn = self.quote_name_unless_alias
        result = ["UPDATE %s" % qn(table)]
        result.append("SET")
        values, update_params = [], []
        for field, model, val in self.query.values:
            if hasattr(val, "prepare_database_save"):
                val = val.prepare_database_save(field)
            else:
                val = field.get_db_prep_save(val, connection=self.connection)

            # Getting the placeholder for the field.
            if hasattr(field, "get_placeholder"):
                placeholder = field.get_placeholder(val, self.connection)
            else:
                placeholder = "%s"

            if hasattr(val, "evaluate"):
                val = SQLEvaluator(val, self.query, allow_joins=False)
            name = field.column
            if hasattr(val, "as_sql"):
                sql, params = val.as_sql(qn, self.connection)
                values.append("%s = %s" % (qn(name), sql))
                update_params.extend(params)
            elif val is not None:
                values.append("%s = %s" % (qn(name), placeholder))
                update_params.append(val)
            else:
                values.append("%s = NULL" % qn(name))
        if not values:
            return "", ()
        result.append(", ".join(values))
        where, params = self.query.where.as_sql(qn=qn, connection=self.connection)
        if where:
            result.append("WHERE %s" % where)
        return " ".join(result), tuple(update_params + params)
Пример #5
0
    def as_sql(self):
        qn = self.connection.ops.quote_name
        opts = self.query.model._meta
        result = ['REPLACE INTO %s' % qn(opts.db_table)]
        # This is a bit ugly, we have to scrape information from the where clause
        # and put it into the field/values list. Sphinx will not accept an UPDATE
        # statement that includes full text data, only INSERT/REPLACE INTO.
        node = self.query.where.children[0]
        if django.VERSION < DJANGO16:
            lvalue, lookup_type, value_annot, params_or_value = node.children[0]
        else:
            lvalue, lookup_type, value_annot, params_or_value = node
        (table_name, column_name, column_type), val = lvalue.process(lookup_type, params_or_value, self.connection)
        fields, values, params = [column_name], ['%s'], [val[0]]
        # Now build the rest of the fields into our query.
        for field, model, val in self.query.values:
            if hasattr(val, 'prepare_database_save'):
                val = val.prepare_database_save(field)
            else:
                val = field.get_db_prep_save(val, connection=self.connection)

            # Getting the placeholder for the field.
            if hasattr(field, 'get_placeholder'):
                placeholder = field.get_placeholder(val, self.connection)
            else:
                placeholder = '%s'

            if hasattr(val, 'evaluate'):
                val = SQLEvaluator(val, self.query, allow_joins=False)
            name = field.column
            if hasattr(val, 'as_sql'):
                sql, params = val.as_sql(qn, self.connection)
                values.append(sql)
                params.extend(params)
            elif val is not None:
                values.append(placeholder)
                params.append(val)
            else:
                values.append('NULL')
            fields.append(name)
        result.append('(%s)' % ', '.join(fields))
        result.append('VALUES (%s)' % ', '.join(values))
        return ' '.join(result), params