pg_conn 0.16.0 → 0.17.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pg_conn/schema_methods.rb +39 -0
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64ed87e1cf308dd73256ca1a3785acee059ffbd8fc30d11303f0b494799b9dcb
|
4
|
+
data.tar.gz: 21076b277ee98f430d9fa19a882ab4a9242815b749a956ec76a210464517162c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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("', '") + "'"
|
data/lib/pg_conn/version.rb
CHANGED
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]
|