pg_conn 0.39.0 → 0.40.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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/TODO +1 -1
  3. data/lib/pg_conn/version.rb +1 -1
  4. data/lib/pg_conn.rb +56 -7
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ace2859257bedbbdeb816bfb04d9d7e23d686942c328e0d521568b6f4fc57e90
4
- data.tar.gz: b803feaaaa619668cf00f57d2c5ffd072f04a149e144eafb061b85d4bf4cfad4
3
+ metadata.gz: f946804765203fcdb2c40e704831e488e2a47c1975ce371217faa528c533141c
4
+ data.tar.gz: fa08dce1b7e10162aaa5e2cd20e0ada630f95a794462528a73969c6bd5ad484c
5
5
  SHA512:
6
- metadata.gz: b2b913861f23ed8db39f8d521571bb054c3ed9ca1526b800c5f4982f9b4a9f285cc60b17dc59194b2df7cc2147019faaef7b838a4e10d4e8659941adb6fddde9
7
- data.tar.gz: aac27bc4b8e7881ded4fdfe350a98be3de919052b9b5fcbca0d6767573dd9592d4922e3b2da968860b146d451b05bb96937e7344941d58048a104c5b8bda9081
6
+ metadata.gz: 41262613001be5709f861d125af4287a82968ce60878dca3c885a34033d0ef78786b76a774738123fbd97ffdaaad5821cf480899cc56772f3691757c831de8a7
7
+ data.tar.gz: de131f33563c6ec6e52c03d4537dd3ab730506534e84a0aae1aefaefc763a05fd45376713a4ee2d91177967c2def949c566bf137376f6c61496f0e7f691ad97f
data/TODO CHANGED
@@ -96,7 +96,6 @@ TODO
96
96
  o Proper implementation of call of functions and procedures: Functions should
97
97
  be called through #value, #tuple etc. and procedures through #call.
98
98
  Proceduer output parameters needs handling too
99
- o Implement search_path
100
99
  o Create an abstract PgConnBase and have PgStmts (writes statements to array)
101
100
  and PgConn (sends statements to server) classes derived from it
102
101
  o fix silent
@@ -119,6 +118,7 @@ TODO
119
118
  + Quote methods (value, identier, ... -> Postgres string)
120
119
  + Have a 'with' method that combines multiple brachet-methods:
121
120
  conn.with(schema: public, transation: true) { ... }
121
+ + Implement search_path
122
122
 
123
123
 
124
124
  REFACTOR
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.39.0"
2
+ VERSION = "0.40.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -465,6 +465,18 @@ module PgConn
465
465
  quote_record_impl(data, schema_name, type, array: true, **opts)
466
466
  end
467
467
 
468
+ # Return current search path. Note that a search path is part of
469
+ # the transaction
470
+ def search_path
471
+ self.value("show search_path").split(/,\s*/) - %w("$user" pg_temp)
472
+ end
473
+
474
+ # Set search path. It accepts a schema or an array of schema names
475
+ def search_path=(schemas)
476
+ schema_array = Array(schemas).flatten - %w("$user" pg_temp) + %w(pg_temp)
477
+ self.exec "set search_path to #{schema_array.join(', ')}"
478
+ end
479
+
468
480
  # :call-seq:
469
481
  # exist?(query)
470
482
  # exist?(table, id)
@@ -893,17 +905,47 @@ module PgConn
893
905
  exec %(delete from #{table} where #{constraint})
894
906
  end
895
907
 
896
- # Execute block with global options and resets afterwards. Currently only
897
- # :silent, :notice and :warning is supported. Very useful in RSpec tests
908
+ # Execute block with the given set of global or local options and reset
909
+ # them afterwards
910
+ #
911
+ # The global options :silent, :notice and :warning are supported, they're
912
+ # very useful in RSpec tests
898
913
  #
899
- # TODO: :error, :fail, :symbol, :schema, :search_path
914
+ # Local options are :search_path that runs the block with the given
915
+ # schemas, :username that runs the block as the given user, and :commit
916
+ # that runs the block in a transaction if true or false; true commits the
917
+ # transaction and false rolls it back. It is not run in a transaction if
918
+ # :commit is nil
900
919
  #
901
920
  def with(**options, &block)
921
+ search_path = options.delete(:search_path)
922
+ username = options.delete(:username)
923
+ commit = options.delete(:commit)
924
+
925
+ saved_options = @options.dup
926
+ saved_search_path = self.search_path if search_path
927
+
902
928
  begin
903
- saved_options = @options.dup
904
929
  set_options(options)
905
- yield
930
+ self.search_path = search_path if search_path
931
+
932
+ inner = lambda {
933
+ if !commit.nil?
934
+ self.transaction(commit: commit) {
935
+ block.yield
936
+ }
937
+ else
938
+ block.yield
939
+ end
940
+ }
941
+
942
+ if username
943
+ self.su(username, &inner)
944
+ else
945
+ inner.call
946
+ end
906
947
  ensure
948
+ self.search_path = saved_search_path if search_path
907
949
  set_options(saved_options)
908
950
  end
909
951
  end
@@ -975,16 +1017,23 @@ module PgConn
975
1017
  # Switch user to the given user and execute the statement before swithcing
976
1018
  # back to the original user
977
1019
  #
978
- # FIXME: The out-commented transaction block makes postspec fail for some reason
1020
+ # FIXME:
1021
+ # The out-commented transaction block makes postspec fail for some
1022
+ # reason. Note that user-switches lives within transactions
1023
+ #
979
1024
  # TODO: Rename 'sudo' because it acts just like it.
1025
+ #
980
1026
  def su(username, &block)
981
1027
  raise Error, "Missing block in call to PgConn::Connection#su" if !block_given?
982
1028
  realuser = self.value "select current_user"
983
1029
  result = nil
984
1030
  # transaction(commit: false) {
1031
+ begin
985
1032
  execute "set session authorization #{username}"
986
1033
  result = yield
1034
+ ensure
987
1035
  execute "set session authorization #{realuser}"
1036
+ end
988
1037
  # }
989
1038
  result
990
1039
  end
@@ -1087,7 +1136,7 @@ module PgConn
1087
1136
  # that the transaction timestamp is set to the start of the first
1088
1137
  # transaction even if transactions are nested
1089
1138
  #
1090
- # FIXME: There is some strange problem in rspec where an #insert handles
1139
+ # FIXME: There is some strange problem in rspec where #insert handles
1091
1140
  # an exception correctly while #exec, #execute, and #transaction does not
1092
1141
  def transaction(commit: true, &block)
1093
1142
  if block_given?
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.39.0
4
+ version: 0.40.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen