Esempio n. 1
0
 async def _async_execute(self, statement):
     connection = (self.instance._get_connection()
                   if self.instance else self.model._get_connection())
     if self._batch:
         if self._batch._connection:
             if (not self._batch._connection_explicit and connection
                     and connection != self._batch._connection):
                 raise CQLEngineException(
                     "BatchQuery queries must be executed "
                     "on the same connection")
         else:
             # set the BatchQuery connection from the model
             self._batch._connection = connection
         return self._batch.add_query(statement)
     else:
         results = await _execute_statement(
             self.model,
             statement,
             self._consistency,
             self._timeout,
             connection=connection,
         )
         if self._if_not_exists or self._if_exists or self._conditional:
             check_applied(results)
         return results
Esempio n. 2
0
    async def async_execute(self):
        if self._executed and self.warn_multiple_exec:
            msg = "Batch executed multiple times."
            if self._context_entered:
                msg += (" If using the batch as a context manager, "
                        "there is no need to call execute directly.")
            warn(msg)
        self._executed = True

        if len(self.queries) == 0:
            # Empty batch is a no-op
            # except for callbacks
            self._execute_callbacks()
            return

        opener = ("BEGIN " +
                  (self.batch_type + " " if self.batch_type else "") +
                  " BATCH")
        if self.timestamp:

            if isinstance(self.timestamp, six.integer_types):
                ts = self.timestamp
            elif isinstance(self.timestamp, (datetime, timedelta)):
                ts = self.timestamp
                if isinstance(self.timestamp, timedelta):
                    ts += datetime.now()  # Apply timedelta
                ts = int(time.mktime(ts.timetuple()) * 1e6 + ts.microsecond)
            else:
                raise ValueError("Batch expects a long, a timedelta, "
                                 "or a datetime")

            opener += " USING TIMESTAMP {0}".format(ts)

        query_list = [opener]
        parameters = {}
        ctx_counter = 0
        for query in self.queries:
            query.update_context_id(ctx_counter)
            ctx = query.get_context()
            ctx_counter += len(ctx)
            query_list.append("  " + str(query))
            parameters.update(ctx)

        query_list.append("APPLY BATCH;")

        tmp = await execute(
            "\n".join(query_list),
            parameters,
            self._consistency,
            self._timeout,
            connection=self._connection,
        )
        check_applied(tmp)

        self.queries = []
        self._execute_callbacks()
Esempio n. 3
0
 async def _async_execute(self, statement):
     if self._batch:
         return self._batch.add_query(statement)
     else:
         connection = self._connection or self.model._get_connection()
         result = await _execute_statement(self.model,
                                           statement,
                                           self._consistency,
                                           self._timeout,
                                           connection=connection)
         if self._if_not_exists or self._if_exists or self._conditional:
             check_applied(result)
         return result