activerecord-pg-format-db-structure 0.1.1 → 0.1.2
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 +4 -0
- data/README.md +1 -1
- data/lib/activerecord-pg-format-db-structure/deparser.rb +76 -18
- data/lib/activerecord-pg-format-db-structure/version.rb +1 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b2d206493754a88a3913e3fb7e3532364f2adbd0dea689226d73d9544c73e98
|
4
|
+
data.tar.gz: df009fb052c07eb578b063785ee2186bfbda7ccd758643ae52285b99c784b49b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e519793dec134c227413416d750ef8be1d3da7414c921369022fc663ae4ae75b789de632e18cd1f08cfa85e719cd87a0b2ed61336db7130e01094196fcb1823
|
7
|
+
data.tar.gz: 64558806595fdecbe27035f5c22b8f3c90b56909a5a2a19b20a1fe6cf2cdbd5dac153cf5d9d860e5ee372144cea5609751b5b1f52dd41072586830a1ae93109e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -338,7 +338,7 @@ Should be run after other operations that inline alter statements.
|
|
338
338
|
|
339
339
|
Returns an SQL string from raw PgQuery statements.
|
340
340
|
|
341
|
-
Relying mostly on `PgQuery.deparse`,
|
341
|
+
Relying mostly on `PgQuery.deparse`, and does a best effort to add some indentation where possible.
|
342
342
|
|
343
343
|
## Development
|
344
344
|
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "pg_query"
|
4
|
-
require "anbt-sql-formatter/formatter"
|
5
4
|
|
6
5
|
module ActiveRecordPgFormatDbStructure
|
7
6
|
# Returns a list of SQL strings from a list of PgQuery::RawStmt.
|
@@ -23,7 +22,7 @@ module ActiveRecordPgFormatDbStructure
|
|
23
22
|
in stmt: { alter_table_stmt: _ }
|
24
23
|
deparse_alter_table_stmt(raw_statement.stmt.alter_table_stmt)
|
25
24
|
in stmt: { select_stmt: _ }
|
26
|
-
|
25
|
+
deparse_select_stmt(raw_statement.stmt.select_stmt)
|
27
26
|
in stmt: { insert_stmt: _ }
|
28
27
|
deparse_insert_statement(raw_statement.stmt.insert_stmt)
|
29
28
|
in stmt: { create_table_as_stmt: _ }
|
@@ -41,9 +40,9 @@ module ActiveRecordPgFormatDbStructure
|
|
41
40
|
"\n#{PgQuery.deparse_stmt(stmt)};"
|
42
41
|
end
|
43
42
|
|
44
|
-
def
|
43
|
+
def deparse_select_stmt(select_stmt)
|
45
44
|
generic_query_str = +"\n\n"
|
46
|
-
generic_query_str <<
|
45
|
+
generic_query_str << deparse_leaf_select_stmt(select_stmt)
|
47
46
|
generic_query_str << ";"
|
48
47
|
end
|
49
48
|
|
@@ -126,7 +125,7 @@ module ActiveRecordPgFormatDbStructure
|
|
126
125
|
create_table_as_stmt_str << ";"
|
127
126
|
|
128
127
|
query_str = +"(\n"
|
129
|
-
query_str <<
|
128
|
+
query_str << deparse_leaf_select_stmt(stmt.query.select_stmt).gsub(/^/, PRETTY_INDENT_STRING)
|
130
129
|
query_str << "\n)"
|
131
130
|
|
132
131
|
create_table_as_stmt_str[placeholder_query_string] = query_str
|
@@ -144,7 +143,7 @@ module ActiveRecordPgFormatDbStructure
|
|
144
143
|
view_stmt_str << ";"
|
145
144
|
|
146
145
|
query_str = +"(\n"
|
147
|
-
query_str <<
|
146
|
+
query_str << deparse_leaf_select_stmt(stmt.query.select_stmt).gsub(/^/, PRETTY_INDENT_STRING)
|
148
147
|
query_str << "\n)"
|
149
148
|
|
150
149
|
view_stmt_str[placeholder_query_string] = query_str
|
@@ -161,27 +160,86 @@ module ActiveRecordPgFormatDbStructure
|
|
161
160
|
)
|
162
161
|
insert_stmt_str << "\n;"
|
163
162
|
|
164
|
-
query_str =
|
165
|
-
|
163
|
+
query_str = if insert_stmt.select_stmt.inner.values_lists.any?
|
164
|
+
deparse_values_list_select_stmt(insert_stmt.select_stmt.inner)
|
165
|
+
else
|
166
|
+
deparse_leaf_select_stmt(insert_stmt.select_stmt.inner)
|
167
|
+
end
|
166
168
|
|
167
169
|
insert_stmt_str[placeholder_query_string] = query_str
|
168
170
|
insert_stmt_str
|
169
171
|
end
|
170
172
|
|
171
|
-
def
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
173
|
+
def deparse_values_list_select_stmt(select_stmt)
|
174
|
+
values_str = +"VALUES\n "
|
175
|
+
values_str << select_stmt.values_lists.map do |values_list|
|
176
|
+
PgQuery.deparse_stmt(PgQuery::SelectStmt.new(values_lists: [values_list])).gsub(/\AVALUES /, "")
|
177
|
+
end.join("\n,")
|
178
|
+
values_str
|
177
179
|
end
|
178
180
|
|
179
|
-
def
|
180
|
-
|
181
|
+
def deparse_leaf_select_stmt(select_stmt) # rubocop:disable Metrics/PerceivedComplexity
|
182
|
+
target_list_placeholder = PgQuery::ResTarget.new(
|
183
|
+
val: { a_const: { sval: { sval: "target_list_placeholder" } } }
|
184
|
+
)
|
185
|
+
|
186
|
+
if select_stmt.with_clause
|
187
|
+
placeholder_with_clause = PgQuery::WithClause.new(
|
188
|
+
**select_stmt.with_clause.to_h,
|
189
|
+
ctes: select_stmt.with_clause.ctes.map do |cte|
|
190
|
+
PgQuery::Node.from(
|
191
|
+
PgQuery::CommonTableExpr.new(
|
192
|
+
**cte.inner.to_h,
|
193
|
+
ctequery: PgQuery::Node.from(placeholder_query_stmt("placeholder_for_#{cte.inner.ctename}_cte"))
|
194
|
+
)
|
195
|
+
)
|
196
|
+
end
|
197
|
+
)
|
198
|
+
end
|
199
|
+
|
200
|
+
select_stmt_str = PgQuery.deparse_stmt(
|
201
|
+
PgQuery::SelectStmt.new(
|
202
|
+
**select_stmt.to_h,
|
203
|
+
with_clause: placeholder_with_clause,
|
204
|
+
target_list: ([PgQuery::Node.from(target_list_placeholder)] if select_stmt.target_list.any?)
|
205
|
+
)
|
206
|
+
)
|
207
|
+
|
208
|
+
if select_stmt.target_list.any?
|
209
|
+
target_list_str = +"\n"
|
210
|
+
target_list_str << select_stmt.target_list.map do |target|
|
211
|
+
deparse_res_target(target.inner).gsub(/^/, PRETTY_INDENT_STRING)
|
212
|
+
end.join(",\n")
|
213
|
+
target_list_str << "\n"
|
214
|
+
|
215
|
+
select_stmt_str[deparse_res_target(target_list_placeholder)] = target_list_str
|
216
|
+
end
|
217
|
+
|
218
|
+
select_stmt.with_clause&.ctes&.each do |cte|
|
219
|
+
cte_str = +"\n"
|
220
|
+
cte_str << deparse_leaf_select_stmt(cte.inner.ctequery.inner).gsub(/^/, PRETTY_INDENT_STRING)
|
221
|
+
cte_str << "\n"
|
222
|
+
|
223
|
+
select_stmt_str["SELECT placeholder_for_#{cte.inner.ctename}_cte"] = cte_str
|
224
|
+
end
|
225
|
+
|
226
|
+
select_stmt_str.gsub!(/ +$/, "")
|
227
|
+
|
228
|
+
select_stmt_str
|
229
|
+
end
|
230
|
+
|
231
|
+
def deparse_res_target(res_target)
|
232
|
+
PgQuery.deparse_stmt(
|
233
|
+
PgQuery::SelectStmt.new(target_list: [PgQuery::Node.from(res_target)])
|
234
|
+
).gsub(/\ASELECT /, "")
|
235
|
+
end
|
236
|
+
|
237
|
+
def placeholder_query_string(placeholder_name = "placeholder")
|
238
|
+
PgQuery.deparse_stmt(placeholder_query_stmt(placeholder_name))
|
181
239
|
end
|
182
240
|
|
183
|
-
def placeholder_query_stmt
|
184
|
-
|
241
|
+
def placeholder_query_stmt(placeholder_name = "placeholder")
|
242
|
+
PgQuery.parse("SELECT #{placeholder_name}").tree.stmts.first.stmt.select_stmt
|
185
243
|
end
|
186
244
|
end
|
187
245
|
end
|
metadata
CHANGED
@@ -1,28 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-pg-format-db-structure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jell
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-01-
|
10
|
+
date: 2025-01-30 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
|
-
- !ruby/object:Gem::Dependency
|
13
|
-
name: anbt-sql-formatter
|
14
|
-
requirement: !ruby/object:Gem::Requirement
|
15
|
-
requirements:
|
16
|
-
- - "~>"
|
17
|
-
- !ruby/object:Gem::Version
|
18
|
-
version: '0.1'
|
19
|
-
type: :runtime
|
20
|
-
prerelease: false
|
21
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
-
requirements:
|
23
|
-
- - "~>"
|
24
|
-
- !ruby/object:Gem::Version
|
25
|
-
version: '0.1'
|
26
12
|
- !ruby/object:Gem::Dependency
|
27
13
|
name: pg_query
|
28
14
|
requirement: !ruby/object:Gem::Requirement
|