pg_conn 0.44.0 → 0.46.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/version.rb +1 -1
- data/lib/pg_conn.rb +79 -54
- 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: bc9f98c5804eb386c76e607d056c12ead0e44feb0df8dcabb85b3c057d824868
|
|
4
|
+
data.tar.gz: d0318211117d792acb5936b6d2b10557548fe491a030898cfc7ed55dfd901ad5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6e2845bab10f1ff11373d43dfda7ad4650c38eff52fc6f874450fd4a9a294bd290be36d9922260ae2d1429bb218f62116b4a15eb521bd48ceaaa4381ac9685e1
|
|
7
|
+
data.tar.gz: c9d413eabf917f15c64dfd2165229203ebc3ca1dbef32ea42858934812dffd1ce257abe821c9a58fa864feaa26348fc76c86980599dad9287f0fc5aa44660cb6
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
|
@@ -498,7 +498,7 @@ module PgConn
|
|
|
498
498
|
quote_record_impl(data, schema_name, type, array: true, **opts)
|
|
499
499
|
end
|
|
500
500
|
|
|
501
|
-
# Return current search path. Note that
|
|
501
|
+
# Return current search path. Note that the search path is part of
|
|
502
502
|
# the transaction
|
|
503
503
|
def search_path
|
|
504
504
|
self.value("show search_path").split(/,\s*/) - %w("$user" pg_temp)
|
|
@@ -832,11 +832,11 @@ module PgConn
|
|
|
832
832
|
end
|
|
833
833
|
|
|
834
834
|
# :call-seq:
|
|
835
|
-
# insert(table, record|records)
|
|
836
|
-
# insert(table, fields, record|records|tuples|values)
|
|
835
|
+
# insert(table, struct|structs|record|records)
|
|
836
|
+
# insert(table, fields, struct|structs|record|records|tuples|values)
|
|
837
837
|
#
|
|
838
|
-
# insert(schema, table, record|records)
|
|
839
|
-
# insert(schema, table, fields, record|records|tuples|values)
|
|
838
|
+
# insert(schema, table, struct|structs|record|records)
|
|
839
|
+
# insert(schema, table, fields, struct|structs|record|records|tuples|values)
|
|
840
840
|
#
|
|
841
841
|
# Insert record(s) in table and return id(s)
|
|
842
842
|
#
|
|
@@ -874,12 +874,15 @@ module PgConn
|
|
|
874
874
|
method = :values # The pg_conn method when multiple records are inserted
|
|
875
875
|
if data.empty?
|
|
876
876
|
return []
|
|
877
|
-
elsif data.first.is_a?(Array) # Tuple (
|
|
877
|
+
elsif data.first.is_a?(Array) # Tuple (Array) element. Requires the 'fields' argument
|
|
878
878
|
fields or raise ArgumentError
|
|
879
879
|
tuples = data
|
|
880
|
-
elsif data.first.is_a?(Hash) # Hash element
|
|
880
|
+
elsif data.first.is_a?(Hash) # Record (Hash) element
|
|
881
881
|
fields ||= data.first.keys
|
|
882
882
|
tuples = data.map { |record| fields.map { |field| record[field] } }
|
|
883
|
+
elsif data.first.is_a?(OpenStruct)
|
|
884
|
+
fields ||= data.first.to_h.keys
|
|
885
|
+
tuples = data.map { |struct| hash = struct.to_h; fields.map { |field| hash[field] } }
|
|
883
886
|
else
|
|
884
887
|
fields.size == 1 or raise ArgumentError, "Illegal number of fields, expected exactly one"
|
|
885
888
|
tuples = data.map { |e| [e] }
|
|
@@ -888,6 +891,11 @@ module PgConn
|
|
|
888
891
|
method = upsert ? :value? : :value # The pg_conn method when only one record is inserted
|
|
889
892
|
fields ||= data.keys
|
|
890
893
|
tuples = [fields.map { |field| data[field] }]
|
|
894
|
+
elsif data.is_a?(OpenStruct)
|
|
895
|
+
method = upsert ? :value? : :value # The pg_conn method when only one record is inserted
|
|
896
|
+
hash = data.to_h
|
|
897
|
+
fields ||= hash.keys
|
|
898
|
+
tuples = [fields.map { |field| hash[field] }]
|
|
891
899
|
else
|
|
892
900
|
raise ArgumentError, "Illegal argument '#{data.inspect}'"
|
|
893
901
|
end
|
|
@@ -931,7 +939,7 @@ module PgConn
|
|
|
931
939
|
exec %(update #{table} set #{assignments} where #{constraint})
|
|
932
940
|
end
|
|
933
941
|
|
|
934
|
-
# Delete record(s)
|
|
942
|
+
# Delete record(s). See also #truncate
|
|
935
943
|
def delete(schema = nil, table, expr)
|
|
936
944
|
table = [schema, table].compact.join(".")
|
|
937
945
|
constraint =
|
|
@@ -945,53 +953,21 @@ module PgConn
|
|
|
945
953
|
exec %(delete from #{table} where #{constraint})
|
|
946
954
|
end
|
|
947
955
|
|
|
948
|
-
#
|
|
949
|
-
#
|
|
950
|
-
#
|
|
951
|
-
# Global options are :silent, :notice and :warning, they're very useful in
|
|
952
|
-
# RSpec tests
|
|
956
|
+
# :call-seq:
|
|
957
|
+
# truncate(qual_table_name...)
|
|
958
|
+
# truncate(schema, table_name...)
|
|
953
959
|
#
|
|
954
|
-
#
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
log = options.delete(:log)
|
|
964
|
-
commit = options.delete(:commit)
|
|
965
|
-
|
|
966
|
-
saved_options = @options.dup
|
|
967
|
-
saved_search_path = self.search_path if search_path
|
|
968
|
-
saved_logger = self.logger if log
|
|
969
|
-
|
|
970
|
-
begin
|
|
971
|
-
set_options(options)
|
|
972
|
-
self.search_path = search_path if search_path
|
|
973
|
-
self.logger = log if !log.nil?
|
|
974
|
-
|
|
975
|
-
inner = lambda {
|
|
976
|
-
if !commit.nil?
|
|
977
|
-
self.transaction(commit: commit) {
|
|
978
|
-
block.yield
|
|
979
|
-
}
|
|
980
|
-
else
|
|
981
|
-
block.yield
|
|
982
|
-
end
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
if username
|
|
986
|
-
self.su(username, &inner)
|
|
987
|
-
else
|
|
988
|
-
inner.call
|
|
989
|
-
end
|
|
990
|
-
ensure
|
|
991
|
-
self.logger = saved_logger if log
|
|
992
|
-
self.search_path = saved_search_path if search_path
|
|
993
|
-
set_options(saved_options)
|
|
960
|
+
# Empty a table using Sql 'truncate'. Arguments are flattened before processing
|
|
961
|
+
def truncate(*args)
|
|
962
|
+
args = args.flatten
|
|
963
|
+
if args.first =~ /\./
|
|
964
|
+
tables = args
|
|
965
|
+
else
|
|
966
|
+
schema = args.shift
|
|
967
|
+
!args.empty? or raise ArgumentError, "Table argument expected"
|
|
968
|
+
tables = args.map { |table| [schema, table].compact.join('.') }
|
|
994
969
|
end
|
|
970
|
+
exec %(truncate #{quote_identifiers(tables)})
|
|
995
971
|
end
|
|
996
972
|
|
|
997
973
|
# Execute SQL statement(s) in a transaction and return the number of
|
|
@@ -1058,6 +1034,55 @@ module PgConn
|
|
|
1058
1034
|
end
|
|
1059
1035
|
end
|
|
1060
1036
|
|
|
1037
|
+
# Execute block with the given set of global or local options and reset
|
|
1038
|
+
# them afterwards
|
|
1039
|
+
#
|
|
1040
|
+
# Global options are :silent, :notice and :warning, they're very useful in
|
|
1041
|
+
# RSpec tests
|
|
1042
|
+
#
|
|
1043
|
+
# Local options are :search_path that runs the block with the given
|
|
1044
|
+
# schemas, :username that runs the block as the given user, :commit that
|
|
1045
|
+
# runs the block in a transaction if true or false; true commits the
|
|
1046
|
+
# transaction and false rolls it back (very rarely useful). It is not run
|
|
1047
|
+
# in a transaction if :commit is nil. :log controls logging like
|
|
1048
|
+
# #logger= but nil (the default) is a nop
|
|
1049
|
+
def with(**options, &block)
|
|
1050
|
+
search_path = options.delete(:search_path)
|
|
1051
|
+
username = options.delete(:username)
|
|
1052
|
+
log = options.delete(:log)
|
|
1053
|
+
commit = options.delete(:commit)
|
|
1054
|
+
|
|
1055
|
+
saved_options = @options.dup
|
|
1056
|
+
saved_search_path = self.search_path if search_path
|
|
1057
|
+
saved_logger = self.logger if log
|
|
1058
|
+
|
|
1059
|
+
begin
|
|
1060
|
+
set_options(options)
|
|
1061
|
+
self.search_path = search_path if search_path
|
|
1062
|
+
self.logger = log if !log.nil?
|
|
1063
|
+
|
|
1064
|
+
inner = lambda {
|
|
1065
|
+
if !commit.nil?
|
|
1066
|
+
self.transaction(commit: commit) {
|
|
1067
|
+
block.yield
|
|
1068
|
+
}
|
|
1069
|
+
else
|
|
1070
|
+
block.yield
|
|
1071
|
+
end
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
if username
|
|
1075
|
+
self.su(username, &inner)
|
|
1076
|
+
else
|
|
1077
|
+
inner.call
|
|
1078
|
+
end
|
|
1079
|
+
ensure
|
|
1080
|
+
self.logger = saved_logger if log
|
|
1081
|
+
self.search_path = saved_search_path if search_path
|
|
1082
|
+
set_options(saved_options)
|
|
1083
|
+
end
|
|
1084
|
+
end
|
|
1085
|
+
|
|
1061
1086
|
# Switch user to the given user and execute the statement before swithcing
|
|
1062
1087
|
# back to the original user
|
|
1063
1088
|
#
|
|
@@ -1269,7 +1294,7 @@ module PgConn
|
|
|
1269
1294
|
end
|
|
1270
1295
|
|
|
1271
1296
|
# Common implementation for #quote_record and #quote_records that avoids
|
|
1272
|
-
# querying the database multiple times or
|
|
1297
|
+
# querying the database multiple times or duplicating the code
|
|
1273
1298
|
#
|
|
1274
1299
|
# @data can be a Hash, Array, or OpenStruct. Hash keys must be symbols or
|
|
1275
1300
|
# strings, they belong to the same namespace so :k and "k" refer to the
|