Skip to content
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

feat: improve CLI #435

Merged
merged 1 commit into from
Feb 29, 2024
Merged

feat: improve CLI #435

merged 1 commit into from
Feb 29, 2024

Conversation

betodealmeida
Copy link
Owner

Summary

Improve the CLI:

  • Introduce an emit_statements function, which takes lines and emit full SQL statements (handling quotes).
  • Introduce a repl function, that handles the REPL prompt and emits SQL lines.

These two simplify the CLI loop tremendously:

    for query in emit_statements(repl(session)):
        start = time.time()
        results = None
        try:
            cursor.execute(query)
            results = cursor.fetchall()
        except Error as ex:
            print(ex)
            continue

        headers = [t[0] for t in cursor.description or []]
        print(tabulate(results, headers=headers))
        duration = time.time() - start
        print(
            f"({len(results)} row{'s' if len(results) != 1 else ''} "
            f"in {duration:.2f}s)\n",
        )

    connection.close()
    print("GoodBye!")

And made it easy to introduce an TTY check so that we can call shillelagh in a pipe, in non-interactive way.

Testing instructions

@betodealmeida betodealmeida merged commit af57ef2 into main Feb 29, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant