pg_conn 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25c23025d55818536e6d89bc5fa40b12ac296f0e61657db387f8a8732bdf3504
4
- data.tar.gz: 7592d3a2b26d7f75f0ea88ad9b8f951422ced0cc913a387eb3f774b31bb1c4eb
3
+ metadata.gz: b6f8840c223cb2dd9a6a5f8b2211e43184eb308114ffdd8bdc76d92f6ea03e16
4
+ data.tar.gz: fbfad4033c2d21abb91b726c183001d4edbaf348d61cb425b4b003dd89a37f35
5
5
  SHA512:
6
- metadata.gz: 0701ef07551bc1db06b654aa12e957ca4ad2286f1050c122af95ce78670ea426e6bf8f566831070200588e2c844eaf70ddab0cc5147941ccdcf6f4dc232c4102
7
- data.tar.gz: 5310090682a9c93a648823b477081eebe31c214554ebe06a44d2f6702ec18c3fd5eb43e16bad0c15178d48a6194e109dbf15adb8bb3f75e645b90581ef3bc948
6
+ metadata.gz: fe6647045b3bad635ed480e201967a20f0b11b5fd71bcbdec6766b6a2f056e3b0c8de045e87f56af90fb429130b2a9f105c46f5de5de31219b8384d18b27b3ca
7
+ data.tar.gz: 5e114bd53908faf4a881799035276151ace147c63156cc006359e6483c182d0522d2a724f4e7b4fd551914739b23240c06a55319b46d4aa68c6bf65f0f8da095
@@ -124,6 +124,45 @@ module PgConn
124
124
  raise NotImplementedError
125
125
  end
126
126
 
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')"
130
+ end
131
+
132
+ # Get the current serial value for the table. Returns nil if the serial has
133
+ # not been used. If :next is true, the next value will be returned
134
+ def get_serial(schema, table, next: false)
135
+ uid = "#{schema}.#{table}"
136
+ next_option = binding.local_variable_get(:next) # because 'next' is a keyword
137
+
138
+ seq = sequence(schema, table) or raise ArgumentError, "Table #{uid} does not have a sequence"
139
+ value = conn.value %(
140
+ select
141
+ case is_called
142
+ when true then last_value
143
+ else null
144
+ end as "value"
145
+ from
146
+ #{seq}
147
+ )
148
+ if next_option
149
+ value&.+(1) || 1
150
+ else
151
+ value
152
+ end
153
+ end
154
+
155
+ # Set the serial value for the table
156
+ def set_serial(schema, table, value)
157
+ uid = "#{schema}.#{table}"
158
+ seq = sequence(schema, table) or raise ArgumentError, "Table #{uid} does not have a sequence"
159
+ if value
160
+ conn.exec "select setval('#{seq}', #{value})"
161
+ else
162
+ conn.exec "select setval('#{seq}', 1, false)"
163
+ end
164
+ end
165
+
127
166
  private
128
167
  def relation_exist_query(schema, relation, kind: nil)
129
168
  kind_sql_list = "'" + (kind.nil? ? %w(r f v m) : Array(kind).flatten).join("', '") + "'"
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.16.0"
2
+ VERSION = "0.17.0"
3
3
  end
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.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen