def execute_request_and_name(self, sql, bind_variables, tablet_type, keyspace_name, shards, keyspace_ids, key_ranges, entity_column_name, entity_keyspace_id_map, not_in_transaction, effective_caller_id, include_event_token, compare_event_token): """Builds the right vtgate_pb2 Request and method for an _execute call. 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. entity_column_name: the column name to vary. entity_keyspace_id_map: map of external id to keyspace id. not_in_transaction: do not create a transaction to a new shard. effective_caller_id: optional vtgate_client.CallerID. include_event_token: boolean on whether to ask for event token. compare_event_token: set the result extras fresher based on this token. Returns: A vtgate_pb2.XXXRequest object. A dict that contains the routing parameters. The name of the remote method called. """ if shards is not None: request = vtgate_pb2.ExecuteShardsRequest(keyspace=keyspace_name) request.shards.extend(shards) routing_kwargs = {'shards': shards} method_name = 'ExecuteShards' elif keyspace_ids is not None: request = vtgate_pb2.ExecuteKeyspaceIdsRequest( keyspace=keyspace_name) request.keyspace_ids.extend(keyspace_ids) routing_kwargs = {'keyspace_ids': keyspace_ids} method_name = 'ExecuteKeyspaceIds' elif key_ranges is not None: request = vtgate_pb2.ExecuteKeyRangesRequest( keyspace=keyspace_name) self._add_key_ranges(request, key_ranges) routing_kwargs = {'keyranges': key_ranges} method_name = 'ExecuteKeyRanges' elif entity_keyspace_id_map is not None: request = vtgate_pb2.ExecuteEntityIdsRequest( keyspace=keyspace_name, entity_column_name=entity_column_name) self._convert_entity_ids(entity_keyspace_id_map, request.entity_keyspace_ids) routing_kwargs = { 'entity_keyspace_id_map': entity_keyspace_id_map, 'entity_column_name': entity_column_name } method_name = 'ExecuteEntityIds' else: request = vtgate_pb2.ExecuteRequest() if keyspace_name: request.keyspace = keyspace_name routing_kwargs = {} method_name = 'Execute' request.query.sql = sql convert_bind_vars(bind_variables, request.query.bind_variables) request.tablet_type = topodata_pb2.TabletType.Value( tablet_type.upper()) request.not_in_transaction = not_in_transaction self._add_caller_id(request, effective_caller_id) self._add_session(request) if include_event_token: request.options.include_event_token = True if compare_event_token: request.options.compare_event_token.CopyFrom(compare_event_token) self.event_token = None self.fresher = None return request, routing_kwargs, method_name
def _execute(self, sql, bind_variables, keyspace_name, tablet_type, shards=None, keyspace_ids=None, keyranges=None, entity_keyspace_id_map=None, entity_column_name=None, not_in_transaction=False, effective_caller_id=None, **kwargs): # FIXME(alainjobart): keyspace should be in routing_kwargs, # as it's not used for v3. # FIXME(alainjobart): the v3 part doesn't take the ptyhon-style queries # for bind variables (the %(xxx)s), but our style (the :xxx). # this is not consistent with the rest. try: routing_kwargs = {} exec_method = None if shards is not None: routing_kwargs['shards'] = shards exec_method = 'ExecuteShards' sql, bind_variables = dbapi.prepare_query_bind_vars( sql, bind_variables) request = vtgate_pb2.ExecuteShardsRequest( query=query_pb2.BoundQuery(sql=sql), tablet_type=topodata_pb2.TabletType.Value( tablet_type.upper()), keyspace=keyspace_name, not_in_transaction=not_in_transaction, ) _add_caller_id(request, effective_caller_id) self._add_session(request) request.shards.extend(shards) _convert_bind_vars(bind_variables, request.query.bind_variables) response = self.stub.ExecuteShards(request, self.timeout) elif keyspace_ids is not None: routing_kwargs['keyspace_ids'] = keyspace_ids exec_method = 'ExecuteKeyspaceIds' sql, bind_variables = dbapi.prepare_query_bind_vars( sql, bind_variables) request = vtgate_pb2.ExecuteKeyspaceIdsRequest( query=query_pb2.BoundQuery(sql=sql), tablet_type=topodata_pb2.TabletType.Value( tablet_type.upper()), keyspace=keyspace_name, not_in_transaction=not_in_transaction, ) _add_caller_id(request, effective_caller_id) self._add_session(request) request.keyspace_ids.extend(keyspace_ids) _convert_bind_vars(bind_variables, request.query.bind_variables) response = self.stub.ExecuteKeyspaceIds(request, self.timeout) elif keyranges is not None: routing_kwargs['keyranges'] = keyranges exec_method = 'ExecuteKeyRanges' sql, bind_variables = dbapi.prepare_query_bind_vars( sql, bind_variables) request = vtgate_pb2.ExecuteKeyRangesRequest( query=query_pb2.BoundQuery(sql=sql), tablet_type=topodata_pb2.TabletType.Value( tablet_type.upper()), keyspace=keyspace_name, not_in_transaction=not_in_transaction, ) _add_caller_id(request, effective_caller_id) self._add_session(request) _add_key_ranges(request, keyranges) _convert_bind_vars(bind_variables, request.query.bind_variables) response = self.stub.ExecuteKeyRanges(request, self.timeout) elif entity_keyspace_id_map is not None: routing_kwargs[ 'entity_keyspace_id_map'] = entity_keyspace_id_map routing_kwargs['entity_column_name'] = entity_column_name exec_method = 'ExecuteEntityIds' sql, bind_variables = dbapi.prepare_query_bind_vars( sql, bind_variables) request = vtgate_pb2.ExecuteEntityIdsRequest( query=query_pb2.BoundQuery(sql=sql), tablet_type=topodata_pb2.TabletType.Value( tablet_type.upper()), keyspace=keyspace_name, entity_column_name=entity_column_name, not_in_transaction=not_in_transaction, ) _add_caller_id(request, effective_caller_id) self._add_session(request) _convert_bind_vars(bind_variables, request.query.bind_variables) _convert_entity_ids(entity_keyspace_id_map, request.entity_keyspace_ids) response = self.stub.ExecuteEntityIds(request, self.timeout) else: exec_method = 'Execute' request = vtgate_pb2.ExecuteRequest( query=query_pb2.BoundQuery(sql=sql), tablet_type=topodata_pb2.TabletType.Value( tablet_type.upper()), not_in_transaction=not_in_transaction, ) _add_caller_id(request, effective_caller_id) self._add_session(request) _convert_bind_vars(bind_variables, request.query.bind_variables) response = self.stub.Execute(request, self.timeout) self.session = response.session _extract_rpc_error(exec_method, response.error) return _get_rowset_from_query_result(response.result) except (face.AbortionError, vtgate_utils.VitessError) as e: self.logger_object.log_private_data(bind_variables) raise _convert_exception(e, sql, keyspace=keyspace_name, tablet_type=tablet_type, **routing_kwargs)