annotato 0.1.13 → 0.1.15
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/Gemfile.lock +1 -1
- data/lib/annotato/index_formatter.rb +72 -1
- data/lib/annotato/line_formatter.rb +1 -1
- data/lib/annotato/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c9018e17dc7b82a75e12b7e43cf5845abf9d397512cfc987aaf40dc4c70af045
|
|
4
|
+
data.tar.gz: ba4b35b5f17dbeebe540e32bc5bbfa4b124667915072bc73d6690429b5e3777f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2d573afafb8041a325f75eca8cf1ee9396520e380d28cd5a293b375e17b60f9bb872525c99a7cf0df7a54da402d5a89cadb36ec5cefbdd6906340f8bcae49a40
|
|
7
|
+
data.tar.gz: 0753e255496f9f03ec1d91ab54317ff435d2133d9e0dee7fa285c30bdef6a8692abe4a79a7d2412784436ec2696bed060feb30c5c0efde384ffcc75ce9c08273
|
data/Gemfile.lock
CHANGED
|
@@ -2,13 +2,84 @@
|
|
|
2
2
|
|
|
3
3
|
module Annotato
|
|
4
4
|
class IndexFormatter
|
|
5
|
+
MAX_LINE = 100
|
|
6
|
+
|
|
5
7
|
def self.format(conn, table_name)
|
|
6
8
|
conn.indexes(table_name).map do |idx|
|
|
7
9
|
cols_list = Array(idx.columns).join(',')
|
|
8
10
|
unique_clause = idx.unique ? " unique" : ""
|
|
9
|
-
|
|
11
|
+
|
|
12
|
+
where_clause = ""
|
|
13
|
+
if idx.where
|
|
14
|
+
where_clause = "\n" + wrap_where(idx.where, max_len: MAX_LINE)
|
|
15
|
+
end
|
|
16
|
+
|
|
10
17
|
"# #{idx.name} (#{cols_list})#{unique_clause}#{where_clause}"
|
|
11
18
|
end
|
|
12
19
|
end
|
|
20
|
+
|
|
21
|
+
# Produces one or more lines, each already prefixed with "# "
|
|
22
|
+
# First line: "# where (..."
|
|
23
|
+
# Next lines: "# ..." (aligned under the "(")
|
|
24
|
+
def self.wrap_where(where_sql, max_len:)
|
|
25
|
+
first_prefix = "# where ("
|
|
26
|
+
cont_prefix = "# " # aligns under the "(" after "where "
|
|
27
|
+
|
|
28
|
+
text = where_sql.to_s
|
|
29
|
+
lines = []
|
|
30
|
+
|
|
31
|
+
# If it already fits, keep it one-liner
|
|
32
|
+
if (first_prefix.length + text.length + 1) <= max_len
|
|
33
|
+
return "#{first_prefix}#{text})"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
remaining = text.dup
|
|
37
|
+
current_prefix = first_prefix
|
|
38
|
+
|
|
39
|
+
while remaining.length > 0
|
|
40
|
+
available = max_len - current_prefix.length - 1 # -1 for closing ")" on last line (or just safety)
|
|
41
|
+
available = 20 if available < 20
|
|
42
|
+
|
|
43
|
+
if remaining.length <= available
|
|
44
|
+
lines << "#{current_prefix}#{remaining}"
|
|
45
|
+
remaining = ""
|
|
46
|
+
break
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Prefer breaking on logical operators within the window
|
|
50
|
+
window = remaining[0, available]
|
|
51
|
+
cut =
|
|
52
|
+
window.rindex(" AND ") ||
|
|
53
|
+
window.rindex(" OR ") ||
|
|
54
|
+
window.rindex(", ") ||
|
|
55
|
+
window.rindex(") ") ||
|
|
56
|
+
window.rindex(" ") # last resort: whitespace
|
|
57
|
+
|
|
58
|
+
# If we found a breakpoint, include it in the line (so operators stay visible)
|
|
59
|
+
if cut
|
|
60
|
+
# If we’re cutting on AND/OR/comma, keep that token at the end of the line when possible
|
|
61
|
+
if window[cut, 5] == " AND " || window[cut, 4] == " OR "
|
|
62
|
+
cut += (window[cut, 5] == " AND " ? 5 : 4)
|
|
63
|
+
elsif window[cut, 2] == ", "
|
|
64
|
+
cut += 2
|
|
65
|
+
elsif window[cut, 2] == ") "
|
|
66
|
+
cut += 2
|
|
67
|
+
else
|
|
68
|
+
cut += 1
|
|
69
|
+
end
|
|
70
|
+
else
|
|
71
|
+
cut = available
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
lines << "#{current_prefix}#{remaining[0, cut].rstrip}"
|
|
75
|
+
remaining = remaining[cut..].to_s.lstrip
|
|
76
|
+
current_prefix = cont_prefix
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Close the paren on the last line
|
|
80
|
+
lines[-1] = "#{lines[-1]})"
|
|
81
|
+
|
|
82
|
+
lines.join("\n")
|
|
83
|
+
end
|
|
13
84
|
end
|
|
14
85
|
end
|
data/lib/annotato/version.rb
CHANGED