示例#1
0
    def stream_execute_request_and_name(self, sql, bind_variables, tablet_type,
                                        keyspace_name, shards, keyspace_ids,
                                        key_ranges, effective_caller_id):
        """Builds the right vtgate_pb2 Request and method for a _stream_execute.

    Args:
      sql: the query to run. Bind Variables in there should be in python format.
      bind_variables: python map of bind variables.
      tablet_type: string tablet type.
      keyspace_name: keyspace to apply the query to.
      shards: array of strings representing the shards.
      keyspace_ids: array of keyspace ids.
      key_ranges: array of keyrange.KeyRange objects.
      effective_caller_id: optional vtgate_client.CallerID.

    Returns:
      A vtgate_pb2.StreamExecuteXXXXRequest object.
      A dict that contains the routing parameters.
      The name of the remote method called.
    """

        if shards is not None:
            request = vtgate_pb2.StreamExecuteShardsRequest(
                keyspace=keyspace_name)
            request.shards.extend(shards)
            routing_kwargs = {'shards': shards}
            method_name = 'StreamExecuteShards'

        elif keyspace_ids is not None:
            request = vtgate_pb2.StreamExecuteKeyspaceIdsRequest(
                keyspace=keyspace_name)
            request.keyspace_ids.extend(keyspace_ids)
            routing_kwargs = {'keyspace_ids': keyspace_ids}
            method_name = 'StreamExecuteKeyspaceIds'

        elif key_ranges is not None:
            request = vtgate_pb2.StreamExecuteKeyRangesRequest(
                keyspace=keyspace_name)
            self._add_key_ranges(request, key_ranges)
            routing_kwargs = {'keyranges': key_ranges}
            method_name = 'StreamExecuteKeyRanges'

        else:
            request = vtgate_pb2.StreamExecuteRequest()
            if keyspace_name:
                request.keyspace = keyspace_name
            routing_kwargs = {}
            method_name = 'StreamExecute'

        request.query.sql = sql
        convert_bind_vars(bind_variables, request.query.bind_variables)
        request.tablet_type = topodata_pb2.TabletType.Value(
            tablet_type.upper())
        self._add_caller_id(request, effective_caller_id)
        return request, routing_kwargs, method_name
示例#2
0
    def _stream_execute(self,
                        sql,
                        bind_variables,
                        keyspace_name,
                        tablet_type,
                        shards=None,
                        keyspace_ids=None,
                        keyranges=None,
                        not_in_transaction=False,
                        effective_caller_id=None,
                        **kwargs):

        try:
            sql, bind_variables = dbapi.prepare_query_bind_vars(
                sql, bind_variables)

            if shards is not None:
                request = vtgate_pb2.StreamExecuteShardsRequest(
                    query=query_pb2.BoundQuery(sql=sql),
                    tablet_type=topodata_pb2.TabletType.Value(
                        tablet_type.upper()),
                    keyspace=keyspace_name,
                )
                _add_caller_id(request, effective_caller_id)
                request.shards.extend(shards)
                _convert_bind_vars(bind_variables,
                                   request.query.bind_variables)
                it = self.stub.StreamExecuteShards(request, self.timeout)

            elif keyspace_ids is not None:
                request = vtgate_pb2.StreamExecuteKeyspaceIdsRequest(
                    query=query_pb2.BoundQuery(sql=sql),
                    tablet_type=topodata_pb2.TabletType.Value(
                        tablet_type.upper()),
                    keyspace=keyspace_name,
                )
                _add_caller_id(request, effective_caller_id)
                request.keyspace_ids.extend(keyspace_ids)
                _convert_bind_vars(bind_variables,
                                   request.query.bind_variables)
                it = self.stub.StreamExecuteKeyspaceIds(request, self.timeout)

            elif keyranges is not None:
                request = vtgate_pb2.StreamExecuteKeyRangesRequest(
                    query=query_pb2.BoundQuery(sql=sql),
                    tablet_type=topodata_pb2.TabletType.Value(
                        tablet_type.upper()),
                    keyspace=keyspace_name,
                )
                _add_caller_id(request, effective_caller_id)
                _add_key_ranges(request, keyranges)
                _convert_bind_vars(bind_variables,
                                   request.query.bind_variables)
                it = self.stub.StreamExecuteKeyRanges(request, self.timeout)

            else:
                request = vtgate_pb2.StreamExecuteRequest(
                    query=query_pb2.BoundQuery(sql=sql),
                    tablet_type=topodata_pb2.TabletType.Value(
                        tablet_type.upper()),
                )
                _add_caller_id(request, effective_caller_id)
                _convert_bind_vars(bind_variables,
                                   request.query.bind_variables)
                it = self.stub.StreamExecute(request, self.timeout)

            first_response = it.next()
        except (face.AbortionError, vtgate_utils.VitessError) as e:
            self.logger_object.log_private_data(bind_variables)
            raise _convert_exception(e,
                                     sql,
                                     keyspace_ids,
                                     keyranges,
                                     keyspace=keyspace_name,
                                     tablet_type=tablet_type)

        fields = []
        conversions = []
        for field in first_response.result.fields:
            fields.append((field.name, field.type))
            conversions.append(field_types_proto3.conversions.get(field.type))

        def row_generator():
            try:
                for response in it:
                    for row in response.result.rows:
                        yield tuple(_make_row(row, conversions))
            except Exception:
                logging.exception('gRPC low-level error')
                raise

        return row_generator(), fields