pg_conn 0.31.0 → 0.32.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 122b4fc4ba631ecac00021695532624fd13a578183812a98617a9f05b5a9104f
4
- data.tar.gz: 02e3a823f17d4c6012a51186a2da124b0dfacf5ece0e4f8f5a6a6c31d97675d1
3
+ metadata.gz: de05fd7cb9954d248fa17a1d770526254d3041581bd854c2e7ac1a3afd07d6c4
4
+ data.tar.gz: 47b0579c83826d3946fff342c9d95ec8ab3efc2119c6c7f117056d7188442582
5
5
  SHA512:
6
- metadata.gz: c2c84bb43ba856724e246d4fe264e739abe01d8abee3e8f39678893e671124b31a9e57748fe4347b5f3b1192e547c5bd76e8f84558f8a5035a12604e2635c0dd
7
- data.tar.gz: dae9131834552ad7a23c16e36ead22ba1ef6b9f0e16e5747ab7cb9c29341120dddbd3cc2dd25969375898659ec3a93924f80c554afe382a659e9bf04ebab4208
6
+ metadata.gz: 4b77f809d2d2d8c431f78cd82b1f4359189731212f5cfa1b3818e043e886277951a2127a670b97259a311f05b750629cc431580eee110eab98eedaa02ff4217b
7
+ data.tar.gz: d9208f1e696f538e2502351a54c4ef17ef43cd8b59692ab2a5216976334b1333b0b1886c8d6e9031ae15b835e44023a77244ea586f0d6b4b9ddb50bed08a6dee
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.31.0"
2
+ VERSION = "0.32.1"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -58,8 +58,8 @@ module PgConn
58
58
  # or 'jsonb'
59
59
  #
60
60
  # Note that a tuple value (an array) must be quoted using #quote_tuple
61
- # because #quote_value would quote the tuple as an array instead of a list
62
- # of values
61
+ # because #quote_value would quote the tuple as an array value instead of a
62
+ # list of values
63
63
  #
64
64
  # The :elem_type option can be a postgres type name (String or Symbol) or
65
65
  # an array of type names. It is used as the required explicit element
@@ -329,6 +329,8 @@ module PgConn
329
329
  end
330
330
  end
331
331
 
332
+ # Mark argument as already being quoted. TODO: Make this the default in all
333
+ # quote methods
332
334
  def literal(arg) Literal.new(arg) end
333
335
 
334
336
  # Connection member method variations of the PgConn quote class methods
@@ -958,6 +960,40 @@ module PgConn
958
960
  end
959
961
  end
960
962
 
963
+ def dump(*query)
964
+ records = self.records(*query)
965
+
966
+ if records.empty?
967
+ puts "No records found"
968
+ else
969
+ headers = records.first.keys
970
+ column_widths = headers.map(&:size)
971
+ column_signs = [nil] * headers.size
972
+
973
+ records.each { |r|
974
+ r.values.each.with_index { |v, i|
975
+ value_width = v.to_s.size
976
+ column_widths[i] = [column_widths[i], value_width].max
977
+
978
+ column_signs[i] ||=
979
+ case v
980
+ when nil; nil
981
+ when Integer; ""
982
+ else
983
+ "-"
984
+ end
985
+ }
986
+ }
987
+
988
+ header_format = column_widths.map { |w,t| "%-#{w}s" }.join(" ")
989
+ body_format = column_widths.zip(column_signs).map { |w,s| "%#{s}#{w}s" }.join(" ")
990
+
991
+ printf "#{header_format}\n", *headers
992
+ printf "#{header_format}\n", *column_widths.map { |w| "-" * w }
993
+ records.each { |r| printf "#{body_format}\n", *r.values }
994
+ end
995
+ end
996
+
961
997
  private
962
998
  # Wrapper around PG::Connection.new that switches to the postgres user
963
999
  # before connecting if the current user is the root user
@@ -988,8 +1024,15 @@ module PgConn
988
1024
  end
989
1025
 
990
1026
  # Common implementation for #quote_record and #quote_records that avoids
991
- # query the database multiple times while not duplication the code. the
992
- # :array flag controls the mode
1027
+ # querying the database multiple times or duplication the code. The :array
1028
+ # flag is true when called via #quote_records
1029
+ #
1030
+ # @data can be a Hash, Array, or OpenStruct. Hash keys must be symbols or
1031
+ # strings, they belong to the same namespace so :k and "k" refer to the
1032
+ # same value
1033
+ #
1034
+ # Note that #quote_record_impl queries the database for information about
1035
+ # the type. TODO Cache this information?
993
1036
  def quote_record_impl(datas, schema_name = nil, type, elem_types: nil, array: nil)
994
1037
  pg_type = [schema_name, type].compact.join('.')
995
1038
  fields = self.values(%(
@@ -1007,7 +1050,7 @@ module PgConn
1007
1050
  literals = datas.map { |data|
1008
1051
  values =
1009
1052
  case data
1010
- when Hash; fields.map { |f| data[f] }
1053
+ when Hash; fields.map { |f| data.key?(f) && data[f] || data.key?(f.to_s) && data[f.to_s] }
1011
1054
  when OpenStruct; fields.map { |f| data.send(f) }
1012
1055
  when Array; data
1013
1056
  else
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.31.0
4
+ version: 0.32.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-14 00:00:00.000000000 Z
11
+ date: 2025-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg