pg_conn 0.48.0 → 0.50.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 +15 -7
- 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: 7d4a247037db05fc559956f4fd4e293c4dc2fe76c57be33aed61aed6e73918d0
|
|
4
|
+
data.tar.gz: af4650f527a0866ea9b0d09e63de11ab8b666bc01e3eb2d178e6283e8ecbac9a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c26a178586d36c17b2a14d1cd1090896abf9f576c574f3616d8555bcc82ce5b434e436145c0944b0729e044df2ecb22e7923a9206443fc14625c2d8628b5c367
|
|
7
|
+
data.tar.gz: 42404a7aeec08776c601fcd29fcb1b97f7ffeddb377b1940913082bc237ed3ae4ac1b75a44997e9ae140553420d78f54f76f3ae4dfc2272457ee62910b4aa031
|
|
@@ -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'
|
|
@@ -653,7 +657,7 @@ module PgConn
|
|
|
653
657
|
|
|
654
658
|
# Return a hash from column name (a Symbol) to field value. It is an error if
|
|
655
659
|
# the query returns more than one record. It blows up if a column name is
|
|
656
|
-
# not a valid ruby symbol
|
|
660
|
+
# not a valid ruby symbol (eg. contains blanks)
|
|
657
661
|
def record(*query)
|
|
658
662
|
r = pg_exec(parse_query *query)
|
|
659
663
|
check_1r(r)
|
|
@@ -727,10 +731,10 @@ module PgConn
|
|
|
727
731
|
# of the record. If the :key_column option is defined it will be used
|
|
728
732
|
# instead of id as the key. It is an error if the id field value is not
|
|
729
733
|
# unique
|
|
730
|
-
def set(query, key_column: :id)
|
|
734
|
+
def set(*query, key_column: :id)
|
|
731
735
|
key_column = key_column.to_sym
|
|
732
736
|
keys = {}
|
|
733
|
-
r = pg_exec(query)
|
|
737
|
+
r = pg_exec(parse_query *query)
|
|
734
738
|
begin
|
|
735
739
|
r.fnumber(key_column.to_s) # Check that key column exists
|
|
736
740
|
rescue ArgumentError
|
|
@@ -750,8 +754,12 @@ module PgConn
|
|
|
750
754
|
# If there is only one remaining field then that value is used instead of a
|
|
751
755
|
# tuple. The optional +key+ argument sets the mapping field and the
|
|
752
756
|
# +symbol+ option convert key to Symbol objects when true
|
|
757
|
+
#
|
|
758
|
+
# The query is a single-argument query expression (either a full SQL query
|
|
759
|
+
# string or the name of a table/view). TODO: Make key an option so we can
|
|
760
|
+
# use full query expressions
|
|
753
761
|
def map(query, key = nil, symbol: false) # TODO Swap arguments
|
|
754
|
-
r = pg_exec(query)
|
|
762
|
+
r = pg_exec(parse_query query)
|
|
755
763
|
begin
|
|
756
764
|
key = (key || r.fname(0)).to_s
|
|
757
765
|
key_index = r.fnumber(key.to_s)
|
|
@@ -763,7 +771,7 @@ module PgConn
|
|
|
763
771
|
r.each_row { |row|
|
|
764
772
|
key_value = row.delete_at(key_index)
|
|
765
773
|
key_value = key_value.to_sym if symbol
|
|
766
|
-
!h.key?(key_value) or raise Error, "Duplicate key: #{key_value}"
|
|
774
|
+
!h.key?(key_value) or raise Error, "Duplicate key: #{key_value.inspect}"
|
|
767
775
|
h[key_value] = (one ? row.first : row)
|
|
768
776
|
}
|
|
769
777
|
h
|
|
@@ -775,7 +783,7 @@ module PgConn
|
|
|
775
783
|
# array of values if there is only one value field
|
|
776
784
|
#
|
|
777
785
|
def multimap(query, key = nil, symbol: false)
|
|
778
|
-
r = pg_exec(query)
|
|
786
|
+
r = pg_exec(parse_query query)
|
|
779
787
|
begin
|
|
780
788
|
key = (key || r.fname(0)).to_s
|
|
781
789
|
key_index = r.fnumber(key.to_s)
|
|
@@ -854,7 +862,7 @@ module PgConn
|
|
|
854
862
|
# TODO
|
|
855
863
|
# insert(table, [fields], field-name-map, object|objects)
|
|
856
864
|
# field-name-map:
|
|
857
|
-
# { database-column-name: object-method-name } # calls method on
|
|
865
|
+
# { database-column-name: object-method-name } # calls method on object
|
|
858
866
|
#
|
|
859
867
|
def insert(*args, upsert: nil, **opts)
|
|
860
868
|
# Normalize arguments
|