Skip to content

Commit

Permalink
Add hyperapi-cli script to interactively run SQL commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alendit committed Nov 7, 2022
1 parent 25d19c5 commit f196f9d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
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.DO_NOT_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()

0 comments on commit f196f9d

Please sign in to comment.