| 811 | return text |
| 812 | |
| 813 | def execute_command(self, text, handle_closed_connection=True): |
| 814 | logger = self.logger |
| 815 | |
| 816 | query = MetaQuery(query=text, successful=False) |
| 817 | |
| 818 | try: |
| 819 | if self.destructive_warning: |
| 820 | if ( |
| 821 | self.destructive_statements_require_transaction |
| 822 | and not self.pgexecute.valid_transaction() |
| 823 | and is_destructive(text, self.destructive_warning) |
| 824 | ): |
| 825 | click.secho("Destructive statements must be run within a transaction.") |
| 826 | raise KeyboardInterrupt |
| 827 | destroy = confirm_destructive_query(text, self.destructive_warning, self.dsn_alias) |
| 828 | if destroy is False: |
| 829 | click.secho("Wise choice!") |
| 830 | raise KeyboardInterrupt |
| 831 | elif destroy: |
| 832 | click.secho("Your call!") |
| 833 | |
| 834 | output, query = self._evaluate_command(text) |
| 835 | except KeyboardInterrupt: |
| 836 | if self.destructive_warning_restarts_connection: |
| 837 | # Restart connection to the database |
| 838 | self.pgexecute.connect() |
| 839 | logger.debug("cancelled query and restarted connection, sql: %r", text) |
| 840 | click.secho("cancelled query and restarted connection", err=True, fg="red") |
| 841 | else: |
| 842 | logger.debug("cancelled query, sql: %r", text) |
| 843 | click.secho("cancelled query", err=True, fg="red") |
| 844 | except NotImplementedError: |
| 845 | click.secho("Not Yet Implemented.", fg="yellow") |
| 846 | except OperationalError as e: |
| 847 | logger.error("sql: %r, error: %r", text, e) |
| 848 | logger.error("traceback: %r", traceback.format_exc()) |
| 849 | click.secho(str(e), err=True, fg="red") |
| 850 | if handle_closed_connection: |
| 851 | self._handle_server_closed_connection(text) |
| 852 | except (PgCliQuitError, EOFError): |
| 853 | raise |
| 854 | except Exception as e: |
| 855 | logger.error("sql: %r, error: %r", text, e) |
| 856 | logger.error("traceback: %r", traceback.format_exc()) |
| 857 | click.secho(str(e), err=True, fg="red") |
| 858 | else: |
| 859 | try: |
| 860 | if self.output_file and not text.startswith(("\\o ", "\\log-file", "\\? ", "\\echo ")): |
| 861 | try: |
| 862 | with open(self.output_file, "a", encoding="utf-8") as f: |
| 863 | should_hide = ( |
| 864 | self.hide_named_query_text |
| 865 | and query.is_special |
| 866 | and query.successful |
| 867 | and self._is_named_query_execution(text) |
| 868 | ) |
| 869 | if not should_hide: |
| 870 | click.echo(text, file=f) |