forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapd_cli.py
executable file
·117 lines (94 loc) · 3.12 KB
/
mapd_cli.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
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
#!/usr/bin/env python
import readline
import rlcompleter
import logging
import sys
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import TSSLSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol
from mapd import MapD
from mapd.ttypes import *
LOG_FILENAME = '/tmp/mapd_completer.log'
logging.basicConfig(filename=LOG_FILENAME,
level=logging.DEBUG,
)
class SimpleCompleter(object):
def __init__(self, options):
self.options = sorted(options)
return
def complete(self, text, state):
response = None
if state == 0:
# This is the first time for this text, so build a match list.
if text:
self.matches = [s
for s in self.options
if s and s.startswith(text)]
logging.debug('%s matches: %s', repr(text), self.matches)
else:
self.matches = self.options[:]
logging.debug('(empty input) matches: %s', self.matches)
# Return the state'th item from the match list,
# if we have that many.
try:
response = self.matches[state]
except IndexError:
response = None
logging.debug('complete(%s, %s) => %s',
repr(text), state, repr(response))
return response
def str_row(row):
return '|'.join([
str(col_val.datum.int_val) if col_val.type == TDatumType.INT else
str(col_val.datum.real_val) if col_val.type == TDatumType.REAL else
col_val.datum.str_val for col_val in row ])
def input_loop(client):
while True:
try:
line = raw_input('mapd> ')
if line == 'quit':
return
query_result = client.select(line)
except MapDException as ex:
print ex.error_msg
continue
for row in query_result.rows:
print str_row(row)
if len(sys.argv) <= 1 or sys.argv[1] == '--help':
print('')
print('Usage: ' + sys.argv[0] + ' [-h host[:port]] [-vi]')
print('')
sys.exit(0)
host = 'localhost'
port = 9090
argi = 1
if sys.argv[argi] == '-h':
parts = sys.argv[argi+1].split(':')
host = parts[0]
if len(parts) > 1:
port = int(parts[1])
argi += 2
vi_mode = False
if argi < len(sys.argv) and (sys.argv[argi] == '-v' or sys.argv[argi] == '-vi'):
vi_mode = True
socket = TSocket.TSocket(host, port)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = MapD.Client(protocol)
transport.open()
# Register our completer function
readline.set_completer(SimpleCompleter([
'SELECT', 'FROM', 'WHERE', 'CREATE', 'TABLE',
'COUNT', 'DESC', 'DISTINCT', 'MIN', 'MAX', 'AVG',
'ORDER BY']).complete)
# Use the tab key for completion
if 'libedit' in readline.__doc__:
readline.parse_and_bind('bind ^I rl_complete')
else:
readline.parse_and_bind('tab: complete')
if vi_mode:
readline.parse_and_bind('set editing-mode vi')
# Prompt the user for text
input_loop(client)