Taql

Taql is a lightweight Ruby gem that provides a convenient interface for executing SQL queries with beautifully formatted results. Whether you need a command-line SQL client for quick database exploration or a programmatic way to run queries within your Ruby application, Taql has you covered.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add taql

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install taql

Usage

~/Developer/writebook % taql "select id, title, author, slug from books"
+----+----------------------+-----------+----------------------+
| ID | TITLE                | AUTHOR    | SLUG                 |
+----+----------------------+-----------+----------------------+
| 1  | The Writebook Manual | 37signals | the-writebook-manual |
+----+----------------------+-----------+----------------------+

Use the --markdown (or -m) flag to generate Markdown output:

~/Developer/check-in % taql --markdown "select id, first_name, last_name, created_at, updated_at from guests limit 3"
| ID | FIRST_NAME | LAST_NAME  | CREATED_AT              | UPDATED_AT              |
|----|------------|------------|-------------------------|-------------------------|
| 1  | Alejandro  | Bustamante | 2023-07-26 16:26:21 UTC | 2023-07-26 16:26:21 UTC |
| 2  | Reina      | Zelaya     | 2023-07-26 16:26:21 UTC | 2023-07-26 16:26:21 UTC |
| 3  | Carla      | Barrios    | 2023-07-26 16:26:21 UTC | 2023-07-26 16:26:21 UTC |

Any valid SQL SELECT statement can be executed:

~/Developer/check-in % taql "select count(id) as guest_count from guests"
+-------------+
| GUEST_COUNT |
+-------------+
| 3721        |
+-------------+

Within a console:

>> Taql.execute("select id, email from users order by created at limit 3").pluck("email")
   (1.2ms)  select id, email from users limit 3
+----+---------------------+
| ID | EMAIL               |
+----+---------------------+
| 1  | [email protected]   |
| 2  | [email protected]     |
| 3  | [email protected] |
+----+---------------------+
=> ["[email protected]", "[email protected]", "[email protected]"]

The return value is a native PG::Result object, which supports mapping or extracting data as shown in the example above.

>> Taql.execute("select * from schema_migrations limit 3")
   (0.7ms)  select * from schema_migrations limit 3
+----------------+
| VERSION        |
+----------------+
| 20240819175830 |
| 20240815131806 |
| 20240815131747 |
+----------------+
=> #<PG::Result:0x000000012ebf6a38 status=PGRES_TUPLES_OK ntuples=3 nfields=1 cmd_tuples=3>

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Releasing

To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/taql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Taql project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.