Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Community-Supported/hyperapi-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# hyperapi-cli
## An interactive HyperAPI SQL cli

This script allows you to interactively execute SQL commands via HyperAPI.

## Usage

```bash
./hyperapi-cli.py [optional hyper database file]
```
36 changes: 36 additions & 0 deletions Community-Supported/hyperapi-cli/hyperapi-cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
import readline
from argparse import ArgumentParser
from tableauhyperapi import HyperProcess, Connection, Telemetry, CreateMode, HyperException


def main():
parser = ArgumentParser("HyperAPI interactive cli.")
parser.add_argument("database", type=str, nargs='?',
help="A Hyper file to attach on startup")

args = parser.parse_args()
create_mode = CreateMode.CREATE_IF_NOT_EXISTS if args.database else CreateMode.NONE

with HyperProcess(Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper_process:
try:
with Connection(hyper_process.endpoint, args.database, create_mode) as connection:
while True:
try:
sql = input("> ")
except (EOFError, KeyboardInterrupt):
return
try:
with connection.execute_query(sql) as result:
print("\t".join(str(column.name)
for column in result.schema.columns))
for row in result:
print("\t".join(str(column) for column in row))
except HyperException as exception:
print(f"Error executing SQL: {exception}")
except HyperException as exception:
print(f"Unable to connect to the database: {exception}")


if __name__ == "__main__":
main()