Module: PgSequencer::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/pg_sequencer/connection_adapters/postgresql_adapter.rb
Instance Method Summary collapse
- #change_sequence(name, options = {}) ⇒ Object
- #change_sequence_sql(name, options = {}) ⇒ Object
- #create_sequence(name, options = {}) ⇒ Object
-
#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 ].
- #drop_sequence(name) ⇒ Object
- #drop_sequence_sql(name) ⇒ Object
- #sequence_options_sql(options = {}) ⇒ Object
- #sequences ⇒ Object
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, = {}) execute change_sequence_sql(name, ) 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, = {}) return "" if .blank? .delete(:start) "ALTER SEQUENCE #{name}#{()}" end |
#create_sequence(name, options = {}) ⇒ Object
8 9 10 |
# File 'lib/pg_sequencer/connection_adapters/postgresql_adapter.rb', line 8 def create_sequence(name, = {}) execute create_sequence_sql(name, ) 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, = {}) .delete(:restart) "CREATE SEQUENCE #{name}#{()}" 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 ( = {}) sql = "" sql << increment_option_sql() if [:increment] or [:increment_by] sql << min_option_sql() sql << max_option_sql() sql << start_option_sql() if [:start] or [:start_with] sql << restart_option_sql() if [:restart] or [:restart_with] sql << cache_option_sql() if [:cache] sql << cycle_option_sql() sql end |
#sequences ⇒ Object
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}") = { :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, ) end all_sequences end |