taql 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 106f6fd29eb626394dfcae398aaf2b79696574c3828b313ca528c60686fb7891
4
- data.tar.gz: b578ac768cd8c7c8aa1e588619868d96c77eab2a67387fb254947b076243bd34
3
+ metadata.gz: 15ee79ff7f0dca8d5b599ca3ae84c95793acdfa1e8a46d66ac0fd57acbe98298
4
+ data.tar.gz: 926398c4f0c84677638bbc15602336efbd0b5d56a96c74c5964bd51ddb750187
5
5
  SHA512:
6
- metadata.gz: 13b105a6407b067a0de7200f5fe2f650a877c6a98ef413962c5278fa84154867aec5960e14a56ab90d5c8a3cd4ca9448202a77eb9568282efca1a7192519acd8
7
- data.tar.gz: 95a82e535d052293134f90d4b1827da6d1eb88bf999aae439b64bfcab40c08e7db5f9959792c02e8862a9bbf60eda8886f2ce0e3fc240f4a72d90da928dbb76a
6
+ metadata.gz: 438695540cf0922a1b95138a1eaee8d98a0e618f895915b935420353759279305327f88c513a366edb5d0df29dfbdb3bbb46a527c1f78dfee74f43091bf21bba
7
+ data.tar.gz: a60d55360eafafdc8f2ecdcded369c1287cc92cd032871af1ceb251162807631148b4ab4800c80d3c25a3f0a4fbf4cdf59e7f762614f716632e28a3037d73844
data/CHANGELOG.md CHANGED
@@ -1,6 +1,17 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased]
3
+ ## [0.2.5] - 2025-05-17
4
+
5
+ - Added support for Markdown
6
+ - Inject connection
7
+ - Add irb to Gemfile
8
+ - Update README
9
+
10
+ ## [0.2.4] - 2025-04-17
11
+
12
+ - Add back missing alias
13
+ - Add back support for older rubies
14
+ - Map with index
4
15
 
5
16
  ## [0.2.3] - 2025-04-17
6
17
 
data/README.md CHANGED
@@ -27,19 +27,21 @@ If bundler is not being used to manage dependencies, install the gem by executin
27
27
  +----+----------------------+-----------+----------------------+
28
28
  ```
29
29
 
30
+ Use the `--markdown` (or `-m`) flag to generate Markdown output:
31
+
30
32
  ```sh
31
- ~/Developer/check-in (main) % taql "select id, first_name, last_name, created_at, updated_at from guests limit 3"
32
- +----+------------+------------+-------------------------+-------------------------+
33
+ ~/Developer/check-in % taql --markdown "select id, first_name, last_name, created_at, updated_at from guests limit 3"
33
34
  | ID | FIRST_NAME | LAST_NAME | CREATED_AT | UPDATED_AT |
34
- +----+------------+------------+-------------------------+-------------------------+
35
+ |----|------------|------------|-------------------------|-------------------------|
35
36
  | 1 | Alejandro | Bustamante | 2023-07-26 16:26:21 UTC | 2023-07-26 16:26:21 UTC |
36
37
  | 2 | Reina | Zelaya | 2023-07-26 16:26:21 UTC | 2023-07-26 16:26:21 UTC |
37
38
  | 3 | Carla | Barrios | 2023-07-26 16:26:21 UTC | 2023-07-26 16:26:21 UTC |
38
- +----+------------+------------+-------------------------+-------------------------+
39
39
  ```
40
40
 
41
+ Any valid SQL SELECT statement can be executed:
42
+
41
43
  ```sh
42
- ~/Developer/check-in (main|!|+) % taql "select count(id) as guest_count from guests"
44
+ ~/Developer/check-in % taql "select count(id) as guest_count from guests"
43
45
  +-------------+
44
46
  | GUEST_COUNT |
45
47
  +-------------+
@@ -47,8 +49,10 @@ If bundler is not being used to manage dependencies, install the gem by executin
47
49
  +-------------+
48
50
  ```
49
51
 
52
+ Within a console:
53
+
50
54
  ```ruby
51
- >> Taql.execute('select id, email from users order by created at limit 3').pluck("email")
55
+ >> Taql.execute("select id, email from users order by created at limit 3").pluck("email")
52
56
  (1.2ms) select id, email from users limit 3
53
57
  +----+---------------------+
54
58
  | ID | EMAIL |
@@ -60,6 +64,8 @@ If bundler is not being used to manage dependencies, install the gem by executin
60
64
  => ["alice@example.com", "bob@example.com", "charlie@example.com"]
61
65
  ```
62
66
 
67
+ The return value is a native PG::Result object, which supports mapping or extracting data as shown in the example above.
68
+
63
69
  ```ruby
64
70
  >> Taql.execute("select * from schema_migrations limit 3")
65
71
  (0.7ms) select * from schema_migrations limit 3
@@ -70,7 +76,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
70
76
  | 20240815131806 |
71
77
  | 20240815131747 |
72
78
  +----------------+
