Пример #1
0
    def prepare(self):
        if self.instance is None:
            raise CQLEngineException("DML Query intance attribute is None")
        assert type(self.instance) == self.model

        nulled_fields = set()
        if self.instance._has_counter:
            raise Exception(
                "'create' and 'save' actions on Counters is not supported. "
                "Use the 'update' mechanism instead."
            )

        insert = InsertStatement(
            self.column_family_name,
            ttl=self._ttl,
            timestamp=self._timestamp,
            if_not_exists=self._if_not_exists,
        )
        static_save_only = len(self.instance._clustering_keys) != 0
        for name, col in self.instance._clustering_keys.items():
            static_save_only = static_save_only and col._val_is_null(
                getattr(self.instance, name, None)
            )
        for name, col in self.instance._columns.items():
            if static_save_only and not col.static and not col.partition_key:
                continue
            val = getattr(self.instance, name, None)
            if col._val_is_null(val):
                if self.instance._values[name].changed:
                    nulled_fields.add(col.db_field_name)
                continue
            insert.add_assignment(col, getattr(self.instance, name, None))

        # set cleanup to delete any nulled columns
        if not static_save_only:
            self.set_delete_null_columns()

        # skip query execution if it's empty
        # caused by pointless update queries
        if not insert.is_empty:
            self.statement = insert
    def test_context_update(self):
        ist = InsertStatement('table', None)
        ist.add_assignment(Column(db_field='a'), 'b')
        ist.add_assignment(Column(db_field='c'), 'd')

        ist.update_context_id(4)
        self.assertEqual(six.text_type(ist),
                         'INSERT INTO table ("a", "c") VALUES (%(4)s, %(5)s)')
        ctx = ist.get_context()
        self.assertEqual(ctx, {'4': 'b', '5': 'd'})
 def test_additional_rendering(self):
     ist = InsertStatement('table', ttl=60)
     ist.add_assignment(Column(db_field='a'), 'b')
     ist.add_assignment(Column(db_field='c'), 'd')
     self.assertIn('USING TTL 60', six.text_type(ist))