Skip to content

Commit e6623e7

Browse files
erlend-aaslandezio-melottiCAM-Gerlach
authored
pythongh-95273: Improve sqlite3.complete_statement docs (python#95840)
Co-authored-by: Ezio Melotti <[email protected]> Co-authored-by: CAM Gerlach <[email protected]>
1 parent 6f6a4e6 commit e6623e7

File tree

3 files changed

+40
-39
lines changed

3 files changed

+40
-39
lines changed

Doc/includes/sqlite3/complete_statement.py

-33
This file was deleted.

Doc/library/sqlite3.rst

+17-6
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,25 @@ Module functions
222222

223223
.. function:: complete_statement(statement)
224224

225-
Returns ``True`` if the string *statement* contains one or more complete SQL
226-
statements terminated by semicolons. It does not verify that the SQL is
227-
syntactically correct, only that there are no unclosed string literals and the
228-
statement is terminated by a semicolon.
225+
Return ``True`` if the string *statement* appears to contain
226+
one or more complete SQL statements.
227+
No syntactic verification or parsing of any kind is performed,
228+
other than checking that there are no unclosed string literals
229+
and the statement is terminated by a semicolon.
229230

230-
This can be used to build a shell for SQLite, as in the following example:
231+
For example::
231232

232-
.. literalinclude:: ../includes/sqlite3/complete_statement.py
233+
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
234+
True
235+
>>> sqlite3.complete_statement("SELECT foo")
236+
False
237+
238+
This function may be useful during command-line input
239+
to determine if the entered text seems to form a complete SQL statement,
240+
or if additional input is needed before calling :meth:`~Cursor.execute`.
241+
242+
See :func:`!runsource` in :source:`Lib/sqlite3/__main__.py`
243+
for real-world use.
233244

234245
.. function:: enable_callback_tracebacks(flag, /)
235246

Lib/sqlite3/__main__.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""A simple SQLite CLI for the sqlite3 module.
2+
3+
Apart from using 'argparse' for the command-line interface,
4+
this module implements the REPL as a thin wrapper around
5+
the InteractiveConsole class from the 'code' stdlib module.
6+
"""
17
import sqlite3
28
import sys
39

@@ -7,6 +13,14 @@
713

814

915
def execute(c, sql, suppress_errors=True):
16+
"""Helper that wraps execution of SQL code.
17+
18+
This is used both by the REPL and by direct execution from the CLI.
19+
20+
'c' may be a cursor or a connection.
21+
'sql' is the SQL string to execute.
22+
"""
23+
1024
try:
1125
for row in c.execute(sql):
1226
print(row)
@@ -21,13 +35,19 @@ def execute(c, sql, suppress_errors=True):
2135

2236

2337
class SqliteInteractiveConsole(InteractiveConsole):
38+
"""A simple SQLite REPL."""
2439

2540
def __init__(self, connection):
2641
super().__init__()
2742
self._con = connection
2843
self._cur = connection.cursor()
2944

3045
def runsource(self, source, filename="<input>", symbol="single"):
46+
"""Override runsource, the core of the InteractiveConsole REPL.
47+
48+
Return True if more input is needed; buffering is done automatically.
49+
Return False is input is a complete statement ready for execution.
50+
"""
3151
match source:
3252
case ".version":
3353
print(f"{sqlite3.sqlite_version}")
@@ -73,6 +93,7 @@ def main():
7393
else:
7494
db_name = repr(args.filename)
7595

96+
# Prepare REPL banner and prompts.
7697
banner = dedent(f"""
7798
sqlite3 shell, running on SQLite version {sqlite3.sqlite_version}
7899
Connected to {db_name}
@@ -86,8 +107,10 @@ def main():
86107
con = sqlite3.connect(args.filename, isolation_level=None)
87108
try:
88109
if args.sql:
110+
# SQL statement provided on the command-line; execute it directly.
89111
execute(con, args.sql, suppress_errors=False)
90112
else:
113+
# No SQL provided; start the REPL.
91114
console = SqliteInteractiveConsole(con)
92115
console.interact(banner, exitmsg="")
93116
finally:

0 commit comments

Comments
 (0)