-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgadwall.py
82 lines (62 loc) · 1.73 KB
/
gadwall.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
"""A command line client to duckdb"""
from cmd import Cmd
try:
import readline # noqa: F401
except ImportError:
pass
import duckdb
__version__ = '0.1.1'
class Gadwall(Cmd):
intro = (
'Welcome to the gadwall, a duckdb shell. '
'Type help or ? to list commands.\n'
)
prompt = 'duckdb> '
def __init__(self, file_name):
super().__init__()
self.file_name = file_name
self.conn = duckdb.connect(file_name)
def do_quit(self, arg):
"""Exit the program"""
self.conn.close()
return True
def do_EOF(self, arg):
return self.do_quit(arg)
def do_db(self, arg):
"""Show current database"""
print(self.file_name)
def do_schema(self, arg):
"""Show database or table schema"""
arg = arg.strip()
if not arg:
sql = 'PRAGMA show_tables;'
else:
sql = f"PRAGMA table_info('{arg}');"
self.default(sql)
def default(self, arg):
if not arg.strip():
return
try:
for row in self.conn.execute(arg).fetchall():
print(' '.join(str(v) for v in row))
except RuntimeError as err:
print(f'ERROR: {err}')
def emptyline(self):
# Override default of repeating last command
return
def main():
from argparse import ArgumentParser
parser = ArgumentParser(description=__doc__)
parser.add_argument(
'filename',
help='database file name (use :memory: for in-memory)',
)
args = parser.parse_args()
cmd = Gadwall(args.filename)
try:
cmd.cmdloop()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()