MCPcopy
hub / github.com/psycopg/psycopg / _exec_command

Method _exec_command

psycopg/psycopg/_connection_base.py:445–489  ·  view source on GitHub ↗

Generator to send a command and receive the result to the backend. Only used to implement internal commands such as "commit", with eventual arguments bound client-side. The cursor can do more complex stuff.

(
        self, command: QueryNoTemplate, result_format: pq.Format = TEXT
    )

Source from the content-addressed store, hash-verified

443 return conn
444
445 def _exec_command(
446 self, command: QueryNoTemplate, result_format: pq.Format = TEXT
447 ) -> PQGen[PGresult | None]:
448 """
449 Generator to send a command and receive the result to the backend.
450
451 Only used to implement internal commands such as "commit", with eventual
452 arguments bound client-side. The cursor can do more complex stuff.
453 """
454 self._check_connection_ok()
455
456 if isinstance(command, str):
457 command = command.encode(self.pgconn._encoding)
458 elif isinstance(command, Composable):
459 command = command.as_bytes(self)
460
461 if self._pipeline:
462 cmd = partial(
463 self.pgconn.send_query_params,
464 command,
465 None,
466 result_format=result_format,
467 )
468 self._pipeline.command_queue.append(cmd)
469 self._pipeline.result_queue.append(None)
470 return None
471
472 # Unless needed, use the simple query protocol, e.g. to interact with
473 # pgbouncer. In pipeline mode we always use the advanced query protocol
474 # instead, see #350
475 if result_format == TEXT:
476 self.pgconn.send_query(command)
477 else:
478 self.pgconn.send_query_params(command, None, result_format=result_format)
479
480 result: PGresult = (yield from generators.execute(self.pgconn))[-1]
481 if result.status != COMMAND_OK and result.status != TUPLES_OK:
482 if result.status == FATAL_ERROR:
483 raise e.error_from_result(result, encoding=self.pgconn._encoding)
484 else:
485 raise e.InterfaceError(
486 f"unexpected result {pq.ExecStatus(result.status).name}"
487 f" from command {command.decode()!r}"
488 )
489 return result
490
491 def _deallocate(self, name: bytes | None) -> PQGen[None]:
492 """

Callers 13

_deallocateMethod · 0.95
_start_queryMethod · 0.95
_commit_genMethod · 0.95
_rollback_genMethod · 0.95
_tpc_begin_genMethod · 0.95
_tpc_prepare_genMethod · 0.95
_tpc_finish_genMethod · 0.95
_enter_genMethod · 0.80
_commit_genMethod · 0.80
_rollback_genMethod · 0.80
_close_genMethod · 0.80
_fetch_genMethod · 0.80

Calls 5

_check_connection_okMethod · 0.95
as_bytesMethod · 0.45
send_queryMethod · 0.45
send_query_paramsMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected