-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
144 lines (119 loc) · 3.82 KB
/
Copy pathcli.py
File metadata and controls
144 lines (119 loc) · 3.82 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import configparser
import sqlite3
import time
import uuid
from duoquest.tsq import TableSketchQuery
def input_db_name(conn):
while True:
db_name = input('Database name (default: concert_singer) > ')
if not db_name:
db_name = 'concert_singer'
cur = conn.cursor()
cur.execute('SELECT 1 FROM databases WHERE name = ?', (db_name,))
if cur.fetchone():
break
else:
print(f'<{db_name}> is not a valid database.')
return db_name
def input_nlq():
nlq = input('NLQ (default: How many singers are there?)> ')
if not nlq:
nlq = 'How many singers are there?'
return nlq
def input_num_cols():
while True:
num_cols = input('Number of columns > ')
try:
num_cols = int(num_cols)
break
except Exception as e:
print('Number of columns should be integer!')
return num_cols
def input_order():
ordered = False
while True:
order_input = input('Should results be ordered? (y/n) > ')
if order_input == 'y':
ordered = True
break
elif order_input == 'n':
break
else:
print('y/n only!')
return ordered
def input_limit():
limit = None
while True:
limit_input = input('Limit results to n tuples? (int or blank) > ')
if not limit_input:
break
try:
limit = int(limit_input)
break
except Exception as e:
print('int or blank only!')
return limit
def input_tsq_types(num_cols):
while True:
types_input = input('Types (`text` or `number`, comma separated)> ')
types = list(map(lambda x: x.strip(), types_input.split(',')))
if any(map(lambda x: x not in ('text', 'number'), types)):
print('Types must be `text` or `number`')
continue
if len(types) != num_cols:
print('Number of types must match number of columns.')
continue
break
return types
def input_tsq_row_count():
tsq_row_count = 0
while True:
tsq_row_count_input = input('Number of TSQ rows (int) > ')
try:
tsq_row_count = int(tsq_row_count_input)
break
except Exception as e:
print('int only!')
return tsq_row_count
def input_tsq_row(row_num, tsq_types):
while True:
row_input = input(f'Row {row_num} (semicolon-separated values) > ')
tsq_row = list(map(lambda x: x.strip(), row_input.split(';')))
validated = True
for i, cell in enumerate(tsq_row):
if tsq_types[i] == 'number':
try:
float(cell)
except Exception as e:
print('At least one cell value is invalid.')
validated = False
break
if validated:
break
return tsq_row
def main():
config = configparser.ConfigParser()
config.read('config.ini')
db_path = config['db']['path']
conn = sqlite3.connect(db_path)
db_name = input_db_name(conn)
nlq = input_nlq()
num_cols = input_num_cols()
tsq = TableSketchQuery(num_cols)
tsq.types = input_tsq_types(num_cols)
tsq_row_count = input_tsq_row_count()
for i in range(tsq_row_count):
tsq.values.append(input_tsq_row(i+1, tsq.types))
tsq.order = input_order()
tsq.limit = input_limit()
print(tsq.to_proto())
cur = conn.cursor()
cur.execute('''INSERT INTO tasks (tid, db, nlq, tsq_proto, status, time)
VALUES (?, ?, ?, ?, ?, ?)''',
(str(uuid.uuid4()), db_name, nlq,
tsq.to_proto().SerializeToString(), 'waiting',
int(time.time())))
conn.commit()
conn.close()
if __name__ == '__main__':
main()