pg_conn 0.21.0 → 0.22.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f293ad25581975cbd57dc5973d072043d6b5cf13093a21bd354720b39c91c12
4
- data.tar.gz: 1ad472d136a9b626717f90058cb220eaaf4c87bced2f8802c8cab68b69bf187a
3
+ metadata.gz: 8d8571dfbe07ffc9e846741807d87d45373553d987e353693aa4599010804b3e
4
+ data.tar.gz: 4d0bd8faced962ac9c0837b3a98f1a6f0ee4fbc0c1c1aa4a12c448472e10376a
5
5
  SHA512:
6
- metadata.gz: 7720aef7f128e0b8e3c47fdd2acb752b1d44af9625653eae0552a2ac619e3c29ae2cad25789f0f0806fe4576104a64210cf5a5d7e7c7fa66817b8538a556bd35
7
- data.tar.gz: 61425eaeabb6b6e0872f6ed49064f8650361ecd8be684b7db2322a6a346265e253489e49be37688c9ef74171733d2c8f19a12d38105a188ba263cf5ea48c9593
6
+ metadata.gz: 3b3ff2ae7e1fa63f697616e16eff4065e5118ef5fe9cb446837cc30b028df5a385b56848e23546c7718f5f704e622cd35175acbd56aacd6fd56774dec9c34627
7
+ data.tar.gz: 4c9428ef6abc6162c068f154575eb1d8c7b63c1aa072af4350c7bacb2c6ea63e0ffea80a470f9ca8b72c635e83ceda7345b54bc6a24d5adc450bf0863fb0df1b
@@ -21,7 +21,7 @@ module PgConn
21
21
  end
22
22
 
23
23
  # Create a new database
24
- def create(database, owner: ENV['USER'], template: "template1")
24
+ def create(database, owner: ENV['USER'], template: "template1")
25
25
  owner_clause = owner ? "owner = \"#{owner}\"" : nil
26
26
  template_clause = template ? "template = \"#{template}\"" : nil
27
27
  stmt = ["create database \"#{database}\"", owner_clause, template_clause].compact.join(" ")
@@ -80,7 +80,7 @@ module PgConn
80
80
  local = !database.nil?
81
81
  begin
82
82
  conn = local ? PgConn.new(database) : self.conn
83
- schemas =
83
+ schemas =
84
84
  conn
85
85
  .values("select nspname from pg_namespace where nspowner != 10 or nspname = 'public'")
86
86
  .select { |schema| !exclude.include?(schema) }
@@ -90,7 +90,7 @@ module PgConn
90
90
  # FIXME FIXME FIXME SECURITY Why grant 'create' to public?
91
91
  conn.exec %(
92
92
  create schema public authorization postgres;
93
- grant usage, create on schema public to public
93
+ grant usage, create on schema public to public
94
94
  ) if public
95
95
  ensure
96
96
  conn&.terminate if local
@@ -18,7 +18,7 @@ module PgConn
18
18
  else
19
19
  nil
20
20
  end
21
- can_login_clause =
21
+ can_login_clause =
22
22
  case can_login
23
23
  when true; "rolcanlogin"
24
24
  when false; "not rolcanlogin"
@@ -62,7 +62,7 @@ module PgConn
62
62
  # counldn't be deleted
63
63
  #
64
64
  # Note that cascade only works if connected to the database where the
65
- # privileges exist.
65
+ # privileges exist.
66
66
  #
67
67
  # TODO The :silent option is used in tests - fix it somehow!
68
68
  def drop(*rolenames, cascade: false, fail: true, silent: false)
@@ -86,7 +86,7 @@ module PgConn
86
86
  superuser_clause = superuser.nil? ? nil : "rolsuper = #{superuser}"
87
87
  can_login_clause = can_login.nil? ? nil : "rolcanlogin = #{can_login}"
88
88
  query = [
89
- "select rolname from pg_roles where true",
89
+ "select rolname from pg_roles where true",
90
90
  database_clause, superuser_clause, can_login_clause
91
91
  ].compact.join(" and ")
92
92
  conn.values(query)
@@ -49,8 +49,8 @@ module PgConn
49
49
  # Empty all tables in the given schema
