taql 0.2.4 → 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 +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +13 -7
- data/lib/taql/cli.rb +13 -3
- data/lib/taql/table.rb +20 -9
- data/lib/taql/version.rb +1 -1
- data/lib/taql.rb +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15ee79ff7f0dca8d5b599ca3ae84c95793acdfa1e8a46d66ac0fd57acbe98298
|
4
|
+
data.tar.gz: 926398c4f0c84677638bbc15602336efbd0b5d56a96c74c5964bd51ddb750187
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 438695540cf0922a1b95138a1eaee8d98a0e618f895915b935420353759279305327f88c513a366edb5d0df29dfbdb3bbb46a527c1f78dfee74f43091bf21bba
|
7
|
+
data.tar.gz: a60d55360eafafdc8f2ecdcded369c1287cc92cd032871af1ceb251162807631148b4ab4800c80d3c25a3f0a4fbf4cdf59e7f762614f716632e28a3037d73844
|
data/CHANGELOG.md
CHANGED
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
|
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
|
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(
|
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
|
-
=>
|
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 :
|
5
|
+
attr_reader :options, :query
|
4
6
|
|
5
7
|
def initialize(argv)
|
6
|
-
@
|
8
|
+
@options = {markdown: false}
|
9
|
+
@query = parse!
|
7
10
|
end
|
8
11
|
|
9
12
|
def run
|
10
13
|
silence { require environment_path }
|
11
|
-
|
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
|
-
|
8
|
+
attr_accessor :markdown
|
9
|
+
|
10
|
+
def initialize(entries, markdown: false)
|
9
11
|
@entries = entries.map { |entry| entry.transform_values(&:to_s) }
|
12
|
+
@markdown = markdown
|
10
13
|
end
|
11
14
|
|
12
15
|
def body
|
@@ -35,10 +38,18 @@ 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
46
|
columns.map { |column| column.map(&:length).max }
|
40
47
|
end
|
41
48
|
|
49
|
+
def edge
|
50
|
+
markdown ? VERTICAL_BAR : PLUS
|
51
|
+
end
|
52
|
+
|
42
53
|
def formatted(segments)
|
43
54
|
segments.map.with_index do |segment, index|
|
44
55
|
[SPACE, segment.ljust(column_widths[index]), SPACE].join
|
@@ -48,20 +59,20 @@ module Taql
|
|
48
59
|
end
|
49
60
|
|
50
61
|
def output
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
62
|
+
[
|
63
|
+
border,
|
64
|
+
formatted(headers.map(&:upcase)),
|
65
|
+
separator,
|
66
|
+
body.map(&method(:formatted)).join("\n"),
|
67
|
+
border
|
68
|
+
].compact.join("\n")
|
58
69
|
end
|
59
70
|
|
60
71
|
def separator
|
61
72
|
columns.map.with_index do |column, index|
|
62
73
|
Array.new(column_widths[index] + 2, DASH).join
|
63
74
|
end.then do |columns|
|
64
|
-
[
|
75
|
+
[edge, columns.join(edge), edge].join
|
65
76
|
end
|
66
77
|
end
|
67
78
|
|
data/lib/taql/version.rb
CHANGED
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
|
-
|
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
|
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.
|
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:
|
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:
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
|
-
rubygems_version: 3.6.
|
54
|
+
rubygems_version: 3.6.7
|
55
55
|
specification_version: 4
|
56
56
|
summary: Tableize Rails SQL queries
|
57
57
|
test_files: []
|