A simple SQLite REPL.
| 35 | |
| 36 | |
| 37 | class SqliteInteractiveConsole(InteractiveConsole): |
| 38 | """A simple SQLite REPL.""" |
| 39 | |
| 40 | def __init__(self, connection): |
| 41 | super().__init__() |
| 42 | self._con = connection |
| 43 | self._cur = connection.cursor() |
| 44 | |
| 45 | 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 if input is a complete statement ready for execution. |
| 50 | """ |
| 51 | if not source or source.isspace(): |
| 52 | return False |
| 53 | if source[0] == ".": |
| 54 | match source[1:].strip(): |
| 55 | case "version": |
| 56 | print(f"{sqlite3.sqlite_version}") |
| 57 | case "help": |
| 58 | print("Enter SQL code and press enter.") |
| 59 | case "quit": |
| 60 | sys.exit(0) |
| 61 | case "": |
| 62 | pass |
| 63 | case _ as unknown: |
| 64 | self.write("Error: unknown command or invalid arguments:" |
| 65 | f' "{unknown}".\n') |
| 66 | else: |
| 67 | if not sqlite3.complete_statement(source): |
| 68 | return True |
| 69 | execute(self._cur, source) |
| 70 | return False |
| 71 | |
| 72 | |
| 73 | def main(*args): |