MCPcopy
hub / github.com/dbcli/pgcli / run

Method run

pgcli/pgexecute.py:317–427  ·  view source on GitHub ↗

Execute the sql in the database and return the results. :param statement: A string containing one or more sql statements :param pgspecial: PGSpecial object :param exception_formatter: A callable that accepts an Exception and returns a formatted (title, rows, h

(
        self,
        statement,
        pgspecial=None,
        exception_formatter=None,
        on_error_resume=False,
        explain_mode=False,
    )

Source from the content-addressed store, hash-verified

315 return ""
316
317 def run(
318 self,
319 statement,
320 pgspecial=None,
321 exception_formatter=None,
322 on_error_resume=False,
323 explain_mode=False,
324 ):
325 """Execute the sql in the database and return the results.
326
327 :param statement: A string containing one or more sql statements
328 :param pgspecial: PGSpecial object
329 :param exception_formatter: A callable that accepts an Exception and
330 returns a formatted (title, rows, headers, status) tuple that can
331 act as a query result. If an exception_formatter is not supplied,
332 psycopg2 exceptions are always raised.
333 :param on_error_resume: Bool. If true, queries following an exception
334 (assuming exception_formatter has been supplied) continue to
335 execute.
336
337 :return: Generator yielding tuples containing
338 (title, rows, headers, status, query, success, is_special)
339 """
340
341 # Remove spaces and EOL
342 statement = statement.strip()
343 if not statement: # Empty string
344 yield None, None, None, None, statement, False, False
345
346 # sql parse doesn't split on a comment first + special
347 # so we're going to do it
348
349 removed_comments = []
350 sqlarr = []
351 cleaned_command = ""
352
353 # could skip if statement doesn't match ^-- or ^/*
354 cleaned_command, removed_comments = remove_beginning_comments(statement)
355
356 sqlarr = sqlparse.split(cleaned_command)
357
358 # now re-add the beginning comments if there are any, so that they show up in
359 # log files etc when running these commands
360
361 if len(removed_comments) > 0:
362 sqlarr = removed_comments + sqlarr
363
364 # run each sql query
365 for sql in sqlarr:
366 # Remove spaces, eol and semi-colons.
367 # Strip comments first so rstrip(";") works when there are
368 # trailing comments after the semicolon, e.g.:
369 # vacuum freeze verbose t; -- 82% towards emergency
370 sql = sqlparse.format(sql, strip_comments=True).strip().rstrip(";")
371 sql = sql.strip()
372 if not sql:
373 continue
374 try:

Callers 3

execute_from_fileMethod · 0.45
_evaluate_commandMethod · 0.45
cliFunction · 0.45

Calls 7

explain_prefixMethod · 0.95
connectMethod · 0.95
execute_normal_sqlMethod · 0.95
_must_raiseMethod · 0.95
exception_formatterFunction · 0.70
executeMethod · 0.45

Tested by

no test coverage detected