Skip to content

Add hyperapi-cli script to interactively run SQL commands #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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()