Пример #1
0
    async def async_update(self, **values):
        """
        Performs an update on the model instance. You can pass in values to
        set on the model for updating, or you can call without values to
        execute an update against any modified fields.
        If no fields on the model have been modified since loading,
        no query will be performed. Model validation is performed normally.
        Setting a value to `None` is equivalent to running a CQL `DELETE` on
        that column.

        It is possible to do a blind update, that is, to update a field without
        having first selected the object out of the database.
        See :ref:`Blind Updates <blind_updates>`
        """
        for column_id, v in values.items():
            col = self._columns.get(column_id)

            # check for nonexistant columns
            if col is None:
                raise ValidationError(
                    "{0}.{1} has no column named: {2}".format(
                        self.__module__, self.__class__.__name__, column_id))

            # check for primary key update attempts
            if col.is_primary_key:
                current_value = getattr(self, column_id)
                if v != current_value:
                    raise ValidationError(
                        "Cannot apply update to primary key '{0}' for {1}.{2}".
                        format(column_id, self.__module__,
                               self.__class__.__name__))

            setattr(self, column_id, v)

        # handle polymorphic models
        if self._is_polymorphic:
            if self._is_polymorphic_base:
                raise PolymorphicModelException(
                    'cannot update polymorphic base model')
            else:
                setattr(self, self._discriminator_column_name,
                        self.__discriminator_value__)

        self.validate()
        await self.__dmlquery__(self.__class__,
                                self,
                                batch=self._batch,
                                ttl=self._ttl,
                                timestamp=self._timestamp,
                                consistency=self.__consistency__,
                                conditional=self._conditional,
                                timeout=self._timeout,
                                if_exists=self._if_exists).async_update()

        self._set_persisted()

        self._timestamp = None

        return self
Пример #2
0
    async def async_update(self, **values):
        if not values:
            return

        nulled_columns = set()
        updated_columns = set()
        us = UpdateStatement(self.column_family_name,
                             where=self._where,
                             ttl=self._ttl,
                             timestamp=self._timestamp,
                             conditionals=self._conditional,
                             if_exists=self._if_exists)
        for name, val in values.items():
            col_name, col_op = self._parse_filter_arg(name)
            col = self.model._columns.get(col_name)
            # check for nonexistant columns
            if col is None:
                raise ValidationError(
                    "{0}.{1} has no column named: {2}".format(
                        self.__module__, self.model.__name__, col_name))
            # check for primary key update attempts
            if col.is_primary_key:
                raise ValidationError(
                    "Cannot apply update to primary key '{0}' for {1}.{2}".
                    format(col_name, self.__module__, self.model.__name__))

            if col_op == 'remove' and isinstance(col, columns.Map):
                if not isinstance(val, set):
                    raise ValidationError(
                        "Cannot apply update operation '{0}' on column '{1}' with value '{2}'. A set is required."
                        .format(col_op, col_name, val))
                val = {v: None for v in val}
            else:
                # we should not provide default values in this use case.
                val = col.validate(val)

            if val is None:
                nulled_columns.add(col_name)
                continue

            us.add_update(col, val, operation=col_op)
            updated_columns.add(col_name)

        if us.assignments:
            await self._async_execute(us)

        if nulled_columns:
            delete_conditional = [
                condition for condition in self._conditional
                if condition.field not in updated_columns
            ] if self._conditional else None
            ds = DeleteStatement(self.column_family_name,
                                 fields=nulled_columns,
                                 where=self._where,
                                 conditionals=delete_conditional,
                                 if_exists=self._if_exists)
            await self._async_execute(ds)
Пример #3
0
 async def async_create(cls, **kwargs):
     extra_columns = set(kwargs.keys()) - set(cls._columns.keys())
     if extra_columns:
         raise ValidationError(
             "Incorrect columns passed: {0}".format(extra_columns))
     return await cls.objects.async_create(**kwargs)