示例#1
0
    def set_delete_null_columns(self, conditionals=None):
        """Set self.cleanup_statement to delete query to remove columns that
        have changed to null.
        """
        ds = DeleteStatement(
            self.column_family_name, conditionals=conditionals, if_exists=self._if_exists
        )
        deleted_fields = False
        static_only = True
        for _, v in self.instance._values.items():
            col = v.column
            if v.deleted:
                ds.add_field(col.db_field_name)
                deleted_fields = True
                static_only &= col.static
            elif isinstance(col, columns.Map):
                uc = MapDeleteClause(col.db_field_name, v.value, v.previous_value)
                if uc.get_context_size() > 0:
                    ds.add_field(uc)
                    deleted_fields = True
                    static_only |= col.static

        if deleted_fields:
            keys = self.model._partition_keys if static_only else self.model._primary_keys
            for name, col in keys.items():
                ds.add_where(col, EqualsOperator(), getattr(self.instance, name))
            self.cleanup_statement = ds
    def test_update(self):
        c = MapDeleteClause('s', {3: 0}, {1: 2, 3: 4, 5: 6})
        c._analyze()
        c.set_context_id(0)

        self.assertEqual(c._removals, [1, 5])
        self.assertEqual(c.get_context_size(), 2)
        self.assertEqual(str(c), '"s"[%(0)s], "s"[%(1)s]')

        ctx = {}
        c.update_context(ctx)
        self.assertEqual(ctx, {'0': 1, '1': 5})
    def test_context_update(self):
        ds = DeleteStatement('table', None)
        ds.add_field(MapDeleteClause('d', {1: 2}, {1: 2, 3: 4}))
        ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')

        ds.update_context_id(7)
        self.assertEqual(six.text_type(ds), 'DELETE "d"[%(8)s] FROM table WHERE "a" = %(7)s')
        self.assertEqual(ds.get_context(), {'7': 'b', '8': 3})
    def test_context_update(self):
        ds = DeleteStatement("table", None)
        ds.add_field(MapDeleteClause("d", {1: 2}, {1: 2, 3: 4}))
        ds.add_where(Column(db_field="a"), EqualsOperator(), "b")

        ds.update_context_id(7)
        self.assertEqual(six.text_type(ds),
                         'DELETE "d"[%(8)s] FROM table WHERE "a" = %(7)s')
        self.assertEqual(ds.get_context(), {"7": "b", "8": 3})