73
- => #<PG::Result:0x000000012ebf6a38 status=PGRES_TUPLES_OK ntuples=3 nfields=1 cmd_tuples=3>
79
+ => \#<PG::Result:0x000000012ebf6a38 status=PGRES_TUPLES_OK ntuples=3 nfields=1 cmd_tuples=3>
74
80
  ```
75
81
 
76
82
  ## Development
data/lib/taql/cli.rb CHANGED
@@ -1,14 +1,18 @@
1
+ require "optparse"
2
+
1
3
  module Taql
2
4
  class Cli
3
- attr_reader :argv
5
+ attr_reader :options, :query
4
6
 
5
7
  def initialize(argv)
6
- @argv = argv
8
+ @options = {markdown: false}
9
+ @query = parse!
7
10
  end
8
11
 
9
12
  def run
10
13
  silence { require environment_path }
11
- Taql.execute(*argv)
14
+
15
+ Taql.execute(*query, options)
12
16
  end
13
17
 
14
18
  private
@@ -17,6 +21,12 @@ module Taql
17
21
  File.expand_path("config/environment", Dir.pwd)
18
22
  end
19
23
 
24
+ def parse!
25
+ OptionParser.new do |parser|
26
+ parser.on("-m", "--markdown", TrueClass, "Output table in Markdown")
27
+ end.parse!(into: options)
28
+ end
29
+
20
30
  def silence
21
31
  original_stdout = $stdout.dup
22
32
  $stdout.reopen(IO::NULL)
data/lib/taql/table.rb CHANGED
@@ -5,8 +5,11 @@ module Taql
5
5
  SPACE = " ".freeze
6
6
  VERTICAL_BAR = "|".freeze
7
7
 
8
- def initialize(entries)
9
- @entries = entries.map { it.transform_values(&:to_s) }
8
+ attr_accessor :markdown
9
+
10
+ def initialize(entries, markdown: false)
11
+ @entries = entries.map { |entry| entry.transform_values(&:to_s) }
12
+ @markdown = markdown
10
13
  end
11
14
 
12
15
  def body
@@ -35,38 +38,44 @@ module Taql
35
38
 
36
39
  attr_reader :entries
37
40
 
41
+ def border
42
+ separator unless markdown
43
+ end
44
+
38
45
  def column_widths
39
- columns.map { it.map(&:length).max }
46
+ columns.map { |column| column.map(&:length).max }
47
+ end
48
+
49
+ def edge
50
+ markdown ? VERTICAL_BAR : PLUS
40
51
  end
41
52
 
42
53
  def formatted(segments)
43
- segments.map do |segment|
44
- cell_index = segments.index(segment)
45
- max_length = column_widths[cell_index]
46
- [SPACE, segment.ljust(max_length), SPACE].join
54
+ segments.map.with_index do |segment, index|
55
+ [SPACE, segment.ljust(column_widths[index]), SPACE].join
47
56
  end.then do |segments|
48
57
  [VERTICAL_BAR, segments.join(VERTICAL_BAR), VERTICAL_BAR].join
49
58
  end
50
59
  end
51
60
 
52
61
  def output
53
- <<~OUTPUT
54
- #{separator}
55
- #{formatted(headers.map(&:upcase))}
56
- #{separator}
57
- #{body.map(&method(:formatted)).join("\n")}
58
- #{separator}
59
- OUTPUT
62
+ [
63
+ border,
64
+ formatted(headers.map(&:upcase)),
65
+ separator,
66
+ body.map(&method(:formatted)).join("\n"),
67
+ border
68
+ ].compact.join("\n")
60
69
  end
61
70
 
62
71
  def separator
63
- columns.map do |column|
64
- column_index = columns.index(column)
65
- max_length = column_widths[column_index]
66
- Array.new(max_length + 2, DASH).join
72
+ columns.map.with_index do |column, index|
73
+ Array.new(column_widths[index] + 2, DASH).join
67
74
  end.then do |columns|
68
- [PLUS, columns.join(PLUS), PLUS].join
75
+ [edge, columns.join(edge), edge].join
69
76
  end
70
77
  end
78
+
79
+ alias_method :to_s, :print
71
80
  end
72
81
  end
data/lib/taql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Taql
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.5"
3
3
  end
data/lib/taql.rb CHANGED
@@ -5,10 +5,10 @@ require_relative "taql/version"
5
5
  module Taql
6
6
  class Error < StandardError; end
7
7
 
8
- def self.execute(query)
9
- ActiveRecord::Base.connection.execute(query).tap do |result|
8
+ def self.execute(query, options, connection: ActiveRecord::Base.connection)
9
+ connection.execute(query).tap do |result|
10
10
  if (results = result.entries).any?
11
- $stdout.puts Table.new(results)
11
+ $stdout.puts Table.new(results, markdown: options[:markdown])
12
12
  end
13
13
  end
14
14
  end
data/taql.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.description = "Taql allows you to pretty print SQL table queries in Rails."
11
11
  spec.homepage = "https://github.com/arzezak/taql"
12
12
  spec.license = "MIT"
13
- spec.required_ruby_version = ">= 3.4"
13
+ spec.required_ruby_version = ">= 3.0.0"
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
16
  spec.metadata["source_code_uri"] = spec.homepage
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Rzezak
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-17 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Taql allows you to pretty print SQL table queries in Rails.
13
13
  email:
@@ -44,14 +44,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.4'
47
+ version: 3.0.0
48
48
  required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  requirements:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 3.6.5
54
+ rubygems_version: 3.6.7
55
55
  specification_version: 4
56
56
  summary: Tableize Rails SQL queries
57
57
  test_files: []