Module: PgSequencer::ConnectionAdapters::PostgreSQLAdapter

Defined in:
lib/pg_sequencer/connection_adapters/postgresql_adapter.rb

Instance Method Summary collapse

Instance Method Details

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



16
17
18
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 16

def change_sequence(name, options = {})
  execute change_sequence_sql(name, options)
end

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



40
41
42
43
44
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 40

def change_sequence_sql(name, options = {})
  return "" if options.blank?
  options.delete(:start)
  "ALTER SEQUENCE #{name}#{sequence_options_sql(options)}"
end

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



8
9
10
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 8

def create_sequence(name, options = {})
  execute create_sequence_sql(name, options)
end

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

CREATE [ TEMPORARY | TEMP ] SEQUENCE name [ INCREMENT [ BY ] increment ]

[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
[ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]

create_sequence “seq_user”,

:increment => 1,
:min       => (1|false),
:max       => (20000|false),
:start     => 1,
:cache     => 5,
:cycle     => true


31
32
33
34
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 31

def create_sequence_sql(name, options = {})
  options.delete(:restart)
  "CREATE SEQUENCE #{name}#{sequence_options_sql(options)}"
end

#drop_sequence(name) ⇒ Object



12
13
14
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 12

def drop_sequence(name)
  execute drop_sequence_sql(name)
end

#drop_sequence_sql(name) ⇒ Object



36
37
38
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 36

def drop_sequence_sql(name)
  "DROP SEQUENCE #{name}"
end

#sequence_options_sql(options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 46

def sequence_options_sql(options = {})
  sql = ""
  sql << increment_option_sql(options)  if options[:increment] or options[:increment_by]
  sql << min_option_sql(options)
  sql << max_option_sql(options)
  sql << start_option_sql(options)      if options[:start]    or options[:start_with]
  sql << restart_option_sql(options)    if options[:restart]  or options[:restart_with]
  sql << cache_option_sql(options)      if options[:cache]
  sql << cycle_option_sql(options)
  sql
end

#sequencesObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 58

def sequences
  # sequence_temp=# select * from temp;
  # -[ RECORD 1 ]-+--------------------
  # sequence_name | temp
  # last_value    | 7
  # start_value   | 1
  # increment_by  | 1
  # max_value     | 9223372036854775807
  # min_value     | 1
  # cache_value   | 1
  # log_cnt       | 26
  # is_cycled     | f
  # is_called     | t
  sequence_names = select_all("SELECT c.relname FROM pg_class c WHERE c.relkind = 'S' order by c.relname asc").map { |row| row['relname'] }
  
  all_sequences = []
  
  sequence_names.each do |sequence_name|
    row = select_one("SELECT * FROM #{sequence_name}")
    
    options = {
      :increment => row['increment_by'].to_i,
      :min       => row['min_value'].to_i,
      :max       => row['max_value'].to_i,
      :start     => row['start_value'].to_i,
      :cache     => row['cache_value'].to_i,
      :cycle     => row['is_cycled'] == 't'
    }
    
    all_sequences << SequenceDefinition.new(sequence_name, options)
  end
  
  all_sequences
end