-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hyperapi-cli script to interactively run SQL commands
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |