Skip to content

Commit 2e3ab11

Browse files
committed
Add hyperapi-cli script to interactively run SQL commands
1 parent 25d19c5 commit 2e3ab11

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# hyperapi-cli
2+
## An interactive HyperAPI SQL cli
3+
4+
This script allows you to interactively execute SQL commands via HyperAPI.
5+
6+
## Usage
7+
8+
```bash
9+
./hyperapi-cli.py [optional hyper database file]
10+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
import readline
3+
from argparse import ArgumentParser
4+
from tableauhyperapi import HyperProcess, Connection, Telemetry, CreateMode, HyperException
5+
6+
7+
def main():
8+
parser = ArgumentParser("HyperAPI interactive cli.")
9+
parser.add_argument("database", type=str, nargs='?',
10+
help="A Hyper file to attach on startup")
11+
12+
args = parser.parse_args()
13+
create_mode = CreateMode.CREATE_IF_NOT_EXISTS if args.database else CreateMode.NONE
14+
15+
with HyperProcess(Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper_process:
16+
try:
17+
with Connection(hyper_process.endpoint, args.database, create_mode) as connection:
18+
while True:
19+
try:
20+
sql = input("> ")
21+
except (EOFError, KeyboardInterrupt):
22+
return
23+
try:
24+
with connection.execute_query(sql) as result:
25+
print("\t".join(str(column.name)
26+
for column in result.schema.columns))
27+
for row in result:
28+
print("\t".join(str(column) for column in row))
29+
except HyperException as exception:
30+
print(f"Error executing SQL: {exception}")
31+
except HyperException as exception:
32+
print(f"Unable to connect to the database: {exception}")
33+
34+
35+
if __name__ == "__main__":
36+
main()

0 commit comments

Comments
 (0)