Class: JDBCHelper::SequenceWrapper
- Inherits:
-
ObjectWrapper
- Object
- ObjectWrapper
- JDBCHelper::SequenceWrapper
- Defined in:
- lib/jdbc-helper/wrapper/sequence_wrapper.rb
Overview
conn.sequence(:my_seq).drop!
Instance Attribute Summary
Attributes inherited from ObjectWrapper
Instance Method Summary collapse
-
#create!(value = 1, increment_by = 1) ⇒ JDBCHelper::SequenceWrapper
Creates the sequence.
-
#currval ⇒ Fixnum
Returns the incremented value of the sequence.
-
#drop! ⇒ JDBCHelper::SequenceWrapper
Drops the sequence.
-
#initialize(conn, name) ⇒ SequenceWrapper
constructor
A new instance of SequenceWrapper.
-
#nextval ⇒ Fixnum
Increments the sequence and returns the value.
-
#reset!(value = 1, increment_by = 1) ⇒ JDBCHelper::SequenceWrapper
Recreates the sequence.
Constructor Details
#initialize(conn, name) ⇒ SequenceWrapper
Returns a new instance of SequenceWrapper.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/jdbc-helper/wrapper/sequence_wrapper.rb', line 18 def initialize(conn, name) super conn, name case conn.driver when /postgres/ @nextval_sql = "select nextval('#{name}')" @currval_sql = "select currval('#{name}')" else @nextval_sql = "select #{name}.nextval from dual" @currval_sql = "select #{name}.currval from dual" end end |
Instance Method Details
#create!(value = 1, increment_by = 1) ⇒ JDBCHelper::SequenceWrapper
Creates the sequence. Cannot be undone.
64 65 66 67 68 69 |
# File 'lib/jdbc-helper/wrapper/sequence_wrapper.rb', line 64 def create! value = 1, increment_by = 1 create = JDBCHelper::SQL.check( "create sequence #{name} start with #{value} increment by #{increment_by}") @connection.update(create) self end |
#currval ⇒ Fixnum
Returns the incremented value of the sequence
39 40 41 |
# File 'lib/jdbc-helper/wrapper/sequence_wrapper.rb', line 39 def currval @connection.query(@currval_sql).to_a[0][0].to_i end |
#drop! ⇒ JDBCHelper::SequenceWrapper
Drops the sequence. Cannot be undone.
55 56 57 58 |
# File 'lib/jdbc-helper/wrapper/sequence_wrapper.rb', line 55 def drop! @connection.update("drop sequence #{name}") self end |
#nextval ⇒ Fixnum
Increments the sequence and returns the value
33 34 35 |
# File 'lib/jdbc-helper/wrapper/sequence_wrapper.rb', line 33 def nextval @connection.query(@nextval_sql).to_a[0][0].to_i end |
#reset!(value = 1, increment_by = 1) ⇒ JDBCHelper::SequenceWrapper
Recreates the sequence. Cannot be undone.
47 48 49 50 51 |
# File 'lib/jdbc-helper/wrapper/sequence_wrapper.rb', line 47 def reset! value = 1, increment_by = 1 drop! rescue nil create! value, increment_by self end |