Class: JDBCHelper::SequenceWrapper

Inherits:
ObjectWrapper show all
Defined in:
lib/jdbc-helper/wrapper/sequence_wrapper.rb

Overview

conn.sequence(:my_seq).drop!

Instance Attribute Summary

Attributes inherited from ObjectWrapper

#connection, #name

Instance Method Summary collapse

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.

Parameters:

  • value (Fixnum) (defaults to: 1)

    Initial value

  • increment_by (Fixnum) (defaults to: 1)

    Increment size for a single nextval call

Returns:



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

#currvalFixnum

Returns the incremented value of the sequence

Returns:

  • (Fixnum)


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.

Returns:



55
56
57
58
# File 'lib/jdbc-helper/wrapper/sequence_wrapper.rb', line 55

def drop!
  @connection.update("drop sequence #{name}")
  self
end

#nextvalFixnum

Increments the sequence and returns the value

Returns:

  • (Fixnum)


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.

Parameters:

  • value (Fixnum) (defaults to: 1)

    Initial value

  • increment_by (Fixnum) (defaults to: 1)

    Increment size for a single nextval call

Returns:



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