Module: Sequel::Sequence::Database::PostgreSQL

Defined in:
lib/sequel/sequence/database/postgresql.rb

Instance Method Summary collapse

Instance Method Details

#check_sequencesObject



31
32
33
# File 'lib/sequel/sequence/database/postgresql.rb', line 31

def check_sequences
  fetch('SELECT * FROM information_schema.sequences ORDER BY sequence_name').all.to_a
end

#create_sequence(name, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sequel/sequence/database/postgresql.rb', line 35

def create_sequence(name, options = {})
  data_type = options[:data_type]
  minvalue = options[:minvalue]
  maxvalue = options[:maxvalue]
  start = options[:start]
  cache = options[:cache]
  cycle = options[:cycle]
  owned_by = options[:owned_by]

  increment = options[:increment] || options[:step]
  if_exists = build_exists_condition(options[:if_exists])
  name = quote_name(name.to_s)

  sql = ["CREATE SEQUENCE #{if_exists} #{name}"]
  sql << "AS #{data_type}" if data_type
  sql << "INCREMENT BY #{increment}" if increment
  sql << "MINVALUE  #{minvalue}" if minvalue
  sql << "MAXVALUE  #{maxvalue}" if maxvalue
  sql << "START WITH #{start}" if start
  sql << "CACHE  #{cache}" if cache
  sql << cycle.to_s if cycle
  sql << "OWNED BY  #{owned_by}" if owned_by
  sql << ';'
  sql << "COMMENT ON SEQUENCE #{name} IS '#{Sequel::Database::SEQUENCE_COMMENT}';"

  run(sql.join("\n"))
end

#currval(name) ⇒ Object Also known as: lastval

for Postgres



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sequel/sequence/database/postgresql.rb', line 79

def currval(name)
  quoted_name = quote(name.to_s)
  out = nil
  fetch("SELECT currval(#{quoted_name})") do |row|
    out = row[:currval]
  end
  out
rescue Sequel::DatabaseError => e
  # We exclude dependence on the postgresql constraint.
  if e.message =~ /\APG::ObjectNotInPrerequisiteState:(.)*is not yet defined in this session\n\z/
    return nextval(name)
  end

  # :nocov:
  raise e
  # :nocov:
end

#custom_sequence?(sequence_name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sequel/sequence/database/postgresql.rb', line 18

def custom_sequence?(sequence_name)
  out = ''
  begin
    fetch("SELECT obj_description('#{sequence_name}'::regclass, 'pg_class');") do |row|
      out = row[:obj_description]
    end
  rescue Sequel::DatabaseError # PG::UndefinedTable
    return false
  end

  out == Sequel::Database::SEQUENCE_COMMENT
end

#drop_sequence(name, options = {}) ⇒ Object



63
64
65
66
67
# File 'lib/sequel/sequence/database/postgresql.rb', line 63

def drop_sequence(name, options = {})
  if_exists = build_exists_condition(options[:if_exists])
  name = quote_name(name.to_s)
  run drop_sequence_table(name, if_exists)
end

#nextval(name) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/sequel/sequence/database/postgresql.rb', line 69

def nextval(name)
  name = quote(name.to_s)
  out = nil
  fetch("SELECT nextval(#{name})") do |row|
    out = row[:nextval]
  end
  out
end

#quote_column_name(name) ⇒ Object



10
11
12
# File 'lib/sequel/sequence/database/postgresql.rb', line 10

def quote_column_name(name)
  PG::Connection.quote_ident(name).freeze
end

#quote_sequence_name(name) ⇒ Object



14
15
16
# File 'lib/sequel/sequence/database/postgresql.rb', line 14

def quote_sequence_name(name)
  PG::Connection.quote_connstr(name).freeze
end

#set_column_default_nextval(table, column, sequence) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/sequel/sequence/database/postgresql.rb', line 109

def set_column_default_nextval(table, column, sequence)
  sql = %(
    ALTER TABLE IF EXISTS #{table}
    ALTER COLUMN #{quote_name(column.to_s)}
    SET DEFAULT nextval(#{quote(sequence.to_s)}::regclass)
  ).strip
  run sql
end

#setval(name, value) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/sequel/sequence/database/postgresql.rb', line 100

def setval(name, value)
  name = quote(name.to_s)
  out = nil
  fetch("SELECT setval(#{name}, #{value})") do |row|
    out = row[:setval]
  end
  out
end