diff options
author | Erlend E. Aasland <erlend.aasland@protonmail.com> | 2022-08-11 23:05:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-11 23:05:12 (GMT) |
commit | e6623e7083ce08a247e5df169bcc749f99327823 (patch) | |
tree | a66de2d1678802222b9eccd7fb196d8b23c4845a /Lib/sqlite3 | |
parent | 6f6a4e6cc5cd76af4a53ffbb62b686142646ac9a (diff) | |
download | cpython-e6623e7083ce08a247e5df169bcc749f99327823.zip cpython-e6623e7083ce08a247e5df169bcc749f99327823.tar.gz cpython-e6623e7083ce08a247e5df169bcc749f99327823.tar.bz2 |
gh-95273: Improve sqlite3.complete_statement docs (#95840)
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
Co-authored-by: CAM Gerlach <CAM.Gerlach@Gerlach.CAM>
Diffstat (limited to 'Lib/sqlite3')
-rw-r--r-- | Lib/sqlite3/__main__.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index c62fad8..f8a5cca 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -1,3 +1,9 @@ +"""A simple SQLite CLI for the sqlite3 module. + +Apart from using 'argparse' for the command-line interface, +this module implements the REPL as a thin wrapper around +the InteractiveConsole class from the 'code' stdlib module. +""" import sqlite3 import sys @@ -7,6 +13,14 @@ from textwrap import dedent def execute(c, sql, suppress_errors=True): + """Helper that wraps execution of SQL code. + + This is used both by the REPL and by direct execution from the CLI. + + 'c' may be a cursor or a connection. + 'sql' is the SQL string to execute. + """ + try: for row in c.execute(sql): print(row) @@ -21,6 +35,7 @@ def execute(c, sql, suppress_errors=True): class SqliteInteractiveConsole(InteractiveConsole): + """A simple SQLite REPL.""" def __init__(self, connection): super().__init__() @@ -28,6 +43,11 @@ class SqliteInteractiveConsole(InteractiveConsole): self._cur = connection.cursor() def runsource(self, source, filename="<input>", symbol="single"): + """Override runsource, the core of the InteractiveConsole REPL. + + Return True if more input is needed; buffering is done automatically. + Return False is input is a complete statement ready for execution. + """ match source: case ".version": print(f"{sqlite3.sqlite_version}") @@ -73,6 +93,7 @@ def main(): else: db_name = repr(args.filename) + # Prepare REPL banner and prompts. banner = dedent(f""" sqlite3 shell, running on SQLite version {sqlite3.sqlite_version} Connected to {db_name} @@ -86,8 +107,10 @@ def main(): con = sqlite3.connect(args.filename, isolation_level=None) try: if args.sql: + # SQL statement provided on the command-line; execute it directly. execute(con, args.sql, suppress_errors=False) else: + # No SQL provided; start the REPL. console = SqliteInteractiveConsole(con) console.interact(banner, exitmsg="") finally: |