pg_conn 0.44.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pg_conn/version.rb +1 -1
  3. data/lib/pg_conn.rb +64 -47
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51ccb11b750c683d57a02f274b4479366eced6d46c8908fed1e47aca302ff367
4
- data.tar.gz: ad97bab6ef9fb0e7f821088a58772252ba72c4c9b695703b6ccd3a32fb1f5f80
3
+ metadata.gz: 1068edc8dc81b7b2b04eecbf4ae901c7777cfbdeec63855baa3db16fee6d1a83
4
+ data.tar.gz: 694ed53ea67c15829d0c0788f76e7d45b00124acc158cb6aaa92205046a68c85
5
5
  SHA512:
6
- metadata.gz: 629d0d6afb690bc1ef2f3490d395f81cf5177f86803f5e6049621fec73bcef07ed4b4c644cbf8e834059c7219076f2c62b1ad3b36e7fa1db344a415c9073213c
7
- data.tar.gz: 50cbacb421a2e4f4332c53737d0b9e8cf88e2b6555fe3c814d728d957f5d1477065f4a5089ff7c11ca77c33634aabf8440bd6aebccee8b92e7c509b7a18ba3ef
6
+ metadata.gz: d7eadc60da62af57bf1b706cdc7ea8aa9361c20847e49cf16a9443dbda1434ae5ff185267c7e30e3c2a3da3cf35eaa3679e6d85bbdc8140325f40156bb94335b
7
+ data.tar.gz: 783af32e37c31aed616b6d9fa60780d484b966b3e8e345d194e96edf4f576a199a16177e080b5d915ec4cba6d4458d86c09ceb5df00f2f5f33938a6a815826c8
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.44.0"
2
+ VERSION = "0.45.0"
3
3
  end
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 a search path is part of
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)
@@ -931,7 +931,7 @@ module PgConn
931
931
  exec %(update #{table} set #{assignments} where #{constraint})
932
932
  end
933
933
 
934
- # Delete record(s)
934
+ # Delete record(s). See also #truncate
935
935
  def delete(schema = nil, table, expr)
936
936
  table = [schema, table].compact.join(".")
937
937
  constraint =
@@ -945,53 +945,21 @@ module PgConn
945
945
  exec %(delete from #{table} where #{constraint})
946
946
  end
947
947
 
948
- # Execute block with the given set of global or local options and reset
949
- # them afterwards
950
- #
951
- # Global options are :silent, :notice and :warning, they're very useful in
952
- # RSpec tests
948
+ # :call-seq:
949
+ # truncate(qual_table_name...)
950
+ # truncate(schema, table_name...)
953
951
  #
954
- # Local options are :search_path that runs the block with the given
955
- # schemas, :username that runs the block as the given user, :commit that
956
- # runs the block in a transaction if true or false; true commits the
957
- # transaction and false rolls it back (very rarely useful). It is not run
958
- # in a transaction if :commit is nil. :log controls logging like
959
- # #logger= but nil (the default) is a nop
960
- def with(**options, &block)
961
- search_path = options.delete(:search_path)
962
- username = options.delete(:username)
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)
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('.') }
994
961
  end
962
+ exec %(truncate #{quote_identifiers(tables)})
995
963
  end
996
964
 
997
965
  # Execute SQL statement(s) in a transaction and return the number of
@@ -1058,6 +1026,55 @@ module PgConn
1058
1026
  end
1059
1027
  end
1060
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
+
1061
1078
  # Switch user to the given user and execute the statement before swithcing
1062
1079
  # back to the original user
1063
1080
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.44.0
4
+ version: 0.45.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen