pg_conn 0.47.0 → 0.49.0
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/lib/pg_conn/schema_methods.rb +12 -5
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +16 -4
- 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: c017287998b7b7c473e0532c44ff8f209d322158c6ef79b97a462270cc1d10ef
|
|
4
|
+
data.tar.gz: 6939299127cda711bf3fd1c0c349737c9f943c026abc13885bb9e699ce6607b0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 01d12b622790f3310a94d1c5191d811a9fb481d488b55f6e00018fe704ec6a9916b67ad5998c15977bc21d29792be96b82f81bcfc90c06100f7af2b1df967d71
|
|
7
|
+
data.tar.gz: 80e0c6c6e7825c949c5f23d3c98e8bc49ddf0653bba13bf666b0816d0e2656b788ea37af52d9009914a961cb197035dbd4643a9ad65fd9c9ccf76b9c46088f63
|
|
@@ -116,6 +116,11 @@ module PgConn
|
|
|
116
116
|
conn.tuples column_list_type_query(schema, relation)
|
|
117
117
|
end
|
|
118
118
|
|
|
119
|
+
# Return type of the given column or nil if not found
|
|
120
|
+
def list_column_type(schema, relation = nil, column)
|
|
121
|
+
conn.tuples(column_list_type_query(schema, relation, column)).first&.last
|
|
122
|
+
end
|
|
123
|
+
|
|
119
124
|
def exist_function(schema, function, signature)
|
|
120
125
|
raise NotImplementedError
|
|
121
126
|
end
|
|
@@ -203,7 +208,7 @@ module PgConn
|
|
|
203
208
|
end
|
|
204
209
|
|
|
205
210
|
def column_list_query(schema, relation)
|
|
206
|
-
|
|
211
|
+
relation_expr = relation ? "relname = '#{relation}'" : nil
|
|
207
212
|
[
|
|
208
213
|
%(
|
|
209
214
|
select '#{schema}' || '.' || c.relname || '.' || a.attname
|
|
@@ -212,12 +217,13 @@ module PgConn
|
|
|
212
217
|
where relnamespace::regnamespace::text = '#{schema}'
|
|
213
218
|
and a.attnum > 0
|
|
214
219
|
),
|
|
215
|
-
|
|
220
|
+
relation_expr
|
|
216
221
|
].compact.join(" and ")
|
|
217
222
|
end
|
|
218
223
|
|
|
219
|
-
def column_list_type_query(schema, relation)
|
|
220
|
-
|
|
224
|
+
def column_list_type_query(schema, relation, column = nil)
|
|
225
|
+
column_expr = column && "a.attname = '#{column}'"
|
|
226
|
+
relation_expr = relation ? "relname = '#{relation}'" : nil
|
|
221
227
|
[
|
|
222
228
|
%(
|
|
223
229
|
select '#{schema}' || '.' || c.relname || '.' || a.attname as "column",
|
|
@@ -227,7 +233,8 @@ module PgConn
|
|
|
227
233
|
where relnamespace::regnamespace::text = '#{schema}'
|
|
228
234
|
and a.attnum > 0
|
|
229
235
|
),
|
|
230
|
-
|
|
236
|
+
column_expr,
|
|
237
|
+
relation_expr
|
|
231
238
|
].compact.join(" and ")
|
|
232
239
|
end
|
|
233
240
|
end
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
|
@@ -53,6 +53,10 @@ module PgConn
|
|
|
53
53
|
# true/false, Time/Date/DateTime, and arrays. Other types may require
|
|
54
54
|
# special handling
|
|
55
55
|
#
|
|
56
|
+
# Arrays are quoted using the Array constructor (Array[...]) so the quoted
|
|
57
|
+
# string can't be used in 'VALUE in (LIST)' constructs, use #quote_values
|
|
58
|
+
# instead
|
|
59
|
+
#
|
|
56
60
|
# Hashes are quoted as a literal JSON expression converted into the given
|
|
57
61
|
# :json_type. If :json_type is nil, it is the application's responsibility to
|
|
58
62
|
# cast the value to either 'json' or 'jsonb'
|
|
@@ -94,6 +98,12 @@ module PgConn
|
|
|
94
98
|
Literal.new values.map { |value| quote_value(value, **opts) }.join(", ")
|
|
95
99
|
end
|
|
96
100
|
|
|
101
|
+
# Quote array as a row - '(value, value, ...)'
|
|
102
|
+
def self.quote_row(row, **opts) = quote_list(row, **opts)
|
|
103
|
+
|
|
104
|
+
# Quote values as a list of rows - '(value, value), (value, value), ...'
|
|
105
|
+
def self.quote_rows(rows, **opts) = rows.map { quote_list(_1, **opts) }.join(', ')
|
|
106
|
+
|
|
97
107
|
# Quote an array of values as a tuple. The element types should be in the
|
|
98
108
|
# same order as the array arguments. #quote_tuples is same as #quote_values
|
|
99
109
|
# except the values may have different types (this makes no difference
|
|
@@ -113,7 +123,7 @@ module PgConn
|
|
|
113
123
|
Literal.new tuples.map { |tuple| "(#{quote_tuple(tuple, **opts)})" }.join(", ")
|
|
114
124
|
end
|
|
115
125
|
|
|
116
|
-
# Quote array elements as a comma separated list of values and enclosed
|
|
126
|
+
# Quote array elements as a comma separated list of values and enclosed in
|
|
117
127
|
# parenthesis. Eg. [1, 2] => "(1, 2)"
|
|
118
128
|
def self.quote_list(array, **opts) = quote_tuples([array], **opts)
|
|
119
129
|
|
|
@@ -471,6 +481,8 @@ module PgConn
|
|
|
471
481
|
def quote_identifiers(idents) = PgConn.quote_identifiers(idents)
|
|
472
482
|
def quote_value(value, **opts) = PgConn.quote_value(value, json_type: self.default_json_type, **opts)
|
|
473
483
|
def quote_values(values, **opts) = PgConn.quote_values(values, json_type: self.default_json_type, **opts)
|
|
484
|
+
def quote_row(row, **opts) = PgConn.quote_row(row, json_type: self.default_json_type, **opts)
|
|
485
|
+
def quote_rows(rows, **opts) = PgConn.quote_rows(rows, json_type: self.default_json_type, **opts)
|
|
474
486
|
def quote_tuple(tuple, **opts) = PgConn.quote_tuple(tuple, json_type: self.default_json_type, **opts)
|
|
475
487
|
def quote_tuples(tuples, **opts) = PgConn.quote_tuples(tuples, json_type: self.default_json_type, **opts)
|
|
476
488
|
|
|
@@ -645,7 +657,7 @@ module PgConn
|
|
|
645
657
|
|
|
646
658
|
# Return a hash from column name (a Symbol) to field value. It is an error if
|
|
647
659
|
# the query returns more than one record. It blows up if a column name is
|
|
648
|
-
# not a valid ruby symbol
|
|
660
|
+
# not a valid ruby symbol (eg. contains blanks)
|
|
649
661
|
def record(*query)
|
|
650
662
|
r = pg_exec(parse_query *query)
|
|
651
663
|
check_1r(r)
|
|
@@ -755,7 +767,7 @@ module PgConn
|
|
|
755
767
|
r.each_row { |row|
|
|
756
768
|
key_value = row.delete_at(key_index)
|
|
757
769
|
key_value = key_value.to_sym if symbol
|
|
758
|
-
!h.key?(key_value) or raise Error, "Duplicate key: #{key_value}"
|
|
770
|
+
!h.key?(key_value) or raise Error, "Duplicate key: #{key_value.inspect}"
|
|
759
771
|
h[key_value] = (one ? row.first : row)
|
|
760
772
|
}
|
|
761
773
|
h
|
|
@@ -846,7 +858,7 @@ module PgConn
|
|
|
846
858
|
# TODO
|
|
847
859
|
# insert(table, [fields], field-name-map, object|objects)
|
|
848
860
|
# field-name-map:
|
|
849
|
-
# { database-column-name: object-method-name } # calls method on
|
|
861
|
+
# { database-column-name: object-method-name } # calls method on object
|
|
850
862
|
#
|
|
851
863
|
def insert(*args, upsert: nil, **opts)
|
|
852
864
|
# Normalize arguments
|