pg_conn 0.16.0 → 0.17.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: 25c23025d55818536e6d89bc5fa40b12ac296f0e61657db387f8a8732bdf3504
4
- data.tar.gz: 7592d3a2b26d7f75f0ea88ad9b8f951422ced0cc913a387eb3f774b31bb1c4eb
3
+ metadata.gz: 64ed87e1cf308dd73256ca1a3785acee059ffbd8fc30d11303f0b494799b9dcb
4
+ data.tar.gz: 21076b277ee98f430d9fa19a882ab4a9242815b749a956ec76a210464517162c
5
5
  SHA512:
6
- metadata.gz: 0701ef07551bc1db06b654aa12e957ca4ad2286f1050c122af95ce78670ea426e6bf8f566831070200588e2c844eaf70ddab0cc5147941ccdcf6f4dc232c4102
7
- data.tar.gz: 5310090682a9c93a648823b477081eebe31c214554ebe06a44d2f6702ec18c3fd5eb43e16bad0c15178d48a6194e109dbf15adb8bb3f75e645b90581ef3bc948
6
+ metadata.gz: f657154a19e12be06b0e98668964f3b157745f3bbd90bad5e242416598e4be887c89dc7ae81ec56a109bf51aa66997e563825c43fb6a73d09e09711360a2395b
7
+ data.tar.gz: 3b5892c3e8f80dcae50cffef9ea128dab6d1bb349d72115c9cb2b5ceff3f0371c1622851192b78ecea8dc572812af6aa9e1b9133ef119b31f81f0772f7b6239a
@@ -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.1"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -558,6 +558,7 @@ module PgConn
558
558
 
559
559
  # Insert record(s) in a table. Returns the id of the inserted record(s)
560
560
  def insert(schema = nil, table, array_or_hash)
561
+ return [] if array_or_hash.empty?
561
562
  table = [schema, table].compact.join(".")
562
563
  is_array = array_or_hash.is_a?(Array)
563
564
  array = is_array ? array_or_hash : [array_or_hash]
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen