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 +4 -4
- data/lib/pg_conn/schema_methods.rb +39 -0
- data/lib/pg_conn/version.rb +1 -1
- 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: b6f8840c223cb2dd9a6a5f8b2211e43184eb308114ffdd8bdc76d92f6ea03e16
|
4
|
+
data.tar.gz: fbfad4033c2d21abb91b726c183001d4edbaf348d61cb425b4b003dd89a37f35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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("', '") + "'"
|
data/lib/pg_conn/version.rb
CHANGED