50
50
  def clean!(schema, exclude: [])
51
51
  conn.session.triggers(false) {
52
- self.list_tables(schema, exclude: exclude).each { |table|
53
- conn.exec "delete from #{schema}.#{table}"
52
+ self.list_tables(schema, exclude: exclude).each { |table|
53
+ conn.exec "delete from #{schema}.#{table}"
54
54
  }
55
55
  }
56
56
  end
@@ -125,8 +125,8 @@ module PgConn
125
125
  end
126
126
 
127
127
  # Return name of the table's sequence (if any)
128
- def sequence(schema, table)
129
- conn.value "select pg_get_serial_sequence('#{schema}.#{table}', 'id')"
128
+ def sequence(schema, table)
129
+ conn.value "select pg_get_serial_sequence('#{schema}.#{table}', 'id')"
130
130
  end
131
131
 
132
132
  # Get the current serial value for the table. Returns nil if the serial has
@@ -168,7 +168,7 @@ module PgConn
168
168
  kind_sql_list = "'" + (kind.nil? ? %w(r f v m) : Array(kind).flatten).join("', '") + "'"
169
169
  %(
170
170
  select 1
171
- from pg_class
171
+ from pg_class
172
172
  where relnamespace::regnamespace::text = '#{schema}'
173
173
  and relname = '#{relation}'
174
174
  and relkind in (#{kind_sql_list})
@@ -183,7 +183,7 @@ module PgConn
183
183
  exclude_expr = exclude.empty? ? "true = true" : "not relname in (#{exclude_list})"
184
184
  %(
185
185
  select relname
186
- from pg_class
186
+ from pg_class
187
187
  where relnamespace::regnamespace::text = '#{schema}'
188
188
  and #{kind_expr}
189
189
  and #{exclude_expr}
@@ -211,7 +211,7 @@ module PgConn
211
211
  join pg_attribute a on a.attrelid = c.oid
212
212
  where relnamespace::regnamespace::text = '#{schema}'
213
213
  and a.attnum > 0
214
- ),
214
+ ),
215
215
  relation_clause
216
216
  ].compact.join(" and ")
217
217
  end
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.21.0"
2
+ VERSION = "0.22.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -515,8 +515,9 @@ module PgConn
515
515
 
516
516
  # Returns a hash from the first field to a tuple of the remaining fields.
517
517
  # If there is only one remaining field then that value is used instead of a
518
- # tuple of that value. The optional +key+ argument sets the mapping field
519
- def map(query, key = nil) # TODO Swap arguments
518
+ # tuple. The optional +key+ argument sets the mapping field and the
519
+ # +symbol+ option convert key to Symbol objects when true
520
+ def map(query, key = nil, symbol: false) # TODO Swap arguments
520
521
  r = pg_exec(query)
521
522
  begin
522
523
  key = (key || r.fname(0)).to_s
@@ -528,13 +529,18 @@ module PgConn
528
529
  h = {}
529
530
  r.each_row { |row|
530
531
  key_value = row.delete_at(key_index)
532
+ key_value = key_value.to_sym if symbol
531
533
  !h.key?(key_value) or raise Error, "Duplicate key: #{key_value}"
532
534
  h[key_value] = (one ? row.first : row)
533
535
  }
534
536
  h
535
537
  end
536
538
 
537
- def multimap(query, key = nil)
539
+ # Like #map but values of duplicate keys are concatenated. It acts as a
540
+ # group-by on the key and array_agg on the remaining values. The value is
541
+ # an array of tuples if the query has more than one value field and an
542
+ # array of values if there is only one value field
543
+ def multimap(query, key = nil, symbol: false)
538
544
  r = pg_exec(query)
539
545
  begin
540
546
  key = (key || r.fname(0)).to_s
@@ -546,6 +552,7 @@ module PgConn
546
552
  h = {}
547
553
  r.each_row { |row|
548
554
  key_value = row.delete_at(key_index)
555
+ key_value = key_value.to_sym if symbol
549
556
  (h[key_value] ||= []) << (one ? row.first : row)
550
557
  }
551
558
  h
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.21.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-07 00:00:00.000000000 Z
11
+ date: 2024-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg