pg_conn 0.43.0 → 0.45.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 +78 -48
- 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: 1068edc8dc81b7b2b04eecbf4ae901c7777cfbdeec63855baa3db16fee6d1a83
|
|
4
|
+
data.tar.gz: 694ed53ea67c15829d0c0788f76e7d45b00124acc158cb6aaa92205046a68c85
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d7eadc60da62af57bf1b706cdc7ea8aa9361c20847e49cf16a9443dbda1434ae5ff185267c7e30e3c2a3da3cf35eaa3679e6d85bbdc8140325f40156bb94335b
|
|
7
|
+
data.tar.gz: 783af32e37c31aed616b6d9fa60780d484b966b3e8e345d194e96edf4f576a199a16177e080b5d915ec4cba6d4458d86c09ceb5df00f2f5f33938a6a815826c8
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
|
@@ -113,6 +113,10 @@ module PgConn
|
|
|
113
113
|
Literal.new tuples.map { |tuple| "(#{quote_tuple(tuple, **opts)})" }.join(", ")
|
|
114
114
|
end
|
|
115
115
|
|
|
116
|
+
# Quote array elements as a comma separated list of values and enclosed en
|
|
117
|
+
# parenthesis. Eg. [1, 2] => "(1, 2)"
|
|
118
|
+
def self.quote_list(array, **opts) = quote_tuples([array], **opts)
|
|
119
|
+
|
|
116
120
|
# Used to mark strings as literals that should not be quoted. This is the
|
|
117
121
|
# case for row and record values
|
|
118
122
|
class Literal < String; end
|
|
@@ -470,6 +474,9 @@ module PgConn
|
|
|
470
474
|
def quote_tuple(tuple, **opts) = PgConn.quote_tuple(tuple, json_type: self.default_json_type, **opts)
|
|
471
475
|
def quote_tuples(tuples, **opts) = PgConn.quote_tuples(tuples, json_type: self.default_json_type, **opts)
|
|
472
476
|
|
|
477
|
+
# Quote an array as a list. Eg. ["1", 2] => ('1', 2)
|
|
478
|
+
def quote_list(values, **opts) = quote_tuples([values], **opts)
|
|
479
|
+
|
|
473
480
|
# Quote a record and cast it into the given type, the type can also be a
|
|
474
481
|
# table or view. 'data' is an array, hash, or struct representation of the
|
|
475
482
|
# record
|
|
@@ -491,7 +498,7 @@ module PgConn
|
|
|
491
498
|
quote_record_impl(data, schema_name, type, array: true, **opts)
|
|
492
499
|
end
|
|
493
500
|
|
|
494
|
-
# Return current search path. Note that
|
|
501
|
+
# Return current search path. Note that the search path is part of
|
|
495
502
|
# the transaction
|
|
496
503
|
def search_path
|
|
497
504
|
self.value("show search_path").split(/,\s*/) - %w("$user" pg_temp)
|
|
@@ -827,6 +834,7 @@ module PgConn
|
|
|
827
834
|
# :call-seq:
|
|
828
835
|
# insert(table, record|records)
|
|
829
836
|
# insert(table, fields, record|records|tuples|values)
|
|
837
|
+
#
|
|
830
838
|
# insert(schema, table, record|records)
|
|
831
839
|
# insert(schema, table, fields, record|records|tuples|values)
|
|
832
840
|
#
|
|
@@ -834,6 +842,12 @@ module PgConn
|
|
|
834
842
|
#
|
|
835
843
|
# There is no variant that takes a single tuple because it would then be
|
|
836
844
|
# impossible to have array or hash field values
|
|
845
|
+
#
|
|
846
|
+
# TODO
|
|
847
|
+
# insert(table, [fields], field-name-map, object|objects)
|
|
848
|
+
# field-name-map:
|
|
849
|
+
# { database-column-name: object-method-name } # calls method on orbejt
|
|
850
|
+
#
|
|
837
851
|
def insert(*args, upsert: nil, **opts)
|
|
838
852
|
# Normalize arguments
|
|
839
853
|
|
|
@@ -917,7 +931,7 @@ module PgConn
|
|
|
917
931
|
exec %(update #{table} set #{assignments} where #{constraint})
|
|
918
932
|
end
|
|
919
933
|
|
|
920
|
-
# Delete record(s)
|
|
934
|
+
# Delete record(s). See also #truncate
|
|
921
935
|
def delete(schema = nil, table, expr)
|
|
922
936
|
table = [schema, table].compact.join(".")
|
|
923
937
|
constraint =
|
|
@@ -931,54 +945,21 @@ module PgConn
|
|
|
931
945
|
exec %(delete from #{table} where #{constraint})
|
|
932
946
|
end
|
|
933
947
|
|
|
934
|
-
#
|
|
935
|
-
#
|
|
936
|
-
#
|
|
937
|
-
# The global options :silent, :notice and :warning are supported, they're
|
|
938
|
-
# very useful in RSpec tests
|
|
939
|
-
#
|
|
940
|
-
# Local options are :search_path that runs the block with the given
|
|
941
|
-
# schemas, :username that runs the block as the given user, :log that
|
|
942
|
-
# controls logging (see #logger=), :commit that runs the block in a
|
|
943
|
-
# transaction if true or false; true commits the transaction and false
|
|
944
|
-
# rolls it back (very rarely useful). It is not run in a transaction if
|
|
945
|
-
# :commit is nil
|
|
948
|
+
# :call-seq:
|
|
949
|
+
# truncate(qual_table_name...)
|
|
950
|
+
# truncate(schema, table_name...)
|
|
946
951
|
#
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
begin
|
|
958
|
-
set_options(options)
|
|
959
|
-
self.search_path = search_path if search_path
|
|
960
|
-
self.logger = log if !log.nil?
|
|
961
|
-
|
|
962
|
-
inner = lambda {
|
|
963
|
-
if !commit.nil?
|
|
964
|
-
self.transaction(commit: commit) {
|
|
965
|
-
block.yield
|
|
966
|
-
}
|
|
967
|
-
else
|
|
968
|
-
block.yield
|
|
969
|
-
end
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
if username
|
|
973
|
-
self.su(username, &inner)
|
|
974
|
-
else
|
|
975
|
-
inner.call
|
|
976
|
-
end
|
|
977
|
-
ensure
|
|
978
|
-
self.logger = saved_logger if log
|
|
979
|
-
self.search_path = saved_search_path if search_path
|
|
980
|
-
set_options(saved_options)
|
|
952
|
+
# Empty a table using Sql 'truncate'. Arguments are flattened before processing
|
|
953
|
+
def truncate(*args)
|
|
954
|
+
args = args.flatten
|
|
955
|
+
if args.first =~ /\./
|
|
956
|
+
tables = args
|
|
957
|
+
else
|
|
958
|
+
schema = args.shift
|
|
959
|
+
!args.empty? or raise ArgumentError, "Table argument expected"
|
|
960
|
+
tables = args.map { |table| [schema, table].compact.join('.') }
|
|
981
961
|
end
|
|
962
|
+
exec %(truncate #{quote_identifiers(tables)})
|
|
982
963
|
end
|
|
983
964
|
|
|
984
965
|
# Execute SQL statement(s) in a transaction and return the number of
|
|
@@ -1045,6 +1026,55 @@ module PgConn
|
|
|
1045
1026
|
end
|
|
1046
1027
|
end
|
|
1047
1028
|
|
|
1029
|
+
# Execute block with the given set of global or local options and reset
|
|
1030
|
+
# them afterwards
|
|
1031
|
+
#
|
|
1032
|
+
# Global options are :silent, :notice and :warning, they're very useful in
|
|
1033
|
+
# RSpec tests
|
|
1034
|
+
#
|
|
1035
|
+
# Local options are :search_path that runs the block with the given
|
|
1036
|
+
# schemas, :username that runs the block as the given user, :commit that
|
|
1037
|
+
# runs the block in a transaction if true or false; true commits the
|
|
1038
|
+
# transaction and false rolls it back (very rarely useful). It is not run
|
|
1039
|
+
# in a transaction if :commit is nil. :log controls logging like
|
|
1040
|
+
# #logger= but nil (the default) is a nop
|
|
1041
|
+
def with(**options, &block)
|
|
1042
|
+
search_path = options.delete(:search_path)
|
|
1043
|
+
username = options.delete(:username)
|
|
1044
|
+
log = options.delete(:log)
|
|
1045
|
+
commit = options.delete(:commit)
|
|
1046
|
+
|
|
1047
|
+
saved_options = @options.dup
|
|
1048
|
+
saved_search_path = self.search_path if search_path
|
|
1049
|
+
saved_logger = self.logger if log
|
|
1050
|
+
|
|
1051
|
+
begin
|
|
1052
|
+
set_options(options)
|
|
1053
|
+
self.search_path = search_path if search_path
|
|
1054
|
+
self.logger = log if !log.nil?
|
|
1055
|
+
|
|
1056
|
+
inner = lambda {
|
|
1057
|
+
if !commit.nil?
|
|
1058
|
+
self.transaction(commit: commit) {
|
|
1059
|
+
block.yield
|
|
1060
|
+
}
|
|
1061
|
+
else
|
|
1062
|
+
block.yield
|
|
1063
|
+
end
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
if username
|
|
1067
|
+
self.su(username, &inner)
|
|
1068
|
+
else
|
|
1069
|
+
inner.call
|
|
1070
|
+
end
|
|
1071
|
+
ensure
|
|
1072
|
+
self.logger = saved_logger if log
|
|
1073
|
+
self.search_path = saved_search_path if search_path
|
|
1074
|
+
set_options(saved_options)
|
|
1075
|
+
end
|
|
1076
|
+
end
|
|
1077
|
+
|
|
1048
1078
|
# Switch user to the given user and execute the statement before swithcing
|
|
1049
1079
|
# back to the original user
|
|
1050
1080
|
#
|