-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
38 lines (34 loc) · 1.11 KB
/
cli.py
File metadata and controls
38 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# <!-- filename: cli.py -->
from rich.table import Table
from rich import print
from db.parser import parse
from db.executor import execute
from db.txn.recovery import recover
from db.txn.flush_worker import start as start_flush
start_flush(interval=0.5)
recover()
def show(rows):
if not rows: print("(no rows)"); return
t=Table(show_header=True)
for h in rows[0]: t.add_column(h)
for r in rows: t.add_row(*[str(v) for v in r.values()])
print(t)
def repl():
print("[green]smallSQL w/ tiny optimizer & WAL (\\quit to exit)[/green]")
buf=[]
while True:
line=input("sql> ")
if line.strip().lower() in ("\\quit","quit","exit"): break
buf.append(line)
if ";" in line:
sql="\n".join(buf); buf=[]
ast=parse(sql)
if not ast: print("[red]syntax error[/red]"); continue
try:
res = execute(ast)
if isinstance(res, list):
show(res)
except ValueError as exc:
print(f"[yellow]Error:[/yellow] {exc}")
if __name__=="__main__":
repl()