Exemple #1
0
    def execute_batch_request_and_name(self, sql_list, bind_variables_list,
                                       keyspace_list, keyspace_ids_list,
                                       shards_list, tablet_type,
                                       as_transaction, effective_caller_id):
        """Builds the right vtgate_pb2 ExecuteBatch query.

    Args:
      sql_list: list os SQL statements.
      bind_variables_list: list of bind variables.
      keyspace_list: list of keyspaces.
      keyspace_ids_list: list of list of keyspace_ids.
      shards_list: list of shards.
      tablet_type: target tablet type.
      as_transaction: execute all statements in a single transaction.
      effective_caller_id: optional vtgate_client.CallerID.

    Returns:
      A proper vtgate_pb2.ExecuteBatchXXX object.
      The name of the remote method to call.
    """
        if keyspace_ids_list and keyspace_ids_list[0]:
            request = vtgate_pb2.ExecuteBatchKeyspaceIdsRequest()
            for sql, bind_variables, keyspace_name, keyspace_ids in zip(
                    sql_list, bind_variables_list, keyspace_list,
                    keyspace_ids_list):
                query = request.queries.add(keyspace=keyspace_name)
                query.query.sql = sql
                self._convert_bind_vars(bind_variables,
                                        query.query.bind_variables)
                query.keyspace_ids.extend(keyspace_ids)
            method_name = 'ExecuteBatchKeyspaceIds'
        else:
            request = vtgate_pb2.ExecuteBatchShardsRequest()
            for sql, bind_variables, keyspace_name, shards in zip(
                    sql_list, bind_variables_list, keyspace_list, shards_list):
                query = request.queries.add(keyspace=keyspace_name)
                query.query.sql = sql
                self._convert_bind_vars(bind_variables,
                                        query.query.bind_variables)
                query.shards.extend(shards)
            method_name = 'ExecuteBatchShards'

        request.tablet_type = topodata_pb2.TabletType.Value(
            tablet_type.upper())
        request.as_transaction = as_transaction
        self._add_caller_id(request, effective_caller_id)
        self._add_session(request)
        return request, method_name
    def _execute_batch(self,
                       sql_list,
                       bind_variables_list,
                       keyspace_list,
                       keyspace_ids_list,
                       shards_list,
                       tablet_type,
                       as_transaction,
                       effective_caller_id=None,
                       **kwargs):

        try:
            if keyspace_ids_list[0]:
                exec_method = 'ExecuteBatchKeyspaceIds'
                request = vtgate_pb2.ExecuteBatchKeyspaceIdsRequest(
                    tablet_type=topodata_pb2.TabletType.Value(
                        tablet_type.upper()),
                    as_transaction=as_transaction,
                )
                _add_caller_id(request, effective_caller_id)
                self._add_session(request)

                for sql, bind_variables, keyspace_name, keyspace_ids in zip(
                        sql_list, bind_variables_list, keyspace_list,
                        keyspace_ids_list):
                    sql, bind_variables = dbapi.prepare_query_bind_vars(
                        sql, bind_variables)
                    query = request.queries.add()
                    query.query.sql = sql
                    query.keyspace = keyspace_name
                    query.keyspace_ids.extend(keyspace_ids)
                    _convert_bind_vars(bind_variables,
                                       query.query.bind_variables)

                response = self.stub.ExecuteBatchKeyspaceIds(
                    request, self.timeout)

            else:
                exec_method = 'ExecuteBatchShards'
                request = vtgate_pb2.ExecuteBatchShardsRequest(
                    tablet_type=topodata_pb2.TabletType.Value(
                        tablet_type.upper()),
                    as_transaction=as_transaction,
                )
                _add_caller_id(request, effective_caller_id)
                self._add_session(request)

                for sql, bind_variables, keyspace_name, shards in zip(
                        sql_list, bind_variables_list, keyspace_list,
                        shards_list):
                    sql, bind_variables = dbapi.prepare_query_bind_vars(
                        sql, bind_variables)
                    query = request.queries.add()
                    query.query.sql = sql
                    query.keyspace = keyspace_name
                    query.shards.extend(shards)
                    _convert_bind_vars(bind_variables,
                                       query.query.bind_variables)

                response = self.stub.ExecuteBatchShards(request, self.timeout)

            self.session = response.session
            _extract_rpc_error(exec_method, response.error)

            rowsets = []
            for result in response.results:
                rowset = _get_rowset_from_query_result(result)
                rowsets.append(rowset)
            return rowsets

        except (face.AbortionError, vtgate_utils.VitessError) as e:
            self.logger_object.log_private_data(bind_variables_list)
            raise _convert_exception(e,
                                     sql_list,
                                     exec_method,
                                     keyspace='',
                                     tablet_type=tablet_type)